28 #if defined(PIOS_INCLUDE_FLASH)
35 static bool pios_flash_partition_get_chip_extents(
const struct pios_flash_partition *partition, uint32_t *chip_start_offset, uint32_t *chip_end_offset);
38 static uint8_t num_partitions;
40 #define MIN(x,y) ((x) < (y) ? (x) : (y))
52 uint8_t partition_table_len,
59 uint32_t basic_capacity = 0;
64 sectors[
i].sector_size;
69 if (num_bytes == basic_capacity) {
78 uint32_t additional_bytes = num_bytes - basic_capacity;
79 uint32_t additional_sectors = additional_bytes / sectors->
sector_size;
88 for (
int i = 0;
i < partition_table_len;
i++) {
90 if (partition_table[
i].
chip_desc == descriptor) {
93 partition_table[
i].
size += additional_bytes;
108 for (uint8_t
i = 0;
i < partition_table_len;
i++) {
119 uint32_t chip_start_offset;
120 uint32_t chip_end_offset;
121 if (!pios_flash_partition_get_chip_extents(partition,
127 PIOS_Assert(partition->
size == (chip_end_offset - chip_start_offset + 1));
130 partitions = partition_table;
131 num_partitions = partition_table_len;
145 for (uint8_t
i = 0;
i < num_partitions;
i++) {
146 if (partitions[
i].label == label) {
147 *partition_id = (uintptr_t) &partitions[
i];
162 return num_partitions;
167 return ((partition >= &partitions[0]) && (partition < &partitions[num_partitions]));
170 struct pios_flash_sector_desc {
176 uint32_t chip_offset;
177 uint32_t partition_offset;
178 uint32_t sector_size;
181 static bool pios_flash_get_partition_first_sector(
const struct pios_flash_partition *partition,
struct pios_flash_sector_desc *curr)
184 uint32_t chip_offset = 0;
192 curr->block_id = block_id;
195 curr->chip_offset = chip_offset;
197 curr->partition_offset = 0;
203 chip_offset += num_sectors_in_range *block->
sector_size;
210 static bool pios_flash_get_partition_next_sector(
const struct pios_flash_partition *partition,
struct pios_flash_sector_desc *curr)
257 static bool pios_flash_partition_get_chip_extents(
const struct pios_flash_partition *partition, uint32_t *chip_start_offset, uint32_t *chip_end_offset)
259 struct pios_flash_sector_desc sector_desc;
260 if (!pios_flash_get_partition_first_sector(partition, §or_desc))
263 if (chip_start_offset)
264 *chip_start_offset = sector_desc.chip_offset;
269 }
while (pios_flash_get_partition_next_sector(partition, §or_desc));
272 *chip_end_offset = sector_desc.chip_offset - 1;
291 PIOS_Assert(PIOS_FLASH_validate_partition(partition));
293 *partition_size = partition->
size;
307 PIOS_Assert(PIOS_FLASH_validate_partition(partition));
309 if (partition_size) {
310 *partition_size = partition->
size;
331 PIOS_Assert(PIOS_FLASH_validate_partition(partition));
351 if (!PIOS_FLASH_validate_partition(partition))
380 PIOS_Assert(PIOS_FLASH_validate_partition(partition));
385 struct pios_flash_sector_desc sector_desc;
386 if (!pios_flash_get_partition_first_sector(partition, §or_desc))
390 uint32_t erase_offset = start_offset;
397 if ((erase_offset >= sector_desc.partition_offset) &&
398 (erase_offset < sector_desc.partition_offset + sector_desc.sector_size)) {
400 if (erase_offset != sector_desc.partition_offset)
404 if (size < sector_desc.sector_size)
410 sector_desc.chip_offset) != 0) {
415 erase_offset += sector_desc.sector_size;
416 size -= sector_desc.sector_size;
418 }
while (pios_flash_get_partition_next_sector(partition, §or_desc));
438 PIOS_Assert(PIOS_FLASH_validate_partition(partition));
443 struct pios_flash_sector_desc sector_desc;
445 if (!pios_flash_get_partition_first_sector(partition, §or_desc))
453 sector_desc.chip_offset) != 0) {
456 }
while (pios_flash_get_partition_next_sector(partition, §or_desc));
478 PIOS_Assert(PIOS_FLASH_validate_partition(partition));
484 if ((partition_offset + len) > partition->
size)
491 uint32_t page_remaining = page_size - (partition_offset % page_size);
492 uint16_t write_size =
MIN(len, page_remaining);
505 partition_offset += write_size;
524 int32_t
PIOS_FLASH_read_data(uintptr_t partition_id, uint32_t partition_offset, uint8_t *data, uint16_t len)
528 PIOS_Assert(PIOS_FLASH_validate_partition(partition));
534 if ((partition_offset + len) > partition->
size)
int32_t PIOS_FLASH_find_partition_id(enum pios_flash_partition_labels label, uintptr_t *partition_id)
int32_t PIOS_FLASH_erase_partition(uintptr_t partition_id)
int32_t(* read_data)(uintptr_t chip_id, uint32_t chip_offset, uint8_t *data, uint16_t len)
enum dfu_partition_label label
Main PiOS header to include all the compiled in PiOS options.
uint16_t PIOS_FLASH_get_num_partitions(void)
void *(* get_pointer)(uintptr_t chip_id, uint32_t chip_offset)
const struct pios_flash_sector_range * sector_blocks
int32_t PIOS_FLASH_get_partition_size(uintptr_t partition_id, uint32_t *partition_size)
void * PIOS_FLASH_get_address(uintptr_t partition_id, uint32_t *partition_size)
int32_t(* erase_sector)(uintptr_t chip_id, uint32_t chip_sector, uint32_t chip_offset)
const struct pios_flash_chip * chip_desc
uint8_t data[XFER_BYTES_PER_PACKET]
int32_t PIOS_FLASH_write_data(uintptr_t partition_id, uint32_t offset, const uint8_t *data, uint16_t len)
int32_t PIOS_FLASH_end_transaction(uintptr_t partition_id)
int32_t(* end_transaction)(uintptr_t chip_id)
int32_t PIOS_FLASH_erase_range(uintptr_t partition_id, uint32_t start_offset, uint32_t size)
void PIOS_FLASH_fixup_partitions_for_capacity(struct pios_flash_partition *partition_table, uint8_t partition_table_len, const struct pios_flash_chip *descriptor, struct pios_flash_sector_range *sectors, uint32_t num_bytes)
int32_t(* write_data)(uintptr_t chip_id, uint32_t chip_offset, const uint8_t *data, uint16_t len)
void PIOS_FLASH_register_partition_table(const struct pios_flash_partition partition_table[], uint8_t num_partitions)
pios_flash_partition_labels
const struct pios_flash_driver * driver
int32_t PIOS_FLASH_read_data(uintptr_t partition_id, uint32_t offset, uint8_t *data, uint16_t len)
int32_t PIOS_FLASH_start_transaction(uintptr_t partition_id)
enum pios_flash_partition_labels label
#define PIOS_Assert(test)
int32_t(* start_transaction)(uintptr_t chip_id)