参考网址:
时间触发嵌入式系统设计模式 第14章 笔记_abc-CSDN博客_时间触发嵌入式系统设计模式
SimpleTimer
[Linux下C实现的自定义定时器](voidAspire/Timer: Linux下C实现的自定义定时器 (github.com))
[CppTimer](berndporr/cppTimer: C++ timer: wrapper around the standard Linux C timer to make your life easier (github.com))
[simple-timer-for-c-language](ielife/simple-timer-for-c-language: high performance timer for linux (github.com))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include <stdio.h> #include "NUC100Series.h" #include "MCU_init.h" #include "SYS_init.h" #include "scheduler.h"
extern void SCH_Init(void) ; extern void SCH_Update(void) ;
void SysTick_Handler(void) { SCH_Update(); }
void InitSysTickClk() { SCH_Init(); SysTick->LOAD = 1000 *CyclesPerUs -1; SysTick->VAL = (0x00); NVIC_EnableIRQ(SysTick_IRQn); SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk |SysTick_CTRL_TICKINT_Msk; }
void LED_Flash_Update(void) {
GPIO_TOGGLE(PC14); }
void LED_Flash_UpdateD(void) {
GPIO_TOGGLE(PC12); }
int main(void) { SYS_Init(); UART_Open(UART0, 115200); printf("Hello World \r\n"); GPIO_SetMode(PC, BIT12, GPIO_MODE_OUTPUT); GPIO_SetMode(PC, BIT14, GPIO_MODE_OUTPUT); GPIO_SetMode(PC, BIT15, GPIO_MODE_OUTPUT); InitSysTickClk(); SCH_Add_Task(LED_Flash_Update, 0, 1000); SCH_Add_Task(LED_Flash_UpdateD, 0, 2000);
while(1) { SCH_Dispatch_Tasks(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #ifndef _SCHEDULER_H #define _SCHEDULER_H
void SCH_Dispatch_Tasks(void); unsigned char SCH_Add_Task(void (*) (void), const unsigned int, const unsigned int); unsigned char SCH_Delete_Task(const unsigned char); void SCH_Report_Status(void);
#define SCH_MAX_TASKS (3) #endif
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
#include "scheduler.h"
typedef unsigned char tByte; typedef unsigned int tWord; typedef unsigned long tLong;
#define ERROR_SCH_TOO_MANY_TASKS (1) #define ERROR_SCH_CANNOT_DELETE_TASK (2)
#define ERROR_SCH_WAITING_FOR_SLAVE_TO_ACK (3) #define ERROR_SCH_WAITING_FOR_START_COMMAND_FROM_MASTER (3)
#define ERROR_SCH_ONE_OR_MORE_SLAVES_DID_NOT_START (4) #define ERROR_SCH_LOST_SLAVE (5)
#define ERROR_SCH_CAN_BUS_ERROR (6)
#define ERROR_I2C_WRITE_BYTE (10) #define ERROR_I2C_READ_BYTE (11) #define ERROR_I2C_WRITE_BYTE_AT24C64 (12) #define ERROR_I2C_READ_BYTE_AT24C64 (13) #define ERROR_I2C_DS1621 (14)
#define ERROR_USART_TI (21) #define ERROR_USART_WRITE_CHAR (22)
#define ERROR_SPI_EXCHANGE_BYTES_TIMEOUT (31) #define ERROR_SPI_X25_TIMEOUT (32) #define ERROR_SPI_MAX1110_TIMEOUT (33)
#define ERROR_ADC_MAX150_TIMEOUT (44)
#define RETURN_NORMAL 0 #define RETURN_ERROR 1
typedef struct { void (* pTask)(void);
unsigned int Delay;
unsigned int Period;
unsigned char RunMe; } sTask;
sTask SCH_tasks_G[SCH_MAX_TASKS];
unsigned char Error_code_G = 0;
static void SCH_Go_To_Sleep(void);
static unsigned int Error_tick_count_G;
static unsigned char Last_error_code_G;
void SCH_Dispatch_Tasks(void) { unsigned char Index;
for (Index = 0; Index < SCH_MAX_TASKS; Index++) { if (SCH_tasks_G[Index].RunMe > 0) { (*SCH_tasks_G[Index].pTask)();
SCH_tasks_G[Index].RunMe -= 1;
if (SCH_tasks_G[Index].Period == 0) { SCH_Delete_Task(Index); } } }
SCH_Report_Status();
SCH_Go_To_Sleep();
}
unsigned char SCH_Add_Task(void (* pFunction)(), const unsigned int DELAY, const unsigned int PERIOD) { unsigned char Index = 0; while ((SCH_tasks_G[Index].pTask != 0) && (Index < SCH_MAX_TASKS)) { Index++; } if (Index == SCH_MAX_TASKS) { Error_code_G = ERROR_SCH_TOO_MANY_TASKS;
return SCH_MAX_TASKS; } SCH_tasks_G[Index].pTask = pFunction; SCH_tasks_G[Index].Delay = DELAY; SCH_tasks_G[Index].Period = PERIOD;
SCH_tasks_G[Index].RunMe = 0;
return Index; }
unsigned char SCH_Delete_Task(const tByte TASK_INDEX) { unsigned char Return_code;
if (SCH_tasks_G[TASK_INDEX].pTask == 0) { Error_code_G = ERROR_SCH_CANNOT_DELETE_TASK;
Return_code = RETURN_ERROR; } else { Return_code = RETURN_NORMAL; } SCH_tasks_G[TASK_INDEX].pTask = 0x0000; SCH_tasks_G[TASK_INDEX].Delay = 0; SCH_tasks_G[TASK_INDEX].Period = 0;
SCH_tasks_G[TASK_INDEX].RunMe = 0;
return Return_code; }
void SCH_Report_Status(void) { #ifdef SCH_REPORT_ERRORS if (Error_code_G != Last_error_code_G) { Error_port = 255 - Error_code_G; Last_error_code_G = Error_code_G;
if (Error_code_G != 0) { Error_tick_count_G = 60000; } else { Error_tick_count_G = 0; } } else { if (Error_tick_count_G != 0) { if (--Error_tick_count_G == 0) { Error_code_G = 0; } } } #endif }
void SCH_Go_To_Sleep() {
}
void SCH_Update(void) { tByte Index;
for (Index = 0; Index < SCH_MAX_TASKS; Index++) { if (SCH_tasks_G[Index].pTask) { if (SCH_tasks_G[Index].Delay == 0) { SCH_tasks_G[Index].RunMe += 1;
if (SCH_tasks_G[Index].Period) { SCH_tasks_G[Index].Delay = SCH_tasks_G[Index].Period; } } else { SCH_tasks_G[Index].Delay -= 1; } } } } void SCH_Init(void) { unsigned char i;
for (i = 0; i < SCH_MAX_TASKS; i++) { SCH_Delete_Task(i); }
Error_code_G = 0; }
|