vsf_err_t stm32_flash_readpage(uint8_t index, uint32_t offset, uint8_t *buff) { uint32_t page_size; switch (index) { case 0: stm32_flash_getcapacity(index, &page_size, NULL); return stm32_flash_read(index, offset, buff, page_size); default: return VSFERR_NOT_SUPPORT; } }
void stm32_flash_write(uint32_t address, const void * data, uint32_t length) { const uint32_t end = address + length; uint32_t i; uint32_t next_page, curr_page; uint16_t offset; #if STATIC_FLASH_BUFFER static uint8_t buf[FLASH_PAGE_SIZE]; #else uint8_t buf[FLASH_PAGE_SIZE]; #endif for(i = address; i < end;) { next_page = (i | (FLASH_PAGE_SIZE-1)) + 1; curr_page = i & ~(FLASH_PAGE_SIZE-1); offset = i-curr_page; if(next_page > end) { next_page = end; } // Read a page from flash and put it into a mirror buffer. stm32_flash_read(curr_page, buf, FLASH_PAGE_SIZE); // Update flash mirror data with new data. memcpy(buf + offset, data, next_page - i); // Erase flash page. ENERGEST_ON(ENERGEST_TYPE_FLASH_WRITE); FLASH_Unlock(); FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); FLASH_ErasePage(i); // Write modified data form mirror buffer into the flash. uint16_t j = 0; while(j < FLASH_PAGE_SIZE) { FLASH_ProgramWord(curr_page + j, *(u32_t *)&buf[j]); j = j + 4; } FLASH_Lock(); ENERGEST_OFF(ENERGEST_TYPE_FLASH_WRITE); data = (uint8_t *)data + next_page - i; i = next_page; } }