main.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "dma.h"
  23. #include "iwdg.h"
  24. #include "usart.h"
  25. #include "gpio.h"
  26. /* Private includes ----------------------------------------------------------*/
  27. /* USER CODE BEGIN Includes */
  28. #include <rtthread.h>
  29. #include "init.h"
  30. #include "buffer.h"
  31. #include "flash.h"
  32. #include "usart.h"
  33. #include "button.h"
  34. #include "display.h"
  35. #include "plan.h"
  36. #include <cm_backtrace.h>
  37. /* USER CODE END Includes */
  38. /* Private typedef -----------------------------------------------------------*/
  39. /* USER CODE BEGIN PTD */
  40. /* USER CODE END PTD */
  41. /* Private define ------------------------------------------------------------*/
  42. /* USER CODE BEGIN PD */
  43. #define HARDWARE_VERSION "V1.0.0"
  44. #define SOFTWARE_VERSION "V0.1.0"
  45. /* USER CODE END PD */
  46. /* Private macro -------------------------------------------------------------*/
  47. /* USER CODE BEGIN PM */
  48. /* USER CODE END PM */
  49. /* Private variables ---------------------------------------------------------*/
  50. /* USER CODE BEGIN PV */
  51. uint8_t inMenu = 0;
  52. /* USER CODE END PV */
  53. /* Private function prototypes -----------------------------------------------*/
  54. void SystemClock_Config(void);
  55. /* USER CODE BEGIN PFP */
  56. /* USER CODE END PFP */
  57. /* Private user code ---------------------------------------------------------*/
  58. /* USER CODE BEGIN 0 */
  59. static rt_err_t exception_hook(void *context) {
  60. extern long list_thread(void);
  61. uint8_t _continue = 1;
  62. rt_enter_critical();
  63. #ifdef RT_USING_FINSH
  64. list_thread();
  65. #endif
  66. /* 建立深度为 16 的函数调用栈缓冲区,深度大小不应该超过 CMB_CALL_STACK_MAX_DEPTH(默认16) */
  67. uint32_t call_stack[16] = {0};
  68. size_t i, depth = 0;
  69. /* 获取当前环境下的函数调用栈,每个元素将会以 32 位地址形式存储, depth 为函数调用栈实际深度 */
  70. depth = cm_backtrace_call_stack(call_stack, sizeof(call_stack), cmb_get_sp());
  71. /* 输出当前函数调用栈信息
  72. * 注意:查看函数名称及具体行号时,需要使用 addr2line 工具转换
  73. */
  74. for (i = 0; i < depth; i++) {
  75. printf("%08x ", call_stack[i]);
  76. }
  77. cm_backtrace_fault(*((uint32_t *)(cmb_get_sp() + sizeof(uint32_t) * 8)), cmb_get_sp() + sizeof(uint32_t) * 9);
  78. while (_continue == 1);
  79. return RT_EOK;
  80. }
  81. void fault_test_by_div0(void) {
  82. volatile int * SCB_CCR = (volatile int *) 0xE000ED14; // SCB->CCR
  83. int x, y, z;
  84. *SCB_CCR |= (1 << 4); /* bit4: DIV_0_TRP. */
  85. x = 10;
  86. y = 0;
  87. z = x / y;
  88. printf("z:%d\n", z);
  89. }
  90. /* USER CODE END 0 */
  91. /**
  92. * @brief The application entry point.
  93. * @retval int
  94. */
  95. int main(void)
  96. {
  97. /* USER CODE BEGIN 1 */
  98. extern IWDG_HandleTypeDef hiwdg;
  99. /* USER CODE END 1 */
  100. /* MCU Configuration--------------------------------------------------------*/
  101. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  102. HAL_Init();
  103. /* USER CODE BEGIN Init */
  104. /* CmBacktrace initialize */
  105. // cm_backtrace_init("CmBacktrace", HARDWARE_VERSION, SOFTWARE_VERSION);
  106. /* set exception hook */
  107. // rt_hw_exception_install(exception_hook);
  108. /* USER CODE END Init */
  109. /* Configure the system clock */
  110. SystemClock_Config();
  111. /* USER CODE BEGIN SysInit */
  112. MX_IWDG_Init();
  113. onbordflash_init();
  114. HAL_IWDG_Refresh(&hiwdg);
  115. /* USER CODE END SysInit */
  116. /* Initialize all configured peripherals */
  117. MX_GPIO_Init();
  118. MX_DMA_Init();
  119. MX_USART1_UART_Init();
  120. MX_UART4_Init();
  121. MX_UART5_Init();
  122. MX_USART2_UART_Init();
  123. MX_USART3_UART_Init();
  124. MX_IWDG_Init();
  125. /* USER CODE BEGIN 2 */
  126. rt_show_version();
  127. thread_init();
  128. thread_beep_init();
  129. output1_off();
  130. output2_off();
  131. // rt_kprintf("\n \\ | /\n");
  132. rt_sem_take(thread_init_sem, RT_WAITING_FOREVER); //等待初始化进程完毕
  133. // init_allflash();
  134. // beep();
  135. // rt_sem_take(thread_init_sem, RT_WAITING_FOREVER); //等待初始化进程完毕
  136. if (WeightConst_SBType == 4)
  137. {
  138. if (HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin) > 0)
  139. {
  140. beep();
  141. rt_thread_mdelay(1000);
  142. if (HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin)> 0 && HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin) > 0)
  143. {
  144. init_allflash();
  145. __HAL_RCC_CLEAR_RESET_FLAGS();
  146. beep();
  147. rt_sem_take(thread_init_sem, RT_WAITING_FOREVER);
  148. }
  149. else if (HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin) > 0)
  150. {
  151. if (WeightConst_SBType == 6) WeightConst_SBType = 1;
  152. inMenu = 1;
  153. }
  154. }
  155. } else {
  156. if (HAL_GPIO_ReadPin(B4_GPIO_Port, B4_Pin) > 0 )
  157. {
  158. beep();
  159. rt_thread_mdelay(1000);
  160. if (HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin)> 0 && HAL_GPIO_ReadPin(B4_GPIO_Port, B4_Pin) > 0)
  161. {
  162. init_allflash();
  163. __HAL_RCC_CLEAR_RESET_FLAGS();
  164. beep();
  165. rt_sem_take(thread_init_sem, RT_WAITING_FOREVER);
  166. }
  167. else if (HAL_GPIO_ReadPin(B4_GPIO_Port, B4_Pin) > 0)
  168. {
  169. if (WeightConst_SBType == 6) WeightConst_SBType = 1;
  170. inMenu = 1;
  171. }
  172. }
  173. }
  174. threadUart_init();
  175. ReadWeight_thread();
  176. if (inMenu==1)
  177. {
  178. beep();
  179. MenuButtonthread();
  180. threadMenuDisplay_init();
  181. }
  182. else {
  183. Button_thread();
  184. SendWifi_thread();
  185. if (WeightConst_SBType != 2)
  186. {
  187. if (WeightConst_SBType == 4) threadDisplay_delay_init();
  188. delay_key_thread();
  189. threadDisplay_init();
  190. setTIME_thread();
  191. setWeightConst_initProduct();
  192. threadRemoteDisplay_init();
  193. sendgetTime();
  194. CheckWeight_thread();
  195. pop_product_thread();
  196. ReadEID_thread();
  197. }
  198. else
  199. {
  200. beep();
  201. CanButton = 1;
  202. }
  203. }
  204. /* USER CODE END 2 */
  205. /* Infinite loop */
  206. /* USER CODE BEGIN WHILE */
  207. while (1)
  208. {
  209. /* USER CODE END WHILE */
  210. /* USER CODE BEGIN 3 */
  211. // rt_kprintf("\n \\ | /\n");
  212. rt_thread_mdelay(500);
  213. HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
  214. HAL_GPIO_TogglePin(led_GPIO_Port, led_Pin);
  215. }
  216. /* USER CODE END 3 */
  217. }
  218. /**
  219. * @brief System Clock Configuration
  220. * @retval None
  221. */
  222. void SystemClock_Config(void)
  223. {
  224. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  225. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  226. /** Initializes the RCC Oscillators according to the specified parameters
  227. * in the RCC_OscInitTypeDef structure.
  228. */
  229. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
  230. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  231. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  232. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  233. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  234. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  235. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  236. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  237. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  238. {
  239. Error_Handler();
  240. }
  241. /** Initializes the CPU, AHB and APB buses clocks
  242. */
  243. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  244. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  245. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  246. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  247. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  248. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  249. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  250. {
  251. Error_Handler();
  252. }
  253. }
  254. /* USER CODE BEGIN 4 */
  255. /* USER CODE END 4 */
  256. /**
  257. * @brief This function is executed in case of error occurrence.
  258. * @retval None
  259. */
  260. void Error_Handler(void)
  261. {
  262. /* USER CODE BEGIN Error_Handler_Debug */
  263. /* User can add his own implementation to report the HAL error return state */
  264. /* USER CODE END Error_Handler_Debug */
  265. }
  266. #ifdef USE_FULL_ASSERT
  267. /**
  268. * @brief Reports the name of the source file and the source line number
  269. * where the assert_param error has occurred.
  270. * @param file: pointer to the source file name
  271. * @param line: assert_param error line source number
  272. * @retval None
  273. */
  274. void assert_failed(uint8_t *file, uint32_t line)
  275. {
  276. /* USER CODE BEGIN 6 */
  277. /* User can add his own implementation to report the file name and line number,
  278. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  279. /* USER CODE END 6 */
  280. }
  281. #endif /* USE_FULL_ASSERT */
  282. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/