From 9faefee27c664a580a1399e9bd7bcf9e8d4b0415 Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Wed, 7 Nov 2018 19:39:02 -0500 Subject: [PATCH] test solo program --- targets/stm32l442/Makefile.test.mk | 75 ++++++++++++ targets/stm32l442/src/app.h | 13 +- targets/stm32l442/src/device.c | 10 +- targets/stm32l442/src/init.c | 8 +- targets/stm32l442/src/main.c | 189 +++-------------------------- 5 files changed, 114 insertions(+), 181 deletions(-) create mode 100644 targets/stm32l442/Makefile.test.mk diff --git a/targets/stm32l442/Makefile.test.mk b/targets/stm32l442/Makefile.test.mk new file mode 100644 index 0000000..402b2d1 --- /dev/null +++ b/targets/stm32l442/Makefile.test.mk @@ -0,0 +1,75 @@ +CC=arm-none-eabi-gcc +CP=arm-none-eabi-objcopy +SZ=arm-none-eabi-size +AR=arm-none-eabi-ar + +# ST related +SRC = src/main.c src/init.c src/flash.c src/led.c +SRC += src/startup_stm32l432xx.s src/system_stm32l4xx.c +SRC += lib/stm32l4xx_ll_gpio.c lib/stm32l4xx_ll_pwr.c lib/stm32l4xx_ll_rcc.c lib/stm32l4xx_ll_tim.c lib/stm32l4xx_ll_utils.c + +OBJ1=$(SRC:.c=.o) +OBJ=$(OBJ1:.s=.o) + +INC = -Isrc/ -Isrc/cmsis/ -Ilib/ -Ilib/usbd/ -I../../fido2/ -I../../fido2/extensions +INC += -I../../tinycbor/src -I../../crypto/sha256 -I../../crypto/micro-ecc +INC += -I../../crypto/tiny-AES-c + +SEARCH=-L../../tinycbor/lib + +LDSCRIPT=stm32l432xx.ld + +CFLAGS= $(INC) + +TARGET=solo +HW=-mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb + +# Nucleo board +#CHIP=STM32L432xx +# Solo +CHIP=STM32L442xx + +DEFINES = -D$(CHIP) -DAES256=1 -DUSE_FULL_LL_DRIVER +DEFINES += -DTEST_SOLO_STM32 -DTEST + +CFLAGS=$(INC) -c $(DEFINES) -Wall -fdata-sections -ffunction-sections $(HW) +LDFLAGS_LIB=$(HW) $(SEARCH) -specs=nano.specs -specs=nosys.specs -Wl,--gc-sections -lnosys +LDFLAGS=$(HW) $(LDFLAGS_LIB) -T$(LDSCRIPT) -Wl,-Map=$(TARGET).map,--cref + + +.PRECIOUS: %.o + +all: $(TARGET).elf + $(SZ) $^ + +%.o: %.c + $(CC) $^ $(HW) -Os $(CFLAGS) -o $@ + +../../crypto/micro-ecc/uECC.o: ../../crypto/micro-ecc/uECC.c + $(CC) $^ $(HW) -O3 $(CFLAGS) -o $@ + +%.o: %.s + $(CC) $^ $(HW) -Os $(CFLAGS) -o $@ + +%.elf: $(OBJ) + $(CC) $^ $(HW) $(LDFLAGS) -o $@ + +%.hex: %.elf + $(CP) -O ihex $^ $(TARGET).hex + $(CP) -O binary $^ $(TARGET).bin + +clean: + rm -f *.o src/*.o src/*.elf *.elf *.hex $(OBJ) + +flash: $(TARGET).hex + STM32_Programmer_CLI -c port=SWD -halt -e all --readunprotect + STM32_Programmer_CLI -c port=SWD -halt -d $(TARGET).hex -rst + +detach: + STM32_Programmer_CLI -c port=usb1 -ob nBOOT0=1 + +cbor: + cd ../../tinycbor/ && make clean + cd ../../tinycbor/ && make CC="$(CC)" AR=$(AR) \ +LDFLAGS="$(LDFLAGS_LIB)" \ +CFLAGS="$(CFLAGS)" diff --git a/targets/stm32l442/src/app.h b/targets/stm32l442/src/app.h index 6a31791..f506427 100644 --- a/targets/stm32l442/src/app.h +++ b/targets/stm32l442/src/app.h @@ -4,19 +4,22 @@ #define DEBUG_UART USART1 -#define DEBUG_LEVEL 1 +#define DEBUG_LEVEL 1 #define NON_BLOCK_PRINTING 0 + +#define BOOT_TO_DFU 0 + //#define USING_DEV_BOARD //#define ENABLE_U2F_EXTENSIONS #define ENABLE_U2F -//#define DISABLE_CTAPHID_PING -//#define DISABLE_CTAPHID_WINK -//#define DISABLE_CTAPHID_CBOR +// #define DISABLE_CTAPHID_PING +// #define DISABLE_CTAPHID_WINK +// #define DISABLE_CTAPHID_CBOR void printing_init(); void hw_init(void); @@ -30,7 +33,7 @@ void hw_init(void); #define SOLO_BUTTON_PORT GPIOA #define SOLO_BUTTON_PIN LL_GPIO_PIN_0 -#define SKIP_BUTTON_CHECK_WITH_DELAY 1 +#define SKIP_BUTTON_CHECK_WITH_DELAY 0 #define SKIP_BUTTON_CHECK_FAST 0 #endif diff --git a/targets/stm32l442/src/device.c b/targets/stm32l442/src/device.c index 3f118ac..2660b45 100644 --- a/targets/stm32l442/src/device.c +++ b/targets/stm32l442/src/device.c @@ -35,7 +35,7 @@ #define AUTH_WORD_ADDR (flash_addr(APPLICATION_END_PAGE)-4) -uint32_t __90_ms = CTAPHID_STATUS_IDLE; +uint32_t __90_ms = 0; uint32_t __device_status = 0; uint32_t __last_update = 0; extern PCD_HandleTypeDef hpcd; @@ -63,7 +63,6 @@ void USB_IRQHandler(void) HAL_PCD_IRQHandler(&hpcd); } - uint32_t millis() { return (((uint32_t)TIM6->CNT) + (__90_ms * 90)); @@ -95,7 +94,12 @@ void device_init() hw_init(); LL_GPIO_SetPinMode(SOLO_BUTTON_PORT,SOLO_BUTTON_PIN,LL_GPIO_MODE_INPUT); LL_GPIO_SetPinPull(SOLO_BUTTON_PORT,SOLO_BUTTON_PIN,LL_GPIO_PULL_UP); + +#if BOOT_TO_DFU + flash_option_bytes_init(1); +#else flash_option_bytes_init(0); +#endif printf1(TAG_GEN,"hello solo\r\n"); } @@ -118,12 +122,14 @@ int usbhid_recv(uint8_t * msg) void usbhid_send(uint8_t * msg) { + printf1(TAG_DUMP,"<< "); dump_hex1(TAG_DUMP, msg, HID_PACKET_SIZE); while (PCD_GET_EP_TX_STATUS(USB, HID_EPIN_ADDR & 0x0f) == USB_EP_TX_VALID) ; USBD_LL_Transmit(&Solo_USBD_Device, HID_EPIN_ADDR, msg, HID_PACKET_SIZE); + } void ctaphid_write_block(uint8_t * data) diff --git a/targets/stm32l442/src/init.c b/targets/stm32l442/src/init.c index dc7e498..904d67e 100644 --- a/targets/stm32l442/src/init.c +++ b/targets/stm32l442/src/init.c @@ -78,17 +78,21 @@ void hw_init(void) /* Initialize all configured peripherals */ MX_GPIO_Init(); - MX_USART1_UART_Init(); MX_TIM2_Init(); MX_TIM6_Init(); + #ifndef TEST_SOLO_STM32 + MX_USART1_UART_Init(); + MX_RNG_Init(); + #endif TIM6->SR = 0; __enable_irq(); NVIC_EnableIRQ(TIM6_IRQn); - +#ifndef TEST_SOLO_STM32 usb_init(); + #endif } static void LL_Init(void) diff --git a/targets/stm32l442/src/main.c b/targets/stm32l442/src/main.c index fc48a63..25e9a5c 100644 --- a/targets/stm32l442/src/main.c +++ b/targets/stm32l442/src/main.c @@ -29,8 +29,6 @@ #include "fifo.h" #include "log.h" -#define IS_BUTTON_PRESSED() (0 == (LL_GPIO_ReadInputPort(SOLO_BUTTON_PORT) & SOLO_BUTTON_PIN)) - #ifdef TEST_SOLO_STM32 #define Error_Handler() _Error_Handler(__FILE__,__LINE__) #define PAGE_SIZE 2048 @@ -41,159 +39,27 @@ #define STATE2_PAGE (PAGES - 2) #define STATE1_PAGE (PAGES - 1) -void test_atomic_counter() + +uint32_t __90_ms = 0; +#define IS_BUTTON_PRESSED() (0 == (LL_GPIO_ReadInputPort(SOLO_BUTTON_PORT) & SOLO_BUTTON_PIN)) + +// Timer6 overflow handler. happens every ~90ms. +void TIM6_DAC_IRQHandler() { - // flash_erase_page(COUNTER1_PAGE); - // flash_erase_page(COUNTER2_PAGE); - int i; - uint32_t c0 = ctap_atomic_count(0); - for (i = 0; i < 128; i++) - { - uint32_t c1 = ctap_atomic_count(0); - if (c1 <= (c0 )) - { - printf("error, count failed %lu <= %lu\r\n",c1,c0); - while(1) - ; - } - printf("%lu\r\n", c1); - c0 = c1; - } - - printf("test faults\r\n"); - - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - - flash_erase_page(COUNTER1_PAGE); - - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - - flash_erase_page(COUNTER1_PAGE); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - - flash_erase_page(COUNTER1_PAGE); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - - flash_erase_page(COUNTER1_PAGE); - - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - - flash_erase_page(COUNTER2_PAGE); - - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - - flash_erase_page(COUNTER2_PAGE); - - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - - flash_erase_page(COUNTER2_PAGE); - - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - - flash_erase_page(COUNTER2_PAGE); - - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - - flash_erase_page(COUNTER1_PAGE); - - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - - flash_erase_page(COUNTER1_PAGE); - - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - printf("%lu\r\n", ctap_atomic_count(0)); - - flash_erase_page(COUNTER1_PAGE); - + // timer is only 16 bits, so roll it over here + TIM6->SR = 0; + __90_ms += 1; } -void test_peripherals() +uint32_t millis() { - uint8_t str[] = "YouCompleteMe: a code-completion engine for Vim"; - uint8_t buf[sizeof(str)]; - float ent; - printf("hello solo\r\n"); - - // Test flash - flash_erase_page(60); - flash_write(flash_addr(60), str, sizeof(str)); - memmove(buf,(uint8_t*)flash_addr(60),sizeof(str)); - printf("flash: \"%s\"\r\n", buf); - // test_atomic_counter(); - - - // Note that 4 byte aligned addresses won't get written correctly. - flash_erase_page(60); - uint32_t count = 0; - flash_write(flash_addr(60) + 0,(uint8_t*)&count,4); - count += 1; - flash_write(flash_addr(60) + 4,(uint8_t*)&count,4); - count += 1; - flash_write(flash_addr(60) + 8,(uint8_t*)&count,4); - count += 1; - flash_write(flash_addr(60) + 12,(uint8_t*)&count,4); - count += 1; - flash_write(flash_addr(60) + 16,(uint8_t*)&count,4); - dump_hex((uint8_t *)flash_addr(60), 20); - - - // test timer - uint32_t t1 = millis(); - delay(100); - uint32_t t2 = millis(); - printf("100 ms delay (%lu)\r\n",t2-t1); - - // test rng - ent = rng_test(64 * 1024); - - printf("entropy of 64KB from RNG: %.6f\r\n", ent); - - /*// Test PWM + weighting of RGB*/ - /*led_test_colors();*/ - fifo_test(); + return (((uint32_t)TIM6->CNT) + (__90_ms * 90)); +} +void _Error_Handler(char *file, int line) +{ + while(1) + { + } } int main(void) @@ -206,26 +72,6 @@ int main(void) LL_GPIO_SetPinPull(SOLO_BUTTON_PORT,SOLO_BUTTON_PIN,LL_GPIO_PULL_UP); flash_option_bytes_init(1); - set_logging_mask( - /*0*/ - // TAG_GEN| - TAG_MC | - TAG_GA | - // TAG_WALLET | - TAG_STOR | - TAG_CP | - TAG_CTAP| -// TAG_HID| - /*TAG_U2F|*/ - TAG_PARSE | - //TAG_TIME| - // TAG_DUMP| - TAG_GREEN| - TAG_RED| - TAG_ERR - ); - - while (1) { uint32_t t0 = millis() % 750; @@ -249,7 +95,6 @@ int main(void) led_rgb(0x151515); } - } }