Loading...
 

SW4STM32 and SW4Linux fully supports the STM32MP1 asymmetric multicore Cortex/A7+M4 MPUs

   With System Workbench for Linux, Embedded Linux on the STM32MP1 family of MPUs from ST was never as simple to build and maintain, even for newcomers in the Linux world. And, if you install System Workbench for Linux in System Workbench for STM32 you can seamlessly develop and debug asymmetric applications running partly on Linux, partly on the Cortex-M4.
You can get more information from the ac6-tools website and download (registration required) various documents highlighting:

System Workbench for STM32


STM32F1 DFU CubeMX and Example Code from STM32Cube_FW_F1_V1.4.0 repo

Hi

When creating a project with CubeMX and selecting DFU as FS class for the USB IP one gets presented with a skeleton set of empty functions as interface.

Within the file /Src/usbd_dfu_if.c are the following functions:

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);


In STM32Cube_FW_F1_V1.4.0 one can find these functions for the DFU standalon example within

STM32Cube_FW_F1_V1.4.0/Projects/STM3210C_EVAL/Applications/USB_Device/DFU_Standalone/Src/usbd_dfu_flash.c


The erase function there is implemented as:

uint16_t Flash_If_Erase(uint32_t Add)
{
  uint32_t NbOfPages = 0;
  uint32_t PageError = 0;
  /* Variable contains Flash operation status */
  HAL_StatusTypeDef status;
  FLASH_EraseInitTypeDef eraseinitstruct;

   /* Get the number of sector to erase from 1st sector*/
  NbOfPages = ((USBD_DFU_APP_END_ADD - USBD_DFU_APP_DEFAULT_ADD) / FLASH_PAGE_SIZE) + 1;
  eraseinitstruct.TypeErase = FLASH_TYPEERASE_PAGES;
  eraseinitstruct.PageAddress = USBD_DFU_APP_DEFAULT_ADD;
  eraseinitstruct.NbPages = NbOfPages;
  status = HAL_FLASHEx_Erase(&eraseinitstruct, &PageError);

  if (status != HAL_OK)
  {
    return 1;
  }
  return 0;
}


Because I have created a bootloader I didn’t use the predefined adresses USBD_DFU_APP_END_ADD USBD_DFU_APP_DEFAULT_ADD but my own Adresses taking the offset of my application into account.

But the function is silly an seemingly untested. The parameter “Add” is not used at all.

The effect is, that for every page (1024 bytes) to be programmed the whole flash memory from APPLICATION_ADDRESS onwarts gets erased. After the download with dfu-util has finished there are only FFs in the memory.

The correct or at least functioning implemetation is as follows:

uint16_t MEM_If_Erase_FS(uint32_t Add)
{
  /* USER CODE BEGIN 2 */ 
  uint32_t NbOfPages = 0;
  uint32_t PageError = 0;
  /* Variable contains Flash operation status */
  HAL_StatusTypeDef status;
  FLASH_EraseInitTypeDef eraseinitstruct;

  /* Get the number of sector to erase from 1st sector*/
  //NbOfPages = ((FLASH_END_ADD - APPLICATION_ADDRESS) / FLASH_PAGE_SIZE) + 1;
  NbOfPages = 1;
  eraseinitstruct.TypeErase = FLASH_TYPEERASE_PAGES;
  //eraseinitstruct.PageAddress = APPLICATION_ADDRESS;
  eraseinitstruct.PageAddress = Add;
  eraseinitstruct.NbPages = NbOfPages;
  status = HAL_FLASHEx_Erase(&eraseinitstruct, &PageError);

  if (status != HAL_OK)
  {
     return 1;
  }

  return (USBD_OK);
  /* USER CODE END 2 */ 
}


By the way I have a question too:

Can someone tell me how to avoid it, that the programming of my application with an offset via openocd and debugger do not erase the bootloader first?

Dieter