Skip to content

Commit 46511c7

Browse files
authored
Small overhaul of OpenCM3 code, rework Nucleo-L4R5ZI target a bit, and add CW308T-STM32F415 target (#259)
* Only compile the specific libopencm3 library needed * Add experimental support for CW308T-STM32F415 * Shut up the linker errors * Shut up unused parameter warning * Fix Keccaktest bin generation * Update libopencm3 * Adapt to renamed constants * Compile the board test with fast and slow clock * Use wrapped symbols instead of overriding * Overhaul clocking for L4R5ZI board
1 parent d4b1f5f commit 46511c7

12 files changed

+164
-21
lines changed

common/hal-opencm3.c

+114-15
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ const struct rcc_clock_scale benchmarkclock = {
5656
#include <libopencm3/stm32/usart.h>
5757
#include <libopencm3/stm32/flash.h>
5858

59+
#define SERIAL_GPIO GPIOA
60+
#define SERIAL_USART USART1
61+
#define SERIAL_PINS (GPIO9 | GPIO10)
62+
#define STM32
63+
#define CW_BOARD
64+
#elif defined(STM32F415RGT6)
65+
#include <libopencm3/stm32/rcc.h>
66+
#include <libopencm3/stm32/gpio.h>
67+
#include <libopencm3/stm32/usart.h>
68+
#include <libopencm3/stm32/flash.h>
69+
5970
#define SERIAL_GPIO GPIOA
6071
#define SERIAL_USART USART1
6172
#define SERIAL_PINS (GPIO9 | GPIO10)
@@ -119,6 +130,7 @@ static void clock_setup(enum clock_mode clock)
119130

120131
flash_prefetch_enable();
121132
#elif defined(CW_BOARD)
133+
(void) clock;
122134
/* Some STM32 Platform */
123135
rcc_periph_clock_enable(RCC_GPIOH);
124136
rcc_osc_off(RCC_HSE);
@@ -131,8 +143,13 @@ static void clock_setup(enum clock_mode clock)
131143
rcc_apb2_frequency = 7372800;
132144
_clock_freq = 7372800;
133145
rcc_set_hpre(RCC_CFGR_HPRE_DIV_NONE);
146+
#if defined(STM32F3)
134147
rcc_set_ppre1(RCC_CFGR_PPRE1_DIV_NONE);
135148
rcc_set_ppre2(RCC_CFGR_PPRE2_DIV_NONE);
149+
#elif defined(STM32F4)
150+
rcc_set_ppre1(RCC_CFGR_PPRE_DIV_NONE);
151+
rcc_set_ppre2(RCC_CFGR_PPRE_DIV_NONE);
152+
#endif
136153
rcc_set_sysclk_source(RCC_CFGR_SW_HSE);
137154
rcc_wait_for_sysclk_status(RCC_HSE);
138155
#elif defined(NUCLEO_BOARD)
@@ -147,8 +164,8 @@ static void clock_setup(enum clock_mode clock)
147164
rcc_apb2_frequency = 16000000;
148165
_clock_freq = 16000000;
149166
rcc_set_hpre(RCC_CFGR_HPRE_NODIV);
150-
rcc_set_ppre1(RCC_CFGR_PPRE1_NODIV);
151-
rcc_set_ppre2(RCC_CFGR_PPRE2_NODIV);
167+
rcc_set_ppre1(RCC_CFGR_PPRE_NODIV);
168+
rcc_set_ppre2(RCC_CFGR_PPRE_NODIV);
152169
flash_dcache_enable();
153170
flash_icache_enable();
154171
flash_set_ws(FLASH_ACR_LATENCY_0WS);
@@ -165,8 +182,8 @@ static void clock_setup(enum clock_mode clock)
165182
rcc_apb2_frequency = 80000000;
166183
_clock_freq = 80000000;
167184
rcc_set_hpre(RCC_CFGR_HPRE_NODIV);
168-
rcc_set_ppre1(RCC_CFGR_PPRE1_NODIV);
169-
rcc_set_ppre2(RCC_CFGR_PPRE2_NODIV);
185+
rcc_set_ppre1(RCC_CFGR_PPRE_NODIV);
186+
rcc_set_ppre2(RCC_CFGR_PPRE_NODIV);
170187
rcc_osc_off(RCC_PLL);
171188
while(rcc_is_osc_ready(RCC_PLL));
172189
/* Configure the PLL oscillator (use CUBEMX tool -> scale HSI16 to 80MHz). */
@@ -189,24 +206,33 @@ static void clock_setup(enum clock_mode clock)
189206
rcc_periph_clock_enable(RCC_PWR);
190207
rcc_periph_clock_enable(RCC_SYSCFG);
191208
pwr_set_vos_scale(PWR_SCALE1);
209+
/* The L4R5ZI chip also needs the R1MODE bit in PWR_CR5 register set, but
210+
OpenCM3 doesn't support this yet. But luckily the default value for the bit
211+
is 1. */
192212
switch (clock) {
193213
case CLOCK_BENCHMARK:
194214
/* Benchmark straight from the HSI16 without prescaling */
195215
rcc_osc_on(RCC_HSI16);
196216
rcc_wait_for_osc_ready(RCC_HSI16);
197-
rcc_ahb_frequency = 16000000;
198-
rcc_apb1_frequency = 16000000;
199-
rcc_apb2_frequency = 16000000;
200-
_clock_freq = 16000000;
217+
rcc_ahb_frequency = 20000000;
218+
rcc_apb1_frequency = 20000000;
219+
rcc_apb2_frequency = 20000000;
220+
_clock_freq = 20000000;
201221
rcc_set_hpre(RCC_CFGR_HPRE_NODIV);
202-
rcc_set_ppre1(RCC_CFGR_PPRE1_NODIV);
203-
rcc_set_ppre2(RCC_CFGR_PPRE2_NODIV);
222+
rcc_set_ppre1(RCC_CFGR_PPRE_NODIV);
223+
rcc_set_ppre2(RCC_CFGR_PPRE_NODIV);
224+
rcc_osc_off(RCC_PLL);
225+
while(rcc_is_osc_ready(RCC_PLL));
226+
/* Configure the PLL oscillator (use CUBEMX tool -> scale HSI16 to 20MHz). */
227+
_rcc_set_main_pll(RCC_PLLCFGR_PLLSRC_HSI16, 1, 10, 2, RCC_PLLCFGR_PLLQ_DIV2, RCC_PLLCFGR_PLLR_DIV8);
228+
/* Enable PLL oscillator and wait for it to stabilize. */
229+
rcc_osc_on(RCC_PLL);
204230
flash_dcache_enable();
205231
flash_icache_enable();
206232
flash_set_ws(FLASH_ACR_LATENCY_0WS);
207233
flash_prefetch_enable();
208-
rcc_set_sysclk_source(RCC_CFGR_SW_HSI16);
209-
rcc_wait_for_sysclk_status(RCC_HSI16);
234+
rcc_set_sysclk_source(RCC_CFGR_SW_PLL);
235+
rcc_wait_for_sysclk_status(RCC_PLL);
210236
break;
211237
case CLOCK_FAST:
212238
default:
@@ -217,12 +243,12 @@ static void clock_setup(enum clock_mode clock)
217243
rcc_apb2_frequency = 120000000;
218244
_clock_freq = 120000000;
219245
rcc_set_hpre(RCC_CFGR_HPRE_NODIV);
220-
rcc_set_ppre1(RCC_CFGR_PPRE1_NODIV);
221-
rcc_set_ppre2(RCC_CFGR_PPRE2_NODIV);
246+
rcc_set_ppre1(RCC_CFGR_PPRE_NODIV);
247+
rcc_set_ppre2(RCC_CFGR_PPRE_NODIV);
222248
rcc_osc_off(RCC_PLL);
223249
while(rcc_is_osc_ready(RCC_PLL));
224250
/* Configure the PLL oscillator (use CUBEMX tool -> scale HSI16 to 120MHz). */
225-
_rcc_set_main_pll(RCC_PLLCFGR_PLLSRC_HSI16, 2, 30, 2u, RCC_PLLCFGR_PLLQ_DIV2, RCC_PLLCFGR_PLLR_DIV2);
251+
_rcc_set_main_pll(RCC_PLLCFGR_PLLSRC_HSI16, 1, 15, 2, RCC_PLLCFGR_PLLQ_DIV2, RCC_PLLCFGR_PLLR_DIV2);
226252
/* Enable PLL oscillator and wait for it to stabilize. */
227253
rcc_osc_on(RCC_PLL);
228254
rcc_wait_for_osc_ready(RCC_PLL);
@@ -235,7 +261,9 @@ static void clock_setup(enum clock_mode clock)
235261
break;
236262
}
237263
rcc_osc_on(RCC_HSI48); /* HSI48 must always be on for RNG */
264+
rcc_wait_for_osc_ready(RCC_HSI48);
238265
rcc_periph_clock_enable(RCC_RNG);
266+
rcc_set_clock48_source(RCC_CCIPR_CLK48SEL_HSI48);
239267
rng_enable();
240268
#else
241269
#error Unsupported platform
@@ -358,3 +386,74 @@ size_t hal_get_stack_size(void)
358386
__asm__ volatile ("mov %0, sp" : "=r" (cur_stack));
359387
return cur_stack - heap_end;
360388
}
389+
390+
/* Implement some system calls to shut up the linker warnings */
391+
392+
#include <errno.h>
393+
#undef errno
394+
extern int errno;
395+
396+
int __wrap__close(int fd)
397+
{
398+
errno = ENOSYS;
399+
(void) fd;
400+
return -1;
401+
}
402+
403+
#include <sys/stat.h>
404+
405+
int __wrap__fstat(int fd, struct stat* buf)
406+
{
407+
(void) fd;
408+
(void) buf;
409+
errno = ENOSYS;
410+
return -1;
411+
}
412+
413+
int __wrap__getpid(void)
414+
{
415+
errno = ENOSYS;
416+
return -1;
417+
}
418+
419+
int __wrap__isatty(int file)
420+
{
421+
(void) file;
422+
errno = ENOSYS;
423+
return 0;
424+
}
425+
426+
int __wrap__kill(int pid, int sig)
427+
{
428+
(void) pid;
429+
(void) sig;
430+
errno = ENOSYS;
431+
return -1;
432+
}
433+
434+
int __wrap__lseek(int fd, int ptr, int dir)
435+
{
436+
(void) fd;
437+
(void) ptr;
438+
(void) dir;
439+
errno = ENOSYS;
440+
return -1;
441+
}
442+
443+
int __wrap__read(int fd, char* ptr, int len)
444+
{
445+
(void) fd;
446+
(void) ptr;
447+
(void) len;
448+
errno = ENOSYS;
449+
return -1;
450+
}
451+
452+
int __wrap__write(int fd, const char* ptr, int len)
453+
{
454+
(void) fd;
455+
(void) ptr;
456+
(void) len;
457+
errno = ENOSYS;
458+
return -1;
459+
}

common/test.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,13 @@ static void memory_timing_test(void)
124124
}
125125
#endif
126126

