示例#1
0
void writeFlash(uint32_t *address, uint32_t *buffer) // page size is 256 bytes
{
  if ((uint32_t) address == 0x08000000) {
    eraseSector(0);
  }
  else if ((uint32_t) address == 0x08004000) {
    eraseSector(1);
  }
  else if ((uint32_t) address == 0x08008000) {
    eraseSector(2);
  }
  else if ((uint32_t) address == 0x0800C000) {
    eraseSector(3);
  }
  else if ((uint32_t) address == 0x08010000) {
    eraseSector(4);
  }
  else if ((uint32_t) address == 0x08020000) {
    eraseSector(5);
  }
  else if ((uint32_t) address == 0x08040000) {
    eraseSector(6);
  }
  else if ((uint32_t) address == 0x08060000) {
    eraseSector(7);
  }

  for (uint32_t i=0; i<FLASH_PAGESIZE/4; i++) {
    /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
     be done by word */

    // Wait for last operation to be completed
    waitFlashIdle();

    FLASH->CR &= CR_PSIZE_MASK;
    FLASH->CR |= FLASH_PSIZE_WORD;
    FLASH->CR |= FLASH_CR_PG;

    *address = *buffer;

    /* Wait for operation to be completed */
    waitFlashIdle();
    FLASH->CR &= (~FLASH_CR_PG);

    /* Check the written value */
    if (*address != *buffer) {
      /* Flash content doesn't match SRAM content */
      return;
    }
    /* Increment FLASH destination address */
    address += 1;
    buffer += 1;
  }
}
示例#2
0
uint32_t program( uint32_t *address, uint32_t *buffer )	// size is 256 bytes
{
	uint32_t i ;

	if ( (uint32_t) address >= 0x08008000 )
	{
		return 1 ;
	}

	if ( (uint32_t) address == 0x08000000 )
	{
		eraseSector( 0 ) ;
	}
	if ( (uint32_t) address == 0x08004000 )
	{
		eraseSector( 1 ) ;
	}
	// Now program the 256 bytes
	 
  for (i = 0 ; i < 64 ; i += 1 )
  {
    /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
       be done by word */ 
    
	  // Wait for last operation to be completed
		waitFlashIdle() ;
  
    FLASH->CR &= CR_PSIZE_MASK;
    FLASH->CR |= FLASH_PSIZE_WORD;
    FLASH->CR |= FLASH_CR_PG;
  
    *address = *buffer ;
        
		__disable_irq() ;
    /* Wait for operation to be completed */
		waitFlashIdle() ;
    FLASH->CR &= (~FLASH_CR_PG);
		__enable_irq() ;
		 
		 /* Check the written value */
    if ( *address != *buffer )
    {
      /* Flash content doesn't match SRAM content */
      return 2 ;
    }
    /* Increment FLASH destination address */
    address += 1 ;
		buffer += 1 ;
  }
  return 0 ;
}
示例#3
0
void eraseSector(uint32_t sector)
{
  waitFlashIdle();

  FLASH->CR &= CR_PSIZE_MASK;
  FLASH->CR |= FLASH_PSIZE_WORD;
  FLASH->CR &= SECTOR_MASK;
  FLASH->CR |= FLASH_CR_SER | (sector << 3);
  FLASH->CR |= FLASH_CR_STRT;

  /* Wait for operation to be completed */
  waitFlashIdle();

  /* if the erase operation is completed, disable the SER Bit */
  FLASH->CR &= (~FLASH_CR_SER);
  FLASH->CR &= SECTOR_MASK;
}