uptime.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "uptime.h"
  2. #define ONE_DAY_RESET_SEC 86400
  3. #define TASK_LIST_SIZE 6
  4. static uint32_t uptime = 0;
  5. task_list_t task_list[TASK_LIST_SIZE];
  6. static uint8_t task_number = 0;
  7. //
  8. void get_uptime(uint32_t *value)
  9. {
  10. *value = uptime;
  11. }
  12. //
  13. void uptime_init(void)
  14. {
  15. crm_clocks_freq_type crm_clocks_freq_struct = {0};
  16. crm_periph_clock_enable(CRM_TMR6_PERIPH_CLOCK, TRUE);
  17. crm_clocks_freq_get(&crm_clocks_freq_struct);
  18. tmr_base_init(TMR6, 9999, (crm_clocks_freq_struct.ahb_freq / 20000) - 1);
  19. tmr_cnt_dir_set(TMR6, TMR_COUNT_UP);
  20. tmr_flag_clear(TMR6, TMR_OVF_FLAG);
  21. nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
  22. nvic_irq_enable(TMR6_GLOBAL_IRQn, 5, 0);
  23. tmr_counter_enable(TMR6, TRUE);
  24. tmr_interrupt_enable(TMR6, TMR_OVF_INT, TRUE);
  25. }
  26. /**
  27. * @brief
  28. * @retval
  29. */
  30. //
  31. void TMR6_GLOBAL_IRQHandler(void)
  32. {
  33. if(tmr_flag_get(TMR6, TMR_OVF_FLAG) != RESET)
  34. {
  35. tmr_flag_clear(TMR6, TMR_OVF_FLAG);
  36. uptime++;
  37. if (uptime >= ONE_DAY_RESET_SEC) {
  38. nvic_system_reset();
  39. }
  40. wdt_task_process();
  41. }
  42. }
  43. //
  44. uint8_t wdt_add_task(uint32_t max)
  45. {
  46. if (task_number == TASK_LIST_SIZE)
  47. return 0;
  48. task_list[task_number++].max_cnt = max;
  49. return task_number - 1;
  50. }
  51. //
  52. void wdt_reset_cnt(uint8_t id)
  53. {
  54. task_list[id].cnt = 0;
  55. }
  56. //
  57. void wdt_task_process(void)
  58. {
  59. for (uint8_t i = 0; i < task_number; i++)
  60. {
  61. if (task_list[i].cnt++ > task_list[i].max_cnt)
  62. {
  63. //printf("Alarm! Task id %u wdt error!\r\n", i);
  64. NVIC_SystemReset();
  65. }
  66. }
  67. }