main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. if (HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin) > 0)
  167. {
  168. beep();
  169. rt_thread_mdelay(1000);
  170. if (HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin)> 0 && HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin) > 0)
  171. {
  172. init_allflash();
  173. __HAL_RCC_CLEAR_RESET_FLAGS();
  174. beep();
  175. rt_sem_take(thread_init_sem, RT_WAITING_FOREVER);
  176. }
  177. else if (HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin) > 0)
  178. {
  179. if (WeightConst_SBType == 6) WeightConst_SBType = 1;
  180. inMenu = 1;
  181. }
  182. }
  183. } else {
  184. if (HAL_GPIO_ReadPin(B4_GPIO_Port, B4_Pin) > 0 )
  185. {
  186. beep();
  187. rt_thread_mdelay(1000);
  188. if (HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin)> 0 && HAL_GPIO_ReadPin(B4_GPIO_Port, B4_Pin) > 0)
  189. {
  190. init_allflash();
  191. __HAL_RCC_CLEAR_RESET_FLAGS();
  192. beep();
  193. rt_sem_take(thread_init_sem, RT_WAITING_FOREVER);
  194. }
  195. else if (HAL_GPIO_ReadPin(B4_GPIO_Port, B4_Pin) > 0)
  196. {
  197. if (WeightConst_SBType == 6) WeightConst_SBType = 1;
  198. inMenu = 1;
  199. }
  200. }
  201. }
  202. MX_IWDG_Init();
  203. threadUart_init();
  204. // threadUart5_init();
  205. ReadWeight_thread();
  206. if (inMenu==1)
  207. {
  208. beep();
  209. MenuButtonthread();
  210. threadMenuDisplay_init();
  211. }
  212. else {
  213. Button_thread();
  214. SendWifi_thread();
  215. if (WeightConst_SBType != 2)
  216. {
  217. if (WeightConst_SBType == 4) threadDisplay_delay_init();
  218. delay_key_thread();
  219. threadDisplay_init();
  220. setTIME_thread();
  221. if (WeightConst_DELAYON==0)
  222. setWeightConst_initProduct();
  223. threadRemoteDisplay_init();
  224. sendgetTime();
  225. CheckWeight_thread();
  226. pop_product_thread();
  227. ReadEID_thread();
  228. }
  229. else
  230. {
  231. beep();
  232. CanButton = 1;
  233. }
  234. }
  235. /* USER CODE END 2 */
  236. /* Infinite loop */
  237. /* USER CODE BEGIN WHILE */
  238. while (1)
  239. {
  240. /* USER CODE END WHILE */
  241. /* USER CODE BEGIN 3 */
  242. rt_thread_mdelay(500);
  243. HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
  244. // HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
  245. // HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
  246. HAL_GPIO_TogglePin(led_GPIO_Port, led_Pin);
  247. }
  248. /* USER CODE END 3 */
  249. }
  250. /**
  251. * @brief System Clock Configuration
  252. * @retval None
  253. */
  254. void SystemClock_Config(void)
  255. {
  256. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  257. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  258. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  259. /** Initializes the RCC Oscillators according to the specified parameters
  260. * in the RCC_OscInitTypeDef structure.
  261. */
  262. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
  263. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  264. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  265. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  266. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  267. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  268. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  269. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  270. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  271. {
  272. Error_Handler();
  273. }
  274. /** Initializes the CPU, AHB and APB buses clocks
  275. */
  276. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  277. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  278. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  279. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  280. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  281. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  282. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  283. {
  284. Error_Handler();
  285. }
  286. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  287. PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  288. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  289. {
  290. Error_Handler();
  291. }
  292. }
  293. /* USER CODE BEGIN 4 */
  294. uint8_t rDataBuffer;
  295. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  296. {
  297. if (huart->Instance == UART5 && isTag>0){
  298. if (isTag>0) {
  299. USART_Push(5, rDataBuffer);
  300. rt_sem_release(thread_EmptyBuffer_sem);
  301. }
  302. HAL_UART_Receive_IT(&huart5, &rDataBuffer, 1);
  303. }
  304. }
  305. /* USER CODE END 4 */
  306. /**
  307. * @brief This function is executed in case of error occurrence.
  308. * @retval None
  309. */
  310. void Error_Handler(void)
  311. {
  312. /* USER CODE BEGIN Error_Handler_Debug */
  313. /* User can add his own implementation to report the HAL error return state */
  314. /* USER CODE END Error_Handler_Debug */
  315. }
  316. #ifdef USE_FULL_ASSERT
  317. /**
  318. * @brief Reports the name of the source file and the source line number
  319. * where the assert_param error has occurred.
  320. * @param file: pointer to the source file name
  321. * @param line: assert_param error line source number
  322. * @retval None
  323. */
  324. void assert_failed(uint8_t *file, uint32_t line)
  325. {
  326. /* USER CODE BEGIN 6 */
  327. /* User can add his own implementation to report the file name and line number,
  328. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  329. /* USER CODE END 6 */
  330. }
  331. #endif /* USE_FULL_ASSERT */
  332. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/