uptime.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #if 0
  28. void TMR6_GLOBAL_IRQHandler(void)
  29. {
  30. if(tmr_flag_get(TMR6, TMR_OVF_FLAG) != RESET)
  31. {
  32. tmr_flag_clear(TMR6, TMR_OVF_FLAG);
  33. uptime++;
  34. if (uptime >= ONE_DAY_RESET_SEC) {
  35. nvic_system_reset();
  36. }
  37. wdt_task_process();
  38. }
  39. }
  40. #endif
  41. //
  42. uint8_t wdt_add_task(uint32_t max)
  43. {
  44. if (task_number == TASK_LIST_SIZE)
  45. return 0;
  46. task_list[task_number++].max_cnt = max;
  47. return task_number - 1;
  48. }
  49. //
  50. void wdt_reset_cnt(uint8_t id)
  51. {
  52. task_list[id].cnt = 0;
  53. }
  54. //
  55. void wdt_task_process(void)
  56. {
  57. for (uint8_t i = 0; i < task_number; i++)
  58. {
  59. if (task_list[i].cnt++ > task_list[i].max_cnt)
  60. {
  61. //printf("Alarm! Task id %u wdt error!\r\n", i);
  62. NVIC_SystemReset();
  63. }
  64. }
  65. }