Skip to content

Commit 5c8bad0

Browse files
committed
Moved hifive1_write_page to separate module
1 parent 637ffa9 commit 5c8bad0

5 files changed

Lines changed: 58 additions & 18 deletions

File tree

arch.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ endif
4545
## RISCV
4646
ifeq ($(ARCH),RISCV)
4747
CROSS_COMPILE:=riscv32-unknown-elf-
48-
CFLAGS+=-fno-builtin-printf -DUSE_PLIC -DUSE_M_TIME -g -march=rv32imac -mabi=ilp32 -mcmodel=medany -nostartfiles -DARCH_RISCV
48+
CFLAGS+=-fno-builtin-printf -DUSE_M_TIME -g -march=rv32imac -mabi=ilp32 -mcmodel=medany -nostartfiles -DARCH_RISCV
4949
LDFLAGS+=-march=rv32imac -mabi=ilp32 -mcmodel=medany
5050
MATH_OBJS += ./lib/wolfssl/wolfcrypt/src/sp_c32.o
5151

hal/hifive1.c

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
#define FESPI_READ_STATUS 0x05 /* Read Status Register */
9494
#define FESPI_WRITE_ENABLE 0x06 /* Write Enable */
9595
#define FESPI_PAGE_PROGRAM 0x02 /* Page Program */
96+
#define FESPI_ROW_PROGRAM 0x62 /* Row Program */
9697
#define FESPI_FAST_READ 0x0B /* Fast Read */
9798
#define FESPI_READ 0x03 /* Normal Read */
9899
#ifdef SPI_QUAD_MODE
@@ -326,7 +327,6 @@ static RAMFUNCTION void fespi_write_address(uint32_t address)
326327
static RAMFUNCTION void fespi_wait_flash_busy(void)
327328
{
328329
uint8_t rx;
329-
fespi_sw_setdir(FESPI_DIR_RX);
330330
fespi_csmode_hold();
331331
fespi_sw_tx(FESPI_READ_STATUS);
332332
rx = fespi_sw_rx();
@@ -335,7 +335,20 @@ static RAMFUNCTION void fespi_wait_flash_busy(void)
335335
rx = fespi_sw_rx();
336336
if ((rx & FESPI_RX_BSY) == 0) {
337337
fespi_csmode_auto();
338-
fespi_sw_setdir(FESPI_DIR_TX);
338+
return;
339+
}
340+
}
341+
}
342+
343+
static RAMFUNCTION void fespi_wait_flash_writing(void)
344+
{
345+
uint8_t rx;
346+
fespi_sw_tx(FESPI_READ_STATUS);
347+
rx = fespi_sw_rx();
348+
while (1) {
349+
fespi_sw_tx(0);
350+
rx = fespi_sw_rx();
351+
if ((rx & FESPI_RX_WE) == 0) {
339352
return;
340353
}
341354
}
@@ -413,20 +426,37 @@ void hal_prepare_boot(void)
413426
{
414427
}
415428

429+
#define FLASH_PAGE_SIZE 256
430+
416431
/* Flash functions must be relocated to RAM for execution */
417432
int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
418433
{
419434
int i;
435+
uint32_t off = address & 0xFF;
436+
uint32_t page = address >> 8;
420437
fespi_wait_txwm();
421438
fespi_swmode();
422-
fespi_wait_flash_busy();
423-
fespi_sw_tx(FESPI_WRITE_ENABLE);
424-
fespi_write_address(address);
425-
for(i = 0; i < len; i++)
426-
fespi_sw_tx(data[i]);
427-
fespi_wait_txwm();
428-
fespi_csmode_auto();
439+
440+
while ((page * FLASH_PAGE_SIZE) < (address + len)) {
441+
fespi_wait_flash_busy();
442+
fespi_wait_txwm();
443+
fespi_sw_setdir(FESPI_DIR_TX);
444+
fespi_csmode_hold();
445+
fespi_sw_tx(FESPI_WRITE_ENABLE);
446+
fespi_sw_tx(FESPI_PAGE_PROGRAM);
447+
fespi_write_address(page << 8 + off);
448+
for(i = off; i < FLASH_PAGE_SIZE; i++) {
449+
fespi_sw_tx(data[i]);
450+
}
451+
fespi_csmode_auto();
452+
fespi_sw_setdir(FESPI_DIR_RX);
453+
fespi_wait_txwm();
454+
page++;
455+
off = 0;
456+
}
457+
fespi_wait_flash_writing();
429458
fespi_hwmode();
459+
430460
return 0;
431461
}
432462

@@ -446,12 +476,15 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
446476
fespi_wait_txwm();
447477
fespi_swmode();
448478
fespi_wait_flash_busy();
479+
449480
for (p = address; p <= end; p += FESPI_FLASH_SECTOR_SIZE) {
450481
fespi_sw_tx(FESPI_WRITE_ENABLE);
451482
fespi_wait_txwm();
452483
fespi_csmode_hold();
484+
fespi_sw_setdir(FESPI_DIR_TX);
453485
fespi_sw_tx(FESPI_ERASE_SECTOR);
454486
fespi_write_address(p);
487+
fespi_sw_setdir(FESPI_DIR_RX);
455488
fespi_wait_txwm();
456489
fespi_csmode_auto();
457490
fespi_wait_flash_busy();

test-app/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ endif
6868

6969
ifeq ($(TARGET),hifive1)
7070
CFLAGS+=-DRAMFUNCTION='__attribute__((used,section(".ramcode")))'
71+
APP_OBJS+=hifive1_write_page.o
7172
endif
7273

7374
standalone:CFLAGS+=-DTEST_APP_STANDALONE

test-app/hifive1.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ extern char uart_read(void);
3131

3232
#define MSGSIZE 16
3333
#define PAGESIZE (0x1000) /* Flash sector: 4K */
34-
static uint8_t flash_page[PAGESIZE];
3534
static const char ERR='!';
3635
static const char START='*';
3736
static const char UPDATE='U';
3837
static const char ACK='#';
3938
static uint8_t msg[MSGSIZE];
4039

40+
uint8_t flash_page[PAGESIZE];
41+
extern void write_page(uint32_t dst);
4142
static void ack(uint32_t _off)
4243
{
4344
uint8_t *off = (uint8_t *)(&_off);
@@ -61,12 +62,6 @@ static int check(uint8_t *pkt, int size)
6162
return -1;
6263
}
6364

64-
static RAMFUNCTION void write_page(uint32_t dst)
65-
{
66-
hal_flash_erase(dst, PAGESIZE);
67-
hal_flash_write(dst, flash_page, PAGESIZE);
68-
}
69-
7065
void main(void) {
7166
uint32_t tlen = 0;
7267
volatile uint32_t recv_seq;
@@ -129,7 +124,7 @@ void main(void) {
129124
{
130125
int psize = r_total - 8;
131126
int flash_page_idx = recv_seq % PAGESIZE;
132-
memcpy(&flash_page[recv_seq % PAGESIZE], msg + 8, psize);
127+
memcpy(&(flash_page[recv_seq % PAGESIZE]), msg + 8, psize);
133128
flash_page_idx += psize;
134129
if ((flash_page_idx == PAGESIZE) || (next_seq + psize >= tot_len)) {
135130
uint32_t dst = ((WOLFBOOT_PARTITION_UPDATE_ADDRESS - 0x20000000) + recv_seq + psize) - flash_page_idx;

test-app/hifive1_write_page.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <stdint.h>
2+
#include "hal.h"
3+
#define PAGESIZE (0x1000) /* Flash sector: 4K */
4+
extern uint8_t flash_page[];
5+
6+
__attribute__((used,section(".ramcode.user")))
7+
void write_page(uint32_t dst)
8+
{
9+
hal_flash_erase(dst, PAGESIZE);
10+
hal_flash_write(dst, flash_page, PAGESIZE);
11+
}

0 commit comments

Comments
 (0)