]> mj.ucw.cz Git - home-hw.git/blobdiff - test-bsb/test.c
Merge branch 'master' of ssh://git.ucw.cz/home/mj/GIT/home-hw
[home-hw.git] / test-bsb / test.c
index f923ffc38f810a60f47c37f8b07b059608d89566..345670e4536502bb86143dae5997de445bae193b 100644 (file)
@@ -12,11 +12,13 @@ static void clock_setup(void)
 {
        rcc_clock_setup_in_hse_8mhz_out_72mhz();
 
+       rcc_periph_clock_enable(RCC_GPIOA);
        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_GPIOA);
        rcc_periph_reset_pulse(RST_GPIOB);
        rcc_periph_reset_pulse(RST_GPIOC);
        rcc_periph_reset_pulse(RST_USART1);
@@ -28,6 +30,10 @@ 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);
+
+       // PB0 = yellow LED*, PB1 = green LED*
+       gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO0 | GPIO1);
+       gpio_set(GPIOB, GPIO0 | GPIO1);
 }
 
 static volatile u32 ms_ticks;
@@ -44,12 +50,14 @@ static void tick_setup(void)
        systick_interrupt_enable();
 }
 
+#if 0
 static void delay_ms(uint ms)
 {
        u32 start_ticks = ms_ticks;
        while (ms_ticks - start_ticks < ms)
                ;
 }
+#endif
 
 static void usart_setup(void)
 {
@@ -60,7 +68,8 @@ static void usart_setup(void)
        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_mode(USART1, USART_MODE_TX);
+       usart_set_mode(USART1, USART_MODE_TX_RX);
        usart_set_parity(USART1, USART_PARITY_NONE);
        usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
 
@@ -68,18 +77,22 @@ static void usart_setup(void)
 
        // 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);
+       gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, GPIO_USART3_TX);
 
        usart_set_baudrate(USART3, 4800);
-       usart_set_databits(USART3, 8);
+       usart_set_databits(USART3, 9);
        usart_set_stopbits(USART3, USART_STOPBITS_1);
-       usart_set_mode(USART3, USART_MODE_RX);
+       usart_set_mode(USART3, USART_MODE_TX_RX);
        usart_set_parity(USART3, USART_PARITY_ODD);
        usart_set_flow_control(USART3, USART_FLOWCONTROL_NONE);
 
        usart_enable(USART3);
 }
 
+static const byte send_pkt[] = {
+       0xdc, 0xc2, 0x00, 0x0b, 0x06, 0x3d, 0x2e, 0x11, 0x25, 0x40, 0x78,
+};
+
 int main(void)
 {
        clock_setup();
@@ -90,16 +103,39 @@ int main(void)
        SCB_VTOR = 0x08000000;
        cm_enable_interrupts();
 
-       u32 start_ticks = ms_ticks;
+       debug_printf("Hello, kitty!\n");
+
+       u32 last_ticks = 0;
+       u32 led_ticks = ms_ticks;
+       uint tx_pos = 0;
        for (;;) {
+               if (USART_SR(USART1) & USART_SR_RXNE) {
+                       uint c = usart_recv(USART1) & 0xff;
+                       if (c == '#')
+                               tx_pos = 1;
+               }
                if (USART_SR(USART3) & USART_SR_RXNE) {
-                       uint x = usart_recv(USART3);
+                       uint x = (usart_recv(USART3) & 0xff) ^ 0xff;
                        debug_printf("%02x ", x);
+                       last_ticks = ms_ticks;
+               }
+               if (tx_pos && (USART_SR(USART3) & USART_SR_TXE)) {
+                       if (tx_pos > sizeof(send_pkt)) {
+                               tx_pos = 0;
+                               debug_putc('.');
+                       } else {
+                               uint x = send_pkt[tx_pos++ - 1];
+                               usart_send(USART3, x ^ 0xff);
+                               debug_putc('>');
+                       }
+               }
+               if (last_ticks && ms_ticks - last_ticks >= 100) {
+                       last_ticks = 0;
+                       debug_putc('\n');
                }
-               if (ms_ticks - start_ticks >= 100) {
-                       start_ticks = ms_ticks;
+               if (ms_ticks - led_ticks >= 100) {
+                       led_ticks = ms_ticks;
                        debug_led_toggle();
-                       debug_putc(' ');
                }
        }