Timer.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include "cmsis_os2.h" // CMSIS RTOS header file
  2. /*----------------------------------------------------------------------------
  3. * Timer: Sample timer functions
  4. *---------------------------------------------------------------------------*/
  5. /*----- One-Shoot Timer Example -----*/
  6. osTimerId_t tim_id1; // timer id
  7. static uint32_t exec1; // argument for the timer call back function
  8. // One-Shoot Timer Function
  9. static void Timer1_Callback (void const *arg) {
  10. // add user code here
  11. }
  12. /*----- Periodic Timer Example -----*/
  13. osTimerId_t tim_id2; // timer id
  14. static uint32_t exec2; // argument for the timer call back function
  15. // Periodic Timer Function
  16. static void Timer2_Callback (void const *arg) {
  17. // add user code here
  18. }
  19. // Example: Create and Start timers
  20. int Init_Timers (void) {
  21. osStatus_t status; // function return status
  22. // Create one-shoot timer
  23. exec1 = 1U;
  24. tim_id1 = osTimerNew((osTimerFunc_t)&Timer1_Callback, osTimerOnce, &exec1, NULL);
  25. if (tim_id1 != NULL) { // One-shot timer created
  26. // start timer with delay 100ms
  27. status = osTimerStart(tim_id1, 100U);
  28. if (status != osOK) {
  29. return -1;
  30. }
  31. }
  32. // Create periodic timer
  33. exec2 = 2U;
  34. tim_id2 = osTimerNew((osTimerFunc_t)&Timer2_Callback, osTimerPeriodic, &exec2, NULL);
  35. if (tim_id2 != NULL) { // Periodic timer created
  36. // start timer with periodic 1000ms interval
  37. status = osTimerStart(tim_id2, 1000U);
  38. if (status != osOK) {
  39. return -1;
  40. }
  41. }
  42. return NULL;
  43. }