127+
#ifndef CLOCK_TEST
128+
#define CLOCK_TEST CLOCK_BENCHMARK
129+
#endif
130+
127131
int main(void)
128132
{
129-
hal_setup(CLOCK_FAST);
133+
hal_setup(CLOCK_TEST);
130134
hal_send_str("Hello world");
131135
send_unsigned("Stack Size", hal_get_stack_size());
132136
unsigned rnd;

common/testfast.c

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test.c

interface.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def parse_arguments():
1010
"-p",
1111
"--platform",
1212
help="The PQM4 platform",
13-
choices=["stm32f4discovery", "nucleo-l476rg", "nucleo-l4r5zi", "cw308t-stm32f3", "mps2-an386"],
13+
choices=["stm32f4discovery", "nucleo-l476rg", "nucleo-l4r5zi", "cw308t-stm32f3", "cw308t-stm32f415", "mps2-an386"],
1414
default="stm32f4discovery",
1515
)
1616
parser.add_argument(
@@ -39,7 +39,7 @@ def get_platform(args):
3939
elif args.platform == "nucleo-l4r5zi":
4040
bin_type = 'hex'
4141
platform = platforms.OpenOCD("st_nucleo_l4r5.cfg", args.uart)
42-
elif args.platform == "cw308t-stm32f3":
42+
elif args.platform in ["cw308t-stm32f3", "cw308t-stm32f415"]:
4343
bin_type = 'hex'
4444
platform = platforms.ChipWhisperer()
4545
elif args.platform == 'mps2-an386':

libopencm3

Submodule libopencm3 updated 413 files

mk/cw308t-stm32f3.mk

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
DEVICE=stm32f303rct7
2+
OPENCM3_TARGET=lib/stm32/f3
23

34
EXCLUDED_SCHEMES = \
45
mupq/pqclean/crypto_sign/sphincs-haraka-256f% \

mk/cw308t-stm32f415.mk

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
DEVICE=stm32f415rgt6
2+
OPENCM3_TARGET=lib/stm32/f4
3+
4+
EXCLUDED_SCHEMES = \
5+
mupq/pqclean/crypto_sign/sphincs-haraka-256f% \
6+
mupq/pqclean/crypto_sign/sphincs-shake256-256f% \
7+
mupq/pqclean/crypto_sign/sphincs-sha256-256f% \
8+
mupq/pqclean/crypto_kem/mceliece% \
9+
mupq/crypto_sign/falcon-1024% \
10+
mupq/crypto_sign/falcon-512% \
11+
crypto_sign/falcon-1024% \
12+
crypto_sign/falcon-512%
13+
14+
include mk/opencm3.mk

mk/nucleo-l476rg.mk

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
DEVICE=stm32l476rg
2+
OPENCM3_TARGET=lib/stm32/l4
23

34
EXCLUDED_SCHEMES = \
45
mupq/pqclean/crypto_kem/mceliece% \

mk/nucleo-l4r5zi.mk

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
DEVICE=stm32l4r5zi
2+
OPENCM3_TARGET=lib/stm32/l4
23

34
EXCLUDED_SCHEMES = \
45
mupq/pqclean/crypto_kem/mceliece% \
@@ -9,4 +10,7 @@ DEVICES_DATA := ldscripts/devices.data
910
elf/boardtest.elf: CPPFLAGS+=-DSRAM_TIMING_TEST -DHAS_SRAM2 -DHAS_SRAM3
1011
elf/boardtest.elf: LDSCRIPT=ldscripts/$(PLATFORM)-ramtest.ld
1112

13+
elf/boardtest-fast.elf: CPPFLAGS+=-DSRAM_TIMING_TEST -DHAS_SRAM2 -DHAS_SRAM3
14+
elf/boardtest-fast.elf: LDSCRIPT=ldscripts/$(PLATFORM)-ramtest.ld
15+
1216
include mk/opencm3.mk

mk/opencm3.mk

+9-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ LDFLAGS += -L$(OPENCM3_DIR)/lib
6161
CPPFLAGS += -I$(OPENCM3_DIR)/include
6262

6363
$(OPENCM3_DIR)/lib/lib$(LIBNAME).a:
64-
$(MAKE) -C $(OPENCM3_DIR)
64+
$(MAKE) -C $(OPENCM3_DIR) $(OPENCM3_TARGET)
6565

6666
obj/common/hal-opencm3.c.o: $(OPENCM3_DIR)/lib/lib$(LIBNAME).a
6767

@@ -89,6 +89,14 @@ CFLAGS += \
8989
LDFLAGS += \
9090
--specs=nosys.specs \
9191
-Wl,--wrap=_sbrk \
92+
-Wl,--wrap=_close \
93+
-Wl,--wrap=_isatty \
94+
-Wl,--wrap=_kill \
95+
-Wl,--wrap=_lseek \
96+
-Wl,--wrap=_read \
97+
-Wl,--wrap=_write \
98+
-Wl,--wrap=_fstat \
99+
-Wl,--wrap=_getpid \
92100
-nostartfiles \
93101
-ffreestanding \
94102
-T$(LDSCRIPT) \

mk/stm32f4discovery.mk

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
DEVICE=stm32f407vg
2+
OPENCM3_TARGET=lib/stm32/f4
23

34
EXCLUDED_SCHEMES = \
45
mupq/pqclean/crypto_kem/mceliece% \
@@ -7,6 +8,7 @@ EXCLUDED_SCHEMES = \
78
include mk/opencm3.mk
89

910
elf/boardtest.elf: CPPFLAGS+=-DSRAM_TIMING_TEST -DHAS_SRAM2 -DHAS_CCM
11+
elf/boardtest-fast.elf: CPPFLAGS+=-DSRAM_TIMING_TEST -DHAS_SRAM2 -DHAS_CCM
1012

1113
elf/crypto_kem_frodokem640aes_m4_%.elf: LDSCRIPT=ldscripts/stm32f4discovery_fullram.ld
1214
elf/mupq_pqclean_crypto_kem_frodokem640shake_opt_%.elf: LDSCRIPT=ldscripts/stm32f4discovery_fullram.ld

mk/tests.mk

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ ifeq ($(AIO),1)
22
elf/boardtest.elf: common/test.c $(LINKDEPS) $(CONFIG)
33
$(compiletest)
44

5+
elf/boardtest-fast.elf: common/testfast.c $(LINKDEPS) $(CONFIG)
6+
$(compiletest)
7+
8+
elf/boardtest-fast.elf: CPPFLAGS += -DCLOCK_TEST=CLOCK_FAST
9+
510
elf/aestest.elf: common/aestest.c $(LINKDEPS) $(CONFIG)
611
$(compiletest)
712

@@ -10,10 +15,14 @@ elf/keccaktest.elf: common/keccaktest.c $(LINKDEPS) $(CONFIG)
1015
else
1116
elf/boardtest.elf: $(call objs,common/test.c) $(LINKDEPS) $(CONFIG)
1217

18+
elf/boardtest-fast.elf: $(call objs,common/testfast.c) $(LINKDEPS) $(CONFIG)
19+
20+
$(call objs,common/testfast.c): CPPFLAGS += -DCLOCK_TEST=CLOCK_FAST
21+
1322
elf/aestest.elf: $(call objs,common/aestest.c) $(LINKDEPS) $(CONFIG)
1423

1524
elf/keccaktest.elf: $(call objs,common/keccaktest.c) $(LINKDEPS) $(CONFIG)
1625
endif
1726

1827
tests: elf/boardtest.elf elf/aestest.elf elf/keccaktest.elf
19-
tests-bin: bin/boardtest.bin bin/aestest.bin bin/keccaktest.elf
28+
tests-bin: bin/boardtest.bin bin/aestest.bin bin/keccaktest.bin

0 commit comments

Comments
 (0)