diff options
author | xengineering <mail2xengineering@protonmail.com> | 2020-10-24 18:14:58 +0200 |
---|---|---|
committer | xengineering <mail2xengineering@protonmail.com> | 2020-10-24 18:30:47 +0200 |
commit | 28a93739db99920431b5d72d948bfc1cddd0a8a7 (patch) | |
tree | 30ef6c95670ff0964cd7f5f9153affde3e1fa52d | |
parent | 0fa18937f6cb17b41a1fe2b193dbb3c2a5c2ba45 (diff) | |
download | stm32f103c8-examples-28a93739db99920431b5d72d948bfc1cddd0a8a7.tar stm32f103c8-examples-28a93739db99920431b5d72d948bfc1cddd0a8a7.tar.zst stm32f103c8-examples-28a93739db99920431b5d72d948bfc1cddd0a8a7.zip |
Add USART Code
-rw-r--r-- | usart/Makefile | 33 | ||||
-rw-r--r-- | usart/main.c | 70 |
2 files changed, 103 insertions, 0 deletions
diff --git a/usart/Makefile b/usart/Makefile new file mode 100644 index 0000000..7ce252d --- /dev/null +++ b/usart/Makefile @@ -0,0 +1,33 @@ +PROJECT = usart +BUILD_DIR = bin + +#SHARED_DIR = ../libraries +CFILES = main.c +#CFILES += api.c +#AFILES += api-asm.S + +# TODO - you will need to edit these two lines! +DEVICE=stm32f103c8 +#OOCD_FILE = board/stm32f4discovery.cfg + +# You shouldn't have to edit anything below here. +VPATH += $(SHARED_DIR) +INCLUDES += $(patsubst %,-I%, . $(SHARED_DIR)) +OPENCM3_DIR=../libopencm3 + +include $(OPENCM3_DIR)/mk/genlink-config.mk +include ../rules.mk +include $(OPENCM3_DIR)/mk/genlink-rules.mk + + +# Black Magic Probe: + +GDB=/usr/bin/arm-none-eabi-gdb +BMP_DEVICE=/dev/ttyACM0 + +bmp: $(PROJECT).elf + $(GDB) $(PROJECT).elf \ + -ex 'set confirm off' \ + -ex 'target extended-remote $(BMP_DEVICE)' \ + -ex 'monitor swdp_scan' \ + -ex 'attach 1' diff --git a/usart/main.c b/usart/main.c new file mode 100644 index 0000000..b92cbbe --- /dev/null +++ b/usart/main.c @@ -0,0 +1,70 @@ + + +#include <libopencm3/stm32/rcc.h> +#include <libopencm3/stm32/gpio.h> +#include <libopencm3/stm32/usart.h> + + +#define DELAY 1000000 + + +void clock_init(void); +void gpio_init(void); +void usart_init(void); +void delay(void); + + +int main(void) +{ + clock_init(); + gpio_init(); + usart_init(); + + while(1){ + delay(); + usart_send_blocking(USART3, 'H'); + usart_send_blocking(USART3, 'e'); + usart_send_blocking(USART3, 'l'); + usart_send_blocking(USART3, 'l'); + usart_send_blocking(USART3, 'o'); + usart_send_blocking(USART3, '\r'); + usart_send_blocking(USART3, '\n'); + delay(); + } + + return 0; +} + + +void clock_init(void) +{ + rcc_periph_clock_enable(RCC_GPIOB); // for USART3 + rcc_periph_clock_enable(RCC_USART3); // for USART3 +} + + +void gpio_init(void) +{ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART3_TX); // TX setup + gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_USART3_RX); // RX setup +} + + +void usart_init(void) +{ + usart_set_baudrate(USART3, 9600); + usart_set_databits(USART3, 8); + usart_set_flow_control(USART3, USART_FLOWCONTROL_NONE); + usart_set_mode(USART3, USART_MODE_TX_RX); + usart_set_parity(USART3, USART_PARITY_NONE); + usart_set_stopbits(USART3, USART_STOPBITS_1); + usart_enable(USART3); +} + + +void delay(void) +{ + for (int i = 0; i < DELAY; i++) { + __asm__("nop"); + } +} |