From: Martin Mares Date: Wed, 19 Feb 2020 21:58:27 +0000 (+0100) Subject: BSB: An attempt at sending X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=5286b9d20dd896a010e0d1db9f0813f5c489951e;p=home-hw.git BSB: An attempt at sending --- diff --git a/test-bsb/Makefile b/test-bsb/Makefile index cab70cc..c6e1c3a 100644 --- a/test-bsb/Makefile +++ b/test-bsb/Makefile @@ -3,6 +3,6 @@ BINARY=test OBJS=test.o LIB_OBJS=util-debug.o -WITH_SERIAL_FLASH=1 +# WITH_SERIAL_FLASH=1 include $(ROOT)/mk/bluepill.mk diff --git a/test-bsb/test.c b/test-bsb/test.c index f923ffc..6ee4a90 100644 --- a/test-bsb/test.c +++ b/test-bsb/test.c @@ -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); @@ -44,12 +46,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 +64,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 +73,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 +99,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(' '); } }