main(6692).c 14 KB

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