]> mj.ucw.cz Git - home-hw.git/commitdiff
test-bsb: Primitive tests...
authorMartin Mares <mj@ucw.cz>
Mon, 17 Feb 2020 21:27:16 +0000 (22:27 +0100)
committerMartin Mares <mj@ucw.cz>
Mon, 17 Feb 2020 21:31:07 +0000 (22:31 +0100)
lib/util.h
test-bsb/Makefile [new file with mode: 0644]
test-bsb/README [new file with mode: 0644]
test-bsb/config.h [new file with mode: 0644]
test-bsb/test.c [new file with mode: 0644]

index e31f7fd610052166c647c0403fe5b9aee1d61b7e..f7fe61dd935e1b4ed59453dcd04d738ac5202dfa 100644 (file)
@@ -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 (file)
index 0000000..cab70cc
--- /dev/null
@@ -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 (file)
index 0000000..d65e8ad
--- /dev/null
@@ -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 (file)
index 0000000..56f35a6
--- /dev/null
@@ -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 (file)
index 0000000..f923ffc
--- /dev/null
@@ -0,0 +1,107 @@
+#include "util.h"
+
+#include <libopencm3/cm3/cortex.h>
+#include <libopencm3/cm3/nvic.h>
+#include <libopencm3/cm3/scb.h>
+#include <libopencm3/cm3/systick.h>
+#include <libopencm3/stm32/rcc.h>
+#include <libopencm3/stm32/gpio.h>
+#include <libopencm3/stm32/usart.h>
+
+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;
+}