test_timer.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Demo: timer
  3. *
  4. * This demo demonstrates using timers
  5. *
  6. */
  7. #include <FreeRTOS.h>
  8. #include <timers.h>
  9. static TimerHandle_t xTimer1 = NULL;
  10. static TimerHandle_t xTimer2 = NULL;
  11. static volatile UBaseType_t uxVariableToIncrement = 0;
  12. static StaticTimer_t xTimerBuffer;
  13. void prvTimerCallback(TimerHandle_t xTimer)
  14. {
  15. UBaseType_t *puxVariableToIncrement;
  16. rt_kprintf("%s time out, period: %d\n", pcTimerGetName(xTimer), xTimerGetPeriod(xTimer));
  17. puxVariableToIncrement = (UBaseType_t *)pvTimerGetTimerID(xTimer);
  18. if (puxVariableToIncrement != NULL)
  19. {
  20. (*puxVariableToIncrement)++;
  21. rt_kprintf("Value: %d\n", *puxVariableToIncrement);
  22. if (*puxVariableToIncrement == 5)
  23. {
  24. xTimerChangePeriod(xTimer, pdMS_TO_TICKS(1000), 0);
  25. }
  26. else if (*puxVariableToIncrement == 10)
  27. {
  28. xTimerStop(xTimer, 0);
  29. }
  30. }
  31. }
  32. int timer_sample(void)
  33. {
  34. xTimer1 = xTimerCreate("Timer 1", pdMS_TO_TICKS(1000), pdFALSE, NULL, prvTimerCallback);
  35. if (xTimer1 == NULL)
  36. {
  37. rt_kprintf("Create timer 1 failed");
  38. return -1;
  39. }
  40. xTimer2 = xTimerCreateStatic("Timer 2", pdMS_TO_TICKS(500), pdTRUE, (void *)&uxVariableToIncrement, prvTimerCallback, &xTimerBuffer);
  41. if (xTimer2 == NULL)
  42. {
  43. rt_kprintf("Create timer 2 failed");
  44. return -1;
  45. }
  46. xTimerStart(xTimer1, 0);
  47. xTimerStart(xTimer2, 0);
  48. return 0;
  49. }
  50. MSH_CMD_EXPORT(timer_sample, timer sample);