main(211).c 14 KB

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