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