Skip to content

Commit e2ea920

Browse files
committed
Move functions to RAM for flash operation
- Remove standard library as much as possible - Do not use memcpy before it is relocated to RAM. - Prefer library functions over RX intrinsics.( Compiler option change)
1 parent 756d569 commit e2ea920

File tree

13 files changed

+577
-362
lines changed

13 files changed

+577
-362
lines changed

IDE/Renesas/e2studio/RX72N/app_RenesasRX01/.cproject

Lines changed: 46 additions & 35 deletions
Large diffs are not rendered by default.

IDE/Renesas/e2studio/RX72N/app_RenesasRX01/.project

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,10 @@
4444
<type>1</type>
4545
<locationURI>PARENT-5-PROJECT_LOC/hal/renesas-rx.c</locationURI>
4646
</link>
47+
<link>
48+
<name>src/string.c</name>
49+
<type>1</type>
50+
<locationURI>PARENT-5-PROJECT_LOC/src/string.c</locationURI>
51+
</link>
4752
</linkedResources>
4853
</projectDescription>

IDE/Renesas/e2studio/RX72N/app_RenesasRX01/app_RenesasRX01.scfg

Lines changed: 236 additions & 236 deletions
Large diffs are not rendered by default.

IDE/Renesas/e2studio/RX72N/app_RenesasRX01/src/app_RenesasRX01.c

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
#include <string.h>
2828
#include "hal.h"
2929
#include "wolfboot/wolfboot.h"
30-
30+
#include "image.h"
31+
#include "r_smc_entry.h"
3132
static const char* state2str(uint8_t s)
3233
{
3334
switch(s) {
@@ -39,6 +40,17 @@ static const char* state2str(uint8_t s)
3940
}
4041
}
4142

43+
static const char* upFlag2str(uint8_t s)
44+
{
45+
switch(s) {
46+
case SECT_FLAG_NEW: return "New";
47+
case SECT_FLAG_SWAPPING: return "Swapping";
48+
case SECT_FLAG_BACKUP: return "Backup";
49+
case SECT_FLAG_UPDATED: return "Updated";
50+
default: return "Unknown";
51+
}
52+
}
53+
4254
static void printPart(uint8_t *part)
4355
{
4456
#ifdef WOLFBOOT_DEBUG_PARTION
@@ -47,16 +59,21 @@ static void printPart(uint8_t *part)
4759
#endif
4860
uint8_t *magic;
4961
uint8_t state;
62+
uint8_t upflag;
5063
uint32_t ver;
5164

5265
magic = part;
5366
printf("Magic: %c%c%c%c\n", magic[0], magic[1], magic[2], magic[3]);
5467
ver = wolfBoot_get_blob_version(part);
5568
printf("Version: %02x\n", ver);
56-
state = *(part + WOLFBOOT_PARTITION_SIZE - sizeof(uint32_t) - 1);
69+
wolfBoot_get_partition_state(0, &state);
5770
printf("Status: %02x (%s)\n", state,state2str(state));
5871
magic = part + WOLFBOOT_PARTITION_SIZE - sizeof(uint32_t);
72+
if (magic[0] != 0x42)
73+
magic = part + WOLFBOOT_PARTITION_SIZE - WOLFBOOT_SECTOR_SIZE - sizeof(uint32_t);
5974
printf("Trailer Mgc: %c%c%c%c\n", magic[0], magic[1], magic[2], magic[3]);
75+
wolfBoot_get_update_sector_flag(0, &upflag);
76+
printf("Update flag: %02x (%s)\n", upflag, upFlag2str(upflag));
6077

6178
#ifdef WOLFBOOT_DEBUG_PARTION
6279
v = (uint32_t *)part;
@@ -68,14 +85,25 @@ static void printPart(uint8_t *part)
6885
#endif
6986

7087
}
71-
88+
#ifdef WOLFBOOT_DEBUG_PARTION
89+
static void verify_flash_write(uint32_t addr, int len)
90+
{
91+
uint8_t *p = (uint8_t *)addr;
92+
int i;
93+
printf("verify addr=0x%08x: ", addr);
94+
for (i = 0; i < len && i < 8; i++) {
95+
printf("%02x ", p[i]);
96+
}
97+
printf("\n");
98+
}
99+
#endif
72100

