通过S32G3 的STM定时器 实现任务任务抢占
1、创建一个空工程
2、创建完成后先生成一版代码
3、编译
4、添加user文件夹来存放自己的代码
my_os.h
/*********************************************************************************************************************/ /*-----------------------------------------------------Includes------------------------------------------------------*/ /*********************************************************************************************************************/ #include "my_os.h" #include "Platform_Types.h" #include "IntCtrl_Ip.h" /*********************************************************************************************************************/ /*------------------------------------------------------Macros-------------------------------------------------------*/ /*********************************************************************************************************************//*********************************************************************************************************************/ /*-------------------------------------------------Global variables--------------------------------------------------*/ /*********************************************************************************************************************//*********************************************************************************************************************/ /*------------------------------------------------Function Prototypes------------------------------------------------*/ /*********************************************************************************************************************//*********************************************************************************************************************/ /*---------------------------------------------Function Implementations----------------------------------------------*/ /*********************************************************************************************************************/ #define STM_0_CR *(unsigned int *)0x4011C000 #define STM_0_CNT *(unsigned int *)0x4011C004 #define STM_0_CCR0 *(unsigned int *)0x4011C010 #define STM_0_CIR0 *(unsigned int *)0x4011C014 #define STM_0_CMP0 *(unsigned int *)0x4011C018#define STM_0_CR_TEN_ENABLED (1<<0) #define STM_0_CR_TEN_DISABLED (0<<0)#define STM_0_CLEAN_CNT (0<<0)#define STM_0_CCR0_ENABLED (1<<0) #define STM_0_CCR0_DISABLED (0<<0)#define STM_0_CIR0_CLEAN_IRQ_FLAG (1<<0)#define STM_1_CR *(unsigned int *)0x40120000 #define STM_1_CNT *(unsigned int *)0x40120004 #define STM_1_CCR0 *(unsigned int *)0x40120010 #define STM_1_CIR0 *(unsigned int *)0x40120014 #define STM_1_CMP0 *(unsigned int *)0x40120018#define STM_1_CR_TEN_ENABLED (1<<0) #define STM_1_CR_TEN_DISABLED (0<<0)#define STM_1_CLEAN_CNT (0<<0)#define STM_1_CCR0_ENABLED (1<<0) #define STM_1_CCR0_DISABLED (0<<0)#define STM_1_CIR0_CLEAN_IRQ_FLAG (1<<0)uint32 STM_0_ISR_Test_Cnt = 0; uint32 STM_0_ISR_Test_Flg = 0; ISR(STM_0_ISR) {STM_0_ISR_Test_Flg = 1;/* Disabled STM0 */STM_0_CR = (unsigned int)STM_0_CR_TEN_DISABLED;/* Disabled ccr0 */STM_0_CCR0 = (unsigned int)STM_0_CCR0_DISABLED;/* Clean count */STM_0_CNT = (unsigned int)STM_0_CLEAN_CNT;/* Enabled STM0 */STM_0_CR |= (unsigned int)STM_0_CR_TEN_ENABLED;/* Enabled ccr0 */STM_0_CCR0 |= (unsigned int)STM_0_CCR0_ENABLED;STM_0_ISR_Test_Cnt++;uint32 cnt_flag_1 = 100000;while(cnt_flag_1--){uint32 cnt_flag_2 = 10000;while(cnt_flag_2--){__asm("nop");}}STM_0_ISR_Test_Flg = 0;/* Clean flag */STM_0_CIR0 |= (unsigned int)STM_0_CIR0_CLEAN_IRQ_FLAG; }uint32 STM_1_ISR_Test_Cnt = 0; uint32 STM_1_ISR_Test_Flg = 0; ISR(STM_1_ISR) {/* Disabled STM1 */STM_1_CR = (unsigned int)STM_1_CR_TEN_DISABLED;/* Disabled ccr0 */STM_1_CCR0 = (unsigned int)STM_1_CCR0_DISABLED;/* Clean count */STM_1_CNT = (unsigned int)STM_1_CLEAN_CNT;/* Enabled STM1 */STM_1_CR |= (unsigned int)STM_1_CR_TEN_ENABLED;/* Enabled ccr0 */STM_1_CCR0 |= (unsigned int)STM_1_CCR0_ENABLED;STM_1_ISR_Test_Cnt++;if(1 == STM_0_ISR_Test_Flg){STM_1_ISR_Test_Flg++;}/* Clean flag */STM_1_CIR0 |= (unsigned int)STM_1_CIR0_CLEAN_IRQ_FLAG; }void my_os_init(void) {/* Interrupt the binding callback function */IntCtrl_Ip_InstallHandler(STM0_IRQn, STM_0_ISR, NULL_PTR);/* Set the interrupt priority */IntCtrl_Ip_SetPriority(STM0_IRQn, 4);/* Enable STM0 interrupt */IntCtrl_Ip_EnableIrq(STM0_IRQn);/* Clean count */STM_0_CNT = (unsigned int)STM_0_CLEAN_CNT;/* Clean flag */STM_0_CIR0 |= (unsigned int)STM_0_CIR0_CLEAN_IRQ_FLAG;/* set cmp data */STM_0_CMP0 = (unsigned int)(133333 * 1000);/* Enabled STM0 */STM_0_CR |= (unsigned int)STM_0_CR_TEN_ENABLED;/* Enabled ccr0 */STM_0_CCR0 |= (unsigned int)STM_0_CCR0_ENABLED;/* Interrupt the binding callback function */IntCtrl_Ip_InstallHandler(STM1_IRQn, STM_1_ISR, NULL_PTR);/* Set the interrupt priority */IntCtrl_Ip_SetPriority(STM1_IRQn, 3);/* Enable STM1 interrupt */IntCtrl_Ip_EnableIrq(STM1_IRQn);/* Clean count */STM_1_CNT = (unsigned int)STM_1_CLEAN_CNT;/* Clean flag */STM_1_CIR0 |= (unsigned int)STM_1_CIR0_CLEAN_IRQ_FLAG;/* set cmp data */STM_1_CMP0 = (unsigned int)(133333);/* Enabled STM1 */STM_1_CR |= (unsigned int)STM_1_CR_TEN_ENABLED;/* Enabled ccr0 */STM_1_CCR0 |= (unsigned int)STM_1_CCR0_ENABLED; }
my_os.h
#ifndef _MY_OS_H_ #define _MY_OS_H_ /*********************************************************************************************************************/ /*-----------------------------------------------------Includes------------------------------------------------------*/ /*********************************************************************************************************************//*********************************************************************************************************************/ /*------------------------------------------------------Macros-------------------------------------------------------*/ /*********************************************************************************************************************//*********************************************************************************************************************/ /*-------------------------------------------------Global variables--------------------------------------------------*/ /*********************************************************************************************************************//*********************************************************************************************************************/ /*------------------------------------------------Function Prototypes------------------------------------------------*/ /*********************************************************************************************************************/ extern void my_os_init(void);#endif
5、添加头文件路径
6、加时钟初始化
int main(void) {/* Write your code here *//* Initialize Clock */Clock_Ip_StatusType Status_Init_Clock = CLOCK_IP_ERROR;Status_Init_Clock = Clock_Ip_Init(Mcu_aClockConfigPB);if (Status_Init_Clock != CLOCK_IP_SUCCESS){while(1); /* Error during initialization. */} my_os_init();for(;;){if(exit_code != 0){break;}}return exit_code; }
7、跑起来
看到STM_1_ISR_Test_Flg累加 说明更高中断的任务来了,就会发生抢占。
S32G中中断优先级数值越小中断等级越高。