port_m33.c stdint.h hardware/clocks.h hardware/structs/clocks.h hardware/structs/m33.h hardware/structs/scb.h hardware/structs/systick.h port_m33.h FPCCR_ASPEN (1u << 31) FPCCR_LSPEN (1u << 30) void void tk_port_init (uint32_t ticks_per_sec) tk_port_init uint32_t ticks_per_sec #include<stdint.h> #include"hardware/clocks.h" #include"hardware/structs/clocks.h"//clock_num_t(clk_sys) #include"hardware/structs/m33.h"//m33_hw->fpccr(memory-mappedFPUcontrol) #include"hardware/structs/scb.h" #include"hardware/structs/systick.h" #include"port_m33.h" #defineFPCCR_ASPEN(1u<<31)//FPU_FPCCR_ASPEN_PosperCMSIScore_cm33.h #defineFPCCR_LSPEN(1u<<30)//FPU_FPCCR_LSPEN_PosperCMSIScore_cm33.h voidtk_port_init(uint32_tticks_per_sec){ //DisableautomaticstatepreservationandlazystackingviaFPCCR: //thekernelexplicitlysaves/restoresS0-S31insoftwareoneveryswitch, //sohardwaremustneverpushVFPframesontotaskstacks. m33_hw->fpccr=(uint32_t)(m33_hw->fpccr&~(FPCCR_ASPEN|FPCCR_LSPEN)); uint32_tclk=clock_get_hz(clk_sys); systick_hw->rvr=((clk+ticks_per_sec/2u)/ticks_per_sec-1u)&0x00FFFFFFu; //RunPendSVatthelowestpriority(15of15onM33)soperipheralISRs //andSysTickalwayspreemptit,ensuringcontextswitcheshappenonlywhenall //higher-priorityinterrupthandlingiscomplete.SysTickrunsatpriority14. //ByteoffsetsfollowCMSIS__NVIC_SetPriority: //SHPRbase=SCB+0x18,byteindex=((IRQn)&0xF)-4. uint8_t*shpr=(uint8_t*)&scb_hw->shpr[0]; shpr[((int32_t)(-2)&0xF)-4]=15u<<4;//PendSV(priority15:lowest) shpr[((int32_t)(-1)&0xF)-4]=14u<<4;//SysTick(priority14) //NOTE:SysTickisintentionallyNOTenabledhere.tk_port_start()enablesit //(ENABLE|TICKINT|CLKSOURCE)inasm,rightbeforejumpingintothefirsttask, //soatickcanneverpreempttherestoftk::start()whileschedulerstateand //MSParenotyetestablished. }