kopia lustrzana https://github.com/micropython/micropython
110 wiersze
3.7 KiB
C
110 wiersze
3.7 KiB
C
#include <stm32f4xx.h>
|
|
#include <stm32f4xx_flash.h>
|
|
|
|
/* Base address of the Flash sectors */
|
|
#define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) /* Base @ of Sector 0, 16 Kbytes */
|
|
#define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08004000) /* Base @ of Sector 1, 16 Kbytes */
|
|
#define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08008000) /* Base @ of Sector 2, 16 Kbytes */
|
|
#define ADDR_FLASH_SECTOR_3 ((uint32_t)0x0800C000) /* Base @ of Sector 3, 16 Kbytes */
|
|
#define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000) /* Base @ of Sector 4, 64 Kbytes */
|
|
#define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000) /* Base @ of Sector 5, 128 Kbytes */
|
|
#define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08040000) /* Base @ of Sector 6, 128 Kbytes */
|
|
#define ADDR_FLASH_SECTOR_7 ((uint32_t)0x08060000) /* Base @ of Sector 7, 128 Kbytes */
|
|
#define ADDR_FLASH_SECTOR_8 ((uint32_t)0x08080000) /* Base @ of Sector 8, 128 Kbytes */
|
|
#define ADDR_FLASH_SECTOR_9 ((uint32_t)0x080A0000) /* Base @ of Sector 9, 128 Kbytes */
|
|
#define ADDR_FLASH_SECTOR_10 ((uint32_t)0x080C0000) /* Base @ of Sector 10, 128 Kbytes */
|
|
#define ADDR_FLASH_SECTOR_11 ((uint32_t)0x080E0000) /* Base @ of Sector 11, 128 Kbytes */
|
|
|
|
static uint32_t GetSector(uint32_t Address);
|
|
|
|
void flash_write(uint32_t flash_dest, const uint32_t *src, uint32_t num_word32) {
|
|
// unlock
|
|
FLASH_Unlock();
|
|
|
|
// Clear pending flags (if any)
|
|
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
|
|
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
|
|
|
|
// Device voltage range supposed to be [2.7V to 3.6V], the operation will be done by word
|
|
if (FLASH_EraseSector(GetSector(flash_dest), VoltageRange_3) != FLASH_COMPLETE) {
|
|
/* Error occurred while sector erase.
|
|
User can add here some code to deal with this error */
|
|
return;
|
|
}
|
|
|
|
/* Program the user Flash area word by word ********************************/
|
|
|
|
for (int i = 0; i < num_word32; i++) {
|
|
if (FLASH_ProgramWord(flash_dest, *src) == FLASH_COMPLETE)
|
|
{
|
|
flash_dest += 4;
|
|
src += 1;
|
|
}
|
|
else
|
|
{
|
|
/* Error occurred while writing data in Flash memory.
|
|
User can add here some code to deal with this error */
|
|
return;
|
|
}
|
|
}
|
|
|
|
// lock
|
|
FLASH_Lock();
|
|
}
|
|
|
|
/**
|
|
* @brief Gets the sector of a given address
|
|
* @param None
|
|
* @retval The sector of a given address
|
|
*/
|
|
static uint32_t GetSector(uint32_t Address)
|
|
{
|
|
uint32_t sector = 0;
|
|
|
|
if((Address < ADDR_FLASH_SECTOR_1) && (Address >= ADDR_FLASH_SECTOR_0))
|
|
{
|
|
sector = FLASH_Sector_0;
|
|
}
|
|
else if((Address < ADDR_FLASH_SECTOR_2) && (Address >= ADDR_FLASH_SECTOR_1))
|
|
{
|
|
sector = FLASH_Sector_1;
|
|
}
|
|
else if((Address < ADDR_FLASH_SECTOR_3) && (Address >= ADDR_FLASH_SECTOR_2))
|
|
{
|
|
sector = FLASH_Sector_2;
|
|
}
|
|
else if((Address < ADDR_FLASH_SECTOR_4) && (Address >= ADDR_FLASH_SECTOR_3))
|
|
{
|
|
sector = FLASH_Sector_3;
|
|
}
|
|
else if((Address < ADDR_FLASH_SECTOR_5) && (Address >= ADDR_FLASH_SECTOR_4))
|
|
{
|
|
sector = FLASH_Sector_4;
|
|
}
|
|
else if((Address < ADDR_FLASH_SECTOR_6) && (Address >= ADDR_FLASH_SECTOR_5))
|
|
{
|
|
sector = FLASH_Sector_5;
|
|
}
|
|
else if((Address < ADDR_FLASH_SECTOR_7) && (Address >= ADDR_FLASH_SECTOR_6))
|
|
{
|
|
sector = FLASH_Sector_6;
|
|
}
|
|
else if((Address < ADDR_FLASH_SECTOR_8) && (Address >= ADDR_FLASH_SECTOR_7))
|
|
{
|
|
sector = FLASH_Sector_7;
|
|
}
|
|
else if((Address < ADDR_FLASH_SECTOR_9) && (Address >= ADDR_FLASH_SECTOR_8))
|
|
{
|
|
sector = FLASH_Sector_8;
|
|
}
|
|
else if((Address < ADDR_FLASH_SECTOR_10) && (Address >= ADDR_FLASH_SECTOR_9))
|
|
{
|
|
sector = FLASH_Sector_9;
|
|
}
|
|
else if((Address < ADDR_FLASH_SECTOR_11) && (Address >= ADDR_FLASH_SECTOR_10))
|
|
{
|
|
sector = FLASH_Sector_10;
|
|
}
|
|
return sector;
|
|
}
|