void config_updateToFlash() { uint16_t *dst = &_flashConfigROMBegin; uint16_t *src = &_flashConfigRAMBegin; bool canUpdateFlash = false; for (; src < &_flashConfigRAMEnd; ++src, ++dst) { if (*dst != *src) { canUpdateFlash = true; break; } } if (false != canUpdateFlash) { dst = &_flashConfigROMBegin; src = &_flashConfigRAMBegin; (void) flash_unlock(); flash_erase_sector(FLASH_CONFIG_SECTOR, FLASH_PROGRAMM_ACCESS_SIZE); for (; src < &_flashConfigRAMEnd; ++src, ++dst) { flash_program_half_word((uint32_t)dst, *src, FLASH_PROGRAMM_ACCESS_SIZE); } (void) flash_lock(); } }
static int32_t pflash_program_bytes(struct FlashInfo* flash, uint32_t src, uint32_t size, uint32_t chksum) { uint32_t i; /* erase */ flash_unlock(); flash_erase_page(flash->addr); flash_lock(); /* verify erase */ for (i=0; i<flash->page_size; i+=4) { if ((*(uint32_t*) (flash->addr + i)) != 0xFFFFFFFF) return -1; } flash_unlock(); /* write full 16 bit words */ for (i=0; i<(size & ~1); i+=2) { flash_program_half_word(flash->addr+i, (uint16_t)(*(uint8_t*)(src+i) | (*(uint8_t*)(src+i+1)) << 8)); } /* fill bytes with a zero */ if (size & 1) { flash_program_half_word(flash->addr+i, (uint16_t)(*(uint8_t*)(src+i))); } /* write size */ flash_program_half_word(flash->addr+flash->page_size-FSIZ, (uint16_t)(size & 0xFFFF)); flash_program_half_word(flash->addr+flash->page_size-FSIZ+2, (uint16_t)((size >> 16) & 0xFFFF)); /* write checksum */ flash_program_half_word(flash->addr+flash->page_size-FCHK, (uint16_t)(chksum & 0xFFFF)); flash_program_half_word(flash->addr+flash->page_size-FCHK+2, (uint16_t)((chksum >> 16) & 0xFFFF)); flash_lock(); /* verify data */ for (i=0; i<size; i++) { if ((*(uint8_t*) (flash->addr+i)) != (*(uint8_t*) (src+i))) return -2; } if (*(uint32_t*) (flash->addr+flash->page_size-FSIZ) != size) return -3; if (*(uint32_t*) (flash->addr+flash->page_size-FCHK) != chksum) return -4; return 0; }
void aseba_flash_write_page(int aseba_page, uint16_t *page_buffer) { int i; for (i = 0; i < ASEBA_PAGE_SIZE_IN_WORDS; i++) { flash_program_half_word((uint32_t)aseba_page_address(aseba_page, i), page_buffer[i]); } flash_lock(); }
void dfu_flash_program_buffer(uint32_t baseaddr, void *buf, int len) { for(int i = 0; i < len; i += 2) flash_program_half_word(baseaddr + i, *(uint16_t*)(buf+i)); /* Call the platform specific dfu event callback. */ dfu_event(); }
void config_store(void) { uint16_t size = sizeof(struct Config); uint32_t addr = CONFIG_ADDR; uint8_t *byte_config = (uint8_t *)&usbrf_config; uint16_t write_word; int i; /* Unlock flash. */ flash_unlock(); /* Erase the config storage page. */ flash_erase_page(CONFIG_ADDR); /* Write config struct to flash. */ write_word = 0xFFFF; for (i = 0; i < size; i++) { write_word = (write_word << 8) | (*(byte_config++)); if ((i % 2) == 1) { flash_program_half_word(addr, write_word); addr += 2; } } if ((i % 2) == 1) { write_word = (write_word << 8) | 0xFF; flash_program_half_word(addr, write_word); } /* Write config CRC to flash. */ /* Lock flash. */ flash_lock(); /* Check flash content for accuracy. */ }
void flash_program_word(uint32_t address, uint32_t data) { flash_program_half_word(address,(uint16_t)data); flash_program_half_word(address+2,(uint16_t)(data>>16)); }
void dfu_flash_program_buffer(uint32_t baseaddr, void *buf, int len) { for(int i = 0; i < len; i += 2) flash_program_half_word(baseaddr + i, *(u16*)(buf+i)); }