From dd1616dba9eadf3b0d553b545e0d42eadac808cd Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Mon, 17 Feb 2020 22:27:16 +0100 Subject: [PATCH] test-bsb: Primitive tests... --- lib/util.h | 2 +- test-bsb/Makefile | 8 ++++ test-bsb/README | 30 +++++++++++++ test-bsb/config.h | 8 ++++ test-bsb/test.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 test-bsb/Makefile create mode 100644 test-bsb/README create mode 100644 test-bsb/config.h create mode 100644 test-bsb/test.c diff --git a/lib/util.h b/lib/util.h index e31f7fd..f7fe61d 100644 --- a/lib/util.h +++ b/lib/util.h @@ -86,7 +86,7 @@ static inline void wait_for_interrupt(void) // util-debug.c -void debug_printf(const char *fmt, ...); +void debug_printf(const char *fmt, ...) __attribute__((format(printf,1,2))); void debug_puts(const char *s); void debug_putc(int c); diff --git a/test-bsb/Makefile b/test-bsb/Makefile new file mode 100644 index 0000000..cab70cc --- /dev/null +++ b/test-bsb/Makefile @@ -0,0 +1,8 @@ +ROOT=.. +BINARY=test +OBJS=test.o +LIB_OBJS=util-debug.o + +WITH_SERIAL_FLASH=1 + +include $(ROOT)/mk/bluepill.mk diff --git a/test-bsb/README b/test-bsb/README new file mode 100644 index 0000000..d65e8ad --- /dev/null +++ b/test-bsb/README @@ -0,0 +1,30 @@ +Assignment of peripherals and pins +================================== + +USART1 debugging +USART3 BSB + + + Blue Pill pinout + +--------------------+ + | VBATT 3.3V | +BluePill LED | PC13 GND | + | PC14 5V | + | PC15 PB9 | + | PA0 PB8 | + | PA1 PB7 | + | PA2 PB6 | + | PA3 PB5 | + | PA4 PB4 | + | PA5 PB3 | + | PA6 PA15 | + | PA7 PA12 | + | PB0 PA11 | + | PB1 PA10 | RXD1 - debugging console +TXD3 - BSB | PB10 PA9 | TXD1 - debugging console +RXD3 - BSB | PB11 PA8 | + | RESET PB15 | + | 3.3 V PB14 | + | GND PB13 | + | GND PB12 | + +--------------------+ diff --git a/test-bsb/config.h b/test-bsb/config.h new file mode 100644 index 0000000..56f35a6 --- /dev/null +++ b/test-bsb/config.h @@ -0,0 +1,8 @@ +// Processor clock + +#define CPU_CLOCK_MHZ 72 + +// Debugging port + +#define DEBUG_USART USART1 +#define DEBUG_LED_BLUEPILL diff --git a/test-bsb/test.c b/test-bsb/test.c new file mode 100644 index 0000000..f923ffc --- /dev/null +++ b/test-bsb/test.c @@ -0,0 +1,107 @@ +#include "util.h" + +#include +#include +#include +#include +#include +#include +#include + +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + + rcc_periph_clock_enable(RCC_GPIOB); + rcc_periph_clock_enable(RCC_GPIOC); + rcc_periph_clock_enable(RCC_USART1); + rcc_periph_clock_enable(RCC_USART3); + + rcc_periph_reset_pulse(RST_GPIOB); + rcc_periph_reset_pulse(RST_GPIOC); + rcc_periph_reset_pulse(RST_USART1); + rcc_periph_reset_pulse(RST_USART3); +} + +static void gpio_setup(void) +{ + // PC13 = BluePill LED + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13); + gpio_clear(GPIOC, GPIO13); +} + +static volatile u32 ms_ticks; + +void sys_tick_handler(void) +{ + ms_ticks++; +} + +static void tick_setup(void) +{ + systick_set_frequency(1000, 72000000); + systick_counter_enable(); + systick_interrupt_enable(); +} + +static void delay_ms(uint ms) +{ + u32 start_ticks = ms_ticks; + while (ms_ticks - start_ticks < ms) + ; +} + +static void usart_setup(void) +{ + // Debugging USART + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_USART1_RX); + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX); + + usart_set_baudrate(USART1, 115200); + usart_set_databits(USART1, 8); + usart_set_stopbits(USART1, USART_STOPBITS_1); + usart_set_mode(USART1, USART_MODE_TX); + usart_set_parity(USART1, USART_PARITY_NONE); + usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE); + + usart_enable(USART1); + + // BSB USART + gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_USART3_RX); + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART3_TX); + + usart_set_baudrate(USART3, 4800); + usart_set_databits(USART3, 8); + usart_set_stopbits(USART3, USART_STOPBITS_1); + usart_set_mode(USART3, USART_MODE_RX); + usart_set_parity(USART3, USART_PARITY_ODD); + usart_set_flow_control(USART3, USART_FLOWCONTROL_NONE); + + usart_enable(USART3); +} + +int main(void) +{ + clock_setup(); + gpio_setup(); + usart_setup(); + tick_setup(); + // This is needed when programming via serial boot loader, harmless otherwise. + SCB_VTOR = 0x08000000; + cm_enable_interrupts(); + + u32 start_ticks = ms_ticks; + for (;;) { + if (USART_SR(USART3) & USART_SR_RXNE) { + uint x = usart_recv(USART3); + debug_printf("%02x ", x); + } + if (ms_ticks - start_ticks >= 100) { + start_ticks = ms_ticks; + debug_led_toggle(); + debug_putc(' '); + } + } + + return 0; +} -- 2.39.5