void flash_erase_all_pages(void) { flash_wait_for_last_operation(); FLASH_CR |= FLASH_CR_MER; /* Enable mass erase. */ FLASH_CR |= FLASH_CR_STRT; /* Trigger the erase. */ flash_wait_for_last_operation(); FLASH_CR &= ~FLASH_CR_MER; /* Disable mass erase. */ }
void flash_erase_all_sectors(u32 program_size) { flash_wait_for_last_operation(); flash_set_program_size(program_size); FLASH_CR |= FLASH_MER; /* Enable mass erase. */ FLASH_CR |= FLASH_STRT; /* Trigger the erase. */ flash_wait_for_last_operation(); FLASH_CR &= ~FLASH_MER; /* Disable mass erase. */ }
void flash_program_option_bytes(u32 data) { flash_wait_for_last_operation(); if (FLASH_OPTCR & FLASH_OPTLOCK) flash_unlock_option_bytes(); FLASH_OPTCR = data & ~0x3; FLASH_OPTCR |= FLASH_OPTSTRT; /* Enable option byte programming. */ flash_wait_for_last_operation(); }
void flash_erase_page(u32 page_address) { flash_wait_for_last_operation(); FLASH_CR |= FLASH_CR_PER; FLASH_AR = page_address; FLASH_CR |= FLASH_CR_STRT; flash_wait_for_last_operation(); FLASH_CR &= ~FLASH_CR_PER; }
void flash_program_half_word(u32 address, u16 data) { flash_wait_for_last_operation(); FLASH_CR |= FLASH_CR_PG; (*(volatile u16 *)address) = data; flash_wait_for_last_operation(); FLASH_CR &= ~FLASH_CR_PG; /* Disable the PG bit. */ }
void flash_writer_page_erase(void *page) { flash_wait_for_last_operation(); FLASH_CR |= FLASH_CR_PER; FLASH_AR = (uint32_t) page; FLASH_CR |= FLASH_CR_STRT; flash_wait_for_last_operation(); FLASH_CR &= ~FLASH_CR_PER; }
void flash_erase_option_bytes(void) { flash_wait_for_last_operation(); if ((FLASH_CR & FLASH_CR_OPTWRE) == 0) flash_unlock_option_bytes(); FLASH_CR |= FLASH_CR_OPTER; /* Enable option byte erase. */ FLASH_CR |= FLASH_CR_STRT; flash_wait_for_last_operation(); FLASH_CR &= ~FLASH_CR_OPTER; /* Disable option byte erase. */ }
void flash_program_option_bytes(u32 address, u16 data) { flash_wait_for_last_operation(); if ((FLASH_CR & FLASH_CR_OPTWRE) == 0) flash_unlock_option_bytes(); FLASH_CR |= FLASH_CR_OPTPG; /* Enable option byte programming. */ (*(volatile u16 *)address) = data; flash_wait_for_last_operation(); FLASH_CR &= ~FLASH_CR_OPTPG; /* Disable option byte programming. */ }
void flash_erase_sector(u32 sector, u32 program_size) { flash_wait_for_last_operation(); flash_set_program_size(program_size); FLASH_CR &= ~(((1 << 0) | (1 << 1) | (1 << 2) | (1 << 3)) << 3); FLASH_CR |= sector; FLASH_CR |= FLASH_STRT; flash_wait_for_last_operation(); FLASH_CR &= ~FLASH_SER; FLASH_CR &= ~(((1 << 0) | (1 << 1) | (1 << 2) | (1 << 3)) << 3); }
void flash_program_byte(uint32_t address, uint8_t data) { flash_wait_for_last_operation(); flash_set_program_size(FLASH_CR_PROGRAM_X8); FLASH_CR |= FLASH_CR_PG; MMIO8(address) = data; flash_wait_for_last_operation(); FLASH_CR &= ~FLASH_CR_PG; /* Disable the PG bit. */ }
void flash_program_half_word(uint32_t address, uint16_t data) { flash_wait_for_last_operation(); flash_set_program_size(FLASH_CR_PROGRAM_X16); FLASH_CR |= FLASH_CR_PG; MMIO16(address) = data; flash_wait_for_last_operation(); FLASH_CR &= ~FLASH_CR_PG; /* Disable the PG bit. */ }
void flash_program_half_word(u32 address, u16 data, u32 program_size) { flash_wait_for_last_operation(); flash_set_program_size(program_size); FLASH_CR |= FLASH_CR_PG; MMIO16(address) = data; flash_wait_for_last_operation(); FLASH_CR &= ~FLASH_CR_PG; /* Disable the PG bit. */ }
void flash_program_option_bytes(uint32_t address, uint16_t data) { flash_wait_for_last_operation(); if ((FLASH_CR & FLASH_CR_OPTWRE) == 0) { flash_unlock_option_bytes(); } FLASH_CR |= FLASH_CR_OPTPG; /* Enable option byte programming. */ MMIO16(address) = data; flash_wait_for_last_operation(); FLASH_CR &= ~FLASH_CR_OPTPG; /* Disable option byte programming. */ }
void flash_program_byte(u32 address, u8 data, u32 program_size) { flash_wait_for_last_operation(); flash_set_program_size(program_size); FLASH_CR |= FLASH_PG; MMIO8(address) = data; flash_wait_for_last_operation(); FLASH_CR &= ~FLASH_PG; /* Disable the PG bit. */ }
void flash_erase_sector(uint8_t sector, uint32_t program_size) { flash_wait_for_last_operation(); flash_set_program_size(program_size); FLASH_CR &= ~(FLASH_CR_SNB_MASK << FLASH_CR_SNB_SHIFT); FLASH_CR |= (sector & FLASH_CR_SNB_MASK) << FLASH_CR_SNB_SHIFT; FLASH_CR |= FLASH_CR_SER; FLASH_CR |= FLASH_CR_STRT; flash_wait_for_last_operation(); FLASH_CR &= ~FLASH_CR_SER; FLASH_CR &= ~(FLASH_CR_SNB_MASK << FLASH_CR_SNB_SHIFT); }
void flash_erase_sector(uint8_t sector, uint32_t program_size) { flash_wait_for_last_operation(); flash_set_program_size(program_size); FLASH_CR &= ~(0xF << 3); FLASH_CR |= (sector << 3) & 0x78; FLASH_CR |= FLASH_CR_SER; FLASH_CR |= FLASH_CR_STRT; flash_wait_for_last_operation(); FLASH_CR &= ~FLASH_CR_SER; FLASH_CR &= ~(0xF << 3); }
/* page address is assumed to be half-word aligned */ void flash_writer_page_write(void *page, void *data, size_t len) { uint8_t *bytes = (uint8_t *) data; uint16_t *flash = (uint16_t *) page; uint16_t half_word; flash_wait_for_last_operation(); size_t count; for (count = len; count > 1; count -= 2) { half_word = *bytes++; half_word |= (uint16_t)*bytes++ << 8; flash_write_half_word(flash++, half_word); } if (count == 1) { half_word = *bytes; /* preserve value of adjacent byte */ half_word |= (uint16_t)(*(uint8_t *)flash) << 8; flash_write_half_word(flash, half_word); } /* reset flags */ FLASH_CR &= ~FLASH_CR_PG; FLASH_SR |= FLASH_SR_EOP; }
void flash_program_half_word(uint32_t address, uint16_t data) { flash_wait_for_last_operation(); if ((MEMORY_SIZE_REG > 512) && (address >= FLASH_BASE+0x00080000)) FLASH_CR2 |= FLASH_CR_PG; else FLASH_CR |= FLASH_CR_PG; MMIO16(address) = data; flash_wait_for_last_operation(); if ((MEMORY_SIZE_REG > 512) && (address >= FLASH_BASE+0x00080000)) FLASH_CR2 &= ~FLASH_CR_PG; else FLASH_CR &= ~FLASH_CR_PG; }
void flash_program_double_word(uint32_t address, uint64_t data) { /* Ensure that all flash operations are complete. */ flash_wait_for_last_operation(); flash_set_program_size(FLASH_CR_PROGRAM_X64); /* Enable writes to flash. */ FLASH_CR |= FLASH_CR_PG; /* Program the first half of the word. */ MMIO64(address) = data; /* Wait for the write to complete. */ flash_wait_for_last_operation(); /* Disable writes to flash. */ FLASH_CR &= ~FLASH_CR_PG; }
void flash_program_word(u32 address, u32 data, u32 program_size) { /* Ensure that all flash operations are complete. */ flash_wait_for_last_operation(); flash_set_program_size(program_size); /* Enable writes to flash. */ FLASH_CR |= FLASH_PG; /* Program the first half of the word. */ MMIO32(address) = data; /* Wait for the write to complete. */ flash_wait_for_last_operation(); /* Disable writes to flash. */ FLASH_CR &= ~FLASH_PG; }
static uint32_t svhandler_flash_lock(void) { /* Wait for any write operation to complete. */ flash_wait_for_last_operation(); /* Disable writes to flash. */ FLASH_CR &= ~FLASH_CR_PG; /* lock flash register */ FLASH_CR |= FLASH_CR_LOCK; /* return flash status register */ return FLASH_SR; }
static void flash_write_half_word(uint16_t *flash, uint16_t half_word) { /* select flash programming */ FLASH_CR |= FLASH_CR_PG; /* perform half-word write */ *flash = half_word; flash_wait_for_last_operation(); }
void flash_program_half_word(uint32_t address, uint16_t data) { flash_wait_for_last_operation(); if ((DESIG_FLASH_SIZE > 512) && (address >= FLASH_BASE+0x00080000)) { FLASH_CR2 |= FLASH_CR_PG; } else { FLASH_CR |= FLASH_CR_PG; } MMIO16(address) = data; flash_wait_for_last_operation(); if ((DESIG_FLASH_SIZE > 512) && (address >= FLASH_BASE+0x00080000)) { FLASH_CR2 &= ~FLASH_CR_PG; } else { FLASH_CR &= ~FLASH_CR_PG; } }
static void svhandler_flash_program(uint32_t psize) { /* Wait for any write operation to complete. */ flash_wait_for_last_operation(); /* check program size argument */ if (psize != FLASH_CR_PROGRAM_X8 && psize != FLASH_CR_PROGRAM_X16 && psize != FLASH_CR_PROGRAM_X32 && psize != FLASH_CR_PROGRAM_X64) return; FLASH_CR = (FLASH_CR & ~(FLASH_CR_PROGRAM_MASK << FLASH_CR_PROGRAM_SHIFT)) | (psize << FLASH_CR_PROGRAM_SHIFT); FLASH_CR |= FLASH_CR_PG; }
void flash_erase_page(uint32_t page_address) { flash_wait_for_last_operation(); if ((MEMORY_SIZE_REG > 512) && (page_address >= FLASH_BASE+0x00080000)) { FLASH_CR2 |= FLASH_CR_PER; FLASH_AR2 = page_address; FLASH_CR2 |= FLASH_CR_STRT; } else { FLASH_CR |= FLASH_CR_PER; FLASH_AR = page_address; FLASH_CR |= FLASH_CR_STRT; } flash_wait_for_last_operation(); if ((MEMORY_SIZE_REG > 512) && (page_address >= FLASH_BASE+0x00080000)) FLASH_CR2 &= ~FLASH_CR_PER; else FLASH_CR &= ~FLASH_CR_PER; }
void flash_program_word(u32 address, u32 data) { /* Ensure that all flash operations are complete. */ flash_wait_for_last_operation(); /* Enable writes to flash. */ FLASH_CR |= FLASH_CR_PG; /* Program the first half of the word. */ (*(volatile u16 *)address) = (u16)data; /* Wait for the write to complete. */ flash_wait_for_last_operation(); /* Program the second half of the word. */ (*(volatile u16 *)(address + 2)) = data >> 16; /* Wait for the write to complete. */ flash_wait_for_last_operation(); /* Disable writes to flash. */ FLASH_CR &= ~FLASH_CR_PG; }
static void svhandler_flash_unlock(void) { flash_wait_for_last_operation(); flash_clear_status_flags(); flash_unlock(); }