73101
static void printPartitions(void)
74102
{
75103
printf("\n=== Boot Partition[%08x] ===\n", WOLFBOOT_PARTITION_BOOT_ADDRESS);
76104
printPart((uint8_t*)WOLFBOOT_PARTITION_BOOT_ADDRESS);
77105
printf("\n=== Update Partition[%08x] ===\n", WOLFBOOT_PARTITION_UPDATE_ADDRESS);
78-
printPart((uint8_t*)WOLFBOOT_PARTITION_UPDATE_ADDRESS);
106+
printPart((uint8_t*)(uintptr_t)WOLFBOOT_PARTITION_UPDATE_ADDRESS);
79107
}
80108

81109
void main(void)

IDE/Renesas/e2studio/RX72N/include/user_settings.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,5 +278,6 @@
278278
#else
279279
# define WOLFSSL_SMALL_STACK
280280
#endif
281-
281+
#define PRINTF_ENABLED
282+
#define NVM_FLASH_WRITEONCE
282283
#endif /* !H_USER_SETTINGS_ */

IDE/Renesas/e2studio/RX72N/resouce/section_info_SWuse.esi

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<?xml version="1.0" encoding="ASCII"?>
22
<com.renesas.linkersection.model:SectionContainer xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:com.renesas.linkersection.model="http:///LinkerSection.ecore">
3-
<sections name="SU">
3+
<sections name="SI">
44
<sectionAddress xsi:type="com.renesas.linkersection.model:FixedAddress" fixedAddress="4"/>
55
</sections>
6-
<sections name="SI"/>
76
<sections name="B_1"/>
87
<sections name="R_1"/>
98
<sections name="B_2"/>
@@ -13,9 +12,12 @@
1312
<sections name="B_8"/>
1413
<sections name="R_8"/>
1514
<sections name="RPFRAM"/>
16-
<sections name="RPFRAM2"/>
15+
<sections name="C_FIRMWARE_UPDATE_CONTROL_BLOCK">
16+
<sectionAddress xsi:type="com.renesas.linkersection.model:FixedAddress" fixedAddress="1048576"/>
17+
</sections>
18+
<sections name="C_FIRMWARE_UPDATE_CONTROL_BLOCK_MIRROR"/>
1719
<sections name="PResetPRG">
18-
<sectionAddress xsi:type="com.renesas.linkersection.model:FixedAddress" fixedAddress="4290839040"/>
20+
<sectionAddress xsi:type="com.renesas.linkersection.model:FixedAddress" fixedAddress="4290772992"/>
1921
</sections>
2022
<sections name="C_1"/>
2123
<sections name="C_2"/>
@@ -27,7 +29,6 @@
2729
<sections name="L"/>
2830
<sections name="P"/>
2931
<sections name="PFRAM"/>
30-
<sections name="PFRAM2"/>
3132
<sections name="EXCEPTVECT">
3233
<sectionAddress xsi:type="com.renesas.linkersection.model:FixedAddress" fixedAddress="4294967168"/>
3334
</sections>

IDE/Renesas/e2studio/RX72N/wolfBoot/.cproject

Lines changed: 109 additions & 38 deletions
Large diffs are not rendered by default.

hal/renesas-rx.c

Lines changed: 74 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,13 @@
6161

6262
/* forward declaration */
6363
int hal_flash_init(void);
64-
64+
#if defined(WOLFBOOT_RENESAS_TSIP) && !defined(WOLFBOOT_RENESAS_APP)
6565
static void hal_panic(void)
6666
{
6767
while(1)
6868
;
6969
}
70+
#endif
7071

7172
#ifdef ENABLE_LED
7273
void hal_led_on(void)
@@ -104,6 +105,21 @@ void hal_delay_us(uint32_t us)
104105
}
105106
}
106107

108+
static flash_err_t flash_check_error()
109+
{
110+
uint32_t st = FLASH_FSTATR;
111+
112+
if (st & FLASH_FSTATR_ILGLERR) return FLASH_ERR_ILGL;
113+
if (st & FLASH_FSTATR_PRGERR) return FLASH_ERR_PRG;
114+
if (st & FLASH_FSTATR_ERSERR) return FLASH_ERR_ERS;
115+
if (st & FLASH_FSTATR_FLWEERR) return FLASH_ERR_FLWE;
116+
if (st & FLASH_FSTATR_FESETERR) return FLASH_ERR_FESET;
117+
if (st & FLASH_FSTATR_SECERR) return FLASH_ERR_SEC;
118+
if (st & FLASH_FSTATR_OTERR) return FLASH_ERR_OT;
119+
120+
return FLASH_OK;
121+
122+
}
107123
#ifdef DEBUG_UART
108124

