main(5936).c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. I2C_HandleTypeDef hi2c1;
  63. TIM_HandleTypeDef htim7;
  64. UART_HandleTypeDef huart1;
  65. UART_HandleTypeDef huart2;
  66. /* USER CODE BEGIN PV */
  67. /* USER CODE END PV */
  68. /* Private function prototypes -----------------------------------------------*/
  69. void SystemClock_Config(void);
  70. static void MX_GPIO_Init(void);
  71. static void MX_USART1_UART_Init(void);
  72. static void MX_USART2_UART_Init(void);
  73. static void MX_TIM7_Init(void);
  74. static void MX_I2C1_Init(void);
  75. static void MX_NVIC_Init(void);
  76. /* USER CODE BEGIN PFP */
  77. /* USER CODE END PFP */
  78. /* Private user code ---------------------------------------------------------*/
  79. /* USER CODE BEGIN 0 */
  80. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  81. {
  82. if(huart->Instance == USART1){
  83. ring_buf[count_in] = rx2_data[0];//(uint8_t)USART2->DR;
  84. // UartDataRecvSet(1);
  85. if(++count_in>=buf_size) count_in=0;
  86. HAL_UART_Receive_IT(&huart1,&rx2_data,1);
  87. }
  88. }
  89. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  90. {
  91. if(htim->Instance == TIM7){
  92. UartTimerCnt++;
  93. LedTimerCnt++;
  94. }
  95. }
  96. void UartDataRecvSet(uint8_t val){
  97. UartDataisReved = val;
  98. }
  99. uint8_t UartDataRecvGet(void){
  100. return UartDataisReved;
  101. }
  102. int _write (int file, uint8_t *ptr, uint16_t len)
  103. {
  104. HAL_UART_Transmit (&huart1, ptr, len, 10);
  105. return len;
  106. }
  107. /* USER CODE END 0 */
  108. /**
  109. * @brief The application entry point.
  110. * @retval int
  111. */
  112. int main(void)
  113. {
  114. /* USER CODE BEGIN 1 */
  115. uint8_t uartindex = 0;
  116. etError crccheck = 0;
  117. uint8_t tempdata[100] = {0,};
  118. /* USER CODE END 1 */
  119. /* MCU Configuration--------------------------------------------------------*/
  120. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  121. HAL_Init();
  122. /* USER CODE BEGIN Init */
  123. /* USER CODE END Init */
  124. /* Configure the system clock */
  125. SystemClock_Config();
  126. /* USER CODE BEGIN SysInit */
  127. /* USER CODE END SysInit */
  128. /* Initialize all configured peripherals */
  129. MX_GPIO_Init();
  130. MX_USART1_UART_Init();
  131. MX_USART2_UART_Init();
  132. MX_TIM7_Init();
  133. MX_I2C1_Init();
  134. /* Initialize interrupts */
  135. MX_NVIC_Init();
  136. /* USER CODE BEGIN 2 */
  137. HAL_TIM_Base_Start_IT(&htim7);
  138. HAL_UART_Receive_IT(&huart1, &rx2_data,1);
  139. setbuf(stdout, NULL); // \n ?��?��?��, printf �???????��?���?????? ?��?��?��
  140. /* USER CODE END 2 */
  141. /* Infinite loop */
  142. /* USER CODE BEGIN WHILE */
  143. while (1)
  144. {
  145. if(count_in != count_out){
  146. UartTimerCnt = 0;
  147. buf[uartindex++] = ring_buf[count_out];
  148. if(++count_out >= buf_size) count_out=0;
  149. }
  150. if(UartDataRecvGet() == 1 && UartTimerCnt > 10){
  151. crccheck = STH30_CheckCrc(&buf[Bluecell_Type],buf[Bluecell_Length]+2,buf[3 + buf[Bluecell_Length]]);
  152. if(crccheck == CHECKSUM_ERROR){`
  153. for(uint8_t i = 0; i < (uartindex); i++){
  154. printf("%02x ",buf[i]);
  155. }
  156. printf("\r\n");
  157. printf("CHECKSUM_ERROR RecvCRC : %02x \r\n",buf[buf[Bluecell_Length]]);
  158. }
  159. else if(crccheck == NO_ERROR){
  160. // Atten_Operate_Mem_RW(&buf[Bluecell_STX]);
  161. }
  162. else{
  163. printf("What Happen?\r\n");
  164. /*NOP*/
  165. }
  166. memset(buf,0x00,buf_size);
  167. uartindex = 0;
  168. UartDataRecvSet(0);
  169. memset(buf,0x00,buf_size);
  170. }
  171. else{
  172. if(LedTimerCnt > 500){
  173. #if 0 // PYJ.2019.03.12_BEGIN --
  174. tempdata[Bluecell_STX] = 0xBE;
  175. tempdata[Bluecell_Type] = ATT_AB_ALARM_READ;
  176. tempdata[Bluecell_Length] = sizeof(Atten_Alarm_t) + 2;
  177. tempdata[tempdata[Bluecell_Length] + 3] = STH30_CreateCrc(&tempdata[Bluecell_Type],tempdata[Bluecell_Length] + 2);
  178. tempdata[tempdata[Bluecell_Length] + 4] = 0xeb;
  179. #endif // PYJ.2019.03.12_END --
  180. HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_15);
  181. LedTimerCnt = 0;
  182. }
  183. }
  184. /* USER CODE END WHILE */
  185. /* USER CODE BEGIN 3 */
  186. }
  187. /* USER CODE END 3 */
  188. }
  189. /**
  190. * @brief System Clock Configuration
  191. * @retval None
  192. */
  193. void SystemClock_Config(void)
  194. {
  195. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  196. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  197. /**Initializes the CPU, AHB and APB busses clocks
  198. */
  199. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  200. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  201. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  202. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  203. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  204. {
  205. Error_Handler();
  206. }
  207. /**Initializes the CPU, AHB and APB busses clocks
  208. */
  209. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  210. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  211. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  212. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  213. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  214. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  215. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  216. {
  217. Error_Handler();
  218. }
  219. }
  220. /**
  221. * @brief NVIC Configuration.
  222. * @retval None
  223. */
  224. static void MX_NVIC_Init(void)
  225. {
  226. /* USART1_IRQn interrupt configuration */
  227. HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
  228. HAL_NVIC_EnableIRQ(USART1_IRQn);
  229. /* USART2_IRQn interrupt configuration */
  230. HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
  231. HAL_NVIC_EnableIRQ(USART2_IRQn);
  232. /* TIM7_IRQn interrupt configuration */
  233. HAL_NVIC_SetPriority(TIM7_IRQn, 0, 0);
  234. HAL_NVIC_EnableIRQ(TIM7_IRQn);
  235. }
  236. /**
  237. * @brief I2C1 Initialization Function
  238. * @param None
  239. * @retval None
  240. */
  241. static void MX_I2C1_Init(void)
  242. {
  243. /* USER CODE BEGIN I2C1_Init 0 */
  244. /* USER CODE END I2C1_Init 0 */
  245. /* USER CODE BEGIN I2C1_Init 1 */
  246. /* USER CODE END I2C1_Init 1 */
  247. hi2c1.Instance = I2C1;
  248. hi2c1.Init.ClockSpeed = 100000;
  249. hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  250. hi2c1.Init.OwnAddress1 = 0;
  251. hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  252. hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  253. hi2c1.Init.OwnAddress2 = 0;
  254. hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  255. hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  256. if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  257. {
  258. Error_Handler();
  259. }
  260. /* USER CODE BEGIN I2C1_Init 2 */
  261. /* USER CODE END I2C1_Init 2 */
  262. }
  263. /**
  264. * @brief TIM7 Initialization Function
  265. * @param None
  266. * @retval None
  267. */
  268. static void MX_TIM7_Init(void)
  269. {
  270. /* USER CODE BEGIN TIM7_Init 0 */
  271. /* USER CODE END TIM7_Init 0 */
  272. TIM_MasterConfigTypeDef sMasterConfig = {0};
  273. /* USER CODE BEGIN TIM7_Init 1 */
  274. /* USER CODE END TIM7_Init 1 */
  275. htim7.Instance = TIM7;
  276. htim7.Init.Prescaler = 800 - 1;
  277. htim7.Init.CounterMode = TIM_COUNTERMODE_UP;
  278. htim7.Init.Period = 10- 1;
  279. htim7.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  280. if (HAL_TIM_Base_Init(&htim7) != HAL_OK)
  281. {
  282. Error_Handler();
  283. }
  284. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  285. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  286. if (HAL_TIMEx_MasterConfigSynchronization(&htim7, &sMasterConfig) != HAL_OK)
  287. {
  288. Error_Handler();
  289. }
  290. /* USER CODE BEGIN TIM7_Init 2 */
  291. /* USER CODE END TIM7_Init 2 */
  292. }
  293. /**
  294. * @brief USART1 Initialization Function
  295. * @param None
  296. * @retval None
  297. */
  298. static void MX_USART1_UART_Init(void)
  299. {
  300. /* USER CODE BEGIN USART1_Init 0 */
  301. /* USER CODE END USART1_Init 0 */
  302. /* USER CODE BEGIN USART1_Init 1 */
  303. /* USER CODE END USART1_Init 1 */
  304. huart1.Instance = USART1;
  305. huart1.Init.BaudRate = 115200;
  306. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  307. huart1.Init.StopBits = UART_STOPBITS_1;
  308. huart1.Init.Parity = UART_PARITY_NONE;
  309. huart1.Init.Mode = UART_MODE_TX_RX;
  310. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  311. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  312. if (HAL_UART_Init(&huart1) != HAL_OK)
  313. {
  314. Error_Handler();
  315. }
  316. /* USER CODE BEGIN USART1_Init 2 */
  317. /* USER CODE END USART1_Init 2 */
  318. }
  319. /**
  320. * @brief USART2 Initialization Function
  321. * @param None
  322. * @retval None
  323. */
  324. static void MX_USART2_UART_Init(void)
  325. {
  326. /* USER CODE BEGIN USART2_Init 0 */
  327. /* USER CODE END USART2_Init 0 */
  328. /* USER CODE BEGIN USART2_Init 1 */
  329. /* USER CODE END USART2_Init 1 */
  330. huart2.Instance = USART2;
  331. huart2.Init.BaudRate = 115200;
  332. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  333. huart2.Init.StopBits = UART_STOPBITS_1;
  334. huart2.Init.Parity = UART_PARITY_NONE;
  335. huart2.Init.Mode = UART_MODE_TX_RX;
  336. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  337. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  338. if (HAL_UART_Init(&huart2) != HAL_OK)
  339. {
  340. Error_Handler();
  341. }
  342. /* USER CODE BEGIN USART2_Init 2 */
  343. /* USER CODE END USART2_Init 2 */
  344. }
  345. /**
  346. * @brief GPIO Initialization Function
  347. * @param None
  348. * @retval None
  349. */
  350. static void MX_GPIO_Init(void)
  351. {
  352. GPIO_InitTypeDef GPIO_InitStruct = {0};
  353. /* GPIO Ports Clock Enable */
  354. __HAL_RCC_GPIOC_CLK_ENABLE();
  355. __HAL_RCC_GPIOA_CLK_ENABLE();
  356. __HAL_RCC_GPIOB_CLK_ENABLE();
  357. /*Configure GPIO pin Output Level */
  358. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_15, GPIO_PIN_RESET);
  359. /*Configure GPIO pin Output Level */
  360. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8|GPIO_PIN_15, GPIO_PIN_RESET);
  361. /*Configure GPIO pin : PC15 */
  362. GPIO_InitStruct.Pin = GPIO_PIN_15;
  363. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  364. GPIO_InitStruct.Pull = GPIO_NOPULL;
  365. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  366. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  367. /*Configure GPIO pins : PA8 PA15 */
  368. GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_15;
  369. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  370. GPIO_InitStruct.Pull = GPIO_NOPULL;
  371. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  372. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  373. }
  374. /* USER CODE BEGIN 4 */
  375. /* USER CODE END 4 */
  376. /**
  377. * @brief This function is executed in case of error occurrence.
  378. * @retval None
  379. */
  380. void Error_Handler(void)
  381. {
  382. /* USER CODE BEGIN Error_Handler_Debug */
  383. /* User can add his own implementation to report the HAL error return state */
  384. /* USER CODE END Error_Handler_Debug */
  385. }
  386. #ifdef USE_FULL_ASSERT
  387. /**
  388. * @brief Reports the name of the source file and the source line number
  389. * where the assert_param error has occurred.
  390. * @param file: pointer to the source file name
  391. * @param line: assert_param error line source number
  392. * @retval None
  393. */
  394. void assert_failed(uint8_t *file, uint32_t line)
  395. {
  396. /* USER CODE BEGIN 6 */
  397. /* User can add his own implementation to report the file name and line number,
  398. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  399. /* USER CODE END 6 */
  400. }
  401. #endif /* USE_FULL_ASSERT */
  402. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/