You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
6.7 KiB

12 months ago
11 months ago
12 months ago
11 months ago
12 months ago
  1. #include "sys_mgr.hpp"
  2. #include <stdlib.h>
  3. using namespace iflytop;
  4. #define TAG "SysMgr"
  5. extern "C" {
  6. /***********************************************************************************************************************
  7. * STM32_CODE_ERROR *
  8. ***********************************************************************************************************************/
  9. void SysMgr_on_Error_Handler() {
  10. ZLOGE(TAG, "Error_Handler\n");
  11. while (1) {
  12. }
  13. }
  14. void SysMgr_on_assert_failed(uint8_t* file, uint32_t line) {
  15. ZLOGE(TAG, "ASSERT: %s [%s:%d]\n", file, line);
  16. while (1) {
  17. }
  18. }
  19. extern uint8_t _end; /* Symbol defined in the linker script */
  20. extern uint8_t _estack; /* Symbol defined in the linker script */
  21. extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */
  22. extern uint8_t* __sbrk_heap_end;
  23. static size_t get_free_heap_size() {
  24. const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size;
  25. const uint8_t* max_heap = (uint8_t*)stack_limit;
  26. uint8_t* prev_heap_end;
  27. return max_heap - __sbrk_heap_end;
  28. // if (__sbrk_heap_end + incr > max_heap) {
  29. // errno = ENOMEM;
  30. // return (void*)-1;
  31. // }
  32. }
  33. /***********************************************************************************************************************
  34. * STM32_ERROR_IRQ *
  35. ***********************************************************************************************************************/
  36. // void SysMgr_on_NMI_Handler(void) { ZLOGI(TAG, "on NMI_Handler"); }
  37. // void SysMgr_on_HardFault_Handler(void) { ZLOGI(TAG, "on HardFault_Handler"); }
  38. // void SysMgr_on_MemManage_Handler(void) { ZLOGI(TAG, "on MemManage_Handler"); }
  39. // void SysMgr_on_BusFault_Handler(void) { ZLOGI(TAG, "on BusFault_Handler"); }
  40. // void SysMgr_on_UsageFault_Handler(void) { ZLOGI(TAG, "on UsageFault_Handler"); }
  41. void NMI_Handler(void) {
  42. printf("E:%s\n", __FUNCTION__);
  43. NVIC_SystemReset();
  44. }
  45. void HardFault_Handler(void) {
  46. printf("E:%s\n", __FUNCTION__);
  47. NVIC_SystemReset();
  48. }
  49. void MemManage_Handler(void) {
  50. printf("E:%s\n", __FUNCTION__);
  51. NVIC_SystemReset();
  52. }
  53. void BusFault_Handler(void) {
  54. printf("E:%s\n", __FUNCTION__);
  55. NVIC_SystemReset();
  56. }
  57. void UsageFault_Handler(void) {
  58. printf("E:%s\n", __FUNCTION__);
  59. NVIC_SystemReset();
  60. }
  61. void DebugMon_Handler(void) {}
  62. static void PVD_Init(void) {
  63. PWR_PVDTypeDef PvdStruct;
  64. HAL_PWR_EnablePVD();
  65. PvdStruct.PVDLevel = PWR_PVDLEVEL_6; /* set PVD thr to 3.1v */
  66. PvdStruct.Mode = PWR_PVD_MODE_IT_RISING;
  67. HAL_PWR_ConfigPVD(&PvdStruct);
  68. /* enable lost power irq */
  69. HAL_NVIC_SetPriority(PVD_IRQn, 0, 0); /* */
  70. HAL_NVIC_EnableIRQ(PVD_IRQn); /* */
  71. }
  72. void PVD_IRQHandler(void) {
  73. if (__HAL_PWR_GET_FLAG(PWR_FLAG_PVDO)) /* 1为VDD小于PVD阈值,掉电情况 */
  74. {
  75. /* 掉电前的紧急处理 */
  76. printf("low power detect......\n");
  77. for (size_t i = 0; i < 1000; i++) {
  78. // wait for lost power
  79. printf("low power detect......\n");
  80. }
  81. NVIC_SystemReset();
  82. }
  83. }
  84. /***********************************************************************************************************************
  85. * FREERTOS_ERROR *
  86. ***********************************************************************************************************************/
  87. void vApplicationStackOverflowHook(xTaskHandle xTask, signed char* pcTaskName) {
  88. ZLOGE(TAG, "StackOverflowHook: %s\n", pcTaskName);
  89. __disable_irq();
  90. while (1) {
  91. }
  92. }
  93. void vApplicationMallocFailedHook(void) {
  94. ZLOGE(TAG, "MallocFailedHook\n");
  95. __disable_irq();
  96. while (1) {
  97. }
  98. }
  99. }
  100. SysMgr* SysMgr::ins() {
  101. static SysMgr s_ins;
  102. return &s_ins;
  103. }
  104. // void SysMgr::regTaskId(osThreadId id) {
  105. // m_task[m_ntask].Id = id;
  106. // m_ntask++;
  107. // }
  108. size_t SysMgr::osGetSysRunTime() { return HAL_GetTick(); }
  109. UBaseType_t uxTaskGetSystemState(TaskStatus_t* const pxTaskStatusArray, const UBaseType_t uxArraySize, uint32_t* const pulTotalRunTime);
  110. void SysMgr::initedFinished() { //
  111. static TaskStatus_t pxTaskStatusArray[SDK_MAX_TASK + 1];
  112. UBaseType_t uxArraySize = SDK_MAX_TASK + 1;
  113. uint32_t pulTotalRunTime;
  114. m_ntask = uxTaskGetSystemState(pxTaskStatusArray, uxArraySize, &pulTotalRunTime);
  115. for (int i = 0; i < m_ntask; i++) {
  116. m_task[i].Id = pxTaskStatusArray[i].xHandle;
  117. }
  118. ZASSERT_INFO(m_ntask < SDK_MAX_TASK, "task num is too large, please increase SDK_MAX_TASK");
  119. PVD_Init();
  120. }
  121. size_t SysMgr::osGetMinimumEverFreeHeapSize() { return ::xPortGetMinimumEverFreeHeapSize(); }
  122. size_t SysMgr::osGetFreeHeapSize() { return ::xPortGetFreeHeapSize(); }
  123. size_t SysMgr::osGetTotalHeapSize() { return configTOTAL_HEAP_SIZE; }
  124. size_t SysMgr::osGetFreeSysHeapSize() { return get_free_heap_size(); }
  125. int32_t SysMgr::getTaskNum() { return m_ntask; }
  126. void SysMgr::dumpSysInfo() {
  127. zlog("---------------Heap Info--------------\n");
  128. zlog("MinimumEverFreeHeapSize : %d\n", osGetMinimumEverFreeHeapSize());
  129. zlog("RTOS FreeHeapSize : %d\n", osGetFreeHeapSize());
  130. zlog("RTOS TotalHeapSize : %d\n", osGetTotalHeapSize());
  131. zlog("Sys FreeHeap : %d\n", get_free_heap_size());
  132. zlog("");
  133. zlog("---------------Task Info--------------\n");
  134. static char buf[40 * SDK_MAX_TASK]; // 40一个任务,最多支持10个任务
  135. vTaskList(buf);
  136. zlog("Name State Priority Stack Num\n");
  137. zlog_raw(buf);
  138. zlog("- TaskInfoEnd -\n");
  139. }
  140. uint32_t SysMgr::osTaskStackRemainingSize(osThreadId id) { return uxTaskGetStackHighWaterMark(id); }
  141. const char* SysMgr::osTaskName(osThreadId id) { return pcTaskGetName(id); }
  142. osThreadId SysMgr::osGetId(int offset) {
  143. if (offset < m_ntask) {
  144. return m_task[offset].Id;
  145. }
  146. return NULL;
  147. }
  148. void SysMgr::osTaskName(osThreadId id, char* name, int bufsize) {
  149. strncpy(name, pcTaskGetName(id), bufsize);
  150. name[bufsize - 1] = 0;
  151. }
  152. void SysMgr::osTaskStackRemainingSize(osThreadId id, uint16_t* remainsize) { *remainsize = uxTaskGetStackHighWaterMark(id); }
  153. void SysMgr::osTaskPriority(osThreadId id, uint16_t* priority) { *priority = uxTaskPriorityGet(id); }
  154. void SysMgr::osTaskGetState(osThreadId id, char* state) {
  155. eTaskState task_state = eTaskGetState(id);
  156. switch (task_state) {
  157. case eRunning:
  158. *state = 'X';
  159. break;
  160. case eReady:
  161. *state = 'R';
  162. break;
  163. case eBlocked:
  164. *state = 'B';
  165. break;
  166. case eSuspended:
  167. *state = 'S';
  168. break;
  169. case eDeleted:
  170. *state = 'D';
  171. break;
  172. default:
  173. *state = '?';
  174. break;
  175. }
  176. }