109125
#ifndef DEBUG_UART_SCI
@@ -210,7 +226,6 @@ void uart_write(const char* buf, unsigned int sz)
210226
void hal_clk_init(void)
211227
{
212228
uint32_t reg, i;
213-
uint16_t stc;
214229
uint8_t cksel = CFG_CKSEL;
215230

216231
PROTECT_OFF(); /* write protect off */
@@ -235,15 +250,15 @@ void hal_clk_init(void)
235250
/* Wait for HOCO oscillator stabilization */
236251
while ((SYS_OSCOVFSR & SYS_OSCOVFSR_HCOVF) == 0) { RX_NOP(); }
237252
#else
238-
if (SYS_HOCOCR & SYS_HOCOCR_HCSTP) {
239-
/* Turn off power to HOCO */
240-
SYS_HOCOPCR |= SYS_HOCOPCR_HOCOPCNT;
241-
}
253+
if (SYS_HOCOCR & SYS_HOCOCR_HCSTP) {
254+
/* Turn off power to HOCO */
255+
SYS_HOCOPCR |= SYS_HOCOPCR_HOCOPCNT;
256+
}
242257
#endif
243258

244259
/* ---- Main-Clock ---- */
245260
#if CFG_CKSEL == 2
246-
/* MOFXIN=0 (not controlled), MODRV2=0 (24MHz), MOSEL=0 (resonator) */
261+
/* MOFXIN=0 (not controlled), MODRV2=0 (24MHz), MOSEL=0 (resonator) */
247262
SYS_MOFCR = 0;
248263

249264
/* OSC stabilization time (9.98 ms * (264 kHZ) + 16)/32 = 82.83) */
@@ -485,8 +500,30 @@ void hal_prepare_boot(void)
485500

486501
}
487502

