123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- #include "main.h"
- static volatile task TaskArray[MAX_TASKS];
- static volatile uint8_t arrayTail;
- static volatile uint32_t TDelay;
- inline void RTOS_Init(void) {
-
- SysTick->LOAD = (uint32_t)(SystemCoreClock/1000 - 1UL);
- NVIC_SetPriority (SysTick_IRQn, (1UL << __NVIC_PRIO_BITS) - 1UL);
- SysTick->VAL = 0UL;
- SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk |
- SysTick_CTRL_TICKINT_Msk |
- SysTick_CTRL_ENABLE_Msk;
-
- arrayTail = 0;
- }
- void RTOS_SetTask (void (*taskFunc)(void), uint32_t taskDelay, uint32_t taskPeriod) {
- int i;
- if (!taskFunc) {
- return;
- }
-
- for(i = 0; i < arrayTail; i++) {
-
- if (TaskArray[i].pFunc == taskFunc) {
- __disable_irq();
- TaskArray[i].delay = taskDelay;
- TaskArray[i].period = taskPeriod;
- TaskArray[i].run = 0;
- __enable_irq();
-
- return;
- }
- }
-
- if (arrayTail < MAX_TASKS) {
-
- __disable_irq();
- TaskArray[arrayTail].pFunc = taskFunc;
- TaskArray[arrayTail].delay = taskDelay;
- TaskArray[arrayTail].period = taskPeriod;
- TaskArray[arrayTail].run = 0;
-
- arrayTail++;
- __enable_irq();
- } else {
-
-
- TIM1->CCER &= ~(TIM_CCER_CC1E);
- TIM3->CCER &= ~(TIM_CCER_CC4E);
- TIM3->CCER &= ~(TIM_CCER_CC3E);
- TIM3->CCER &= ~(TIM_CCER_CC2E);
- TIM3->CCER &= ~(TIM_CCER_CC1E);
- GPIOA->BSRR = 0x10;
-
- TIM1->CCR2 = 0xff;
- TIM1->CCR3 = 0x7f;
- TIM1->CCR4 = 0x0;
- while (1);
- }
- }
- inline void RTOS_DeleteTask (void (*taskFunc)(void)) {
- int i;
-
- for (i=0; i<arrayTail; i++) {
-
- if (TaskArray[i].pFunc == taskFunc) {
- __disable_irq();
-
- if (i != (arrayTail - 1)) {
-
- TaskArray[i] = TaskArray[arrayTail - 1];
- }
-
- arrayTail--;
- __enable_irq();
- return;
- }
- }
- }
- void RTOS_DispatchTask(void) {
- int i;
- void (*function) (void);
-
- for (i=0; i<arrayTail; i++) {
-
- if (TaskArray[i].run != 0) {
-
- function = TaskArray[i].pFunc;
- if (TaskArray[i].period == 0) {
-
- RTOS_DeleteTask(TaskArray[i].pFunc);
- } else {
-
- TaskArray[i].run = 0;
-
- if (!TaskArray[i].delay) {
-
- TaskArray[i].delay = TaskArray[i].period-1;
-
- }
- }
-
- (*function)();
- }
- }
- }
- void tdelay_ms(uint32_t msek) {
- TDelay = msek;
- do {
- if (TDelay > 1) {
-
- RTOS_DispatchTask();
- }
-
- __WFI();
- } while (TDelay != 0);
- }
- #pragma GCC optimize ("O3")
- void SysTick_Handler(void) {
-
- int i;
-
- for (i=0; i<arrayTail; i++) {
-
- if (TaskArray[i].delay == 0) {
-
- TaskArray[i].run = 1;
- } else {
-
- TaskArray[i].delay--;
- }
- }
-
- if (TDelay != 0) {
- TDelay --;
- }
- }
|