Skip to content

Commit d49de56

Browse files
committed
Address fenrir, copilot, and internal review
- Makefile: Add cppcheck comparePointers suppressions for lpc54s018 startup.c/syscalls.c - main.c: Fix non-atomic 64-bit tick_ms reads with get_tick_ms() (disable/enable interrupts) - startup.c: Restore __libc_init_array() call (matches other ports) - lpc_enet.c: Add RX_BUF_SIZE bounds check in eth_poll to prevent buffer over-read - fix_checksum.py: Auto-detect vector table offset (0x0 for RAM build, 0x200 for flash) - target_ram.ld: Clarify SRAM2 is 32KB on J4M package - flash.sh: Add flash helper script (matches N6 port's flash.sh)
1 parent 96a246f commit d49de56

File tree

8 files changed

+75
-28
lines changed

8 files changed

+75
-28
lines changed

.github/workflows/codespell.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ jobs:
2323
uses: codespell-project/actions-codespell@v2
2424
with:
2525
skip: .git,./IDE,*.der,*.pem
26-
ignore_words_list: inh,inout,keypair,nd,parm,rcv,ser,tha,HSI,TE,UE,Synopsys,synopsys
26+
ignore_words_list: inh,inout,keypair,nd,parm,rcv,ser,tha,HSI,TE,UE,Synopsys,synopsys,FRO

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ CPPCHECK_FLAGS=--enable=warning,performance,portability,missingInclude \
123123
--suppress=comparePointers:src/port/stm32n6/syscalls.c \
124124
--suppress=comparePointers:src/port/va416xx/startup.c \
125125
--suppress=comparePointers:src/port/va416xx/syscalls.c \
126+
--suppress=comparePointers:src/port/lpc54s018/startup.c \
127+
--suppress=comparePointers:src/port/lpc54s018/syscalls.c \
126128
--disable=style \
127129
--std=c99 --language=c \
128130
--platform=unix64 \

src/port/lpc54s018/fix_checksum.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,52 @@
22
# fix_checksum.py - compute LPC vector table checksum
33
#
44
# The LPC54S018 boot ROM requires that vector table entries 0-7 sum to zero.
5-
# The vector table starts at offset 0x200 in the binary (after the 512-byte
6-
# SPIFI configuration block).
7-
#
8-
# This script patches entry[7] so that sum(entries[0:8]) == 0 (mod 2^32).
5+
# Auto-detects whether vector table is at offset 0 (RAM build) or 0x200
6+
# (SPIFI flash build with 512-byte config block).
97

108
import struct
119
import sys
1210

13-
SPIFI_CONFIG_SIZE = 0x200 # 512-byte SPIFI config block before vector table
11+
def find_vector_offset(data):
12+
"""Detect vector table offset by checking for valid SP."""
13+
for off in (0, 0x200):
14+
if off + 32 > len(data):
15+
continue
16+
sp = struct.unpack_from('<I', data, off)[0]
17+
if 0x20000000 <= sp <= 0x20030000:
18+
return off
19+
return 0
1420

1521
def main():
1622
if len(sys.argv) != 2:
17-
print(f"Usage: {sys.argv[0]} <app.bin>")
23+
print("Usage: %s <app.bin>" % sys.argv[0])
1824
sys.exit(1)
1925

2026
fname = sys.argv[1]
2127
with open(fname, 'r+b') as f:
22-
f.seek(SPIFI_CONFIG_SIZE)
23-
data = f.read(32)
24-
if len(data) < 32:
25-
print(f"Error: file too small (need at least {SPIFI_CONFIG_SIZE + 32} bytes)")
28+
data = f.read()
29+
off = find_vector_offset(data)
30+
31+
if off + 32 > len(data):
32+
print("Error: file too small")
2633
sys.exit(1)
2734

28-
vecs = list(struct.unpack('<8I', data))
29-
# Compute checksum: entry[7] = -(sum of entries 0-6) mod 2^32
35+
vecs = list(struct.unpack_from('<8I', data, off))
3036
partial_sum = sum(vecs[:7]) & 0xFFFFFFFF
3137
cksum = (0x100000000 - partial_sum) & 0xFFFFFFFF
32-
vecs[7] = cksum
3338

34-
# Write back
35-
f.seek(SPIFI_CONFIG_SIZE + 7 * 4)
39+
f.seek(off + 7 * 4)
3640
f.write(struct.pack('<I', cksum))
3741

38-
# Verify
3942
with open(fname, 'rb') as f:
40-
f.seek(SPIFI_CONFIG_SIZE)
43+
f.seek(off)
4144
vecs = struct.unpack('<8I', f.read(32))
4245
total = sum(vecs) & 0xFFFFFFFF
4346
if total != 0:
44-
print(f"ERROR: checksum verification failed (sum=0x{total:08X})")
47+
print("ERROR: checksum verification failed (sum=0x%08X)" % total)
4548
sys.exit(1)
4649

47-
print(f"Vector checksum patched: entry[7]=0x{cksum:08X}")
50+
print("Vector checksum patched: offset=0x%X entry[7]=0x%08X" % (off, cksum))
4851

4952
if __name__ == '__main__':
5053
main()

src/port/lpc54s018/flash.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/bash
2+
# Flash wolfIP to LPC54S018M-EVK SRAM via pyocd (on-board Link2 CMSIS-DAP)
3+
set -e
4+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
5+
ELF="${SCRIPT_DIR}/app.elf"
6+
BIN="${SCRIPT_DIR}/app.bin"
7+
8+
[ -f "$ELF" ] || { echo "app.elf not found. Run 'make' first."; exit 1; }
9+
10+
# Extract initial SP (word 0) and entry point (word 1) from binary
11+
INIT_SP=$(od -A n -t x4 -N 4 "$BIN" | awk '{print "0x"$1}')
12+
ENTRY=$(od -A n -j 4 -t x4 -N 4 "$BIN" | awk '{print "0x"$1}')
13+
14+
echo "Loading app.elf to SRAM via pyocd"
15+
echo " SP: ${INIT_SP}, Entry: ${ENTRY}"
16+
17+
pyocd commander -t lpc54608 -c "
18+
halt;
19+
load ${ELF};
20+
write32 0xE000ED08 0x20000000;
21+
write32 0x40000220 0x78;
22+
wreg sp ${INIT_SP};
23+
wreg pc ${ENTRY};
24+
go;
25+
exit
26+
"
27+
28+
echo "Done. Monitor UART: screen /dev/ttyACM0 115200"

src/port/lpc54s018/main.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,23 @@ void SysTick_Handler(void)
8585
tick_ms++;
8686
}
8787

