dfu
一:CUBEMAX配置
2.引脚一览
3.USB配置
#define FLASH_DESC_STR "@InternalFlash /0x08000000/03*016Ka,01*016Kg,01*064Kg,07*128Kg,04*016Kg,01*064Kg,07*128Kg"
上面的这个是默认生成的,stm32f407的扇区配置,我们的事STM32F103RCT6 扇区大小2K 共128个,修改如下
#define FLASH_DESC_STR "@Internal Flash /0x08000000/24*002Ka,104*002Kg"
boot区24*2=48k 该参数配合图三设置的 0x0800c000,该新芯片后面的扇区大小一样所以直接104*2即可,407的因为扇区大小不一样所以分开写,其他芯片看手册即可
二: boot代码介绍
1.usbd_dfu_if.c修改 不同IC区别只有扇区擦除函数有差异,最后那个状态获取函数随便填个时间就行
/**
******************************************************************************
* @file : usbd_dfu_if.c
* @brief : Usb device for Download Firmware Update.
******************************************************************************
* This notice applies to any and all portions of this file
* that are not between comment pairs USER CODE BEGIN and
* USER CODE END. Other portions of this file, whether
* inserted by the user or by software development tools
* are owned by their respective copyright owners.
*
* Copyright (c) 2018 STMicroelectronics International N.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted, provided that the following conditions are met:
*
* 1. Redistribution of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of other
* contributors to this software may be used to endorse or promote products
* derived from this software without specific written permission.
* 4. This software, including modifications and/or derivative works of this
* software, must execute solely and exclusively on microcontroller or
* microprocessor devices manufactured by or for STMicroelectronics.
* 5. Redistribution and use of this software other than as permitted under
* this license is void and will automatically terminate your rights under
* this license.
*
* THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
* limitED TO, THE IMPLIED WARRANTIES OF MERCHANTAbility, FITNESS FOR A
* PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
* RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
* SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, indirect,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "usbd_dfu_if.h"
/* USER CODE BEGIN INCLUDE */
/* USER CODE END INCLUDE */
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
/* USER CODE END PV */
/** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
* @brief Usb device.
* @{
*/
/** @defgroup USBD_DFU
* @brief Usb DFU device module.
* @{
*/
/** @defgroup USBD_DFU_Private_typesDefinitions
* @brief Private types.
* @{
*/
/* USER CODE BEGIN PRIVATE_TYPES */
/* USER CODE END PRIVATE_TYPES */
/**
* @}
*/
/** @defgroup USBD_DFU_Private_Defines
* @brief Private defines.
* @{
*/
//#define FLASH_DESC_STR "@Internal Flash /0x08000000/03*016Ka,01*016Kg,01*064Kg,07*128Kg,04*016Kg,01*064Kg,07*128Kg"
/* USER CODE BEGIN PRIVATE_DEFINES */
#define FLASH_DESC_STR "@Internal Flash /0x08000000/24*002Ka,104*002Kg"
/* USER CODE END PRIVATE_DEFINES */
/**
* @}
*/
/** @defgroup USBD_DFU_Private_Macros
* @brief Private macros.
* @{
*/
/* USER CODE BEGIN PRIVATE_MACRO */
/* USER CODE END PRIVATE_MACRO */
/**
* @}
*/
/** @defgroup USBD_DFU_Private_Variables
* @brief Private variables.
* @{
*/
/* USER CODE BEGIN PRIVATE_VARIABLES */
/* USER CODE END PRIVATE_VARIABLES */
/**
* @}
*/
/** @defgroup USBD_DFU_exported_Variables
* @brief Public variables.
* @{
*/
extern USBD_handleTypeDef hUsbDeviceFS;
/* USER CODE BEGIN EXPORTED_VARIABLES */
/* USER CODE END EXPORTED_VARIABLES */
/**
* @}
*/
/** @defgroup USBD_DFU_Private_Functionprototypes
* @brief Private functions declaration.
* @{
*/
static uint16_t MEM_If_Init_FS(void);
static uint16_t MEM_If_erase_FS(uint32_t Add);
static uint16_t MEM_If_Write_FS(uint8_t *src, uint8_t *dest, uint32_t Len);
static uint8_t *MEM_If_Read_FS(uint8_t *src, uint8_t *dest, uint32_t Len);
static uint16_t MEM_If_DeInit_FS(void);
static uint16_t MEM_If_GetStatus_FS(uint32_t Add, uint8_t Cmd, uint8_t *buffer);
/* USER CODE BEGIN PRIVATE_FUNCTIONS_DECLARATION */
/* USER CODE END PRIVATE_FUNCTIONS_DECLARATION */
/**
* @}
*/
#if defined ( __ICCARM__ ) /* IAR Compiler */
#pragma data_alignment=4
#endif
__ALIGN_BEGIN USBD_DFU_MediaTypeDef USBD_DFU_fops_FS __ALIGN_END =
{
(uint8_t*)FLASH_DESC_STR,
MEM_If_Init_FS,
MEM_If_DeInit_FS,
MEM_If_Erase_FS,
MEM_If_Write_FS,
MEM_If_Read_FS,
MEM_If_GetStatus_FS
};
/* Private functions ---------------------------------------------------------*/
/**
* @brief Memory initialization routine.
* @retval USBD_OK if operation is successful, MAL_FAIL else.
*/
uint16_t MEM_If_Init_FS(void)
{
/* USER CODE BEGIN 0 */
HAL_FLASH_Unlock();
#if USE_printF_DEBUG
printf("HAL_FLASH_Unlock\r\n");
#endif
return (USBD_OK);
/* USER CODE END 0 */
}
/**
* @brief De-Initializes Memory
* @retval USBD_OK if operation is successful, MAL_FAIL else
*/
uint16_t MEM_If_DeInit_FS(void)
{
/* USER CODE BEGIN 1 */
HAL_FLASH_Lock();
#if USE_PRINTF_DEBUG
printf("HAL_FLASH_Lock\r\n");
#endif
return (USBD_OK);
/* USER CODE END 1 */
}
/**
* @brief Erase sector.
* @param Add: Address of sector to be erased.
* @retval 0 if operation is successful, MAL_FAIL else.
*/
uint16_t MEM_If_Erase_FS(uint32_t Add)
{
/* USER CODE BEGIN 2 */
uint32_t PageERROR = 0;
FLASH_EraseInitTypeDef f;
/*扇区擦除 根据具体的芯片实现,*/
f.TypeErase = FLASH_TYPEERASE_PAGES;
f.PageAddress = Add;
f.NbPages = 1;
if(HAL_FLASHEx_Erase(&f, &PageError) != HAL_OK)
{
#if USE_PRINTF_DEBUG
printf("Erase %x err\r\n",Add);
#endif
return 1;
}
#if USE_PRINTF_DEBUG
printf("Erase %x ok\r\n",Add);
#endif
return (0);
/* USER CODE END 2 */
}
/**
* @brief Memory write routine.
* @param src: Pointer to the source buffer. Address to be written to.
* @param dest: Pointer to the destination buffer.
* @param Len: Number of data to be written (in bytes).
* @retval USBD_OK if operation is successful, MAL_FAIL else.
*/
uint16_t MEM_If_Write_FS(uint8_t *src, uint8_t *dest, uint32_t Len)
{
/* USER CODE BEGIN 3 */
uint16_t i;
for(i=0;i<Len;i+=4)
{
if(HAL_OK==HAL_FLASH_Program(FLASH_TYPEPROGRAM_word,(uint32_t)(dest+i),*(uint32_t*)(src+i)))
{
if(*(uint32_t *)(src + i) != *(uint32_t*)(dest+i))
{
#if USE_PRINTF_DEBUG
printf("Wrtie err2\r\n");
#endif
return (2);
}
}
else
{
#if USE_PRINTF_DEBUG
printf("Wrtie err1\r\n");
#endif
return (1);
}
}
#if USE_PRINTF_DEBUG
printf("Write src=%x Len=%d ok\r\n",(uint32_t)(dest),Len);
#endif
return (0);
/* USER CODE END 3 */
}
/**
* @brief Memory read routine.
* @param src: Pointer to the source buffer. Address to be written to.
* @param dest: Pointer to the destination buffer.
* @param Len: Number of data to be read (in bytes).
* @retval Pointer to the physical address where data should be read.
*/
uint8_t *MEM_If_Read_FS(uint8_t *src, uint8_t *dest, uint32_t Len)
{
/* Return a valid address to avoid HardFault */
/* USER CODE BEGIN 4 */
uint32_t i = 0;
uint8_t *psrc = src;
for(i = 0; i < Len; i++)
{
dest[i] = *psrc++;
}
/* Return a valid address to avoid HardFault */
#if USE_PRINTF_DEBUG
printf("Read src=%x Len=%d ok\r\n",(uint32_t)(dest),Len);
#endif
return (uint8_t*)(dest);
//return (uint8_t*)(USBD_OK);
/* USER CODE END 4 */
}
/**
* @brief Get status routine
* @param Add: Address to be read from
* @param Cmd: Number of data to be read (in bytes)
* @param buffer: used for returning the time necessary for a program or an erase operation
* @retval USBD_OK if operation is successful
*/
uint16_t MEM_If_GetStatus_FS(uint32_t Add, uint8_t Cmd, uint8_t *buffer)
{
/* USER CODE BEGIN 5 */
switch (Cmd)
{
case DFU_MEDIA_PROGRAM:
buffer[1] = (uint8_t)FLASH_TIMEOUT_VALUE;//50s 该参数应该是上位机超时判断的参数
buffer[2] = (uint8_t)(FLASH_TIMEOUT_VALUE << 8);
buffer[3] = 0;
break;
case DFU_MEDIA_ERASE:
buffer[1] = (uint8_t)FLASH_TIMEOUT_VALUE;
buffer[2] = (uint8_t)(FLASH_TIMEOUT_VALUE << 8);
buffer[3] = 0;
default:
break;
}
#if USE_PRINTF_DEBUG
printf("GetStatus Add=%x Cmd=%d ok\r\n",Add,Cmd);
#endif
return (USBD_OK);
/* USER CODE END 5 */
}
/* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */
/* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
2.main.c修改 通过开机检测IO状态判断进入APP还是BOOT
typedef void (*pFunction)(void);
pFunction JumpToApplication;
uint32_t JumpAddress;
/* USER CODE END 0 */
/**
* @brief The application entry point.
*
* @retval None
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
HAL_Delay(100);
MX_GPIO_Init();
MX_USART1_UART_Init();
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
// MX_GPIO_Init();
// MX_USB_DEVICE_Init();
// MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_2) == GPIO_PIN_SET)
{
#if USE_PRINTF_DEBUG
printf("JumpToApplication 1\r\n");
#endif
/* Test if user code is programmed starting from USBD_DFU_APP_DEFAULT_ADD address */
if(((*(__IO uint32_t*)USBD_DFU_APP_DEFAULT_ADD) & 0x2FFE0000 ) == 0x20000000)
{
/* Jump to user application */
#if USE_PRINTF_DEBUG
printf("JumpToApplication 2\r\n");
#endif
JumpAddress = *(__IO uint32_t*) (USBD_DFU_APP_DEFAULT_ADD + 4);
JumpToApplication = (pFunction) JumpAddress;
/* Initialize user application's Stack Pointer */
__set_MSP((*(__IO uint32_t*) USBD_DFU_APP_DEFAULT_ADD ));
JumpToApplication();
while(1);
}
}
/* USER CODE END 2 */
#if USE_PRINTF_DEBUG
printf("MX_USB_DEVICE_Init()\r\n");
#endif
MX_USB_DEVICE_Init();
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
HAL_Delay(3000);
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
相关阅读
大家好,今天我跟大家分享一下学习STM32的一些收获,希望对初学STM32的同学有所启示。当然,本人也是一名初学者,如果有谈论不妥之处,还望
DS18B20 数字温度传感器实验 STM32 虽然内部自带了温度传感器,但是因为芯片温升较大等问题,与实际温度差别较大,所以,本章我们将向大
使用STM32 USB IAP 使用stm32的usb做IAP升级功能,利用stm32cubemx生成代码。参考http://www.51hei.com/bbs/dpj-40235-1.html h
原文:编码器速度和方向检测,371电机方向与速度检测,stm32编码器接口模式 转载链接:http://www.yfrobot.com/thread-2411-1-1.html 出
原文:点击打开链接本教程介绍步进电机驱动和细分的工作原理,以及stm32103为主控芯片制作的一套自平衡的两轮车系统,附带原理图pcb