Fixed lots of write/read stuff.
Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
44
flash.c
44
flash.c
@@ -53,13 +53,11 @@ const uintptr_t start_data_pool = (XIP_BASE + FLASH_TARGET_OFFSET);
|
|||||||
const uintptr_t end_data_pool = (XIP_BASE + PICO_FLASH_SIZE_BYTES)-FLASH_DATA_HEADER_SIZE; //This is a fixed value. DO NOT CHANGE
|
const uintptr_t end_data_pool = (XIP_BASE + PICO_FLASH_SIZE_BYTES)-FLASH_DATA_HEADER_SIZE; //This is a fixed value. DO NOT CHANGE
|
||||||
#define FLASH_ADDR_DATA_STORAGE_START start_data_pool
|
#define FLASH_ADDR_DATA_STORAGE_START start_data_pool
|
||||||
|
|
||||||
extern int flash_erase_page (uintptr_t addr);
|
extern int flash_program_block(uintptr_t addr, const uint8_t *data, size_t len);
|
||||||
extern int flash_program_halfword (uintptr_t addr, uint16_t data);
|
extern int flash_program_halfword (uintptr_t addr, uint16_t data);
|
||||||
int flash_program_block(uintptr_t addr, const uint8_t *data, size_t len) ;
|
extern int flash_program_uintptr(uintptr_t, uintptr_t);
|
||||||
extern int flash_check_blank (const uint8_t *p_start, size_t size);
|
|
||||||
extern int flash_write (uintptr_t dst_addr, const uint8_t *src, size_t len);
|
|
||||||
|
|
||||||
void low_flash_available();
|
extern void low_flash_available();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Flash data pool managenent
|
* Flash data pool managenent
|
||||||
@@ -83,34 +81,36 @@ void low_flash_available();
|
|||||||
* DATA: data * LEN
|
* DATA: data * LEN
|
||||||
* PAD: optional byte for 16-bit alignment
|
* PAD: optional byte for 16-bit alignment
|
||||||
*/
|
*/
|
||||||
|
|
||||||
uintptr_t allocate_free_addr(uint16_t size) {
|
uintptr_t allocate_free_addr(uint16_t size) {
|
||||||
if (size > FLASH_SECTOR_SIZE)
|
if (size > FLASH_SECTOR_SIZE)
|
||||||
return 0x0; //ERROR
|
return 0x0; //ERROR
|
||||||
size_t real_size = size+sizeof(uint16_t)+sizeof(uintptr_t)+sizeof(uint16_t); //len+len size+next address+fid
|
size_t real_size = size+sizeof(uint16_t)+sizeof(uintptr_t)+sizeof(uint16_t); //len+len size+next address+fid
|
||||||
for (uintptr_t base = end_data_pool; base >= start_data_pool; base = *(uintptr_t *)base) {
|
uintptr_t next_base = 0x0;
|
||||||
|
for (uintptr_t base = end_data_pool; base >= start_data_pool; base = next_base) {
|
||||||
uintptr_t addr_alg = base & -FLASH_SECTOR_SIZE; //start address of sector
|
uintptr_t addr_alg = base & -FLASH_SECTOR_SIZE; //start address of sector
|
||||||
uintptr_t potential_addr = base-real_size;
|
uintptr_t potential_addr = base-real_size;
|
||||||
if (*(uintptr_t *)base == 0x0) { //we are at the end
|
next_base = flash_read_uintptr(base);
|
||||||
|
if (next_base == 0x0) { //we are at the end
|
||||||
//now we check if we fit in the current sector
|
//now we check if we fit in the current sector
|
||||||
if (addr_alg <= potential_addr) //it fits in the current sector
|
if (addr_alg <= potential_addr) //it fits in the current sector
|
||||||
{
|
{
|
||||||
*(uintptr_t *)potential_addr = 0x0;
|
flash_program_uintptr(potential_addr, 0x0);
|
||||||
*(uintptr_t *)base = potential_addr;
|
flash_program_uintptr(base, potential_addr);
|
||||||
return potential_addr;
|
return potential_addr;
|
||||||
}
|
}
|
||||||
else if (addr_alg-FLASH_SECTOR_SIZE >= start_data_pool) { //check whether it fits in the next sector, so we take addr_aligned as the base
|
else if (addr_alg-FLASH_SECTOR_SIZE >= start_data_pool) { //check whether it fits in the next sector, so we take addr_aligned as the base
|
||||||
potential_addr = addr_alg-real_size;
|
potential_addr = addr_alg-real_size;
|
||||||
*(uintptr_t *)potential_addr = 0x0;
|
flash_program_uintptr(potential_addr, 0x0);
|
||||||
*(uintptr_t *)base = potential_addr;
|
flash_program_uintptr(base, potential_addr);
|
||||||
return potential_addr;
|
return potential_addr;
|
||||||
}
|
}
|
||||||
return 0x0;
|
return 0x0;
|
||||||
}
|
}
|
||||||
//we check if |base-(next_addr+size_next_addr)| >= |base-potential_addr|
|
//we check if |base-(next_addr+size_next_addr)| > |base-potential_addr| only if fid != 1xxx (not size blocked)
|
||||||
else if (base-(*(uintptr_t *)base+*(uint16_t *)(*(uintptr_t *)base+sizeof(uintptr_t)+sizeof(uint16_t))) >= base-potential_addr && addr_alg <= potential_addr) {
|
else if (addr_alg <= potential_addr && base-(next_base+flash_read_uint16(next_base+sizeof(uintptr_t)+sizeof(uint16_t))) > base-potential_addr && flash_read_uint16(next_base+sizeof(uintptr_t)+sizeof(uint16_t)) & 0x1000 != 0x1000) {
|
||||||
*(uintptr_t *)potential_addr = *(uintptr_t *)base;
|
flash_program_uintptr(potential_addr, next_base);
|
||||||
*(uintptr_t *)base = potential_addr;
|
flash_program_uintptr(base, potential_addr);
|
||||||
return potential_addr;
|
return potential_addr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -118,11 +118,11 @@ uintptr_t allocate_free_addr(uint16_t size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int flash_clear_file(file_t *file) {
|
int flash_clear_file(file_t *file) {
|
||||||
uintptr_t prev_addr = (uintptr_t)(file->data+*(uint16_t *)file->data);
|
uintptr_t prev_addr = (uintptr_t)(file->data+flash_read_uint16(file->data)+sizeof(uint16_t));
|
||||||
uintptr_t base_addr = (uintptr_t)file->data-sizeof(uintptr_t);
|
uintptr_t base_addr = (uintptr_t)file->data-sizeof(uintptr_t);
|
||||||
uintptr_t next_addr = *(uintptr_t *)base_addr;
|
uintptr_t next_addr = flash_read_uintptr(base_addr);
|
||||||
*(uintptr_t *)prev_addr = next_addr;
|
flash_program_uintptr(prev_addr, next_addr);
|
||||||
*(uint16_t *)file->data = 0;
|
flash_program_halfword((uintptr_t)file->data, 0);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,11 +130,10 @@ int flash_write_data_to_file(file_t *file, const uint8_t *data, uint16_t len) {
|
|||||||
if (len > FLASH_SECTOR_SIZE)
|
if (len > FLASH_SECTOR_SIZE)
|
||||||
return 1;
|
return 1;
|
||||||
if (file->data) { //already in flash
|
if (file->data) { //already in flash
|
||||||
uint16_t size_file_flash = *(uint16_t *)file->data;
|
uint16_t size_file_flash = flash_read_uint16(file->data);
|
||||||
if (len <= size_file_flash) { //it fits, no need to move it
|
if (len <= size_file_flash) { //it fits, no need to move it
|
||||||
flash_program_halfword((uintptr_t)file->data, len);
|
flash_program_halfword((uintptr_t)file->data, len);
|
||||||
flash_program_block((uintptr_t)file->data+sizeof(uint16_t), data, len);
|
flash_program_block((uintptr_t)file->data+sizeof(uint16_t), data, len);
|
||||||
low_flash_available();
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
else { //we clear the old file
|
else { //we clear the old file
|
||||||
@@ -148,7 +147,6 @@ int flash_write_data_to_file(file_t *file, const uint8_t *data, uint16_t len) {
|
|||||||
flash_program_halfword(new_addr+sizeof(uintptr_t), file->fid);
|
flash_program_halfword(new_addr+sizeof(uintptr_t), file->fid);
|
||||||
flash_program_halfword((uintptr_t)file->data, len);
|
flash_program_halfword((uintptr_t)file->data, len);
|
||||||
flash_program_block((uintptr_t)file->data+sizeof(uint16_t), data, len);
|
flash_program_block((uintptr_t)file->data+sizeof(uint16_t), data, len);
|
||||||
low_flash_available();
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user