Advertisement
Guest User

Untitled

a guest
Apr 9th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.56 KB | None | 0 0
  1.  
  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 International N.V.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted, provided that the following conditions are met:
  18. *
  19. * 1. Redistribution of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. Neither the name of STMicroelectronics nor the names of other
  25. * contributors to this software may be used to endorse or promote products
  26. * derived from this software without specific written permission.
  27. * 4. This software, including modifications and/or derivative works of this
  28. * software, must execute solely and exclusively on microcontroller or
  29. * microprocessor devices manufactured by or for STMicroelectronics.
  30. * 5. Redistribution and use of this software other than as permitted under
  31. * this license is void and will automatically terminate your rights under
  32. * this license.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  35. * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  37. * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  38. * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  39. * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  40. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  42. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  43. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  44. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  45. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. *
  47. ******************************************************************************
  48. */
  49. /* Includes ------------------------------------------------------------------*/
  50. #include "main.h"
  51. #include "stm32f2xx_hal.h"
  52. #include "fatfs.h"
  53. #include "usb_host.h"
  54. #include "gpio.h"
  55.  
  56. /* USER CODE BEGIN Includes */
  57. #include "crc32.h"
  58. #include "LFSR32.h"
  59. #include "leds.h"
  60. #include "lcd.h"
  61. /* USER CODE END Includes */
  62.  
  63. /* Private variables ---------------------------------------------------------*/
  64.  
  65. /* USER CODE BEGIN PV */
  66. /* Private variables ---------------------------------------------------------*/
  67.  
  68. // Flash configuration
  69. #define MAIN_PROGRAM_START_ADDRESS (uint32_t)0x08020000
  70. #define FW_FILE_NAME "PDS.usb"
  71. /* USER CODE END PV */
  72.  
  73. /* Private function prototypes -----------------------------------------------*/
  74. void SystemClock_Config(void);
  75. void MX_USB_HOST_Process(void);
  76.  
  77. /* USER CODE BEGIN PFP */
  78. /* Private function prototypes -----------------------------------------------*/
  79. bool checkValidity(); // Проверка открытого файла на применимость
  80. void jumpToApplication(); // Переход в основную программу
  81. void updateFW();
  82. void alarmMode();
  83.  
  84. /* USER CODE END PFP */
  85.  
  86. /* USER CODE BEGIN 0 */
  87.  
  88. // Переменные для перехода в основную программу
  89. typedef void (*pFunction)(void);
  90. uint32_t jumpAddress;
  91. pFunction Jump_To_Application;
  92.  
  93. // USB/FAT
  94. extern ApplicationTypeDef Appli_state;
  95. FATFS fs;
  96. DIR dr;
  97. FIL file;
  98. FRESULT res;
  99.  
  100. /* USER CODE END 0 */
  101.  
  102. /**
  103. * @brief The application entry point.
  104. *
  105. * @retval None
  106. */
  107. int main(void)
  108. {
  109. /* USER CODE BEGIN 1 */
  110.  
  111. /* USER CODE END 1 */
  112.  
  113. /* MCU Configuration----------------------------------------------------------*/
  114.  
  115. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  116. HAL_Init();
  117.  
  118. /* USER CODE BEGIN Init */
  119.  
  120. /* USER CODE END Init */
  121.  
  122. /* Configure the system clock */
  123. SystemClock_Config();
  124.  
  125. /* USER CODE BEGIN SysInit */
  126.  
  127. /* USER CODE END SysInit */
  128.  
  129. /* Initialize all configured peripherals */
  130. MX_GPIO_Init();
  131. /* USER CODE BEGIN 2 */
  132.  
  133. /* USER CODE END 2 */
  134.  
  135. /* Infinite loop */
  136. /* USER CODE BEGIN WHILE */
  137. while (1)
  138. {
  139. /* USER CODE END WHILE */
  140. led_power(true);
  141. lcd_init();
  142. lcd_backlight(true);
  143.  
  144. MX_USB_HOST_Process();
  145.  
  146. /* USER CODE BEGIN 3 */
  147. if (!HAL_GPIO_ReadPin(ENCODER_BTN_GPIO_Port, ENCODER_BTN_Pin))
  148. {
  149. lcd_clear();
  150. lcd_set_cursor(0, 0);
  151. lcd_printf(" Поиск USB...");
  152. MX_USB_HOST_Init();
  153. MX_FATFS_Init();
  154. while (Appli_state != APPLICATION_READY)
  155. {
  156. MX_USB_HOST_Process();
  157. }
  158.  
  159. lcd_clear();
  160. lcd_set_cursor(0, 0);
  161. lcd_printf("Поиск ");
  162. lcd_printf(FW_FILE_NAME);
  163. lcd_printf("...");
  164. HAL_Delay(1000);
  165.  
  166.  
  167. res = f_mount(&fs, USBHPath, 1);
  168. if (res != FR_OK)
  169. {
  170. lcd_clear();
  171. lcd_set_cursor(0, 2);
  172. lcd_printf("Ошибка: #%d", res);
  173. alarmMode();
  174. }
  175. else {
  176. res = f_open(&file, FW_FILE_NAME, FA_READ);
  177. if (res != FR_OK)
  178. {
  179. lcd_clear();
  180. lcd_set_cursor(0, 2);
  181. lcd_printf("Файл ");
  182. lcd_printf(FW_FILE_NAME);
  183. lcd_set_cursor(1, 3);
  184. lcd_printf("не найден!");
  185. alarmMode();
  186. }
  187. else
  188. {
  189. // Проверяем правильность прошивки
  190. if (!checkValidity()){
  191. lcd_clear();
  192. lcd_set_cursor(0, 0);
  193. lcd_printf("Проверка ");
  194. lcd_printf(FW_FILE_NAME);
  195. lcd_set_cursor(1, 0);
  196. lcd_printf(" не пройдена! ");
  197. alarmMode();
  198. }
  199.  
  200. updateFW();
  201.  
  202. f_close(&file);
  203.  
  204.  
  205.  
  206. // Если ручку не отпустили, просим её отпустить,
  207. // инчае после перезапуска снова влезем в обновление
  208. if (!HAL_GPIO_ReadPin(ENCODER_BTN_GPIO_Port, ENCODER_BTN_Pin))
  209. {
  210. lcd_clear();
  211. lcd_set_cursor(0, 0);
  212. lcd_printf(" Для завершения");
  213. lcd_set_cursor(1, 0);
  214. lcd_printf("отпустите ручку!");
  215. }
  216. while(!HAL_GPIO_ReadPin(ENCODER_BTN_GPIO_Port, ENCODER_BTN_Pin));
  217.  
  218. HAL_Delay(100);
  219. HAL_NVIC_SystemReset();
  220. }
  221. }
  222. }
  223. else{
  224. jumpToApplication();
  225. }
  226.  
  227. }
  228. /* USER CODE END 3 */
  229.  
  230. }
  231.  
  232. /**
  233. * @brief System Clock Configuration
  234. * @retval None
  235. */
  236. void SystemClock_Config(void)
  237. {
  238.  
  239. RCC_OscInitTypeDef RCC_OscInitStruct;
  240. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  241.  
  242. /**Initializes the CPU, AHB and APB busses clocks
  243. */
  244. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  245. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  246. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  247. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  248. RCC_OscInitStruct.PLL.PLLM = 20;
  249. RCC_OscInitStruct.PLL.PLLN = 192;
  250. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  251. RCC_OscInitStruct.PLL.PLLQ = 5;
  252. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  253. {
  254. _Error_Handler(__FILE__, __LINE__);
  255. }
  256.  
  257. /**Initializes the CPU, AHB and APB busses clocks
  258. */
  259. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  260. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  261. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  262. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  263. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  264. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  265.  
  266. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
  267. {
  268. _Error_Handler(__FILE__, __LINE__);
  269. }
  270.  
  271. /**Configure the Systick interrupt time
  272. */
  273. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  274.  
  275. /**Configure the Systick
  276. */
  277. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  278.  
  279. /* SysTick_IRQn interrupt configuration */
  280. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  281. }
  282.  
  283. /* USER CODE BEGIN 4 */
  284.  
  285. void alarmMode()
  286. {
  287. led_alarm(true);
  288. led_power(false);
  289. while(true);
  290. }
  291.  
  292. bool checkValidity()
  293. {
  294. uint8_t programByte = 0;
  295. uint32_t programBytesToRead = 0;
  296. uint32_t programBytesCounter = 0;
  297. uint32_t lcd_set_cursor_pos = 0;
  298. uint32_t lcd_set_cursor_pos_old = 1;
  299. uint32_t readBytes = 0;
  300. uint32_t crc32_calculated = 0;
  301. uint32_t crc32_known = 0;
  302.  
  303. lcd_clear();
  304. lcd_set_cursor(0, 0);
  305. lcd_printf(" Проверка файла ");
  306.  
  307. programBytesToRead = f_size(&file);
  308. resetSeed_lfsr32();
  309. Crc32_reset();
  310. f_rewind(&file);
  311.  
  312. while (programBytesCounter < (programBytesToRead - 4))
  313. {
  314. lcd_set_cursor_pos = programBytesCounter/(programBytesToRead/16);
  315. if (lcd_set_cursor_pos != lcd_set_cursor_pos_old)
  316. {
  317. lcd_set_cursor(1, lcd_set_cursor_pos);
  318. lcd_printf(".");
  319. lcd_set_cursor_pos_old = lcd_set_cursor_pos;
  320. HAL_Delay(100);
  321. }
  322.  
  323. f_read(&file, &programByte, 1, &readBytes);
  324. crc32_calculated = Crc32_accumulate(programByte);
  325. programBytesCounter++;
  326. }
  327.  
  328.  
  329. f_read(&file, &crc32_known, 4, &readBytes);
  330.  
  331.  
  332. return (crc32_known == crc32_calculated);
  333. }
  334.  
  335. // Обновление flash из файла
  336. void updateFW(){
  337. lcd_clear();
  338. lcd_set_cursor(0, 0);
  339. lcd_printf(" Обновление ");
  340. lcd_set_cursor(1, 0);
  341.  
  342. // Сначала чистим FLASH
  343. FLASH_EraseInitTypeDef EraseInitStruct;
  344. uint32_t SECTORError = 0;
  345. HAL_FLASH_Unlock();
  346.  
  347. EraseInitStruct.TypeErase = TYPEERASE_SECTORS;
  348. EraseInitStruct.VoltageRange = VOLTAGE_RANGE_3;
  349. EraseInitStruct.Sector = 5;
  350. EraseInitStruct.NbSectors = 1;
  351. HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError);
  352. lcd_printf(".");
  353.  
  354. EraseInitStruct.Sector = 6;
  355. EraseInitStruct.NbSectors = 1;
  356. HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError);
  357. lcd_printf(".");
  358.  
  359. EraseInitStruct.Sector = 7;
  360. EraseInitStruct.NbSectors = 1;
  361. HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError);
  362. lcd_printf(".");
  363.  
  364.  
  365. //////////////////////////////////////////////////////////////////////////////////////
  366.  
  367. uint8_t programByte = 0;
  368. uint32_t programBytesToRead = 0;
  369. uint32_t programBytesCounter = 0;
  370. uint32_t lcd_set_cursor_pos = 0;
  371. uint32_t lcd_set_cursor_pos_old = 0;
  372. uint32_t readBytes = 0;
  373.  
  374. // Пишем содержимое файла во FLASH
  375. programBytesToRead = f_size(&file);
  376. resetSeed_lfsr32();
  377. f_rewind(&file);
  378.  
  379. while (programBytesCounter < (programBytesToRead - 4))
  380. {
  381. lcd_set_cursor_pos = programBytesCounter/(programBytesToRead/16 + 3);
  382. if (lcd_set_cursor_pos != lcd_set_cursor_pos_old)
  383. {
  384. lcd_set_cursor(1, lcd_set_cursor_pos);
  385. lcd_printf(".");
  386. lcd_set_cursor_pos_old = lcd_set_cursor_pos;
  387. }
  388. f_read(&file, &programByte, 1, &readBytes);
  389. programByte ^= random_byte_lfsr32();
  390. HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, (uint64_t)(MAIN_PROGRAM_START_ADDRESS + programBytesCounter), (uint64_t)programByte);
  391. programBytesCounter++;
  392. }
  393.  
  394. HAL_FLASH_Lock();
  395.  
  396. }
  397.  
  398. // Переход в основную программу
  399. void jumpToApplication()
  400. {
  401.  
  402. uint32_t prog = *(__IO uint32_t*) (MAIN_PROGRAM_START_ADDRESS);
  403. if ((prog == 0xFFFFFFFF))
  404. {
  405. lcd_clear();
  406. lcd_set_cursor(0, 0);
  407. lcd_printf(" Установите ");
  408. lcd_set_cursor(1, 0);
  409. lcd_printf(" прошивку с USB ");
  410. while(HAL_GPIO_ReadPin(ENCODER_BTN_GPIO_Port, ENCODER_BTN_Pin));
  411. HAL_Delay(100);
  412. HAL_NVIC_SystemReset();
  413. }
  414.  
  415. HAL_DeInit();
  416. __disable_irq ();
  417. jumpAddress = *(__IO uint32_t*) (MAIN_PROGRAM_START_ADDRESS + 4);
  418. Jump_To_Application = (pFunction) jumpAddress;
  419. SCB->VTOR = MAIN_PROGRAM_START_ADDRESS;
  420. __set_MSP(*(__IO uint32_t*) MAIN_PROGRAM_START_ADDRESS);
  421. Jump_To_Application();
  422.  
  423.  
  424.  
  425. }
  426. /* USER CODE END 4 */
  427.  
  428. /**
  429. * @brief Period elapsed callback in non blocking mode
  430. * @note This function is called when TIM1 interrupt took place, inside
  431. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  432. * a global variable "uwTick" used as application time base.
  433. * @param htim : TIM handle
  434. * @retval None
  435. */
  436. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  437. {
  438. /* USER CODE BEGIN Callback 0 */
  439.  
  440. /* USER CODE END Callback 0 */
  441. if (htim->Instance == TIM1) {
  442. HAL_IncTick();
  443. }
  444. /* USER CODE BEGIN Callback 1 */
  445.  
  446. /* USER CODE END Callback 1 */
  447. }
  448.  
  449. /**
  450. * @brief This function is executed in case of error occurrence.
  451. * @param file: The file name as string.
  452. * @param line: The line in file as a number.
  453. * @retval None
  454. */
  455. void _Error_Handler(char *file, int line)
  456. {
  457. /* USER CODE BEGIN Error_Handler_Debug */
  458. /* User can add his own implementation to report the HAL error return state */
  459. while(1)
  460. {
  461. }
  462. /* USER CODE END Error_Handler_Debug */
  463. }
  464.  
  465. #ifdef USE_FULL_ASSERT
  466. /**
  467. * @brief Reports the name of the source file and the source line number
  468. * where the assert_param error has occurred.
  469. * @param file: pointer to the source file name
  470. * @param line: assert_param error line source number
  471. * @retval None
  472. */
  473. void assert_failed(uint8_t* file, uint32_t line)
  474. {
  475. /* USER CODE BEGIN 6 */
  476. /* User can add his own implementation to report the file name and line number,
  477. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  478. /* USER CODE END 6 */
  479. }
  480. #endif /* USE_FULL_ASSERT */
  481.  
  482. /**
  483. * @}
  484. */
  485.  
  486. /**
  487. * @}
  488. */
  489.  
  490. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement