一.系统时钟初始化
1.main函数的各种调用,验证参数
kernelliteos_aplatformmain.c->main()
kernelliteos_akernelcommonlos_config.c->OsMain()
kernelliteos_aarcharmarmsrclos_hw_tick.c->OsTickInit()
- systemClock
- tickPerSecond
- LITE_OS_SEC_TEXT_INIT UINT32 OsTickInit(UINT32 systemClock, UINT32 tickPerSecond)
- {
- HalClockInit();
- return LOS_OK;
- }
复制代码
2.先获取当前时钟频率,注册中断
kernelliteos_aplatformhwarmtimerarm_genericarm_generic_timer.c
- OS_TICK_INT_NUM
- MIN_INTERRUPT_PRIORITY
- OsTickEntry
- LITE_OS_SEC_TEXT_INIT VOID HalClockInit(VOID)
- { ...
- g_sysClock = HalClockFreqRead();
-
-
- ret = LOS_HwiCreate(OS_TICK_INT_NUM, MIN_INTERRUPT_PRIORITY, 0, OsTickEntry, 0);
-
-
- ...
- }
复制代码
二.时钟中断的执行函数OsTickEntry()
kernelliteos_aplatformhwarmtimerarm_genericarm_generic_timer.c
不过此时这是注册了这个函数,时钟并未启动,得执行了(三.启动时钟)之后才会调用这个函数
- LITE_OS_SEC_TEXT VOID OsTickEntry(VOID)
- {
- TimerCtlWrite(0);
- OsTickHandler();
- TimerCvalWrite(TimerCvalRead() + OS_CYCLE_PER_TICK);
- TimerCtlWrite(1);
-
- }
复制代码
三.启动时钟
main() => OsStart(VOID) => OsTickStart() => HalClockStart(VOID)
kernelliteos_aplatformhwarmtimerarm_genericarm_generic_timer.c => HalClockStart(VOID)
- LITE_OS_SEC_TEXT_INIT VOID HalClockStart(VOID)
- {
- HalIrqUnmask(OS_TICK_INT_NUM);
- TimerCtlWrite(0);
- TimerTvalWrite(OS_CYCLE_PER_TICK);
- TimerCtlWrite(1);
- }
复制代码
1. HalIrqUnmask; //接收中断(通过设置寄存器,允许CPU响应该中断)
- HalIrqUnmask(OS_TICK_INT_NUM);
- HalIrqUnmask(29);
- GIC_REG_32(GICD_ISENABLER(29 >> 5)) = 1U << (29 % 32);
- (GICD_ISENABLER(29 >> 5))拆开
- GIC_REG_32(GICD_OFFSET + 0x100 + (29 >> 5) * 4) = 1U << (29 % 32);/* 中断使能 Registers */
- GIC_REG_32拆开,(29 % 32)=1D
- GIC_BASE_ADDR + (GICD_OFFSET + 0x100 + (29 >> 5) * 4) = 1U << (29 % 32)
- #define GIC_BASE_ADDR IO_DEVICE_ADDR(0x3F00A100)
- #define GICD_OFFSET 0x1000 /* interrupt distributor offset */
复制代码
2.TimerCtlWrite(0); //关闭Timer
WRITE_TIMER_REG32(TIMER_REG_CTL, 0);
ARM_SYSREG_WRITE(TIMER_REG_CTL, 0)
- ARM_SYSREG_WRITE(TIMER_REG(_CTL), 0)
- ARM_SYSREG_WRITE(CP15_REG(c14, 0, c2, 1)), 0)
- "mcr " (CP15_REG(c14, 0, c2, 1) :: "r" (val)
- 反汇编
- r8 0
- mcr p15, #0, r8, c14, c2, #1 CNTP_CTL,PL1物理定时器控制寄存器
复制代码
3.TimerTvalWrite(OS_CYCLE_PER_TICK); //设置Tval
- 反汇编
- r0 192000
- mcr p15, #0, r0, c14, c2, #0 CNTP_TVAL,PL1物理时间值寄存器
复制代码
4.TimerCtlWrite(1); //再开启Timer
- 反汇编
- r5 1
- mcr p15, #0, r5, c14, c2, #1 CNTP_CTL,PL1物理定时器控制寄存器
复制代码
代码移植
Z:brightharmony-100askkernelliteos_aplatformhwarminterruptgicgic_v2.c
- VOID HalIrqUnmask(UINT32 vector)
- {
- if ((vector > OS_USER_HWI_MAX) || (vector < OS_USER_HWI_MIN)) {
- return;
- }
-
- *(volatile UINT32 *)((UINTPTR)IO_DEVICE_ADDR(0x3F00B218)) = 1;
- }
复制代码
Z:brightharmony-100askkernelliteos_aplatformhwarmtimerarm_genericarm_generic_timer.c
- STATIC_INLINE VOID TimerCtlWrite(UINT32 cntpCtl)
- {
-
- if(cntpCtl == 0){
- *(volatile UINT32 *)((UINTPTR)IO_DEVICE_ADDR(0x3F00B408)) = 0x003E0000;
- }
- else
- {
- *(volatile UINT32 *)((UINTPTR)IO_DEVICE_ADDR(0x3F00B408)) = 0x003E00A2;
- }
- }
复制代码
Z:brightharmony-100askkernelliteos_aplatformhwarmtimerarm_genericarm_generic_timer.c
- STATIC_INLINE VOID TimerTvalWrite(UINT32 tval)
- {
-
- *(volatile UINT32 *)((UINTPTR)IO_DEVICE_ADDR(0x3F00B400)) = tval;
- }
复制代码