Skip to content

Commit

Permalink
Asynchronous flash row write for pic32.
Browse files Browse the repository at this point in the history
  • Loading branch information
eerimoq committed Sep 11, 2018
1 parent d867f6e commit 9e9ab07
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/drivers/ports/pic32mm/flash_port.h
Expand Up @@ -38,4 +38,16 @@ struct flash_driver_t {
struct flash_device_t *dev_p;
};

/**
* Start an asynchronous flash row operations of 256 bytes.
*/
int flash_async_write_row(struct flash_driver_t *self_p,
uintptr_t dst,
const void *src_p);

/**
* Start for current asynchronous flash operations to finish.
*/
int flash_async_wait(struct flash_driver_t *self_p);

#endif
43 changes: 43 additions & 0 deletions src/drivers/ports/pic32mm/flash_port.i
Expand Up @@ -145,3 +145,46 @@ static int flash_port_erase(struct flash_driver_t *self_p,

return (res);
}

int flash_async_write_row(struct flash_driver_t *self_p,
uintptr_t dst,
const void *src_p)
{
volatile struct pic32mm_flash_t *regs_p;

regs_p = self_p->dev_p->regs_p;
regs_p = PIC32MM_FLASH;

pic32mm_reg_write(&regs_p->NVMADDR, dst);
pic32mm_reg_write(&regs_p->NVMSRCADDR,
PIC32MM_PHYSICAL_ADDRESS((uint32_t)src_p));
pic32mm_reg_write(&regs_p->NVMCON,
(PIC32MM_FLASH_NVMCON_WREN
| PIC32MM_FLASH_NVMCON_NVMOP_ROW_PROGRAM));
write_protect_unlock(regs_p);
pic32mm_reg_set(&regs_p->NVMCON, PIC32MM_FLASH_NVMCON_WR);

return (0);
}

int flash_async_wait(struct flash_driver_t *self_p)
{
int res;
volatile struct pic32mm_flash_t *regs_p;

res = 0;
regs_p = self_p->dev_p->regs_p;
regs_p = PIC32MM_FLASH;

wait_for_operation_completed(regs_p);

if (pic32mm_reg_read(&regs_p->NVMCON) & PIC32MM_FLASH_NVMCON_WRERR) {
res = -1;
}

pic32mm_reg_clr(&regs_p->NVMCON, PIC32MM_FLASH_NVMCON_WREN);

write_protect_lock(regs_p);

return (res);
}
3 changes: 2 additions & 1 deletion src/mcus/pic32mm/pic32mm.h
Expand Up @@ -305,6 +305,8 @@ struct pic32mm_conf_t {
#define PIC32MM_UDID_SIZE 0x00000014
#define PIC32MM_UDID_END 0x1fc41854

#define PIC32MM_PHYSICAL_ADDRESS(address) ((address) & 0x1fffffff)

/* Interrupt service routine. */
#define ISR(vector) \
void isr_ ## vector(int vector_number)
Expand Down Expand Up @@ -369,5 +371,4 @@ struct pic32mm_conf_t {
"r" (value) : \
"memory")


#endif

0 comments on commit 9e9ab07

Please sign in to comment.