main.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. /* USER CODE END PTD */
  48. /* Private define ------------------------------------------------------------*/
  49. /* USER CODE BEGIN PD */
  50. /* USER CODE END PD */
  51. /* Private macro -------------------------------------------------------------*/
  52. /* USER CODE BEGIN PM */
  53. /* USER CODE END PM */
  54. /* Private variables ---------------------------------------------------------*/
  55. TIM_HandleTypeDef htim7;
  56. UART_HandleTypeDef huart1;
  57. /* USER CODE BEGIN PV */
  58. uint8_t ring_buffer[255];
  59. uint32_t ring_header = 0;
  60. uint32_t ring_tail = 0;
  61. volatile uint32_t UartTimerCnt = 0;
  62. volatile uint32_t LedTimerCnt = 0;
  63. volatile uint32_t FirmwareTimerCnt = 0;
  64. volatile uint8_t rx3_data[1] = {0,};
  65. #define FirmwareUpdataAck 0x11
  66. #define FirmwareUpdataNak 0x22
  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_TIM7_Init(void);
  73. static void MX_NVIC_Init(void);
  74. /* USER CODE BEGIN PFP */
  75. void Uart1_Data_Send(uint8_t* data,uint8_t size);
  76. /* USER CODE END PFP */
  77. /* Private user code ---------------------------------------------------------*/
  78. /* USER CODE BEGIN 0 */
  79. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  80. {
  81. if(huart->Instance == USART1)//RGB Comunication
  82. {
  83. ring_buffer[ring_header] = rx3_data[0];
  84. if(++ring_header>=100){ ring_header = 0; }
  85. HAL_UART_Receive_IT(&huart1,&rx3_data[0],1);
  86. }
  87. }
  88. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  89. {
  90. if(htim->Instance == TIM7){
  91. UartTimerCnt++;
  92. LedTimerCnt++;
  93. FirmwareTimerCnt++;
  94. }
  95. }
  96. void Uart1_Data_Send(uint8_t* data,uint8_t size){
  97. HAL_UART_Transmit(&huart1, data,size, 10);
  98. }
  99. int _write (int file, uint8_t *ptr, uint16_t len)
  100. {
  101. HAL_UART_Transmit (&huart1, ptr, len, 10);
  102. return len;
  103. }
  104. #define StartAddr ((uint32_t)0x08004000)
  105. #if 1 // PYJ.2019.03.19_BEGIN --
  106. #define FLASH_USER_START_ADDR StartAddr /* Start @ of user Flash area */
  107. #define FLASH_USER_END_ADDR StartAddr + ((uint32_t)0x000FFFF) /* End @ of user Flash area */
  108. uint32_t Address = StartAddr;
  109. uint8_t Flash_RGB_Data_Write(uint8_t* data){
  110. uint16_t Firmdata = 0;
  111. uint8_t ret = 0;
  112. for(uint8_t i = 0; i < data[bluecell_length] - 2; i+=2){
  113. Firmdata = ((data[(bluecell_length + 1) + i]) & 0x00FF);
  114. Firmdata += ((data[(bluecell_length + 1) + (i + 1)] << 8) & 0xFF00);
  115. if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,Address , (uint16_t)Firmdata) != HAL_OK){
  116. printf("HAL NOT OK \n");
  117. ret = 1;
  118. }
  119. Address += 2;
  120. }
  121. return ret;
  122. }
  123. uint8_t flashinit = 0;
  124. uint8_t Flash_write(uint8_t* data) // ?^‘e↑‘i?:???
  125. {
  126. /*Variable used for Erase procedure*/
  127. static FLASH_EraseInitTypeDef EraseInitStruct;
  128. static uint32_t PAGEError = 0;
  129. uint8_t ret = 0;
  130. /* Fill EraseInit structure*/
  131. EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  132. EraseInitStruct.PageAddress = FLASH_USER_START_ADDR;
  133. EraseInitStruct.NbPages = (FLASH_USER_END_ADDR - FLASH_USER_START_ADDR) / FLASH_PAGE_SIZE;
  134. __HAL_RCC_TIM7_CLK_DISABLE(); // e」�i?↑????▲e:↑e?? ???i」???�?????�
  135. HAL_FLASH_Unlock(); // lock ??e↑?
  136. if(flashinit == 0){
  137. flashinit= 1;
  138. //FLASH_PageErase(StartAddr);
  139. if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK){
  140. printf("Erase Failed \r\n");
  141. }
  142. }
  143. ret = Flash_RGB_Data_Write(&data[bluecell_stx]);
  144. HAL_FLASH_Lock(); // lock ???e,↑e↑‘
  145. __HAL_RCC_TIM7_CLK_ENABLE(); // e」�i?↑????▲e:↑e?? ??◇??�??`??�?????�
  146. return ret;
  147. }
  148. void Flash_InitRead(void) // ?^‘e↑‘i?:???
  149. {
  150. uint32_t Address = 0;
  151. Address = StartAddr;
  152. for(uint32_t i = 0; i < 16; i++ ){
  153. printf("%08x : %02X \n",Address ,*(uint8_t*)Address);
  154. Address++;
  155. }
  156. }
  157. typedef void (*fptr)(void);
  158. fptr jump_to_app;
  159. uint32_t jump_addr;
  160. void Jump_App(void){
  161. __HAL_RCC_TIM7_CLK_DISABLE(); // e」�i?↑????▲e:↑e?? ???i」???�?????�
  162. printf("boot loader start\n"); //e� ̄i?↑i」∽ i��e?〕
  163. jump_addr = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4);
  164. jump_to_app = (fptr) jump_addr;
  165. /* init user app's sp */
  166. printf("jump!\n");
  167. __set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS);
  168. jump_to_app();
  169. }
  170. void FirmwareUpdateStart(uint8_t* data){
  171. uint8_t ret = 0,crccheck = 0;
  172. uint8_t tempdata[5] = {0xbe,FirmwareUpdataAck,0x02,0,0xbe};
  173. crccheck = STH30_CheckCrc(&data[bluecell_type],data[bluecell_length],data[data[bluecell_length] + 1]);
  174. if(crccheck == NO_ERROR){
  175. tempdata[bluecell_type] = FirmwareUpdataAck;
  176. if(data[bluecell_type] == 0xDD || data[bluecell_type] == 0xEE)//Start Firmware byte
  177. ret = Flash_write(&data[0]);
  178. if(ret == 1)
  179. tempdata[bluecell_type] = FirmwareUpdataNak;
  180. }else{
  181. for(uint8_t i = 0; i < data[bluecell_length] + 3; i++)
  182. printf("%02x ",data[i]);
  183. printf("Check Sum error \n");
  184. tempdata[bluecell_type] = FirmwareUpdataNak;
  185. }
  186. tempdata[bluecell_crc] = STH30_CreateCrc(&tempdata[bluecell_type],tempdata[bluecell_length]);
  187. if(data[bluecell_type] != 0xEE && data[bluecell_type] != RGB_Reset){
  188. Uart1_Data_Send(&tempdata[bluecell_stx],tempdata[bluecell_length] + 3);
  189. }
  190. if(data[bluecell_type] == 0xEE)
  191. printf("update Complete \n");
  192. }
  193. #endif // PYJ.2019.03.19_END --
  194. typedef void (*pFunction)(void);
  195. pFunction JumpToApplication;
  196. uint32_t JumpAddress;
  197. uint32_t FlashProtection = 0;
  198. /* USER CODE END 0 */
  199. /**
  200. * @brief The application entry point.
  201. * @retval int
  202. */
  203. int main(void)
  204. {
  205. /* USER CODE BEGIN 1 */
  206. uint8_t data[255] = {0,};
  207. uint8_t bootdata[5] = {0xbe,RGB_BootStart,0x02,0,0xeb};
  208. uint8_t cnt = 0;
  209. uint8_t uartrecv = 0;
  210. /* USER CODE END 1 */
  211. /* MCU Configuration--------------------------------------------------------*/
  212. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  213. HAL_Init();
  214. /* USER CODE BEGIN Init */
  215. /* USER CODE END Init */
  216. /* Configure the system clock */
  217. SystemClock_Config();
  218. /* USER CODE BEGIN SysInit */
  219. /* USER CODE END SysInit */
  220. /* Initialize all configured peripherals */
  221. MX_GPIO_Init();
  222. MX_USART1_UART_Init();
  223. MX_TIM7_Init();
  224. /* Initialize interrupts */
  225. MX_NVIC_Init();
  226. /* USER CODE BEGIN 2 */
  227. HAL_TIM_Base_Start_IT(&htim7);
  228. HAL_UART_Receive_IT(&huart1, &rx3_data[0],1);
  229. setbuf(stdout, NULL); // \n ?i��i��?i��i��?i��i��, printf i�???????i��i��?i��i��i�?????? ?i��i��?i��i��?i��i��
  230. #if 1 // PYJ.2019.03.04_BEGIN --
  231. printf("****************************************\r\n");
  232. printf("RGB Project\r\n");
  233. printf("Build at %s %s\r\n", __DATE__, __TIME__);
  234. printf("Copyright (c) 2019. BLUECELL\r\n");
  235. printf("****************************************\r\n");
  236. #endif // PYJ.2019.03.04_END --
  237. //RGB_Sensor_PowerOnOff(0);
  238. FLASH_If_Init();
  239. Flash_InitRead();
  240. //Flash_InitRead();
  241. bootdata[bluecell_crc] = STH30_CreateCrc(&bootdata[bluecell_type],bootdata[bluecell_length]);
  242. HAL_Delay(100);
  243. Uart1_Data_Send(&bootdata[bluecell_stx],bootdata[bluecell_length] + 3);
  244. /* USER CODE END 2 */
  245. /* Infinite loop */
  246. /* USER CODE BEGIN WHILE */
  247. while (1)
  248. {
  249. /* Test if user code is programmed starting from address "APPLICATION_ADDRESS" */
  250. if(FirmwareTimerCnt > 3000){
  251. Jump_App();
  252. }
  253. if(ring_tail != ring_header){ // <-------
  254. data[cnt++] = ring_buffer[ring_tail++];
  255. if(ring_tail >= 100){ ring_tail = 0; }
  256. UartTimerCnt = 0;
  257. uartrecv = 1;
  258. }
  259. if(uartrecv == 1 && UartTimerCnt > 100){
  260. FirmwareUpdateStart(&data[0]);
  261. memset(&data[0],0,100);
  262. cnt = 0;
  263. uartrecv = 0;
  264. FirmwareTimerCnt = 0;
  265. }
  266. if(LedTimerCnt > 500){
  267. HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_15);
  268. LedTimerCnt = 0;
  269. }
  270. /* USER CODE END WHILE */
  271. /* USER CODE BEGIN 3 */
  272. }
  273. /* USER CODE END 3 */
  274. }
  275. /**
  276. * @brief System Clock Configuration
  277. * @retval None
  278. */
  279. void SystemClock_Config(void)
  280. {
  281. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  282. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  283. /**Initializes the CPU, AHB and APB busses clocks
  284. */
  285. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  286. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  287. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  288. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  289. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  290. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4;
  291. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  292. {
  293. Error_Handler();
  294. }
  295. /**Initializes the CPU, AHB and APB busses clocks
  296. */
  297. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  298. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  299. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  300. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  301. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  302. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  303. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  304. {
  305. Error_Handler();
  306. }
  307. }
  308. /**
  309. * @brief NVIC Configuration.
  310. * @retval None
  311. */
  312. static void MX_NVIC_Init(void)
  313. {
  314. /* USART1_IRQn interrupt configuration */
  315. HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
  316. HAL_NVIC_EnableIRQ(USART1_IRQn);
  317. /* TIM7_IRQn interrupt configuration */
  318. HAL_NVIC_SetPriority(TIM7_IRQn, 0, 0);
  319. HAL_NVIC_EnableIRQ(TIM7_IRQn);
  320. }
  321. /**
  322. * @brief TIM7 Initialization Function
  323. * @param None
  324. * @retval None
  325. */
  326. static void MX_TIM7_Init(void)
  327. {
  328. /* USER CODE BEGIN TIM7_Init 0 */
  329. /* USER CODE END TIM7_Init 0 */
  330. TIM_MasterConfigTypeDef sMasterConfig = {0};
  331. /* USER CODE BEGIN TIM7_Init 1 */
  332. /* USER CODE END TIM7_Init 1 */
  333. htim7.Instance = TIM7;
  334. htim7.Init.Prescaler = 1600-1;
  335. htim7.Init.CounterMode = TIM_COUNTERMODE_UP;
  336. htim7.Init.Period = 10-1;
  337. htim7.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  338. if (HAL_TIM_Base_Init(&htim7) != HAL_OK)
  339. {
  340. Error_Handler();
  341. }
  342. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  343. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  344. if (HAL_TIMEx_MasterConfigSynchronization(&htim7, &sMasterConfig) != HAL_OK)
  345. {
  346. Error_Handler();
  347. }
  348. /* USER CODE BEGIN TIM7_Init 2 */
  349. /* USER CODE END TIM7_Init 2 */
  350. }
  351. /**
  352. * @brief USART1 Initialization Function
  353. * @param None
  354. * @retval None
  355. */
  356. static void MX_USART1_UART_Init(void)
  357. {
  358. /* USER CODE BEGIN USART1_Init 0 */
  359. /* USER CODE END USART1_Init 0 */
  360. /* USER CODE BEGIN USART1_Init 1 */
  361. /* USER CODE END USART1_Init 1 */
  362. huart1.Instance = USART1;
  363. huart1.Init.BaudRate = 115200;
  364. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  365. huart1.Init.StopBits = UART_STOPBITS_1;
  366. huart1.Init.Parity = UART_PARITY_NONE;
  367. huart1.Init.Mode = UART_MODE_TX_RX;
  368. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  369. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  370. if (HAL_UART_Init(&huart1) != HAL_OK)
  371. {
  372. Error_Handler();
  373. }
  374. /* USER CODE BEGIN USART1_Init 2 */
  375. /* USER CODE END USART1_Init 2 */
  376. }
  377. /**
  378. * @brief GPIO Initialization Function
  379. * @param None
  380. * @retval None
  381. */
  382. static void MX_GPIO_Init(void)
  383. {
  384. GPIO_InitTypeDef GPIO_InitStruct = {0};
  385. /* GPIO Ports Clock Enable */
  386. __HAL_RCC_GPIOC_CLK_ENABLE();
  387. __HAL_RCC_GPIOA_CLK_ENABLE();
  388. /*Configure GPIO pin Output Level */
  389. HAL_GPIO_WritePin(Boot_LD_GPIO_Port, Boot_LD_Pin, GPIO_PIN_RESET);
  390. /*Configure GPIO pin : Boot_LD_Pin */
  391. GPIO_InitStruct.Pin = Boot_LD_Pin;
  392. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  393. GPIO_InitStruct.Pull = GPIO_NOPULL;
  394. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  395. HAL_GPIO_Init(Boot_LD_GPIO_Port, &GPIO_InitStruct);
  396. }
  397. /* USER CODE BEGIN 4 */
  398. /* USER CODE END 4 */
  399. /**
  400. * @brief This function is executed in case of error occurrence.
  401. * @retval None
  402. */
  403. void Error_Handler(void)
  404. {
  405. /* USER CODE BEGIN Error_Handler_Debug */
  406. /* User can add his own implementation to report the HAL error return state */
  407. /* USER CODE END Error_Handler_Debug */
  408. }
  409. #ifdef USE_FULL_ASSERT
  410. /**
  411. * @brief Reports the name of the source file and the source line number
  412. * where the assert_param error has occurred.
  413. * @param file: pointer to the source file name
  414. * @param line: assert_param error line source number
  415. * @retval None
  416. */
  417. void assert_failed(uint8_t *file, uint32_t line)
  418. {
  419. /* USER CODE BEGIN 6 */
  420. /* User can add his own implementation to report the file name and line number,
  421. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  422. /* USER CODE END 6 */
  423. }
  424. #endif /* USE_FULL_ASSERT */
  425. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/