cancel
Showing results for 
Search instead for 
Did you mean: 

can anyone tell me why its not continue read ,write and erase is not happening...in this program, only one time it will done read write and erase and next time when its again going in the while loop nothing happening...

SDhka.1
Associate II

 while (1)

 {

  /* USER CODE END WHILE */

 uint32_t Page=0x000000C8; //0x08040000

uint32_t B =FLASH_BANK_2 ;

 

FLASH_PageErase(Page,B);

HAL_FLASH_Unlock();

__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);

uint64_t d={0x1111111111111123};

volatile uint32_t flashaddresss =0x08040000;  // give the address where we need

     HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,flashaddresss,d );

   HAL_FLASH_Unlock();

__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);

FLASH_PageErase(flashaddresss,B);//1

HAL_FLASH_Lock();

  /* USER CODE BEGIN 3 */

 }

 /* USER CODE END 3 */

}

5 REPLIES 5

Probably shouldn't do it in a tight loop

Instrument code better.

Output success/failure codes.

Avoid using the debugger to step-thru.

Output progress and memory content to the console.

I'm sure the initial erase needs the flash memory unlocked.

Perhaps look at examples in the Cube/HAL directories for whatever STM32 you are using.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
SDhka.1
Associate II

hi if you have any program like this please share ...

Pavel A.
Evangelist III

> uint32_t Page=0x000000C8;

Really? Which STM32 has 0xC8 = 200 flash pages per bank?

//****************************************************************************
// Quick Rough STM32L5 Flash Example  - sourcer32@gmail.com
//****************************************************************************
 
static uint32_t GetBank(uint32_t Addr)
{
#if 0 // Single Bank Device
  return(FLASH_BANK_1);
#else // Dual Bank Device
  uint32_t bank = 0;
 
  if (Addr < (FLASH_BASE + FLASH_BANK_SIZE))
  {
    bank = FLASH_BANK_1;
  }
  else if (Addr < (FLASH_BASE + FLASH_BANK_SIZE*2))
  {
    bank = FLASH_BANK_2;
  }
 
  return(bank);
#endif
}
 
//****************************************************************************
 
#define FLASH_START_ADDRESS 0x08040000  // Unused spot for example
 
// Function to write data to the flash memory
//  BYTE pointer, Size in BYTES, put what you want in a STRUCTURE
 
void write_flash(uint8_t *data, uint32_t size)
{
  FLASH_EraseInitTypeDef EraseInitStruct = {0};
  uint32_t addr = FLASH_START_ADDRESS; // 64-bit aligned
  uint64_t value;
  uint32_t PageError;
 
  if (!size) return; // Empty request
 
  size = (size + 7) & ~7; // Round up to 64-bit words
 
  // Unlock the flash memory for writing
  HAL_FLASH_Unlock();
 
  // Clear any pending errors
  __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
 
  // Fill EraseInit structure, spanning size of write operation, within bank
  EraseInitStruct.TypeErase   = FLASH_TYPEERASE_PAGES;
  EraseInitStruct.Banks       = GetBank(addr); // Bank
  EraseInitStruct.Page        = GetPage(addr); // Starting Page
  EraseInitStruct.NbPages     = GetPage(addr + size - 1) - EraseInitStruct.Page + 1; // Page Count, at least 1
 
  if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)
  {
     // Insert code to handle the error here
 
    HAL_FLASH_Lock();
    return;
  }
 
  size /= sizeof(uint64_t); // Byte count to 64-bit word count
 
  // Iterate over the array of data and write each element to the flash memory
  while(size--)
  {
    memcpy(&value, data, sizeof(uint64_t)); // alignment safe copy of byte array into 64-bit value
 
    if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, addr, value) != HAL_OK)
    {
      // Insert code to handle the error here
 
      HAL_FLASH_Lock();
      return;
    }
 
    addr += sizeof(uint64_t); // Advance address
    data += sizeof(uint64_t); // Advance byte buffer
  }
 
  // Lock the flash memory after writing is complete
  HAL_FLASH_Lock();
}
 
//****************************************************************************
 
typedef struct _FLASH_STRUCT {
 
  uint32_t sequence;
 
  char string[32];
 
  uint32_t checksum;
 
} FLASH_STRUCT;
 
FLASH_STRUCT flash_struct[1] = {0};
 
//****************************************************************************
 
void test_flash(void)
{
  int i;
  uint8_t *p;
 
  flash_struct->sequence++;
 
  strcpy(flash_struct->string, "This is a test");
 
  // Disable instruction cache prior to internal cacheable memory update
  if (HAL_ICACHE_Disable() != HAL_OK)
  {
    Error_Handler();
  }
 
  /// Write my structure to flash
  write_flash((uint8_t *)flash_struct, sizeof(flash_struct));
 
  // Enable instruction cache after the internal cacheable memory update
  if (HAL_ICACHE_Enable() != HAL_OK)
  {
    Error_Handler();
  }
 
  // Dump my structure in flash
  p = (uint8_t *)FLASH_START_ADDRESS;
 
  for(i=0; i<sizeof(flash_struct); i++)
  {
    if (i && ((i % 16) == 0)) putchar('\n');
    printf(" %02X", p[i]);
  }
  putchar('\n');
}
 
//****************************************************************************

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..