main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. MX_IWDG_Init();
  117. onbordflash_init();
  118. HAL_IWDG_Refresh(&hiwdg);
  119. /* USER CODE END SysInit */
  120. /* Initialize all configured peripherals */
  121. MX_GPIO_Init();
  122. MX_DMA_Init();
  123. MX_USART1_UART_Init();
  124. MX_UART4_Init();
  125. //MX_UART5_Init();
  126. MX_USART2_UART_Init();
  127. MX_USART3_UART_Init();
  128. MX_IWDG_Init();
  129. MX_RTC_Init();
  130. /* USER CODE BEGIN 2 */
  131. rt_show_version();
  132. /* 使能备份时钟:备份寄存器 */
  133. __HAL_RCC_BKP_CLK_ENABLE();
  134. HAL_PWR_EnableBkUpAccess();
  135. XbeeConst_FrameNum = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR3);
  136. if(READ_BIT(RCC->CSR, RCC_CSR_IWDGRSTF) != RESET) //看门狗重启不需要获取时间
  137. {
  138. uint16_t timeval1,timeval2;
  139. timeval1 = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1);
  140. timeval2 = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR2);
  141. TimeConst.Buffer[0]=timeval1 & 0xFF;
  142. TimeConst.Buffer[1]=(timeval1>>8) & 0xFF;
  143. TimeConst.Buffer[2]=timeval2 & 0xFF;
  144. TimeConst.Buffer[3]=(timeval2>>8) & 0xFF;
  145. }
  146. thread_init();
  147. WeightConst_Product_Run_time= HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR3);
  148. WeightConst_DELAYON= HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR4);
  149. WeightConst_DELAYTIME= HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR5);
  150. WeightConst_WeightTargetAllowMin = 32767; //目标重量的允许跳转范围
  151. WeightConst_WeightTargetAllowMax = -32768; //目标重量的允许跳转范围
  152. pf_comp.data.delaytime = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR6);
  153. //WeightConst_WeightCurTem = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR7);
  154. thread_beep_init();
  155. output1_off();
  156. output2_off();
  157. pwr_lock_on();
  158. AT_on();
  159. rt_kprintf("\n \\ | /\n");
  160. rt_sem_take(thread_init_sem, RT_WAITING_FOREVER); //等待初始化进程完毕
  161. // init_allflash();
  162. // beep();
  163. // rt_sem_take(thread_init_sem, RT_WAITING_FOREVER); //等待初始化进程完毕
  164. if (WeightConst_SBType == 4)
  165. {
  166. init_allflash();
  167. __HAL_RCC_CLEAR_RESET_FLAGS();
  168. if (HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin) > 0)
  169. {
  170. beep();
  171. rt_thread_mdelay(1000);
  172. // if (HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin)> 0 && HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin) > 0)
  173. // {
  174. // init_allflash();
  175. // __HAL_RCC_CLEAR_RESET_FLAGS();
  176. // beep();
  177. // rt_sem_take(thread_init_sem, RT_WAITING_FOREVER);
  178. // }
  179. // else
  180. if (HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin) > 0)
  181. {
  182. if (WeightConst_SBType == 6) WeightConst_SBType = 1;
  183. inMenu = 1;
  184. }
  185. }
  186. } else {
  187. if (HAL_GPIO_ReadPin(B4_GPIO_Port, B4_Pin) > 0 )
  188. {
  189. beep();
  190. rt_thread_mdelay(1000);
  191. if (HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin)> 0 && HAL_GPIO_ReadPin(B4_GPIO_Port, B4_Pin) > 0)
  192. {
  193. init_allflash();
  194. __HAL_RCC_CLEAR_RESET_FLAGS();
  195. beep();
  196. rt_sem_take(thread_init_sem, RT_WAITING_FOREVER);
  197. }
  198. else if (HAL_GPIO_ReadPin(B4_GPIO_Port, B4_Pin) > 0)
  199. {
  200. if (WeightConst_SBType == 6) WeightConst_SBType = 1;
  201. inMenu = 1;
  202. }
  203. }
  204. }
  205. MX_IWDG_Init();
  206. threadUart_init();
  207. // threadUart5_init();
  208. ReadWeight_thread();
  209. if (inMenu==1)
  210. {
  211. beep();
  212. MenuButtonthread();
  213. threadMenuDisplay_init();
  214. }
  215. else {
  216. Button_thread();
  217. SendWifi_thread();
  218. if (WeightConst_SBType != 2)
  219. {
  220. if (WeightConst_SBType == 4) threadDisplay_delay_init();
  221. delay_key_thread();
  222. threadDisplay_init();
  223. setTIME_thread();
  224. if (WeightConst_DELAYON==0)
  225. setWeightConst_initProduct();
  226. threadRemoteDisplay_init();
  227. sendgetTime();
  228. CheckWeight_thread();
  229. pop_product_thread();
  230. ReadEID_thread();
  231. }
  232. else
  233. {
  234. beep();
  235. CanButton = 1;
  236. }
  237. }
  238. /* USER CODE END 2 */
  239. /* Infinite loop */
  240. /* USER CODE BEGIN WHILE */
  241. while (1)
  242. {
  243. /* USER CODE END WHILE */
  244. /* USER CODE BEGIN 3 */
  245. rt_thread_mdelay(500);
  246. HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
  247. // HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
  248. // HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
  249. HAL_GPIO_TogglePin(led_GPIO_Port, led_Pin);
  250. }
  251. /* USER CODE END 3 */
  252. }
  253. /**
  254. * @brief System Clock Configuration
  255. * @retval None
  256. */
  257. void SystemClock_Config(void)
  258. {
  259. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  260. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  261. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  262. /** Initializes the RCC Oscillators according to the specified parameters
  263. * in the RCC_OscInitTypeDef structure.
  264. */
  265. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
  266. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  267. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  268. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  269. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  270. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  271. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  272. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  273. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  274. {
  275. Error_Handler();
  276. }
  277. /** Initializes the CPU, AHB and APB buses clocks
  278. */
  279. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  280. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  281. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  282. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  283. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  284. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  285. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  286. {
  287. Error_Handler();
  288. }
  289. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  290. PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  291. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  292. {
  293. Error_Handler();
  294. }
  295. }
  296. /* USER CODE BEGIN 4 */
  297. uint8_t rDataBuffer;
  298. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  299. {
  300. if (huart->Instance == UART5 && isTag>0){
  301. if (isTag>0) {
  302. USART_Push(5, rDataBuffer);
  303. rt_sem_release(thread_EmptyBuffer_sem);
  304. }
  305. HAL_UART_Receive_IT(&huart5, &rDataBuffer, 1);
  306. }
  307. }
  308. /* USER CODE END 4 */
  309. /**
  310. * @brief This function is executed in case of error occurrence.
  311. * @retval None
  312. */
  313. void Error_Handler(void)
  314. {
  315. /* USER CODE BEGIN Error_Handler_Debug */
  316. /* User can add his own implementation to report the HAL error return state */
  317. /* USER CODE END Error_Handler_Debug */
  318. }
  319. #ifdef USE_FULL_ASSERT
  320. /**
  321. * @brief Reports the name of the source file and the source line number
  322. * where the assert_param error has occurred.
  323. * @param file: pointer to the source file name
  324. * @param line: assert_param error line source number
  325. * @retval None
  326. */
  327. void assert_failed(uint8_t *file, uint32_t line)
  328. {
  329. /* USER CODE BEGIN 6 */
  330. /* User can add his own implementation to report the file name and line number,
  331. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  332. /* USER CODE END 6 */
  333. }
  334. #endif /* USE_FULL_ASSERT */
  335. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/