From 32da9c4e8cb2b5252c13b363db7c28675cf60bb5 Mon Sep 17 00:00:00 2001 From: jaseg Date: Tue, 23 Mar 2021 19:04:11 +0100 Subject: Fix up firmware and demos --- fw/src/demo.c | 102 +++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 69 insertions(+), 33 deletions(-) (limited to 'fw/src/demo.c') diff --git a/fw/src/demo.c b/fw/src/demo.c index 4d6a040..f20e35c 100644 --- a/fw/src/demo.c +++ b/fw/src/demo.c @@ -24,8 +24,8 @@ #include #include #include -#include -#include +#include +#include #include #include #include @@ -87,15 +87,16 @@ static inline void delay(uint32_t n) { /* Set STM32 to 168 MHz. */ static void clock_setup(void) { - rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_168MHZ]); + rcc_clock_setup_hse_3v3(&rcc_hse_8mhz_3v3[RCC_CLOCK_3V3_168MHZ]); rcc_periph_clock_enable(RCC_GPIOA); rcc_periph_clock_enable(RCC_GPIOB); + rcc_periph_clock_enable(RCC_GPIOC); rcc_periph_clock_enable(RCC_GPIOD); rcc_periph_clock_enable(RCC_GPIOE); rcc_periph_clock_enable(RCC_USART1); - rcc_periph_clock_enable(RCC_USART2); + rcc_periph_clock_enable(RCC_UART4); rcc_periph_clock_enable(RCC_OTGFS); rcc_periph_clock_enable(RCC_TIM6); rcc_periph_clock_enable(RCC_DMA2); @@ -139,7 +140,7 @@ static void finish_interrupted_scrub(void) { /* setup 10kHz timer */ static void tim6_setup(void) { - timer_reset(TIM6); + rcc_periph_reset_pulse(RST_TIM6); timer_set_prescaler(TIM6, 8400 - 1); // 84Mhz/10kHz - 1 timer_set_period(TIM6, 65535); // Overflow in ~6.5 seconds timer_enable_irq(TIM6, TIM_DIER_UIE); @@ -180,13 +181,16 @@ static void gpio_setup(void) //gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO6 | GPIO7); //gpio_set(GPIOA, GPIO6 | GPIO7); - /* Status LEDs (PE4-15) */ - gpio_mode_setup(GPIOE, GPIO_MODE_INPUT, GPIO_PUPD_NONE, 0xfff0); - /* Alarm LEDs (PA6,7) */ gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO6 | GPIO7); gpio_set(GPIOA, GPIO6 | GPIO7); + /* Mode LEDs */ + gpio_mode_setup(GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO13 | GPIO14); + + /* Mode button */ + gpio_mode_setup(GPIOE, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO15); + /* Speaker */ gpio_mode_setup(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO10); gpio_set(GPIOB, GPIO10); @@ -199,12 +203,9 @@ static void gpio_setup(void) gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO9 | GPIO10); gpio_set_af(GPIOA, GPIO_AF7, GPIO9 | GPIO10); - /* USART2 (host link) */ - gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2 | GPIO3); - gpio_set_af(GPIOA, GPIO_AF7, GPIO2 | GPIO3); - - /* K0 (PE4)/K1 (PE3) buttons */ - //gpio_mode_setup(GPIOE, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO3 | GPIO4); + /* UART4 (host link) */ + gpio_mode_setup(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO10 | GPIO11); + gpio_set_af(GPIOC, GPIO_AF8, GPIO10 | GPIO11); } struct hid_report { @@ -221,7 +222,7 @@ void pairing_input(uint8_t modbyte, uint8_t keycode); void pairing_parse_report(struct hid_report *buf, uint8_t len); /* Minimum number of bytes of handshake hash to confirm during pairing */ -#define MIN_PAIRING_SEQUENCE_LENGTH 8 +#define MIN_PAIRING_SEQUENCE_LENGTH 4 int pairing_check(struct NoiseState *st, const char *buf) { //LOG_PRINTF("Checking pairing\n"); @@ -491,10 +492,10 @@ void DMA_ISR(DEBUG_USART_DMA_NUM, DEBUG_USART_DMA_STREAM_NUM)(void) { /*@ requires \valid_read(&pkt->type) && \valid_read(pkt->payload + (0..payload_length-1)); requires \valid(st); requires \valid(st->handshake); - requires \separated(st, st->rx_cipher, st->tx_cipher, st->handshake, (uint8_t *)pkt->payload, &usart2_out, &st->handshake_hash); - requires \valid(usart2_out); + requires \separated(st, st->rx_cipher, st->tx_cipher, st->handshake, (uint8_t *)pkt->payload, &uart4_out, &st->handshake_hash); + requires \valid(uart4_out); - assigns pairing_buf_pos, *usart2_out, *st; + assigns pairing_buf_pos, *uart4_out, *st; assigns st->handshake, st->handshake_state, st->rx_cipher, st->tx_cipher; @*/ @@ -515,7 +516,7 @@ void handle_host_packet(struct NoiseState *st, const struct control_packet *pkt, } else { LOG_PRINTF("Too many failed handshake attempts, not starting another one\n"); struct control_packet out = { .type=HOST_TOO_MANY_FAILS }; - send_packet(usart2_out, (uint8_t *)&out, sizeof(out)); + send_packet(uart4_out, (uint8_t *)&out, sizeof(out)); } } else if (pkt->type == HOST_HANDSHAKE) { @@ -525,7 +526,7 @@ void handle_host_packet(struct NoiseState *st, const struct control_packet *pkt, TRACING_CLEAR(TR_NOISE_HANDSHAKE); LOG_PRINTF("Reporting handshake error to host\n"); struct control_packet out = { .type=HOST_CRYPTO_ERROR }; - send_packet(usart2_out, (uint8_t *)&out, sizeof(out)); + send_packet(uart4_out, (uint8_t *)&out, sizeof(out)); } else TRACING_CLEAR(TR_NOISE_HANDSHAKE); } else { @@ -555,10 +556,10 @@ int main(void) /* end unsafe debug code */ #endif - usart_dma_init(usart2_out); - usart_enable_rx_interrupt(USART2); - nvic_enable_irq(NVIC_USART2_IRQ); - nvic_set_priority(NVIC_USART2_IRQ, 3<<4); + usart_dma_init(uart4_out); + usart_enable_rx_interrupt(UART4); + nvic_enable_irq(NVIC_UART4_IRQ); + nvic_set_priority(NVIC_UART4_IRQ, 3<<4); nvic_set_priority(debug_out_s.irqn, 1<<4); LOG_PRINTF("\n==================================\n"); @@ -602,20 +603,52 @@ int main(void) gpio_clear(GPIOA, GPIO6); gpio_clear(GPIOA, GPIO7); gpio_clear(GPIOB, GPIO10); + + int last_btn_st = 0; + int debounce_ctr = 0; + int mode_debug = 0; +#define DEBOUNCE_IVL 1000 + gpio_set(GPIOE, GPIO13); + gpio_clear(GPIOE, GPIO14); + while (23) { delay(1); + if (debounce_ctr > 0) { + debounce_ctr -= 1; + + } else { + if (gpio_get(GPIOE, GPIO15)) { + if (!last_btn_st) { + debounce_ctr = DEBOUNCE_IVL; + + mode_debug = !mode_debug; + + if (mode_debug) { + gpio_clear(GPIOE, GPIO13); + gpio_set(GPIOE, GPIO14); + + } else { + gpio_set(GPIOE, GPIO13); + gpio_clear(GPIOE, GPIO14); + } + } + + last_btn_st = 1; + + } else { + last_btn_st = 0; + } + } + + /* led_ctr++; if (led_ctr == 10) { gpio_clear(GPIOA, GPIO6); gpio_clear(GPIOA, GPIO7); - } else if (led_ctr == 300) { - gpio_mode_setup(GPIOE, GPIO_MODE_INPUT, GPIO_PUPD_NONE, 0xfff0); } else if (led_ctr == 400) { if (++led_idx == 12) led_idx = 0; - gpio_mode_setup(GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, 1<<(4+led_idx)); - gpio_clear(GPIOE, 0xfff0); if (led_idx & 1) gpio_set(GPIOA, GPIO6); else @@ -638,7 +671,7 @@ int main(void) } else { gpio_clear(GPIOB, GPIO10); } - continue; + */ if (++poll_ctr == 10) { poll_ctr = 0; @@ -652,12 +685,13 @@ int main(void) host_packet_length = 0; /* Acknowledge to USART ISR the buffer has been handled */ } else if (host_packet_length < 0) { /* USART error */ + ssize_t err = host_packet_length; host_packet_length = 0; /* Acknowledge to USART ISR the error has been handled */ if (noise_state.handshake_state < HANDSHAKE_DONE_UNKNOWN_HOST) { - LOG_PRINTF("USART error, aborting handshake\n"); + LOG_PRINTF("USART error, aborting handshake: %zd\n", err); struct control_packet pkt = { .type=HOST_COMM_ERROR }; - send_packet(usart2_out, (uint8_t *)&pkt, sizeof(pkt)); + send_packet(uart4_out, (uint8_t *)&pkt, sizeof(pkt)); if (reset_protocol_handshake(&noise_state)) LOG_PRINTF("Error starting protocol handshake.\n"); @@ -666,15 +700,17 @@ int main(void) } } - if (noise_state.handshake_state == HANDSHAKE_IN_PROGRESS) { +#if 0 + if (noise_state.handshake_state == HANDSHAKE_PHASE1 || noise_state.handshake_state == HANDSHAKE_PHASE2) { TRACING_SET(TR_NOISE_HANDSHAKE); if (try_continue_noise_handshake(&noise_state, NULL, 0)) { /* handle outgoing messages */ TRACING_CLEAR(TR_NOISE_HANDSHAKE); LOG_PRINTF("Reporting handshake error to host\n"); struct control_packet pkt = { .type=HOST_CRYPTO_ERROR }; - send_packet(usart2_out, (uint8_t *)&pkt, sizeof(pkt)); + send_packet(uart4_out, (uint8_t *)&pkt, sizeof(pkt)); } else TRACING_CLEAR(TR_NOISE_HANDSHAKE); } +#endif } } -- cgit