fix subscripting
This commit is contained in:
parent
3618461f9b
commit
864910559b
@ -74,6 +74,8 @@ const nvm_bytearray_obj_t common_hal_mcu_nvm_obj = {
|
|||||||
.base = {
|
.base = {
|
||||||
.type = &nvm_bytearray_type,
|
.type = &nvm_bytearray_type,
|
||||||
},
|
},
|
||||||
|
.len = CIRCUITPY_INTERNAL_NVM_SIZE,
|
||||||
|
.start_address = (uint8_t*) (FLASH_SIZE - CIRCUITPY_INTERNAL_NVM_SIZE),
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -35,37 +35,34 @@
|
|||||||
extern uint32_t __fatfs_flash_start_addr[];
|
extern uint32_t __fatfs_flash_start_addr[];
|
||||||
extern uint32_t __fatfs_flash_length[];
|
extern uint32_t __fatfs_flash_length[];
|
||||||
|
|
||||||
#define NVM_START_ADDR ((uint32_t)__fatfs_flash_start_addr + \
|
|
||||||
(uint32_t)__fatfs_flash_length - CIRCUITPY_INTERNAL_NVM_SIZE)
|
|
||||||
|
|
||||||
uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t *self) {
|
uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t *self) {
|
||||||
return CIRCUITPY_INTERNAL_NVM_SIZE;
|
return self->len;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void write_page(uint32_t page_addr, uint32_t offset, uint32_t len, uint8_t *bytes) {
|
static void write_page(uint8_t* page_addr, uint32_t offset, uint32_t len, uint8_t *bytes) {
|
||||||
// Write a whole page to flash, buffering it first and then erasing and rewriting
|
// Write a whole page to flash, buffering it first and then erasing and rewriting
|
||||||
// it since we can only clear a whole page at a time.
|
// it since we can only clear a whole page at a time.
|
||||||
|
|
||||||
if (offset == 0 && len == FLASH_PAGE_SIZE) {
|
if (offset == 0 && len == FLASH_PAGE_SIZE) {
|
||||||
nrf_nvm_safe_flash_page_write(page_addr, bytes);
|
nrf_nvm_safe_flash_page_write((uint32_t) page_addr, bytes);
|
||||||
} else {
|
} else {
|
||||||
uint8_t buffer[FLASH_PAGE_SIZE];
|
uint8_t buffer[FLASH_PAGE_SIZE];
|
||||||
memcpy(buffer, (uint8_t *)page_addr, FLASH_PAGE_SIZE);
|
memcpy(buffer, page_addr, FLASH_PAGE_SIZE);
|
||||||
memcpy(buffer + offset, bytes, len);
|
memcpy(buffer + offset, bytes, len);
|
||||||
nrf_nvm_safe_flash_page_write(page_addr, buffer);
|
nrf_nvm_safe_flash_page_write((uint32_t) page_addr, buffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t *self,
|
bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t *self,
|
||||||
uint32_t start_index, uint8_t* values, uint32_t len) {
|
uint32_t start_index, uint8_t* values, uint32_t len) {
|
||||||
|
|
||||||
uint32_t address = NVM_START_ADDR + start_index;
|
uint32_t address = ((uint32_t) self->start_address) + start_index;
|
||||||
uint32_t offset = address % FLASH_PAGE_SIZE;
|
uint32_t offset = address % FLASH_PAGE_SIZE;
|
||||||
uint32_t page_addr = address - offset;
|
uint32_t page_addr = address - offset;
|
||||||
|
|
||||||
while (len) {
|
while (len) {
|
||||||
uint32_t write_len = MIN(len, FLASH_PAGE_SIZE - offset);
|
uint32_t write_len = MIN(len, FLASH_PAGE_SIZE - offset);
|
||||||
write_page(page_addr, offset, write_len, values);
|
write_page((uint8_t*) page_addr, offset, write_len, values);
|
||||||
len -= write_len;
|
len -= write_len;
|
||||||
values += write_len;
|
values += write_len;
|
||||||
page_addr += FLASH_PAGE_SIZE;
|
page_addr += FLASH_PAGE_SIZE;
|
||||||
@ -76,5 +73,5 @@ bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t *self,
|
|||||||
|
|
||||||
void common_hal_nvm_bytearray_get_bytes(nvm_bytearray_obj_t *self,
|
void common_hal_nvm_bytearray_get_bytes(nvm_bytearray_obj_t *self,
|
||||||
uint32_t start_index, uint32_t len, uint8_t* values) {
|
uint32_t start_index, uint32_t len, uint8_t* values) {
|
||||||
memcpy(values, (uint8_t *)(NVM_START_ADDR + start_index), len);
|
memcpy(values, self->start_address + start_index, len);
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
mp_obj_base_t base;
|
mp_obj_base_t base;
|
||||||
uint32_t start_address;
|
uint8_t* start_address;
|
||||||
uint32_t len;
|
uint32_t len;
|
||||||
} nvm_bytearray_obj_t;
|
} nvm_bytearray_obj_t;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user