dfu_st_cubemx_main.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under Ultimate Liberty license
  13. * SLA0044, the "License"; You may not use this file except in compliance with
  14. * the License. You may obtain a copy of the License at:
  15. * www.st.com/SLA0044
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. /* Private includes ----------------------------------------------------------*/
  23. /* USER CODE BEGIN Includes */
  24. #include "usbd_core.h"
  25. #include "usb_dfu.h"
  26. /* USER CODE END Includes */
  27. /* Private typedef -----------------------------------------------------------*/
  28. /* USER CODE BEGIN PTD */
  29. /* USER CODE END PTD */
  30. /* Private define ------------------------------------------------------------*/
  31. /* USER CODE BEGIN PD */
  32. /* USER CODE END PD */
  33. /* Private macro -------------------------------------------------------------*/
  34. /* USER CODE BEGIN PM */
  35. /* USER CODE END PM */
  36. /* Private variables ---------------------------------------------------------*/
  37. UART_HandleTypeDef huart1;
  38. PCD_HandleTypeDef hpcd_USB_FS;
  39. /* USER CODE BEGIN PV */
  40. /* USER CODE END PV */
  41. /* Private function prototypes -----------------------------------------------*/
  42. void SystemClock_Config(void);
  43. static void MX_GPIO_Init(void);
  44. static void MX_USART1_UART_Init(void);
  45. static void MX_USB_PCD_Init(void);
  46. /* USER CODE BEGIN PFP */
  47. typedef void (*pFunction)(void);
  48. static void jump_app(void)
  49. {
  50. pFunction JumpToApplication;
  51. uint32_t JumpAddress;
  52. if (((*(__IO uint32_t *)USBD_DFU_APP_DEFAULT_ADD) & 0x2FFFB000) == 0x20000000)
  53. {
  54. /* Jump to user application */
  55. /*!< Jump to app reset_handler */
  56. JumpAddress = *(__IO uint32_t *)(USBD_DFU_APP_DEFAULT_ADD + 4);
  57. JumpToApplication = (pFunction)JumpAddress;
  58. /* Initialize user application's Stack Pointer */
  59. __set_MSP(*(__IO uint32_t *)USBD_DFU_APP_DEFAULT_ADD);
  60. JumpToApplication();
  61. }
  62. }
  63. /* USER CODE END PFP */
  64. /* Private user code ---------------------------------------------------------*/
  65. /* USER CODE BEGIN 0 */
  66. int fputc(int ch, FILE *f)
  67. {
  68. while ((USART1->SR & USART_SR_TXE) == 0)
  69. ;
  70. USART1->DR = ch;
  71. return ch;
  72. }
  73. void usb_dc_low_level_init(void)
  74. {
  75. /* Peripheral clock enable */
  76. __HAL_RCC_USB_CLK_ENABLE();
  77. /* USB interrupt Init */
  78. HAL_NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 0, 0);
  79. HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
  80. }
  81. uint8_t *dfu_read_flash(uint8_t *src, uint8_t *dest, uint32_t len)
  82. {
  83. uint32_t i = 0;
  84. uint8_t *psrc = src;
  85. for (i = 0; i < len; i++)
  86. {
  87. dest[i] = *psrc++;
  88. }
  89. /* Return a valid address to avoid HardFault */
  90. return (uint8_t *)(dest);
  91. }
  92. uint16_t dfu_write_flash(uint8_t *src, uint8_t *dest, uint32_t len)
  93. {
  94. HAL_FLASH_Unlock();
  95. uint32_t i = 0;
  96. for (i = 0; i < len; i += 4)
  97. {
  98. /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
  99. * be done by byte */
  100. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t)(dest + i),
  101. *(uint32_t *)(src + i)) == HAL_OK)
  102. {
  103. /* Check the written value */
  104. if (*(uint32_t *)(src + i) != *(uint32_t *)(dest + i))
  105. {
  106. /* Flash content doesn't match SRAM content */
  107. return (1);
  108. }
  109. }
  110. else
  111. {
  112. /* Error occurred while writing data in Flash memory */
  113. return (2);
  114. }
  115. }
  116. return 0;
  117. }
  118. uint16_t dfu_erase_flash(uint32_t add)
  119. {
  120. HAL_FLASH_Unlock();
  121. uint32_t PageError;
  122. /* Variable contains Flash operation status */
  123. HAL_StatusTypeDef status;
  124. FLASH_EraseInitTypeDef eraseinitstruct;
  125. eraseinitstruct.TypeErase = FLASH_TYPEERASE_PAGES;
  126. eraseinitstruct.PageAddress = add;
  127. eraseinitstruct.NbPages = 1U;
  128. status = HAL_FLASHEx_Erase(&eraseinitstruct, &PageError);
  129. return 0;
  130. }
  131. void dfu_leave(void)
  132. {
  133. NVIC_SystemReset();
  134. }
  135. /* USER CODE END 0 */
  136. /**
  137. * @brief The application entry point.
  138. * @retval int
  139. */
  140. int main(void)
  141. {
  142. /* USER CODE BEGIN 1 */
  143. jump_app();
  144. /* USER CODE END 1 */
  145. /* MCU Configuration--------------------------------------------------------*/
  146. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  147. HAL_Init();
  148. /* USER CODE BEGIN Init */
  149. /* USER CODE END Init */
  150. /* Configure the system clock */
  151. SystemClock_Config();
  152. /* USER CODE BEGIN SysInit */
  153. /* USER CODE END SysInit */
  154. /* Initialize all configured peripherals */
  155. MX_GPIO_Init();
  156. MX_USART1_UART_Init();
  157. //MX_USB_PCD_Init();
  158. /* USER CODE BEGIN 2 */
  159. // extern void cdc_acm_msc_init(void);
  160. // cdc_acm_msc_init();
  161. extern void dfu_flash_init(void);
  162. dfu_flash_init();
  163. /* USER CODE END 2 */
  164. /* Infinite loop */
  165. /* USER CODE BEGIN WHILE */
  166. while (1) {
  167. /* USER CODE END WHILE */
  168. /* USER CODE BEGIN 3 */
  169. // extern void cdc_acm_data_send_with_dtr_test(void);
  170. // cdc_acm_data_send_with_dtr_test();
  171. // HAL_Delay(100);
  172. }
  173. /* USER CODE END 3 */
  174. }
  175. /**
  176. * @brief System Clock Configuration
  177. * @retval None
  178. */
  179. void SystemClock_Config(void)
  180. {
  181. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  182. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  183. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  184. /** Initializes the RCC Oscillators according to the specified parameters
  185. * in the RCC_OscInitTypeDef structure.
  186. */
  187. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  188. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  189. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  190. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  191. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  192. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  193. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  194. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  195. {
  196. Error_Handler();
  197. }
  198. /** Initializes the CPU, AHB and APB buses clocks
  199. */
  200. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  201. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  202. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  203. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  204. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  205. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  206. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  207. {
  208. Error_Handler();
  209. }
  210. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
  211. PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
  212. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  213. {
  214. Error_Handler();
  215. }
  216. }
  217. /**
  218. * @brief USART1 Initialization Function
  219. * @param None
  220. * @retval None
  221. */
  222. static void MX_USART1_UART_Init(void)
  223. {
  224. /* USER CODE BEGIN USART1_Init 0 */
  225. /* USER CODE END USART1_Init 0 */
  226. /* USER CODE BEGIN USART1_Init 1 */
  227. /* USER CODE END USART1_Init 1 */
  228. huart1.Instance = USART1;
  229. huart1.Init.BaudRate = 115200;
  230. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  231. huart1.Init.StopBits = UART_STOPBITS_1;
  232. huart1.Init.Parity = UART_PARITY_NONE;
  233. huart1.Init.Mode = UART_MODE_TX_RX;
  234. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  235. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  236. if (HAL_UART_Init(&huart1) != HAL_OK)
  237. {
  238. Error_Handler();
  239. }
  240. /* USER CODE BEGIN USART1_Init 2 */
  241. /* USER CODE END USART1_Init 2 */
  242. }
  243. /**
  244. * @brief USB Initialization Function
  245. * @param None
  246. * @retval None
  247. */
  248. static void MX_USB_PCD_Init(void)
  249. {
  250. /* USER CODE BEGIN USB_Init 0 */
  251. /* USER CODE END USB_Init 0 */
  252. /* USER CODE BEGIN USB_Init 1 */
  253. /* USER CODE END USB_Init 1 */
  254. hpcd_USB_FS.Instance = USB;
  255. hpcd_USB_FS.Init.dev_endpoints = 8;
  256. hpcd_USB_FS.Init.speed = PCD_SPEED_FULL;
  257. hpcd_USB_FS.Init.low_power_enable = DISABLE;
  258. hpcd_USB_FS.Init.lpm_enable = DISABLE;
  259. hpcd_USB_FS.Init.battery_charging_enable = DISABLE;
  260. if (HAL_PCD_Init(&hpcd_USB_FS) != HAL_OK)
  261. {
  262. Error_Handler();
  263. }
  264. /* USER CODE BEGIN USB_Init 2 */
  265. /* USER CODE END USB_Init 2 */
  266. }
  267. /**
  268. * @brief GPIO Initialization Function
  269. * @param None
  270. * @retval None
  271. */
  272. static void MX_GPIO_Init(void)
  273. {
  274. /* GPIO Ports Clock Enable */
  275. __HAL_RCC_GPIOD_CLK_ENABLE();
  276. __HAL_RCC_GPIOA_CLK_ENABLE();
  277. }
  278. /* USER CODE BEGIN 4 */
  279. /* USER CODE END 4 */
  280. /**
  281. * @brief This function is executed in case of error occurrence.
  282. * @retval None
  283. */
  284. void Error_Handler(void)
  285. {
  286. /* USER CODE BEGIN Error_Handler_Debug */
  287. /* User can add his own implementation to report the HAL error return state */
  288. __disable_irq();
  289. while (1) {
  290. }
  291. /* USER CODE END Error_Handler_Debug */
  292. }
  293. #ifdef USE_FULL_ASSERT
  294. /**
  295. * @brief Reports the name of the source file and the source line number
  296. * where the assert_param error has occurred.
  297. * @param file: pointer to the source file name
  298. * @param line: assert_param error line source number
  299. * @retval None
  300. */
  301. void assert_failed(uint8_t *file, uint32_t line)
  302. {
  303. /* USER CODE BEGIN 6 */
  304. /* User can add his own implementation to report the file name and line number,
  305. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  306. /* USER CODE END 6 */
  307. }
  308. #endif /* USE_FULL_ASSERT */
  309. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/