main(7805).c 15 KB

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