From 10cbb998e43dbaef8d4cf7c8fe97c8265b34fea3 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Wed, 31 Jul 2019 00:06:39 +0200 Subject: [PATCH] Protab: USART --- protab/usart1/Makefile | 6 ++++++ protab/usart1/usart.c | 38 ++++++++++++++++++++++++++++++++++++++ protab/usart2/Makefile | 6 ++++++ protab/usart2/usart.c | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 protab/usart1/Makefile create mode 100644 protab/usart1/usart.c create mode 100644 protab/usart2/Makefile create mode 100644 protab/usart2/usart.c diff --git a/protab/usart1/Makefile b/protab/usart1/Makefile new file mode 100644 index 0000000..117ad11 --- /dev/null +++ b/protab/usart1/Makefile @@ -0,0 +1,6 @@ +ROOT=../.. +BINARY=usart +OBJS=usart.o +LIB_OBJS= + +include $(ROOT)/mk/bluepill.mk diff --git a/protab/usart1/usart.c b/protab/usart1/usart.c new file mode 100644 index 0000000..cca6770 --- /dev/null +++ b/protab/usart1/usart.c @@ -0,0 +1,38 @@ +#include +#include +#include + +int main(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + rcc_periph_clock_enable(RCC_GPIOA); + rcc_periph_clock_enable(RCC_GPIOC); + rcc_periph_clock_enable(RCC_USART1); + + // PC13 = BluePill LED + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13); + + // PA10 = RX1, PA9 = TX1 + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO10); + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO9); + + usart_set_baudrate(USART1, 115200); + usart_set_databits(USART1, 8); + usart_set_stopbits(USART1, USART_STOPBITS_1); + usart_set_mode(USART1, USART_MODE_TX_RX); + usart_set_parity(USART1, USART_PARITY_NONE); + usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE); + usart_enable(USART1); + + int c = 32; + for (;;) { + gpio_toggle(GPIOC, GPIO13); + + usart_send(USART1, c); + usart_wait_send_ready(USART1); + + c++; + if (c == 127) + c = 32; + } +} diff --git a/protab/usart2/Makefile b/protab/usart2/Makefile new file mode 100644 index 0000000..117ad11 --- /dev/null +++ b/protab/usart2/Makefile @@ -0,0 +1,6 @@ +ROOT=../.. +BINARY=usart +OBJS=usart.o +LIB_OBJS= + +include $(ROOT)/mk/bluepill.mk diff --git a/protab/usart2/usart.c b/protab/usart2/usart.c new file mode 100644 index 0000000..0157140 --- /dev/null +++ b/protab/usart2/usart.c @@ -0,0 +1,36 @@ +#include +#include +#include + +int main(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + rcc_periph_clock_enable(RCC_GPIOA); + rcc_periph_clock_enable(RCC_GPIOC); + rcc_periph_clock_enable(RCC_USART1); + + // PC13 = BluePill LED + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13); + + // PA10 = RX1, PA9 = TX1 + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO10); + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO9); + + usart_set_baudrate(USART1, 115200); + usart_set_databits(USART1, 8); + usart_set_stopbits(USART1, USART_STOPBITS_1); + usart_set_mode(USART1, USART_MODE_TX_RX); + usart_set_parity(USART1, USART_PARITY_NONE); + usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE); + usart_enable(USART1); + + for (;;) { + gpio_toggle(GPIOC, GPIO13); + + usart_wait_recv_ready(USART1); + int c = usart_recv(USART1); + + usart_send(USART1, c); + usart_wait_send_ready(USART1); + } +} -- 2.39.2