Moved hifive1_write_page to separate module

pull/14/head
Daniele Lacamera 2019-06-20 15:46:47 +02:00
parent 637ffa9801
commit 5c8bad047c
5 changed files with 58 additions and 18 deletions

View File

@ -45,7 +45,7 @@ endif
## RISCV
ifeq ($(ARCH),RISCV)
CROSS_COMPILE:=riscv32-unknown-elf-
CFLAGS+=-fno-builtin-printf -DUSE_PLIC -DUSE_M_TIME -g -march=rv32imac -mabi=ilp32 -mcmodel=medany -nostartfiles -DARCH_RISCV
CFLAGS+=-fno-builtin-printf -DUSE_M_TIME -g -march=rv32imac -mabi=ilp32 -mcmodel=medany -nostartfiles -DARCH_RISCV
LDFLAGS+=-march=rv32imac -mabi=ilp32 -mcmodel=medany
MATH_OBJS += ./lib/wolfssl/wolfcrypt/src/sp_c32.o

View File

@ -93,6 +93,7 @@
#define FESPI_READ_STATUS 0x05 /* Read Status Register */
#define FESPI_WRITE_ENABLE 0x06 /* Write Enable */
#define FESPI_PAGE_PROGRAM 0x02 /* Page Program */
#define FESPI_ROW_PROGRAM 0x62 /* Row Program */
#define FESPI_FAST_READ 0x0B /* Fast Read */
#define FESPI_READ 0x03 /* Normal Read */
#ifdef SPI_QUAD_MODE
@ -326,7 +327,6 @@ static RAMFUNCTION void fespi_write_address(uint32_t address)
static RAMFUNCTION void fespi_wait_flash_busy(void)
{
uint8_t rx;
fespi_sw_setdir(FESPI_DIR_RX);
fespi_csmode_hold();
fespi_sw_tx(FESPI_READ_STATUS);
rx = fespi_sw_rx();
@ -335,7 +335,20 @@ static RAMFUNCTION void fespi_wait_flash_busy(void)
rx = fespi_sw_rx();
if ((rx & FESPI_RX_BSY) == 0) {
fespi_csmode_auto();
fespi_sw_setdir(FESPI_DIR_TX);
return;
}
}
}
static RAMFUNCTION void fespi_wait_flash_writing(void)
{
uint8_t rx;
fespi_sw_tx(FESPI_READ_STATUS);
rx = fespi_sw_rx();
while (1) {
fespi_sw_tx(0);
rx = fespi_sw_rx();
if ((rx & FESPI_RX_WE) == 0) {
return;
}
}
@ -413,20 +426,37 @@ void hal_prepare_boot(void)
{
}
#define FLASH_PAGE_SIZE 256
/* Flash functions must be relocated to RAM for execution */
int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
{
int i;
uint32_t off = address & 0xFF;
uint32_t page = address >> 8;
fespi_wait_txwm();
fespi_swmode();
fespi_wait_flash_busy();
fespi_sw_tx(FESPI_WRITE_ENABLE);
fespi_write_address(address);
for(i = 0; i < len; i++)
fespi_sw_tx(data[i]);
fespi_wait_txwm();
fespi_csmode_auto();
while ((page * FLASH_PAGE_SIZE) < (address + len)) {
fespi_wait_flash_busy();
fespi_wait_txwm();
fespi_sw_setdir(FESPI_DIR_TX);
fespi_csmode_hold();
fespi_sw_tx(FESPI_WRITE_ENABLE);
fespi_sw_tx(FESPI_PAGE_PROGRAM);
fespi_write_address(page << 8 + off);
for(i = off; i < FLASH_PAGE_SIZE; i++) {
fespi_sw_tx(data[i]);
}
fespi_csmode_auto();
fespi_sw_setdir(FESPI_DIR_RX);
fespi_wait_txwm();
page++;
off = 0;
}
fespi_wait_flash_writing();
fespi_hwmode();
return 0;
}
@ -446,12 +476,15 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
fespi_wait_txwm();
fespi_swmode();
fespi_wait_flash_busy();
for (p = address; p <= end; p += FESPI_FLASH_SECTOR_SIZE) {
fespi_sw_tx(FESPI_WRITE_ENABLE);
fespi_wait_txwm();
fespi_csmode_hold();
fespi_sw_setdir(FESPI_DIR_TX);
fespi_sw_tx(FESPI_ERASE_SECTOR);
fespi_write_address(p);
fespi_sw_setdir(FESPI_DIR_RX);
fespi_wait_txwm();
fespi_csmode_auto();
fespi_wait_flash_busy();

View File

@ -68,6 +68,7 @@ endif
ifeq ($(TARGET),hifive1)
CFLAGS+=-DRAMFUNCTION='__attribute__((used,section(".ramcode")))'
APP_OBJS+=hifive1_write_page.o
endif
standalone:CFLAGS+=-DTEST_APP_STANDALONE

View File

@ -31,13 +31,14 @@ extern char uart_read(void);
#define MSGSIZE 16
#define PAGESIZE (0x1000) /* Flash sector: 4K */
static uint8_t flash_page[PAGESIZE];
static const char ERR='!';
static const char START='*';
static const char UPDATE='U';
static const char ACK='#';
static uint8_t msg[MSGSIZE];
uint8_t flash_page[PAGESIZE];
extern void write_page(uint32_t dst);
static void ack(uint32_t _off)
{
uint8_t *off = (uint8_t *)(&_off);
@ -61,12 +62,6 @@ static int check(uint8_t *pkt, int size)
return -1;
}
static RAMFUNCTION void write_page(uint32_t dst)
{
hal_flash_erase(dst, PAGESIZE);
hal_flash_write(dst, flash_page, PAGESIZE);
}
void main(void) {
uint32_t tlen = 0;
volatile uint32_t recv_seq;
@ -129,7 +124,7 @@ void main(void) {
{
int psize = r_total - 8;
int flash_page_idx = recv_seq % PAGESIZE;
memcpy(&flash_page[recv_seq % PAGESIZE], msg + 8, psize);
memcpy(&(flash_page[recv_seq % PAGESIZE]), msg + 8, psize);
flash_page_idx += psize;
if ((flash_page_idx == PAGESIZE) || (next_seq + psize >= tot_len)) {
uint32_t dst = ((WOLFBOOT_PARTITION_UPDATE_ADDRESS - 0x20000000) + recv_seq + psize) - flash_page_idx;

View File

@ -0,0 +1,11 @@
#include <stdint.h>
#include "hal.h"
#define PAGESIZE (0x1000) /* Flash sector: 4K */
extern uint8_t flash_page[];
__attribute__((used,section(".ramcode.user")))
void write_page(uint32_t dst)
{
hal_flash_erase(dst, PAGESIZE);
hal_flash_write(dst, flash_page, PAGESIZE);
}