main(5814).c 14 KB

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