main(6668).c 14 KB

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