88+
/* Atomic read of 64-bit tick_ms on 32-bit Cortex-M4.
89+
* Briefly disables interrupts to prevent torn reads. */
90+
static uint64_t get_tick_ms(void)
91+
{
92+
uint64_t val;
93+
__asm volatile ("cpsid i" ::: "memory");
94+
val = tick_ms;
95+
__asm volatile ("cpsie i" ::: "memory");
96+
return val;
97+
}
98+
8899
uint32_t wolfIP_getrandom(void)
89100
{
90101
static uint32_t lfsr;
91102
static int seeded = 0;
92103
if (!seeded) {
93-
lfsr = (uint32_t)tick_ms;
104+
lfsr = (uint32_t)get_tick_ms();
94105
if (lfsr == 0U) lfsr = 0x1A2B3C4DU;
95106
seeded = 1;
96107
}
@@ -102,8 +113,8 @@ uint32_t wolfIP_getrandom(void)
102113

103114
static void delay_ms(uint32_t ms)
104115
{
105-
uint64_t target = tick_ms + ms;
106-
while (tick_ms < target) { }
116+
uint64_t target = get_tick_ms() + ms;
117+
while (get_tick_ms() < target) { }
107118
}
108119

109120
static void clock_init(void)
@@ -257,7 +268,7 @@ int main(void)
257268
/* IP configuration: DHCP or static fallback */
258269
#ifdef DHCP
259270
printf("Starting DHCP...\r\n");
260-
(void)wolfIP_poll(IPStack, tick_ms); /* Prime last_tick */
271+
(void)wolfIP_poll(IPStack, get_tick_ms()); /* Prime last_tick */
261272
(void)dhcp_client_init(IPStack);
262273
#else
263274
{
@@ -289,13 +300,13 @@ int main(void)
289300
{
290301
uint64_t last_led_ms = 0;
291302
#ifdef DHCP
292-
uint64_t dhcp_start_ms = tick_ms;
293-
uint64_t dhcp_reinit_ms = tick_ms;
303+
uint64_t dhcp_start_ms = get_tick_ms();
304+
uint64_t dhcp_reinit_ms = get_tick_ms();
294305
int dhcp_done = 0;
295306
#endif
296307

297308
for (;;) {
298-
uint64_t now = tick_ms;
309+
uint64_t now = get_tick_ms();
299310
(void)wolfIP_poll(IPStack, now);
300311

301312
#ifdef DHCP

src/port/lpc54s018/startup.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ extern uint32_t _edata;
2626
extern uint32_t _sbss;
2727
extern uint32_t _ebss;
2828

29+
extern void __libc_init_array(void);
2930
int main(void);
3031

3132
/* Enable SRAM1/2 clocks before touching BSS or stack in those regions */
@@ -45,6 +46,7 @@ void Reset_Handler(void)
4546
for (dst = &_sbss; dst < &_ebss; )
4647
*dst++ = 0u;
4748

49+
__libc_init_array();
4850
(void)main();
4951
while (1) { }
5052
}

src/port/lpc54s018/target_ram.ld

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*
33
* SRAM0: 64KB @ 0x20000000 - code, data, BSS
44
* SRAM1: 64KB @ 0x20010000 - BSS overflow
5-
* SRAM2: 32KB @ 0x20020000 - stack (separate to avoid BSS/stack overlap)
5+
* SRAM2: 32KB @ 0x20020000 - stack only (32KB on J4M package, not 64KB)
66
*/
77
MEMORY
88
{

src/port/lpc_enet/lpc_enet.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ static int eth_poll(struct wolfIP_ll_dev *dev, void *frame, uint32_t len)
404404
status = desc->des3;
405405
if ((status & (RDES3_FS | RDES3_LS)) == (RDES3_FS | RDES3_LS)) {
406406
frame_len = status & RDES3_PL_MASK;
407+
if (frame_len > RX_BUF_SIZE) frame_len = RX_BUF_SIZE;
407408
if (frame_len > len) frame_len = len;
408409
if (frame_len > 0)
409410
memcpy(frame, rx_buffers[rx_idx], frame_len);

0 commit comments

Comments
 (0)