503+
#ifdef __CCRX__
504+
/* copy RAM functions from ROM to RAM */
505+
static void copyfuncs(void)
506+
{
507+
unsigned char *dst, *src;
508+
size_t n;
509+
510+
src = __sectop("PFRAM");
511+
dst = __sectop("RPFRAM");
512+
n = (size_t)((unsigned char*)__secend("PFRAM") -
513+
(unsigned char*)__sectop("PFRAM"));
514+
wolfBoot_printf("RAM Function start = 0x%p\n", __sectop("RPFRAM"));
515+
wolfBoot_printf("RAM Function end = 0x%p\n", __secend("RPFRAM"));
516+
wolfBoot_printf("RAM Function size = %d\n", n);
517+
while(src < __secend("PFRAM")) {
518+
*dst++ = *src++;
519+
}
520+
}
521+
#endif
488522
int hal_flash_init(void)
489523
{
524+
#ifdef __CCRX__
525+
copyfuncs();
526+
#endif
490527
/* Flash Write Enable */
491528
FLASH_FWEPROR = FLASH_FWEPROR_FLWE;
492529

@@ -506,23 +543,33 @@ int hal_flash_init(void)
506543
/* write up to 128 bytes at a time */
507544
#define FLASH_FACI_CODE_BLOCK_SZ \
508545
(FLASH_FACI_CMD_PROGRAM_CODE_LENGTH * FLASH_FACI_CMD_PROGRAM_DATA_LENGTH)
546+
#ifdef __CCRX__
547+
#pragma section FRAM
548+
#endif
509549
int RAMFUNCTION hal_flash_write(uint32_t addr, const uint8_t *data, int len)
510550
{
511-
int ret, i, chunk;
551+
int i;
512552
uint8_t codeblock[FLASH_FACI_CODE_BLOCK_SZ];
513553
uint16_t* data16 = (uint16_t*)data;
554+
uint32_t block_base;
555+
uint32_t offset;
556+
int write_size;
557+
int ret;
514558

515559
while (len > 0) {
516-
/* handle partial remainder */
517-
if (len < FLASH_FACI_CODE_BLOCK_SZ) {
518-
uint8_t *src = (uint8_t*)addr;
519-
int remain = FLASH_FACI_CODE_BLOCK_SZ - len;
520-
memcpy(codeblock, data16, len);
521-
memcpy(codeblock + len, src + len, remain);
522-
data16 = (uint16_t*)codeblock;
523-
}
560+
/* Align address to 128-byte boundary */
561+
block_base = addr & ~(FLASH_FACI_CODE_BLOCK_SZ - 1);
562+
offset = addr - block_base;
563+
564+
memcpy(codeblock, (uint8_t*)block_base, FLASH_FACI_CODE_BLOCK_SZ);
565+
write_size = FLASH_FACI_CODE_BLOCK_SZ - offset;
566+
if (write_size > len)
567+
write_size = len;
524568

525-
FLASH_FSADDR = addr;
569+
memcpy(&codeblock[offset], data, write_size);
570+
data16 = (uint16_t*)codeblock;
571+
572+
FLASH_FSADDR = block_base;
526573
/* flash program command */
527574
FLASH_FACI_CMD8 = FLASH_FACI_CMD_PROGRAM;
528575
/* number of 16-bit blocks: for code blocks is always 0x40 (64) */
@@ -539,9 +586,12 @@ int RAMFUNCTION hal_flash_write(uint32_t addr, const uint8_t *data, int len)
539586

540587
/* Wait for FCU operation to complete */
541588
while ((FLASH_FSTATR & FLASH_FSTATR_FRDY) == 0);
542-
543-
len -= FLASH_FACI_CODE_BLOCK_SZ;
544-
addr += FLASH_FACI_CODE_BLOCK_SZ;
589+
if ((ret = flash_check_error()) != FLASH_OK) {
590+
return ret;
591+
}
592+
len -= write_size;
593+
addr += write_size;
594+
data += write_size;
545595
}
546596
return 0;
547597
}
@@ -579,7 +629,6 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
579629

580630
static int RAMFUNCTION hal_flash_write_faw(uint32_t faw)
581631
{
582-
volatile uint8_t* cmdArea = (volatile uint8_t*)FLASH_FACI_CMD_AREA;
583632

584633
#ifndef BIG_ENDIAN_ORDER
585634
#if defined(__CCRX__)
@@ -639,7 +688,9 @@ void RAMFUNCTION hal_flash_lock(void)
639688
FLASH_FENTRYR_CODE_READ | FLASH_FENTRYR_DATA_READ);
640689
return;
641690
}
642-
691+
#ifdef __CCRX__
692+
#pragma section
693+
#endif
643694
#if !defined(WOLFBOOT_NO_PARTITIONS) && !defined(TARGET_library)
644695
void* hal_get_primary_address(void)
645696
{
@@ -648,6 +699,6 @@ void* hal_get_primary_address(void)
648699

649700
void* hal_get_update_address(void)
650701
{
651-
return (void*)WOLFBOOT_PARTITION_UPDATE_ADDRESS;
702+
return (void*)(uintptr_t)WOLFBOOT_PARTITION_UPDATE_ADDRESS;
652703
}
653704
#endif

hal/renesas-rx.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,4 +411,15 @@ void hal_delay_us(uint32_t us);
411411
#define ICU_PIBR(x) (*(volatile uint8_t *)(SYSTEM_BASE + 0x7700 + (x)))
412412
#define ICU_PIAR(x) (*(volatile uint8_t *)(SYSTEM_BASE + 0x7900 + (x)))
413413

414+
typedef enum {
415+
FLASH_ERR_ILGL = -7,
416+
FLASH_ERR_PRG = -6,
417+
FLASH_ERR_ERS = -5,
418+
FLASH_ERR_FLWE = -4,
419+
FLASH_ERR_FESET = -3,
420+
FLASH_ERR_SEC = -2,
421+
FLASH_ERR_OT = -1,
422+
FLASH_ERR_UNKNOWN = -99,
423+
FLASH_OK = 0
424+
} flash_err_t;
414425
#endif /* !_WOLFBOOT_RENESAS_RX_H_ */

src/boot_renesas.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ void do_boot(const uint32_t *app_offset)
9292
#if defined(__RX__)
9393
/* Do unconditional jump (r1 = app_offset) */
9494
#if defined(__CCRX__)
95+
printf("app_offset 0x%p\n", app_offset);
9596
longJump(app_offset);
9697
#else
9798
app_entry = (void(*))(*app_offset);

0 commit comments

Comments
 (0)