main(2218).c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. ** This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * COPYRIGHT(c) 2019 STMicroelectronics
  14. *
  15. * Redistribution and use in source and binary forms, with or without modification,
  16. * are permitted provided that the following conditions are met:
  17. * 1. Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. ******************************************************************************
  38. */
  39. /* USER CODE END Header */
  40. /* Includes ------------------------------------------------------------------*/
  41. #include "main.h"
  42. /* Private includes ----------------------------------------------------------*/
  43. /* USER CODE BEGIN Includes */
  44. /* USER CODE END Includes */
  45. /* Private typedef -----------------------------------------------------------*/
  46. /* USER CODE BEGIN PTD */
  47. uint8_t rx2_data[2];
  48. uint8_t ring_buf[buf_size];
  49. uint8_t count_in, count_out;
  50. uint8_t UartDataisReved;
  51. volatile uint32_t UartTimerCnt = 0;
  52. uint32_t LedTimerCnt = 0;
  53. uint8_t buf[buf_size] = {0,};
  54. /* USER CODE END PTD */
  55. /* Private define ------------------------------------------------------------*/
  56. /* USER CODE BEGIN PD */
  57. /* USER CODE END PD */
  58. /* Private macro -------------------------------------------------------------*/
  59. /* USER CODE BEGIN PM */
  60. /* USER CODE END PM */
  61. /* Private variables ---------------------------------------------------------*/
  62. TIM_HandleTypeDef htim7;
  63. UART_HandleTypeDef huart1;
  64. UART_HandleTypeDef huart2;
  65. /* USER CODE BEGIN PV */
  66. /* USER CODE END PV */
  67. /* Private function prototypes -----------------------------------------------*/
  68. void SystemClock_Config(void);
  69. static void MX_GPIO_Init(void);
  70. static void MX_USART1_UART_Init(void);
  71. static void MX_USART2_UART_Init(void);
  72. static void MX_TIM7_Init(void);
  73. static void MX_NVIC_Init(void);
  74. /* USER CODE BEGIN PFP */
  75. /* USER CODE END PFP */
  76. /* Private user code ---------------------------------------------------------*/
  77. /* USER CODE BEGIN 0 */
  78. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  79. {
  80. if(huart->Instance == USART1){
  81. ring_buf[count_in] = rx2_data[0];//(uint8_t)USART2->DR;
  82. // UartDataRecvSet(1);
  83. if(++count_in>=buf_size) count_in=0;
  84. HAL_UART_Receive_IT(&huart1,&rx2_data,1);
  85. }
  86. }
  87. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  88. {
  89. if(htim->Instance == TIM7){
  90. UartTimerCnt++;
  91. LedTimerCnt++;
  92. }
  93. }
  94. void UartDataRecvSet(uint8_t val){
  95. UartDataisReved = val;
  96. }
  97. uint8_t UartDataRecvGet(void){
  98. return UartDataisReved;
  99. }
  100. int _write (int file, uint8_t *ptr, uint16_t len)
  101. {
  102. HAL_UART_Transmit (&huart1, ptr, len, 10);
  103. return len;
  104. }
  105. /* USER CODE END 0 */
  106. /**
  107. * @brief The application entry point.
  108. * @retval int
  109. */
  110. int main(void)
  111. {
  112. /* USER CODE BEGIN 1 */
  113. uint8_t uartindex = 0;
  114. etError crccheck = 0;
  115. uint8_t tempdata[100] = {0,};
  116. /* USER CODE END 1 */
  117. /* MCU Configuration--------------------------------------------------------*/
  118. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  119. HAL_Init();
  120. /* USER CODE BEGIN Init */
  121. /* USER CODE END Init */
  122. /* Configure the system clock */
  123. SystemClock_Config();
  124. /* USER CODE BEGIN SysInit */
  125. /* USER CODE END SysInit */
  126. /* Initialize all configured peripherals */
  127. MX_GPIO_Init();
  128. MX_USART1_UART_Init();
  129. MX_USART2_UART_Init();
  130. MX_TIM7_Init();
  131. /* Initialize interrupts */
  132. MX_NVIC_Init();
  133. /* USER CODE BEGIN 2 */
  134. HAL_TIM_Base_Start_IT(&htim7);
  135. HAL_UART_Receive_IT(&huart1, &rx2_data,1);
  136. setbuf(stdout, NULL); // \n ?��?��?��, printf �???????��?���?????? ?��?��?��
  137. /* USER CODE END 2 */
  138. /* Infinite loop */
  139. /* USER CODE BEGIN WHILE */
  140. while (1)
  141. {
  142. if(count_in != count_out){
  143. UartTimerCnt = 0;
  144. buf[uartindex++] = ring_buf[count_out];
  145. if(++count_out >= buf_size) count_out=0;
  146. }
  147. if(UartDataRecvGet() == 1 && UartTimerCnt > 10){
  148. crccheck = STH30_CheckCrc(&buf[Bluecell_Type],buf[Bluecell_Length],buf[buf[Bluecell_Length] + 1]);
  149. if(crccheck == CHECKSUM_ERROR){`
  150. for(uint8_t i = 0; i < (uartindex); i++){
  151. printf("%02x ",buf[i]);
  152. }
  153. printf("\r\n");
  154. printf("CHECKSUM_ERROR RecvCRC : %02x \r\n",buf[buf[Bluecell_Length]]);
  155. }
  156. else if(crccheck == NO_ERROR){
  157. // Atten_Operate_Mem_RW(&buf[Bluecell_STX]);
  158. }
  159. else{
  160. printf("What Happen?\r\n");
  161. /*NOP*/
  162. }
  163. memset(buf,0x00,buf_size);
  164. uartindex = 0;
  165. UartDataRecvSet(0);
  166. memset(buf,0x00,buf_size);
  167. }
  168. else{
  169. if(LedTimerCnt > 500){
  170. #if 0 // PYJ.2019.03.12_BEGIN --
  171. tempdata[Bluecell_STX] = 0xBE;
  172. tempdata[Bluecell_Type] = ATT_AB_ALARM_READ;
  173. tempdata[Bluecell_Length] = sizeof(Atten_Alarm_t) + 2;
  174. tempdata[tempdata[Bluecell_Length] + 3] = STH30_CreateCrc(&tempdata[Bluecell_Type],tempdata[Bluecell_Length] + 2);
  175. tempdata[tempdata[Bluecell_Length] + 4] = 0xeb;
  176. #endif // PYJ.2019.03.12_END --
  177. HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_15);
  178. LedTimerCnt = 0;
  179. }
  180. }
  181. /* USER CODE END WHILE */
  182. /* USER CODE BEGIN 3 */
  183. }
  184. /* USER CODE END 3 */
  185. }
  186. /**
  187. * @brief System Clock Configuration
  188. * @retval None
  189. */
  190. void SystemClock_Config(void)
  191. {
  192. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  193. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  194. /**Initializes the CPU, AHB and APB busses clocks
  195. */
  196. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  197. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  198. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  199. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  200. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  201. {
  202. Error_Handler();
  203. }
  204. /**Initializes the CPU, AHB and APB busses clocks
  205. */
  206. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  207. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  208. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  209. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  210. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  211. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  212. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  213. {
  214. Error_Handler();
  215. }
  216. }
  217. /**
  218. * @brief NVIC Configuration.
  219. * @retval None
  220. */
  221. static void MX_NVIC_Init(void)
  222. {
  223. /* USART1_IRQn interrupt configuration */
  224. HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
  225. HAL_NVIC_EnableIRQ(USART1_IRQn);
  226. /* USART2_IRQn interrupt configuration */
  227. HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
  228. HAL_NVIC_EnableIRQ(USART2_IRQn);
  229. /* TIM7_IRQn interrupt configuration */
  230. HAL_NVIC_SetPriority(TIM7_IRQn, 0, 0);
  231. HAL_NVIC_EnableIRQ(TIM7_IRQn);
  232. }
  233. /**
  234. * @brief TIM7 Initialization Function
  235. * @param None
  236. * @retval None
  237. */
  238. static void MX_TIM7_Init(void)
  239. {
  240. /* USER CODE BEGIN TIM7_Init 0 */
  241. /* USER CODE END TIM7_Init 0 */
  242. TIM_MasterConfigTypeDef sMasterConfig = {0};
  243. /* USER CODE BEGIN TIM7_Init 1 */
  244. /* USER CODE END TIM7_Init 1 */
  245. htim7.Instance = TIM7;
  246. htim7.Init.Prescaler = 800 - 1;
  247. htim7.Init.CounterMode = TIM_COUNTERMODE_UP;
  248. htim7.Init.Period = 10- 1;
  249. htim7.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  250. if (HAL_TIM_Base_Init(&htim7) != HAL_OK)
  251. {
  252. Error_Handler();
  253. }
  254. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  255. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  256. if (HAL_TIMEx_MasterConfigSynchronization(&htim7, &sMasterConfig) != HAL_OK)
  257. {
  258. Error_Handler();
  259. }
  260. /* USER CODE BEGIN TIM7_Init 2 */
  261. /* USER CODE END TIM7_Init 2 */
  262. }
  263. /**
  264. * @brief USART1 Initialization Function
  265. * @param None
  266. * @retval None
  267. */
  268. static void MX_USART1_UART_Init(void)
  269. {
  270. /* USER CODE BEGIN USART1_Init 0 */
  271. /* USER CODE END USART1_Init 0 */
  272. /* USER CODE BEGIN USART1_Init 1 */
  273. /* USER CODE END USART1_Init 1 */
  274. huart1.Instance = USART1;
  275. huart1.Init.BaudRate = 115200;
  276. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  277. huart1.Init.StopBits = UART_STOPBITS_1;
  278. huart1.Init.Parity = UART_PARITY_NONE;
  279. huart1.Init.Mode = UART_MODE_TX_RX;
  280. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  281. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  282. if (HAL_UART_Init(&huart1) != HAL_OK)
  283. {
  284. Error_Handler();
  285. }
  286. /* USER CODE BEGIN USART1_Init 2 */
  287. /* USER CODE END USART1_Init 2 */
  288. }
  289. /**
  290. * @brief USART2 Initialization Function
  291. * @param None
  292. * @retval None
  293. */
  294. static void MX_USART2_UART_Init(void)
  295. {
  296. /* USER CODE BEGIN USART2_Init 0 */
  297. /* USER CODE END USART2_Init 0 */
  298. /* USER CODE BEGIN USART2_Init 1 */
  299. /* USER CODE END USART2_Init 1 */
  300. huart2.Instance = USART2;
  301. huart2.Init.BaudRate = 115200;
  302. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  303. huart2.Init.StopBits = UART_STOPBITS_1;
  304. huart2.Init.Parity = UART_PARITY_NONE;
  305. huart2.Init.Mode = UART_MODE_TX_RX;
  306. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  307. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  308. if (HAL_UART_Init(&huart2) != HAL_OK)
  309. {
  310. Error_Handler();
  311. }
  312. /* USER CODE BEGIN USART2_Init 2 */
  313. /* USER CODE END USART2_Init 2 */
  314. }
  315. /**
  316. * @brief GPIO Initialization Function
  317. * @param None
  318. * @retval None
  319. */
  320. static void MX_GPIO_Init(void)
  321. {
  322. GPIO_InitTypeDef GPIO_InitStruct = {0};
  323. /* GPIO Ports Clock Enable */
  324. __HAL_RCC_GPIOC_CLK_ENABLE();
  325. __HAL_RCC_GPIOA_CLK_ENABLE();
  326. /*Configure GPIO pin Output Level */
  327. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_15, GPIO_PIN_RESET);
  328. /*Configure GPIO pin : PC15 */
  329. GPIO_InitStruct.Pin = GPIO_PIN_15;
  330. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  331. GPIO_InitStruct.Pull = GPIO_NOPULL;
  332. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  333. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  334. }
  335. /* USER CODE BEGIN 4 */
  336. /* USER CODE END 4 */
  337. /**
  338. * @brief This function is executed in case of error occurrence.
  339. * @retval None
  340. */
  341. void Error_Handler(void)
  342. {
  343. /* USER CODE BEGIN Error_Handler_Debug */
  344. /* User can add his own implementation to report the HAL error return state */
  345. /* USER CODE END Error_Handler_Debug */
  346. }
  347. #ifdef USE_FULL_ASSERT
  348. /**
  349. * @brief Reports the name of the source file and the source line number
  350. * where the assert_param error has occurred.
  351. * @param file: pointer to the source file name
  352. * @param line: assert_param error line source number
  353. * @retval None
  354. */
  355. void assert_failed(uint8_t *file, uint32_t line)
  356. {
  357. /* USER CODE BEGIN 6 */
  358. /* User can add his own implementation to report the file name and line number,
  359. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  360. /* USER CODE END 6 */
  361. }
  362. #endif /* USE_FULL_ASSERT */
  363. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/