diff --git a/Makefile b/Makefile index b97a66d..e63f0ec 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,9 @@ platform=2 +EFM32_DEBUGGER= -s 440083537 --device EFM32JG1B200F128GM32 +#EFM32_DEBUGGER= -s 440121060 #dev board + src = $(wildcard pc/*.c) $(wildcard fido2/*.c) $(wildcard crypto/sha256/*.c) crypto/tiny-AES-c/aes.c obj = $(src:.c=.o) uECC.o @@ -37,11 +40,16 @@ efm32com: cd '.\efm32\GNU ARM v7.2.1 - Debug' && $(MAKE) all efm32prog: cd '.\efm32\GNU ARM v7.2.1 - Debug' && $(MAKE) all - commander flash '.\efm32\GNU ARM v7.2.1 - Debug\EFM32.hex' -s 440121060 + commander flash '.\efm32\GNU ARM v7.2.1 - Debug\EFM32.hex' $(EFM32_DEBUGGER) -p "0x1E7FC:0x00000000:4" +efm32read: + cd '.\efm32\GNU ARM v7.2.1 - Debug' && $(MAKE) all + commander swo read $(EFM32_DEBUGGER) + + efm32bootprog: cd '.\efm32boot\GNU ARM v7.2.1 - Debug' && $(MAKE) all - commander flash '.\efm32boot\GNU ARM v7.2.1 - Debug\efm32boot.hex' -s 440121060 + commander flash '.\efm32boot\GNU ARM v7.2.1 - Debug\efm32boot.hex' $(EFM32_DEBUGGER) --masserase $(name): $(obj) $(CC) $(LDFLAGS) -o $@ $(obj) $(LDFLAGS) diff --git a/efm32/.cproject b/efm32/.cproject index e854489..418ca56 100644 --- a/efm32/.cproject +++ b/efm32/.cproject @@ -23,7 +23,7 @@ - + @@ -31,10 +31,10 @@ + - + @@ -158,7 +159,7 @@ - + @@ -219,14 +220,14 @@ - + - + diff --git a/efm32/.project b/efm32/.project index 513394f..8fc5500 100644 --- a/efm32/.project +++ b/efm32/.project @@ -35,5 +35,15 @@ 2 C:/Users/conor/Desktop/u2f-one/fido2/ + + CMSIS/EFM32JG1B/startup_gcc_efm32jg1b.s + 1 + STUDIO_SDK_LOC/platform/Device/SiliconLabs/EFM32JG1B/Source/GCC/startup_efm32jg1b.S + + + CMSIS/EFM32JG1B/system_efm32jg1b.c + 1 + STUDIO_SDK_LOC/platform/Device/SiliconLabs/EFM32JG1B/Source/system_efm32jg1b.c + diff --git a/efm32/EFM32_EFM32JG1B200F128GM32.hwconf b/efm32/EFM32_EFM32JG1B200F128GM32.hwconf new file mode 100644 index 0000000..df031c7 --- /dev/null +++ b/efm32/EFM32_EFM32JG1B200F128GM32.hwconf @@ -0,0 +1,107 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/efm32/inc/app.h b/efm32/inc/app.h index b4adfcb..d9b4b40 100644 --- a/efm32/inc/app.h +++ b/efm32/inc/app.h @@ -10,9 +10,9 @@ #define DEBUG_LEVEL 1 -#define PRINTING_USE_VCOM +//#define PRINTING_USE_VCOM -#define USING_DEV_BOARD +//#define USING_DEV_BOARD //#define BRIDGE_TO_WALLET @@ -22,10 +22,12 @@ void printing_init(); -#define TEST -#define TEST_POWER +//#define TEST +//#define TEST_POWER // GPIO assignments #define NFC_DEV_SS gpioPortF,2 +#define LED_INIT_VALUE 0x001000 + #endif /* SRC_APP_H_ */ diff --git a/efm32/src/InitDevice.c b/efm32/src/InitDevice.c index 4b9d0c4..bd654cc 100644 --- a/efm32/src/InitDevice.c +++ b/efm32/src/InitDevice.c @@ -23,6 +23,7 @@ #include "em_cryotimer.h" #include "em_crypto.h" #include "em_gpio.h" +#include "em_timer.h" #include "em_usart.h" // [Library includes]$ @@ -38,6 +39,7 @@ extern void enter_DefaultMode_from_RESET(void) { ADC0_enter_DefaultMode_from_RESET(); USART0_enter_DefaultMode_from_RESET(); USART1_enter_DefaultMode_from_RESET(); + TIMER0_enter_DefaultMode_from_RESET(); CRYOTIMER_enter_DefaultMode_from_RESET(); PORTIO_enter_DefaultMode_from_RESET(); // [Config Calls]$ @@ -50,19 +52,8 @@ extern void enter_DefaultMode_from_RESET(void) { extern void EMU_enter_DefaultMode_from_RESET(void) { // $[EMU Initialization] - /* Initialize DCDC regulator */ - EMU_DCDCInit_TypeDef dcdcInit = EMU_DCDCINIT_DEFAULT; - - dcdcInit.powerConfig = emuPowerConfig_DcdcToDvdd; - dcdcInit.dcdcMode = emuDcdcMode_LowNoise; - dcdcInit.mVout = 1800; - dcdcInit.em01LoadCurrent_mA = 15; - dcdcInit.em234LoadCurrent_uA = 10; - dcdcInit.maxCurrent_mA = 200; - dcdcInit.anaPeripheralPower = emuDcdcAnaPeripheralPower_DCDC; - dcdcInit.reverseCurrentControl = 160; - - EMU_DCDCInit(&dcdcInit); + /* External power circuit not wired for DCDC; shut down regulator */ + EMU_DCDCPowerOff(); /* Initialize EM2/EM3 mode */ EMU_EM23Init_TypeDef em23Init = EMU_EM23INIT_DEFAULT; @@ -102,7 +93,7 @@ extern void CMU_enter_DefaultMode_from_RESET(void) { CMU_HFXOInit(&hfxoInit); /* Setting system HFRCO frequency */ - CMU_HFRCOFreqSet (cmuHFRCOFreq_4M0Hz); + CMU_HFRCOFreqSet (cmuHFRCOFreq_38M0Hz); /* Using HFRCO as high frequency clock, HFCLK */ CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFRCO); @@ -133,6 +124,9 @@ extern void CMU_enter_DefaultMode_from_RESET(void) { /* Enable clock for CRYOTIMER */ CMU_ClockEnable(cmuClock_CRYOTIMER, true); + /* Enable clock for TIMER0 */ + CMU_ClockEnable(cmuClock_TIMER0, true); + /* Enable clock for USART0 */ CMU_ClockEnable(cmuClock_USART0, true); @@ -279,23 +273,23 @@ extern void USART0_enter_DefaultMode_from_RESET(void) { // $[USART_InitIO] /* Disable CLK pin */ USART0->ROUTELOC0 = (USART0->ROUTELOC0 & (~_USART_ROUTELOC0_CLKLOC_MASK)) - | USART_ROUTELOC0_CLKLOC_LOC0; + | USART_ROUTELOC0_CLKLOC_LOC4; USART0->ROUTEPEN = USART0->ROUTEPEN & (~USART_ROUTEPEN_CLKPEN); /* Disable CS pin */ USART0->ROUTELOC0 = (USART0->ROUTELOC0 & (~_USART_ROUTELOC0_CSLOC_MASK)) - | USART_ROUTELOC0_CSLOC_LOC0; + | USART_ROUTELOC0_CSLOC_LOC3; USART0->ROUTEPEN = USART0->ROUTEPEN & (~USART_ROUTEPEN_CSPEN); - /* Set up CTS pin */ + /* Disable CTS pin */ USART0->ROUTELOC1 = (USART0->ROUTELOC1 & (~_USART_ROUTELOC1_CTSLOC_MASK)) - | USART_ROUTELOC1_CTSLOC_LOC30; - USART0->ROUTEPEN = USART0->ROUTEPEN | USART_ROUTEPEN_CTSPEN; + | USART_ROUTELOC1_CTSLOC_LOC2; + USART0->ROUTEPEN = USART0->ROUTEPEN & (~USART_ROUTEPEN_CTSPEN); - /* Set up RTS pin */ + /* Disable RTS pin */ USART0->ROUTELOC1 = (USART0->ROUTELOC1 & (~_USART_ROUTELOC1_RTSLOC_MASK)) - | USART_ROUTELOC1_RTSLOC_LOC30; - USART0->ROUTEPEN = USART0->ROUTEPEN | USART_ROUTEPEN_RTSPEN; + | USART_ROUTELOC1_RTSLOC_LOC1; + USART0->ROUTEPEN = USART0->ROUTEPEN & (~USART_ROUTEPEN_RTSPEN); /* Set up RX pin */ USART0->ROUTELOC0 = (USART0->ROUTELOC0 & (~_USART_ROUTELOC0_RXLOC_MASK)) @@ -304,7 +298,7 @@ extern void USART0_enter_DefaultMode_from_RESET(void) { /* Set up TX pin */ USART0->ROUTELOC0 = (USART0->ROUTELOC0 & (~_USART_ROUTELOC0_TXLOC_MASK)) - | USART_ROUTELOC0_TXLOC_LOC0; + | USART_ROUTELOC0_TXLOC_LOC20; USART0->ROUTEPEN = USART0->ROUTEPEN | USART_ROUTEPEN_TXPEN; // [USART_InitIO]$ @@ -348,7 +342,7 @@ extern void USART1_enter_DefaultMode_from_RESET(void) { initsync.databits = usartDatabits8; initsync.master = 1; initsync.msbf = 1; - initsync.clockMode = usartClockMode1; + initsync.clockMode = usartClockMode0; #if defined( USART_INPUT_RXPRS ) && defined( USART_TRIGCTRL_AUTOTXTEN ) initsync.prsRxEnable = 0; initsync.prsRxCh = 0; @@ -371,32 +365,32 @@ extern void USART1_enter_DefaultMode_from_RESET(void) { // $[USART_InitIO] /* Set up CLK pin */ USART1->ROUTELOC0 = (USART1->ROUTELOC0 & (~_USART_ROUTELOC0_CLKLOC_MASK)) - | USART_ROUTELOC0_CLKLOC_LOC11; + | USART_ROUTELOC0_CLKLOC_LOC4; USART1->ROUTEPEN = USART1->ROUTEPEN | USART_ROUTEPEN_CLKPEN; /* Disable CS pin */ USART1->ROUTELOC0 = (USART1->ROUTELOC0 & (~_USART_ROUTELOC0_CSLOC_MASK)) - | USART_ROUTELOC0_CSLOC_LOC0; + | USART_ROUTELOC0_CSLOC_LOC3; USART1->ROUTEPEN = USART1->ROUTEPEN & (~USART_ROUTEPEN_CSPEN); /* Disable CTS pin */ USART1->ROUTELOC1 = (USART1->ROUTELOC1 & (~_USART_ROUTELOC1_CTSLOC_MASK)) - | USART_ROUTELOC1_CTSLOC_LOC0; + | USART_ROUTELOC1_CTSLOC_LOC2; USART1->ROUTEPEN = USART1->ROUTEPEN & (~USART_ROUTEPEN_CTSPEN); /* Disable RTS pin */ USART1->ROUTELOC1 = (USART1->ROUTELOC1 & (~_USART_ROUTELOC1_RTSLOC_MASK)) - | USART_ROUTELOC1_RTSLOC_LOC0; + | USART_ROUTELOC1_RTSLOC_LOC1; USART1->ROUTEPEN = USART1->ROUTEPEN & (~USART_ROUTEPEN_RTSPEN); /* Set up RX pin */ USART1->ROUTELOC0 = (USART1->ROUTELOC0 & (~_USART_ROUTELOC0_RXLOC_MASK)) - | USART_ROUTELOC0_RXLOC_LOC11; + | USART_ROUTELOC0_RXLOC_LOC6; USART1->ROUTEPEN = USART1->ROUTEPEN | USART_ROUTEPEN_RXPEN; /* Set up TX pin */ USART1->ROUTELOC0 = (USART1->ROUTELOC0 & (~_USART_ROUTELOC0_TXLOC_MASK)) - | USART_ROUTELOC0_TXLOC_LOC11; + | USART_ROUTELOC0_TXLOC_LOC8; USART1->ROUTEPEN = USART1->ROUTEPEN | USART_ROUTEPEN_TXPEN; // [USART_InitIO]$ @@ -477,21 +471,124 @@ extern void LDMA_enter_DefaultMode_from_RESET(void) { extern void TIMER0_enter_DefaultMode_from_RESET(void) { // $[TIMER0 I/O setup] + /* Set up CC0 */ + TIMER0->ROUTELOC0 = (TIMER0->ROUTELOC0 & (~_TIMER_ROUTELOC0_CC0LOC_MASK)) + | TIMER_ROUTELOC0_CC0LOC_LOC18; + TIMER0->ROUTEPEN = TIMER0->ROUTEPEN | TIMER_ROUTEPEN_CC0PEN; + /* Set up CC1 */ + TIMER0->ROUTELOC0 = (TIMER0->ROUTELOC0 & (~_TIMER_ROUTELOC0_CC1LOC_MASK)) + | TIMER_ROUTELOC0_CC1LOC_LOC16; + TIMER0->ROUTEPEN = TIMER0->ROUTEPEN | TIMER_ROUTEPEN_CC1PEN; + /* Set up CC2 */ + TIMER0->ROUTELOC0 = (TIMER0->ROUTELOC0 & (~_TIMER_ROUTELOC0_CC2LOC_MASK)) + | TIMER_ROUTELOC0_CC2LOC_LOC20; + TIMER0->ROUTEPEN = TIMER0->ROUTEPEN | TIMER_ROUTEPEN_CC2PEN; + /* Set up CDTI0 */ + TIMER0->ROUTELOC2 = (TIMER0->ROUTELOC2 & (~_TIMER_ROUTELOC2_CDTI0LOC_MASK)) + | TIMER_ROUTELOC2_CDTI0LOC_LOC3; + TIMER0->ROUTEPEN = TIMER0->ROUTEPEN & (~TIMER_ROUTEPEN_CDTI0PEN); + /* Set up CDTI1 */ + TIMER0->ROUTELOC2 = (TIMER0->ROUTELOC2 & (~_TIMER_ROUTELOC2_CDTI1LOC_MASK)) + | TIMER_ROUTELOC2_CDTI1LOC_LOC2; + TIMER0->ROUTEPEN = TIMER0->ROUTEPEN & (~TIMER_ROUTEPEN_CDTI1PEN); + /* Set up CDTI2 */ + TIMER0->ROUTELOC2 = (TIMER0->ROUTELOC2 & (~_TIMER_ROUTELOC2_CDTI2LOC_MASK)) + | TIMER_ROUTELOC2_CDTI2LOC_LOC1; + TIMER0->ROUTEPEN = TIMER0->ROUTEPEN & (~TIMER_ROUTEPEN_CDTI2PEN); // [TIMER0 I/O setup]$ // $[TIMER0 initialization] + TIMER_Init_TypeDef init = TIMER_INIT_DEFAULT; + + init.enable = 1; + init.debugRun = 0; + init.dmaClrAct = 0; + init.sync = 0; + init.clkSel = timerClkSelHFPerClk; + init.prescale = timerPrescale512; + init.fallAction = timerInputActionNone; + init.riseAction = timerInputActionNone; + init.mode = timerModeUp; + init.quadModeX4 = 0; + init.oneShot = 0; + init.count2x = 0; + init.ati = 0; + TIMER_Init(TIMER0, &init); // [TIMER0 initialization]$ // $[TIMER0 CC0 init] + TIMER_InitCC_TypeDef initCC0 = TIMER_INITCC_DEFAULT; + + initCC0.prsInput = false; + initCC0.prsSel = timerPRSSELCh0; + initCC0.edge = timerEdgeRising; + initCC0.mode = timerCCModePWM; + initCC0.eventCtrl = timerEventEveryEdge; + initCC0.filter = 0; + initCC0.cofoa = timerOutputActionNone; + initCC0.cufoa = timerOutputActionNone; + initCC0.cmoa = timerOutputActionToggle; + initCC0.coist = 0; + initCC0.outInvert = 0; + TIMER_InitCC(TIMER0, 0, &initCC0); // [TIMER0 CC0 init]$ // $[TIMER0 CC1 init] + TIMER_InitCC_TypeDef initCC1 = TIMER_INITCC_DEFAULT; + + initCC1.prsInput = false; + initCC1.prsSel = timerPRSSELCh0; + initCC1.edge = timerEdgeRising; + initCC1.mode = timerCCModePWM; + initCC1.eventCtrl = timerEventEveryEdge; + initCC1.filter = 0; + initCC1.cofoa = timerOutputActionNone; + initCC1.cufoa = timerOutputActionNone; + initCC1.cmoa = timerOutputActionToggle; + initCC1.coist = 0; + initCC1.outInvert = 0; + TIMER_InitCC(TIMER0, 1, &initCC1); // [TIMER0 CC1 init]$ // $[TIMER0 CC2 init] + TIMER_InitCC_TypeDef initCC2 = TIMER_INITCC_DEFAULT; + + initCC2.prsInput = false; + initCC2.prsSel = timerPRSSELCh0; + initCC2.edge = timerEdgeRising; + initCC2.mode = timerCCModePWM; + initCC2.eventCtrl = timerEventEveryEdge; + initCC2.filter = 0; + initCC2.cofoa = timerOutputActionNone; + initCC2.cufoa = timerOutputActionNone; + initCC2.cmoa = timerOutputActionToggle; + initCC2.coist = 0; + initCC2.outInvert = 0; + TIMER_InitCC(TIMER0, 2, &initCC2); // [TIMER0 CC2 init]$ // $[TIMER0 DTI init] + TIMER_InitDTI_TypeDef initDTI = TIMER_INITDTI_DEFAULT; + + initDTI.enable = 0; + initDTI.activeLowOut = 0; + initDTI.invertComplementaryOut = 0; + initDTI.autoRestart = 0; + initDTI.enablePrsSource = 0; + initDTI.prsSel = timerPRSSELCh0; + initDTI.prescale = timerPrescale1; + initDTI.riseTime = 1; + initDTI.fallTime = 1; + initDTI.enableFaultSourceCoreLockup = 1; + initDTI.enableFaultSourceDebugger = 1; + initDTI.faultSourcePrsSel0 = 0; + initDTI.faultSourcePrsSel0 = timerPRSSELCh0; + initDTI.faultSourcePrsSel1 = 0; + initDTI.faultSourcePrsSel1 = timerPRSSELCh0; + initDTI.faultAction = timerDtiFaultActionInactive; + initDTI.outputsEnableMask = 0 | TIMER_DTOGEN_DTOGCC0EN + | TIMER_DTOGEN_DTOGCC1EN | TIMER_DTOGEN_DTOGCC2EN; + TIMER_InitDTI(TIMER0, &initDTI); // [TIMER0 DTI init]$ } @@ -596,51 +693,51 @@ extern void PORTIO_enter_DefaultMode_from_RESET(void) { // $[Port A Configuration] - /* Pin PA0 is configured to Push-pull */ - GPIO_PinModeSet(gpioPortA, 0, gpioModePushPull, 0); + /* Pin PA0 is configured to Input enabled */ + GPIO_PinModeSet(gpioPortA, 0, gpioModeInput, 0); - /* Pin PA1 is configured to Input enabled with pull-up */ - GPIO_PinModeSet(gpioPortA, 1, gpioModeInputPull, 1); - - /* Pin PA3 is configured to Push-pull */ - GPIO_PinModeSet(gpioPortA, 3, gpioModePushPull, 0); - - /* Pin PA4 is configured to Open-drain with pull-up and filter */ - GPIO_PinModeSet(gpioPortA, 4, gpioModeWiredAndPullUpFilter, 1); - - /* Pin PA5 is configured to Push-pull */ - GPIO_PinModeSet(gpioPortA, 5, gpioModePushPull, 1); + /* Pin PA1 is configured to Input enabled */ + GPIO_PinModeSet(gpioPortA, 1, gpioModeInput, 0); // [Port A Configuration]$ // $[Port B Configuration] + /* Pin PB11 is configured to Push-pull */ + GPIO_PinModeSet(gpioPortB, 11, gpioModePushPull, 1); + + /* Pin PB12 is configured to Input enabled with pull-up */ + GPIO_PinModeSet(gpioPortB, 12, gpioModeInputPull, 1); + /* Pin PB13 is configured to Push-pull */ GPIO_PinModeSet(gpioPortB, 13, gpioModePushPull, 1); + + /* Pin PB15 is configured to Push-pull */ + GPIO_PinModeSet(gpioPortB, 15, gpioModePushPull, 1); // [Port B Configuration]$ // $[Port C Configuration] - - /* Pin PC6 is configured to Push-pull */ - GPIO_PinModeSet(gpioPortC, 6, gpioModePushPull, 1); - - /* Pin PC7 is configured to Input enabled with pull-up */ - GPIO_PinModeSet(gpioPortC, 7, gpioModeInputPull, 1); - - /* Pin PC8 is configured to Push-pull */ - GPIO_PinModeSet(gpioPortC, 8, gpioModePushPull, 1); - - /* Pin PC9 is configured to Input enabled with pull-up */ - GPIO_PinModeSet(gpioPortC, 9, gpioModeInputPull, 1); - - /* Pin PC10 is configured to Push-pull */ - GPIO_PinModeSet(gpioPortC, 10, gpioModePushPull, 1); // [Port C Configuration]$ // $[Port D Configuration] + /* Pin PD9 is configured to Push-pull */ + GPIO_PinModeSet(gpioPortD, 9, gpioModePushPull, 1); + /* Pin PD10 is configured to Push-pull */ GPIO_PinModeSet(gpioPortD, 10, gpioModePushPull, 1); + /* Pin PD11 is configured to Input enabled with pull-up */ + GPIO_PinModeSet(gpioPortD, 11, gpioModeInputPull, 1); + + /* Pin PD12 is configured to Push-pull */ + GPIO_PinModeSet(gpioPortD, 12, gpioModePushPull, 1); + + /* Pin PD13 is configured to Input enabled with pull-up */ + GPIO_PinModeSet(gpioPortD, 13, gpioModeInputPull, 1); + + /* Pin PD14 is configured to Push-pull */ + GPIO_PinModeSet(gpioPortD, 14, gpioModePushPull, 1); + /* Pin PD15 is configured to Push-pull */ GPIO_PinModeSet(gpioPortD, 15, gpioModePushPull, 1); // [Port D Configuration]$ @@ -649,21 +746,6 @@ extern void PORTIO_enter_DefaultMode_from_RESET(void) { // [Port E Configuration]$ // $[Port F Configuration] - - /* Pin PF2 is configured to Push-pull */ - GPIO_PinModeSet(gpioPortF, 2, gpioModePushPull, 1); - - /* Pin PF3 is configured to Open-drain with pull-up and filter */ - GPIO_PinModeSet(gpioPortF, 3, gpioModeWiredAndPullUpFilter, 1); - - /* Pin PF4 is configured to Push-pull */ - GPIO_PinModeSet(gpioPortF, 4, gpioModePushPull, 0); - - /* Pin PF5 is configured to Push-pull */ - GPIO_PinModeSet(gpioPortF, 5, gpioModePushPull, 0); - - /* Pin PF6 is configured to Input enabled with pull-up */ - GPIO_PinModeSet(gpioPortF, 6, gpioModeInputPull, 1); // [Port F Configuration]$ } diff --git a/efm32/src/device.c b/efm32/src/device.c index 3abc375..148095f 100644 --- a/efm32/src/device.c +++ b/efm32/src/device.c @@ -15,6 +15,7 @@ #include "em_cmu.h" #include "em_msc.h" #include "em_i2c.h" +#include "em_timer.h" #include "InitDevice.h" #include "cbor.h" @@ -26,6 +27,8 @@ #include "crypto.h" #include "nfc.h" +#ifdef USING_DEV_BOARD + #define MSG_AVAIL_PIN gpioPortC,9 #define RDY_PIN gpioPortC,10 #define RW_PIN gpioPortD,11 @@ -33,19 +36,34 @@ #define LED1_PIN gpioPortF,4 #define LED2_PIN gpioPortF,5 +#else + +#define MSG_AVAIL_PIN gpioPortA,1 +#define RDY_PIN gpioPortA,0 +#define RW_PIN gpioPortD,15 +#define RESET_PIN gpioPortB,15 +#define LED1_PIN gpioPortD,9 +#define LED2_PIN gpioPortD,10 +#define LED3_PIN gpioPortD,14 +#define BUTTON_PIN gpioPortD,13 + +#endif + #define PAGE_SIZE 2048 -#define PAGES 128 -#define COUNTER_PAGE 125 -#define STATE1_PAGE 126 -#define STATE2_PAGE 127 +#define PAGES 64 +#define COUNTER_PAGE (PAGES - 3) +#define STATE1_PAGE (PAGES - 2) +#define STATE2_PAGE (PAGES - 1) -#define APPLICATION_START_ADDR 0x8000 -#define APPLICATION_START_PAGE (0x8000/PAGE_SIZE) +#define APPLICATION_START_ADDR 0x4000 +#define APPLICATION_START_PAGE (0x4000/PAGE_SIZE) + +#define APPLICATION_END_ADDR (PAGE_SIZE*(PAGES - 3)-4) // NOT included in application +#define APPLICATION_END_PAGE ((PAGES - 3)) // 125 is NOT included in application + +#define AUTH_WORD_ADDR (PAGE_SIZE*(PAGES - 3)-4) -#define APPLICATION_END_ADDR (PAGE_SIZE*125-4) // NOT included in application -#define APPLICATION_END_PAGE (125) // 125 is NOT included in application -#define AUTH_WORD_ADDR (PAGE_SIZE*125-4) static void init_atomic_counter() { @@ -105,6 +123,27 @@ uint32_t ctap_atomic_count(int sel) return count; } +static uint32_t _color; +uint32_t get_RBG() +{ + return _color; +} + +void RGB(uint32_t hex) +{ + uint16_t r = 256 - ((hex & 0xff0000) >> 16); + uint16_t g = 256 - ((hex & 0xff00) >> 8); + uint16_t b = 256 - ((hex & 0xff) >> 0); + + TIMER_CompareBufSet(TIMER0, 0, g); // green + TIMER_CompareBufSet(TIMER0, 1, r); // red + TIMER_CompareBufSet(TIMER0, 2, b); // blue + _color = hex; +} + + +#define IS_BUTTON_PRESSED() (GPIO_PinInGet(BUTTON_PIN) == 0) + // Verify the user // return 1 if user is verified, 0 if not int ctap_user_verification(uint8_t arg) @@ -116,6 +155,30 @@ int ctap_user_verification(uint8_t arg) // Return 1 for user is present, 0 user not present int ctap_user_presence_test() { + uint32_t t1 = millis(); + RGB(0x304010); + while (IS_BUTTON_PRESSED()) + { + if (t1 + 5000 < millis()) + return 0; + } + + t1 = millis(); + + do + { + if (t1 + 5000 < millis()) + return 0; + if (! IS_BUTTON_PRESSED()) + continue; + delay(1); + } + while (! IS_BUTTON_PRESSED()); + + RGB(0x001040); + + delay(50); + return 1; } @@ -129,14 +192,60 @@ void ctaphid_write_block(uint8_t * data) } #endif +#ifdef IS_BOOTLOADER // two different colors between bootloader and app void heartbeat() { - GPIO_PinOutToggle(LED1_PIN); - GPIO_PinOutToggle(LED2_PIN); - nfc_test(); - // printf("heartbeat %d %d\r\n", beat++,CRYOTIMER->CNT); -} + static int state = 0; + static uint32_t val = (LED_INIT_VALUE >> 8) & 0xff; + int but = IS_BUTTON_PRESSED(); + + if (state) + { + val--; + } + else + { + val++; + } + + if (val > 30 || val < 1) + { + state = !state; + } + +// if (but) RGB(val * 2); +// else + RGB((val << 16) | (val*2 << 8)); + +} +#else +void heartbeat() +{ + static int state = 0; + static uint32_t val = (LED_INIT_VALUE >> 8) & 0xff; + int but = IS_BUTTON_PRESSED(); + + + if (state) + { + val--; + } + else + { + val++; + } + + if (val > 30 || val < 1) + { + state = !state; + } + + if (but) RGB(val * 2); + else RGB(val << 8); + +} +#endif uint32_t millis() { return CRYOTIMER->CNT; @@ -186,9 +295,9 @@ int usbhid_recv(uint8_t * msg) wait_for_efm8_busy(); - // msgs_to_recv--; - // printf(">> "); - // dump_hex(msg,64); +// // msgs_to_recv--; +// printf(">> "); +// dump_hex(msg,64); return 64; } @@ -209,6 +318,8 @@ void usbhid_send(uint8_t * msg) USART_SpiTransfer(USART1, *msg++); } wait_for_efm8_busy(); + + delay(10); // uint32_t t2 = millis(); // printf("wait time: %u\n", (uint32_t)(t2-t1)); @@ -334,11 +445,14 @@ void bootloader_init(void) // status LEDS GPIO_PinModeSet(LED1_PIN, gpioModePushPull, - 0); + 1); // red GPIO_PinModeSet(LED2_PIN, gpioModePushPull, - 1); + 1); // green + GPIO_PinModeSet(LED3_PIN, + gpioModePushPull, + 1); // blue // EFM8 RDY/BUSY GPIO_PinModeSet(RDY_PIN, gpioModeInput, 0); @@ -369,11 +483,14 @@ void device_init(void) // status LEDS GPIO_PinModeSet(LED1_PIN, gpioModePushPull, - 0); + 1); // red GPIO_PinModeSet(LED2_PIN, gpioModePushPull, - 1); + 1); // green + GPIO_PinModeSet(LED3_PIN, + gpioModePushPull, + 1); // blue // EFM8 RDY/BUSY GPIO_PinModeSet(RDY_PIN, gpioModeInput, 0); @@ -387,6 +504,9 @@ void device_init(void) // Reset EFM8 GPIO_PinModeSet(RESET_PIN, gpioModePushPull, 1); + TIMER_TopSet(TIMER0, 255); + + RGB(LED_INIT_VALUE); printing_init(); diff --git a/efm32/src/nfc.c b/efm32/src/nfc.c index d5e1a9c..11c35d6 100644 --- a/efm32/src/nfc.c +++ b/efm32/src/nfc.c @@ -20,7 +20,7 @@ #define NFC_DEV_ADDR (0xa0|(0x0<<1)) #define NFC_DEV_USART USART1 - +#ifndef IS_BOOTLOADER I2C_TransferReturn_TypeDef I2CSPM_Transfer(I2C_TypeDef *i2c, I2C_TransferSeq_TypeDef *seq) { I2C_TransferReturn_TypeDef ret; @@ -357,3 +357,5 @@ void nfc_test() } } + +#endif diff --git a/efm32/src/printing.c b/efm32/src/printing.c index c09fd30..4b644c9 100644 --- a/efm32/src/printing.c +++ b/efm32/src/printing.c @@ -27,7 +27,7 @@ void setupSWOForPrint(void) /* Enable Serial wire output pin */ GPIO->ROUTEPEN |= GPIO_ROUTEPEN_SWVPEN; - /* Set location 0 */ + /* Set location 0 */ GPIO->ROUTELOC0 = GPIO_ROUTELOC0_SWVLOC_LOC0; /* Enable output on pin - GPIO Port F, Pin 2 */ @@ -76,7 +76,9 @@ int RETARGET_ReadChar(void) void printing_init() { +#ifdef USING_DEV_BOARD // GPIO_PinModeSet(gpioPortA,5,gpioModePushPull,1); // VCOM enable +#endif } #endif diff --git a/efm32boot/.cproject b/efm32boot/.cproject index f54cab7..5bb2be4 100644 --- a/efm32boot/.cproject +++ b/efm32boot/.cproject @@ -23,14 +23,14 @@ - + @@ -71,15 +70,14 @@ @@ -95,10 +93,10 @@ - - - - + + + + @@ -126,7 +124,7 @@ - + @@ -145,12 +143,12 @@ @@ -162,12 +160,12 @@ + + + diff --git a/efm32boot/.project b/efm32boot/.project index 6753d30..a9b2d9b 100644 --- a/efm32boot/.project +++ b/efm32boot/.project @@ -33,5 +33,15 @@ 2 $%7BPARENT-1-PROJECT_LOC%7D/fido2 + + CMSIS/EFM32JG1B/startup_gcc_efm32jg1b.s + 1 + STUDIO_SDK_LOC/platform/Device/SiliconLabs/EFM32JG1B/Source/GCC/startup_efm32jg1b.S + + + CMSIS/EFM32JG1B/system_efm32jg1b.c + 1 + STUDIO_SDK_LOC/platform/Device/SiliconLabs/EFM32JG1B/Source/system_efm32jg1b.c + diff --git a/efm32boot/emlib/em_timer.c b/efm32boot/emlib/em_timer.c new file mode 100644 index 0000000..6999d6a --- /dev/null +++ b/efm32boot/emlib/em_timer.c @@ -0,0 +1,253 @@ +/***************************************************************************//** + * @file em_timer.c + * @brief Timer/counter (TIMER) Peripheral API + * @version 5.2.2 + ******************************************************************************* + * # License + * Copyright 2016 Silicon Laboratories, Inc. http://www.silabs.com + ******************************************************************************* + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + * + * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no + * obligation to support this Software. Silicon Labs is providing the + * Software "AS IS", with no express or implied warranties of any kind, + * including, but not limited to, any implied warranties of merchantability + * or fitness for any particular purpose or warranties against infringement + * of any proprietary rights of a third party. + * + * Silicon Labs will not be liable for any consequential, incidental, or + * special damages, or any other relief, or for any claim by any third party, + * arising from your use of this Software. + * + ******************************************************************************/ + +#include "em_timer.h" +#if defined(TIMER_COUNT) && (TIMER_COUNT > 0) + +#include "em_assert.h" + +/***************************************************************************//** + * @addtogroup emlib + * @{ + ******************************************************************************/ + +/***************************************************************************//** + * @addtogroup TIMER + * @brief Timer/Counter (TIMER) Peripheral API + * @details + * The timer module consists of three main parts: + * @li General timer config and enable control. + * @li Compare/capture control. + * @li Dead time insertion control (may not be available for all timers). + * @{ + ******************************************************************************/ + +/******************************************************************************* + ************************** GLOBAL FUNCTIONS ******************************* + ******************************************************************************/ + +/***************************************************************************//** + * @brief + * Initialize TIMER. + * + * @details + * Notice that counter top must be configured separately with for instance + * TIMER_TopSet(). In addition, compare/capture and dead-time insertion + * init must be initialized separately if used. That should probably + * be done prior to the use of this function if configuring the TIMER to + * start when initialization is completed. + * + * @param[in] timer + * Pointer to TIMER peripheral register block. + * + * @param[in] init + * Pointer to TIMER initialization structure. + ******************************************************************************/ +void TIMER_Init(TIMER_TypeDef *timer, const TIMER_Init_TypeDef *init) +{ + EFM_ASSERT(TIMER_REF_VALID(timer)); + + /* Stop timer if specified to be disabled (dosn't hurt if already stopped) */ + if (!(init->enable)) { + timer->CMD = TIMER_CMD_STOP; + } + + /* Reset counter */ + timer->CNT = _TIMER_CNT_RESETVALUE; + + timer->CTRL = ((uint32_t)(init->prescale) << _TIMER_CTRL_PRESC_SHIFT) + | ((uint32_t)(init->clkSel) << _TIMER_CTRL_CLKSEL_SHIFT) + | ((uint32_t)(init->fallAction) << _TIMER_CTRL_FALLA_SHIFT) + | ((uint32_t)(init->riseAction) << _TIMER_CTRL_RISEA_SHIFT) + | ((uint32_t)(init->mode) << _TIMER_CTRL_MODE_SHIFT) + | (init->debugRun ? TIMER_CTRL_DEBUGRUN : 0) + | (init->dmaClrAct ? TIMER_CTRL_DMACLRACT : 0) + | (init->quadModeX4 ? TIMER_CTRL_QDM_X4 : 0) + | (init->oneShot ? TIMER_CTRL_OSMEN : 0) + +#if defined(TIMER_CTRL_X2CNT) && defined(TIMER_CTRL_ATI) + | (init->count2x ? TIMER_CTRL_X2CNT : 0) + | (init->ati ? TIMER_CTRL_ATI : 0) +#endif + | (init->sync ? TIMER_CTRL_SYNC : 0); + + /* Start timer if specified to be enabled (dosn't hurt if already started) */ + if (init->enable) { + timer->CMD = TIMER_CMD_START; + } +} + +/***************************************************************************//** + * @brief + * Initialize TIMER compare/capture channel. + * + * @details + * Notice that if operating channel in compare mode, the CCV and CCVB register + * must be set separately as required. + * + * @param[in] timer + * Pointer to TIMER peripheral register block. + * + * @param[in] ch + * Compare/capture channel to init for. + * + * @param[in] init + * Pointer to TIMER initialization structure. + ******************************************************************************/ +void TIMER_InitCC(TIMER_TypeDef *timer, + unsigned int ch, + const TIMER_InitCC_TypeDef *init) +{ + EFM_ASSERT(TIMER_REF_VALID(timer)); + EFM_ASSERT(TIMER_CH_VALID(ch)); + + timer->CC[ch].CTRL = + ((uint32_t)(init->eventCtrl) << _TIMER_CC_CTRL_ICEVCTRL_SHIFT) + | ((uint32_t)(init->edge) << _TIMER_CC_CTRL_ICEDGE_SHIFT) + | ((uint32_t)(init->prsSel) << _TIMER_CC_CTRL_PRSSEL_SHIFT) + | ((uint32_t)(init->cufoa) << _TIMER_CC_CTRL_CUFOA_SHIFT) + | ((uint32_t)(init->cofoa) << _TIMER_CC_CTRL_COFOA_SHIFT) + | ((uint32_t)(init->cmoa) << _TIMER_CC_CTRL_CMOA_SHIFT) + | ((uint32_t)(init->mode) << _TIMER_CC_CTRL_MODE_SHIFT) + | (init->filter ? TIMER_CC_CTRL_FILT_ENABLE : 0) + | (init->prsInput ? TIMER_CC_CTRL_INSEL_PRS : 0) + | (init->coist ? TIMER_CC_CTRL_COIST : 0) + | (init->outInvert ? TIMER_CC_CTRL_OUTINV : 0); +} + +#if defined(_TIMER_DTCTRL_MASK) +/***************************************************************************//** + * @brief + * Initialize the TIMER DTI unit. + * + * @param[in] timer + * Pointer to TIMER peripheral register block. + * + * @param[in] init + * Pointer to TIMER DTI initialization structure. + ******************************************************************************/ +void TIMER_InitDTI(TIMER_TypeDef *timer, const TIMER_InitDTI_TypeDef *init) +{ + EFM_ASSERT(TIMER0 == timer); + + /* Make sure the DTI unit is disabled while initializing. */ + TIMER_EnableDTI(timer, false); + + /* Setup the DTCTRL register. + The enable bit will be set at the end of the function if specified. */ + timer->DTCTRL = + (init->autoRestart ? TIMER_DTCTRL_DTDAS : 0) + | (init->activeLowOut ? TIMER_DTCTRL_DTIPOL : 0) + | (init->invertComplementaryOut ? TIMER_DTCTRL_DTCINV : 0) + | (init->enablePrsSource ? TIMER_DTCTRL_DTPRSEN : 0) + | ((uint32_t)(init->prsSel) << _TIMER_DTCTRL_DTPRSSEL_SHIFT); + + /* Setup the DTTIME register. */ + timer->DTTIME = + ((uint32_t)(init->prescale) << _TIMER_DTTIME_DTPRESC_SHIFT) + | ((uint32_t)(init->riseTime) << _TIMER_DTTIME_DTRISET_SHIFT) + | ((uint32_t)(init->fallTime) << _TIMER_DTTIME_DTFALLT_SHIFT); + + /* Setup the DTFC register. */ + timer->DTFC = + (init->enableFaultSourceCoreLockup ? TIMER_DTFC_DTLOCKUPFEN : 0) + | (init->enableFaultSourceDebugger ? TIMER_DTFC_DTDBGFEN : 0) + | (init->enableFaultSourcePrsSel0 ? TIMER_DTFC_DTPRS0FEN : 0) + | (init->enableFaultSourcePrsSel1 ? TIMER_DTFC_DTPRS1FEN : 0) + | ((uint32_t)(init->faultAction) << _TIMER_DTFC_DTFA_SHIFT) + | ((uint32_t)(init->faultSourcePrsSel0) << _TIMER_DTFC_DTPRS0FSEL_SHIFT) + | ((uint32_t)(init->faultSourcePrsSel1) << _TIMER_DTFC_DTPRS1FSEL_SHIFT); + + /* Setup the DTOGEN register. */ + timer->DTOGEN = init->outputsEnableMask; + + /* Clear any previous DTI faults. */ + TIMER_ClearDTIFault(timer, TIMER_GetDTIFault(timer)); + + /* Enable/disable before returning. */ + TIMER_EnableDTI(timer, init->enable); +} +#endif + +/***************************************************************************//** + * @brief + * Reset TIMER to same state as after a HW reset. + * + * @note + * The ROUTE register is NOT reset by this function, in order to allow for + * centralized setup of this feature. + * + * @param[in] timer + * Pointer to TIMER peripheral register block. + ******************************************************************************/ +void TIMER_Reset(TIMER_TypeDef *timer) +{ + int i; + + EFM_ASSERT(TIMER_REF_VALID(timer)); + + /* Make sure disabled first, before resetting other registers */ + timer->CMD = TIMER_CMD_STOP; + + timer->CTRL = _TIMER_CTRL_RESETVALUE; + timer->IEN = _TIMER_IEN_RESETVALUE; + timer->IFC = _TIMER_IFC_MASK; + timer->TOPB = _TIMER_TOPB_RESETVALUE; + /* Write TOP after TOPB to invalidate TOPB (clear TIMER_STATUS_TOPBV) */ + timer->TOP = _TIMER_TOP_RESETVALUE; + timer->CNT = _TIMER_CNT_RESETVALUE; + /* Do not reset route register, setting should be done independently */ + /* (Note: ROUTE register may be locked by DTLOCK register.) */ + + for (i = 0; TIMER_CH_VALID(i); i++) { + timer->CC[i].CTRL = _TIMER_CC_CTRL_RESETVALUE; + timer->CC[i].CCV = _TIMER_CC_CCV_RESETVALUE; + timer->CC[i].CCVB = _TIMER_CC_CCVB_RESETVALUE; + } + + /* Reset dead time insertion module, no effect on timers without DTI */ + +#if defined(TIMER_DTLOCK_LOCKKEY_UNLOCK) + /* Unlock DTI registers first in case locked */ + timer->DTLOCK = TIMER_DTLOCK_LOCKKEY_UNLOCK; + + timer->DTCTRL = _TIMER_DTCTRL_RESETVALUE; + timer->DTTIME = _TIMER_DTTIME_RESETVALUE; + timer->DTFC = _TIMER_DTFC_RESETVALUE; + timer->DTOGEN = _TIMER_DTOGEN_RESETVALUE; + timer->DTFAULTC = _TIMER_DTFAULTC_MASK; +#endif +} + +/** @} (end addtogroup TIMER) */ +/** @} (end addtogroup emlib) */ +#endif /* defined(TIMER_COUNT) && (TIMER_COUNT > 0) */ diff --git a/efm32boot/inc/app.h b/efm32boot/inc/app.h index d22dd79..a84e127 100644 --- a/efm32boot/inc/app.h +++ b/efm32boot/inc/app.h @@ -8,19 +8,25 @@ #ifndef SRC_APP_H_ #define SRC_APP_H_ +#include + #define IS_BOOTLOADER #define DEBUG_LEVEL 0 -#define PRINTING_USE_VCOM +//#define PRINTING_USE_VCOM -#define USING_DEV_BOARD +//#define USING_DEV_BOARD #define BRIDGE_TO_WALLET -#define JUMP_LOC 0x8000 +#define JUMP_LOC 0x4000 +#ifdef USING_DEV_BOARD #define PUSH_BUTTON gpioPortF,6 +#else +#define PUSH_BUTTON gpioPortD,13 +#endif //#define DISABLE_CTAPHID_PING #define DISABLE_CTAPHID_WINK @@ -32,6 +38,8 @@ int bootloader_bridge(uint8_t klen, uint8_t * keyh); int is_authorized_to_boot(); +#define LED_INIT_VALUE 0x101000 + extern uint8_t REBOOT_FLAG; #endif /* SRC_APP_H_ */ diff --git a/efm32boot/src/main.c b/efm32boot/src/main.c index 984600a..e21d66b 100644 --- a/efm32boot/src/main.c +++ b/efm32boot/src/main.c @@ -1,5 +1,6 @@ #include "em_device.h" #include "em_chip.h" +#include "em_timer.h" #include "device.h" #include "app.h" @@ -63,10 +64,15 @@ bootmode: printf1(TAG_GEN,"Reflash condition detected\n"); ctaphid_init(); reset_efm8(); + TIMER0_enter_DefaultMode_from_RESET(); + TIMER_TopSet(TIMER0, 255); + + RGB(LED_INIT_VALUE); + /* Infinite loop */ int count = 0; while (1) { - if (millis() - t1 > 1000) + if (millis() - t1 > 10) { /*printf("heartbeat %ld\n", beat++);*/ heartbeat(); @@ -100,7 +106,7 @@ bootmode: printf1(TAG_GEN,"Normal boot\n"); - if (is_authorized_to_boot()) + if (is_authorized_to_boot() ) { BOOT_boot(); } else { diff --git a/efm8/.cproject b/efm8/.cproject index 3ec816d..3561848 100644 --- a/efm8/.cproject +++ b/efm8/.cproject @@ -170,7 +170,7 @@ - + diff --git a/efm8/inc/app.h b/efm8/inc/app.h index c94a73e..5027bb6 100644 --- a/efm8/inc/app.h +++ b/efm8/inc/app.h @@ -13,8 +13,7 @@ void usb_transfer_complete(); void spi_transfer_complete(); -#define EFM32_RW_PIN P1_B2 -#define MSG_RDY_INT_PIN P1_B1 + #define INPUT_ENDPOINT EP2OUT #define OUTPUT_ENDPOINT EP3IN diff --git a/efm8/src/descriptors.c b/efm8/src/descriptors.c index 194fa6f..dec482d 100644 --- a/efm8/src/descriptors.c +++ b/efm8/src/descriptors.c @@ -127,14 +127,22 @@ SI_SEGMENT_VARIABLE(configDesc[], #define LANG_STRING htole16( SLAB_USB_LANGUAGE ) #define MFR_STRING 'S','i','l','i','c','o','n',' ','L','a','b','s','\0' #define MFR_SIZE 13 -#define PROD_STRING 'E','O','S',' ','W','a','l','l','e','t','\0' -#define PROD_SIZE 11 + #define SER_STRING '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','\0' #define SER_SIZE 17 #define CFG_STRING 'C','o','n','f','i','g',' ','#','1','\0' #define CFG_SIZE 10 +#ifdef BRIDGE_TO_WALLET #define INT0_STRING 'E','O','S',' ','W','a','l','l','e','t','\0' #define INT0_SIZE 11 +#define PROD_STRING 'E','O','S',' ','W','a','l','l','e','t','\0' +#define PROD_SIZE 11 +#else +#define INT0_STRING 'S','o','l','o',' ','K','e','y','\0' +#define INT0_SIZE 9 +#define PROD_STRING 'S','o','l','o',' ','K','e','y','\0' +#define PROD_SIZE 9 +#endif LANGID_STATIC_CONST_STRING_DESC( langDesc[], LANG_STRING ); UTF16LE_PACKED_STATIC_CONST_STRING_DESC( mfrDesc[], MFR_STRING, MFR_SIZE); diff --git a/efm8/src/main.c b/efm8/src/main.c index e68f69e..f4e6dbe 100644 --- a/efm8/src/main.c +++ b/efm8/src/main.c @@ -7,8 +7,20 @@ #define BUFFER_SIZE 12 -#define SIGNAL_WRITE_BSY() P1_B2 = 0 // Set P1 low -#define SIGNAL_WRITE_RDY() P1_B2 = 1 // Set P1 high +#ifdef USING_DEVELOPMENT_BOARD +#define RW_PIN P2_B3 +#define BUSY_PIN P1_B2 +#define MSG_RDY_PIN P1_B1 +#else +#define RW_PIN P0_B1 +#define BUSY_PIN P0_B2 +#define MSG_RDY_PIN P0_B3 +#endif + +#define SIGNAL_WRITE_BSY() BUSY_PIN = 0 // Set P1 low +#define SIGNAL_WRITE_RDY() BUSY_PIN = 1 // Set P1 high + + data uint8_t write_ptr = 0; data uint8_t read_ptr = 0; @@ -63,6 +75,7 @@ void usb_writeback_complete() void spi_transfer_complete() { + if (count > 0) count--; i_ptr = 0; read_ptr++; @@ -107,9 +120,9 @@ int main(void) { SCON0_TI = 1; - P2_B0 = 1; +// P2_B0 = 1; - MSG_RDY_INT_PIN = 1; + MSG_RDY_PIN = 1; // enable SPI interrupts // SPI0FCN1 = SPI0FCN1 | (1<<4); @@ -121,7 +134,7 @@ int main(void) { // SPI0FCN0 &= ~3; // FIFO threshold 0x0 SPI0FCN1 |= (1); // Enable RX fifo - cprints("hello,world\r\n"); +// cprints("hello,world\r\n"); reset = RSTSRC; @@ -131,13 +144,12 @@ int main(void) { RSTSRC = (1<<4); } - MSG_RDY_INT_PIN = 1; + MSG_RDY_PIN = 1; SIGNAL_WRITE_BSY(); while (1) { - - if (P2_B3 == 0) + if (RW_PIN == 0) { i_ptr = 0; SPI0FCN0 |= (1<<6); // Flush TX fifo buffer @@ -153,7 +165,7 @@ int main(void) { SPI0DAT = (hidmsgbuf+read_ptr*64)[i_ptr++]; } - while(P2_B3 == 0) + while(RW_PIN == 0) { } @@ -162,7 +174,7 @@ int main(void) { spi_transfer_complete(); if (count == 0) { - MSG_RDY_INT_PIN = 1; + MSG_RDY_PIN = 1; } SPI0FCN0 = SPI0FCN0 | (1<<2); // flush RX fifo @@ -181,6 +193,7 @@ int main(void) { // Did we RX data and have room? if ((SPI0CFG & (0x1)) == 0 && USB_TX_COUNT < 511/2) { + writebackbuf[writebackbuf_count++] = SPI0DAT; SIGNAL_WRITE_RDY(); @@ -204,11 +217,14 @@ int main(void) { if (millis() - t1 > 1500) { +#ifdef USING_DEVELOPMENT_BOARD P1_B5 = k++&1; +#endif t1 = millis(); } // if (!USBD_EpIsBusy(EP2OUT) && !USBD_EpIsBusy(EP3IN) && lastcount==count) if (!USBD_EpIsBusy(INPUT_ENDPOINT) && lastcount==count) +// if (lastcount==count) { // cprintd("sched read to ",1,(int)(hidmsgbuf + write_ptr*64)); if (count == BUFFER_SIZE) @@ -217,11 +233,12 @@ int main(void) { } else { +// cprints("sched read\r\n"); USBD_Read(INPUT_ENDPOINT, hidmsgbuf + write_ptr*64, 64, true); } } - +// cprints("it\r\n"); if (lastcount != count) { @@ -229,7 +246,7 @@ int main(void) { { // cputd(debugRi); cprints(">> "); // dump_hex(debugR,64); - MSG_RDY_INT_PIN = 0; + MSG_RDY_PIN = 0; } else { diff --git a/tools/http2udb.py b/tools/http2udb.py index 036e1f4..d6d6341 100644 --- a/tools/http2udb.py +++ b/tools/http2udb.py @@ -120,8 +120,9 @@ class UDPBridge(BaseHTTPRequestHandler): h = base64.b64encode(h.encode()) h = to_websafe(h.decode()) - START = 0x8000 - END = 2048 * 125 - 4 + num_pages = 64 + START = 0x4000 + END = 2048 * (num_pages - 3) - 4 ih = IntelHex(HEX_FILE) segs = ih.segments() @@ -132,7 +133,8 @@ class UDPBridge(BaseHTTPRequestHandler): print('im_size: ', im_size) print('firmware_size: ', len(arr)) - sig = sha256((arr).tobytes()) + byts = (arr).tobytes() if hasattr(arr,'tobytes') else (arr).tostring() + sig = sha256(byts) print('hash', binascii.hexlify(sig)) sig = sk.sign_digest(sig) diff --git a/web/js/wallet.js b/web/js/wallet.js index 69acf80..e752af5 100644 --- a/web/js/wallet.js +++ b/web/js/wallet.js @@ -1314,7 +1314,8 @@ async function run_tests() { async function test_bootloader() { - var addr = 0x8000; + var addr = 0x4000; + var num_pages = 64; var p = await dev.is_bootloader(); TEST(p.status == 'CTAP1_SUCCESS', 'Device is in bootloader mode'); @@ -1324,16 +1325,16 @@ async function run_tests() { p = await dev.bootloader_write(0, randdata); TEST(p.status == 'CTAP2_ERR_NOT_ALLOWED', 'Denies accessing invalid address'); - p = await dev.bootloader_write(0x8000-4, randdata); + p = await dev.bootloader_write(addr-4, randdata); TEST(p.status == 'CTAP2_ERR_NOT_ALLOWED', 'Denies accessing invalid address'); - p = await dev.bootloader_write(2048 * 125-4, randdata); + p = await dev.bootloader_write(2048 * (num_pages-3)-4, randdata); TEST(p.status == 'CTAP2_ERR_NOT_ALLOWED', 'Denies accessing invalid address'); - p = await dev.bootloader_write(2048 * 126, randdata); + p = await dev.bootloader_write(2048 * (num_pages-2), randdata); TEST(p.status == 'CTAP2_ERR_NOT_ALLOWED', 'Denies accessing invalid address'); - p = await dev.bootloader_write(2048 * 129, randdata); + p = await dev.bootloader_write(2048 * (num_pages+1), randdata); TEST(p.status == 'CTAP2_ERR_NOT_ALLOWED', 'Denies accessing invalid address'); @@ -1371,14 +1372,14 @@ async function run_tests() { } //while(1) - //{ - //await device_start_over(); - //await test_pin(); - //await test_crypto(); - //await test_rng(); - //} + { + await device_start_over(); + await test_pin(); + await test_crypto(); + await test_rng(); + } //await benchmark(); - await test_persistence(); + //await test_persistence(); //await test_bootloader(); diff --git a/web/simple-https-server.py b/web/simple-https-server.py index 1c69750..3279b69 100644 --- a/web/simple-https-server.py +++ b/web/simple-https-server.py @@ -4,10 +4,12 @@ from http.server import HTTPServer, SimpleHTTPRequestHandler import ssl +host =('localhost', 4443) httpd = HTTPServer(('localhost', 4443), SimpleHTTPRequestHandler) httpd.socket = ssl.wrap_socket (httpd.socket, keyfile="localhost.key", certfile='localhost.crt', server_side=True) +print('serving on ', host) httpd.serve_forever()