main(7137).c 15 KB

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