main(6512).c 14 KB

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