main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 "rtc.h"
  25. #include "usart.h"
  26. #include "gpio.h"
  27. /* Private includes ----------------------------------------------------------*/
  28. /* USER CODE BEGIN Includes */
  29. #include <rtthread.h>
  30. #include "init.h"
  31. #include "buffer.h"
  32. #include "flash.h"
  33. #include "usart.h"
  34. #include "button.h"
  35. #include "display.h"
  36. #include "plan.h"
  37. #include "syswatch.h"
  38. #include <cm_backtrace.h>
  39. /* USER CODE END Includes */
  40. /* Private typedef -----------------------------------------------------------*/
  41. /* USER CODE BEGIN PTD */
  42. /* USER CODE END PTD */
  43. /* Private define ------------------------------------------------------------*/
  44. /* USER CODE BEGIN PD */
  45. #define HARDWARE_VERSION "V1.0.0"
  46. #define SOFTWARE_VERSION "V0.1.0"
  47. /* USER CODE END PD */
  48. /* Private macro -------------------------------------------------------------*/
  49. /* USER CODE BEGIN PM */
  50. /* USER CODE END PM */
  51. /* Private variables ---------------------------------------------------------*/
  52. /* USER CODE BEGIN PV */
  53. uint8_t inMenu = 0;
  54. /* USER CODE END PV */
  55. /* Private function prototypes -----------------------------------------------*/
  56. void SystemClock_Config(void);
  57. /* USER CODE BEGIN PFP */
  58. /* USER CODE END PFP */
  59. /* Private user code ---------------------------------------------------------*/
  60. /* USER CODE BEGIN 0 */
  61. static rt_err_t exception_hook(void *context) {
  62. extern long list_thread(void);
  63. uint8_t _continue = 1;
  64. rt_enter_critical();
  65. #ifdef RT_USING_FINSH
  66. list_thread();
  67. #endif
  68. /* 建立深度为 16 的函数调用栈缓冲区,深度大小不应该超过 CMB_CALL_STACK_MAX_DEPTH(默认16) */
  69. uint32_t call_stack[16] = {0};
  70. size_t i, depth = 0;
  71. /* 获取当前环境下的函数调用栈,每个元素将会以 32 位地址形式存储, depth 为函数调用栈实际深度 */
  72. depth = cm_backtrace_call_stack(call_stack, sizeof(call_stack), cmb_get_sp());
  73. /* 输出当前函数调用栈信息
  74. * 注意:查看函数名称及具体行号时,需要使用 addr2line 工具转换
  75. */
  76. for (i = 0; i < depth; i++) {
  77. printf("%08x ", call_stack[i]);
  78. }
  79. cm_backtrace_fault(*((uint32_t *)(cmb_get_sp() + sizeof(uint32_t) * 8)), cmb_get_sp() + sizeof(uint32_t) * 9);
  80. while (_continue == 1);
  81. return RT_EOK;
  82. }
  83. void fault_test_by_div0(void) {
  84. volatile int * SCB_CCR = (volatile int *) 0xE000ED14; // SCB->CCR
  85. int x, y, z;
  86. *SCB_CCR |= (1 << 4); /* bit4: DIV_0_TRP. */
  87. x = 10;
  88. y = 0;
  89. z = x / y;
  90. printf("z:%d\n", z);
  91. }
  92. /* USER CODE END 0 */
  93. /**
  94. * @brief The application entry point.
  95. * @retval int
  96. */
  97. int main(void)
  98. {
  99. /* USER CODE BEGIN 1 */
  100. extern IWDG_HandleTypeDef hiwdg;
  101. extern unsigned char XbeeConst_FrameNum;
  102. /* USER CODE END 1 */
  103. /* MCU Configuration--------------------------------------------------------*/
  104. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  105. HAL_Init();
  106. /* USER CODE BEGIN Init */
  107. /* CmBacktrace initialize */
  108. // cm_backtrace_init("CmBacktrace", HARDWARE_VERSION, SOFTWARE_VERSION);
  109. /* set exception hook */
  110. // rt_hw_exception_install(exception_hook);
  111. /* USER CODE END Init */
  112. /* Configure the system clock */
  113. SystemClock_Config();
  114. /* USER CODE BEGIN SysInit */
  115. MX_GPIO_Init();
  116. pwr_lock_on();
  117. MX_IWDG_Init();
  118. onbordflash_init();
  119. HAL_IWDG_Refresh(&hiwdg);
  120. /* USER CODE END SysInit */
  121. /* Initialize all configured peripherals */
  122. MX_GPIO_Init();
  123. MX_DMA_Init();
  124. MX_USART1_UART_Init();
  125. MX_UART4_Init();
  126. MX_UART5_Init();
  127. MX_USART2_UART_Init();
  128. MX_USART3_UART_Init();
  129. MX_IWDG_Init();
  130. MX_RTC_Init();
  131. /* USER CODE BEGIN 2 */
  132. rt_show_version();
  133. /* 使能备份时钟:备份寄存器 */
  134. __HAL_RCC_BKP_CLK_ENABLE();
  135. HAL_PWR_EnableBkUpAccess();
  136. XbeeConst_FrameNum = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR3);
  137. if(READ_BIT(RCC->CSR, RCC_CSR_IWDGRSTF) != RESET) //看门狗重启不需要获取时间
  138. {
  139. uint16_t timeval1,timeval2;
  140. timeval1 = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1);
  141. timeval2 = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR2);
  142. TimeConst.Buffer[0]=timeval1 & 0xFF;
  143. TimeConst.Buffer[1]=(timeval1>>8) & 0xFF;
  144. TimeConst.Buffer[2]=timeval2 & 0xFF;
  145. TimeConst.Buffer[3]=(timeval2>>8) & 0xFF;
  146. }
  147. thread_init();
  148. WeightConst_Product_Run_time= HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR3);
  149. WeightConst_DELAYON= HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR4);
  150. WeightConst_DELAYTIME= HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR5);
  151. pf_comp.data.delaytime= HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR6);
  152. thread_beep_init();
  153. output1_off();
  154. output2_off();
  155. AT_off();
  156. // rt_kprintf("\n \\ | /\n");
  157. rt_sem_take(thread_init_sem, RT_WAITING_FOREVER); //等待初始化进程完毕
  158. // init_allflash();
  159. // beep();
  160. // rt_sem_take(thread_init_sem, RT_WAITING_FOREVER); //等待初始化进程完毕
  161. if (WeightConst_SBType == 4)
  162. {
  163. if (HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin) > 0)
  164. {
  165. beep();
  166. rt_thread_mdelay(1000);
  167. if (HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin)> 0 && HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin) > 0)
  168. {
  169. init_allflash();
  170. __HAL_RCC_CLEAR_RESET_FLAGS();
  171. beep();
  172. rt_sem_take(thread_init_sem, RT_WAITING_FOREVER);
  173. }
  174. else if (HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin) > 0)
  175. {
  176. if (WeightConst_SBType == 6) WeightConst_SBType = 1;
  177. inMenu = 1;
  178. }
  179. }
  180. } else {
  181. if (HAL_GPIO_ReadPin(B4_GPIO_Port, B4_Pin) > 0 )
  182. {
  183. beep();
  184. rt_thread_mdelay(1000);
  185. if (HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin)> 0 && HAL_GPIO_ReadPin(B4_GPIO_Port, B4_Pin) > 0)
  186. {
  187. init_allflash();
  188. __HAL_RCC_CLEAR_RESET_FLAGS();
  189. beep();
  190. rt_sem_take(thread_init_sem, RT_WAITING_FOREVER);
  191. }
  192. else if (HAL_GPIO_ReadPin(B4_GPIO_Port, B4_Pin) > 0)
  193. {
  194. if (WeightConst_SBType == 6) WeightConst_SBType = 1;
  195. inMenu = 1;
  196. }
  197. }
  198. }
  199. MX_IWDG_Init();
  200. threadUart_init();
  201. // threadUart5_init();
  202. ReadWeight_thread();
  203. if (inMenu==1)
  204. {
  205. beep();
  206. MenuButtonthread();
  207. threadMenuDisplay_init();
  208. }
  209. else {
  210. Button_thread();
  211. SendWifi_thread();
  212. if (WeightConst_SBType != 2)
  213. {
  214. if (WeightConst_SBType == 4) threadDisplay_delay_init();
  215. delay_key_thread();
  216. threadDisplay_init();
  217. setTIME_thread();
  218. if (WeightConst_DELAYON==0)
  219. setWeightConst_initProduct();
  220. threadRemoteDisplay_init();
  221. sendgetTime();
  222. CheckWeight_thread();
  223. pop_product_thread();
  224. ReadEID_thread();
  225. }
  226. else
  227. {
  228. beep();
  229. CanButton = 1;
  230. }
  231. }
  232. /* USER CODE END 2 */
  233. /* Infinite loop */
  234. /* USER CODE BEGIN WHILE */
  235. while (1)
  236. {
  237. /* USER CODE END WHILE */
  238. /* USER CODE BEGIN 3 */
  239. rt_thread_mdelay(500);
  240. HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
  241. HAL_GPIO_TogglePin(led_GPIO_Port, led_Pin);
  242. }
  243. /* USER CODE END 3 */
  244. }
  245. /**
  246. * @brief System Clock Configuration
  247. * @retval None
  248. */
  249. void SystemClock_Config(void)
  250. {
  251. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  252. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  253. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  254. /** Initializes the RCC Oscillators according to the specified parameters
  255. * in the RCC_OscInitTypeDef structure.
  256. */
  257. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
  258. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  259. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  260. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  261. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  262. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  263. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  264. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  265. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  266. {
  267. Error_Handler();
  268. }
  269. /** Initializes the CPU, AHB and APB buses clocks
  270. */
  271. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  272. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  273. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  274. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  275. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  276. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  277. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  278. {
  279. Error_Handler();
  280. }
  281. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  282. PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  283. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  284. {
  285. Error_Handler();
  286. }
  287. }
  288. /* USER CODE BEGIN 4 */
  289. uint8_t rDataBuffer;
  290. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  291. {
  292. if (huart->Instance == UART5 && isTag>0){
  293. if (isTag) {
  294. USART_Push(5, rDataBuffer);
  295. rt_sem_release(thread_EmptyBuffer_sem);
  296. }
  297. HAL_UART_Receive_IT(&huart5, &rDataBuffer, 1);
  298. }
  299. }
  300. /* USER CODE END 4 */
  301. /**
  302. * @brief This function is executed in case of error occurrence.
  303. * @retval None
  304. */
  305. void Error_Handler(void)
  306. {
  307. /* USER CODE BEGIN Error_Handler_Debug */
  308. /* User can add his own implementation to report the HAL error return state */
  309. /* USER CODE END Error_Handler_Debug */
  310. }
  311. #ifdef USE_FULL_ASSERT
  312. /**
  313. * @brief Reports the name of the source file and the source line number
  314. * where the assert_param error has occurred.
  315. * @param file: pointer to the source file name
  316. * @param line: assert_param error line source number
  317. * @retval None
  318. */
  319. void assert_failed(uint8_t *file, uint32_t line)
  320. {
  321. /* USER CODE BEGIN 6 */
  322. /* User can add his own implementation to report the file name and line number,
  323. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  324. /* USER CODE END 6 */
  325. }
  326. #endif /* USE_FULL_ASSERT */
  327. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/