From 8faa004882c835c6f4820b22b5b54f6fbfb88b9c Mon Sep 17 00:00:00 2001 From: eddyem Date: Sun, 15 Jan 2017 10:01:48 +0300 Subject: [PATCH] add USART over DMA --- F0/Readme.md | 5 +- F0/uart/Makefile | 127 ++++++++++++++++++++++++++++++++++++ F0/uart/Readme | 2 + F0/uart/ld/stm32f042k.ld | 31 +++++++++ F0/uart/main.c | 114 ++++++++++++++++++++++++++++++++ F0/uart/uart.bin | Bin 0 -> 2284 bytes F0/uart/uart.geanypro | 47 ++++++++++++++ F0/uart/usart.c | 137 +++++++++++++++++++++++++++++++++++++++ F0/uart/usart.h | 43 ++++++++++++ 9 files changed, 505 insertions(+), 1 deletion(-) create mode 100644 F0/uart/Makefile create mode 100644 F0/uart/Readme create mode 100644 F0/uart/ld/stm32f042k.ld create mode 100644 F0/uart/main.c create mode 100755 F0/uart/uart.bin create mode 100644 F0/uart/uart.geanypro create mode 100644 F0/uart/usart.c create mode 100644 F0/uart/usart.h diff --git a/F0/Readme.md b/F0/Readme.md index 74e21fc..24d1c95 100644 --- a/F0/Readme.md +++ b/F0/Readme.md @@ -1,2 +1,5 @@ Samples for STM32F042-nucleo and chinese STM32F030-based devboard -blink - simple LED blink +================================= + +- blink - simple LED blink +- uart - USART over DMA with hardware end-of-string detection diff --git a/F0/uart/Makefile b/F0/uart/Makefile new file mode 100644 index 0000000..c7148bf --- /dev/null +++ b/F0/uart/Makefile @@ -0,0 +1,127 @@ +BINARY = uart +BOOTPORT ?= /dev/ttyUSB0 +BOOTSPEED ?= 115200 +# change this linking script depending on particular MCU model, +# for example, if you have STM32F103VBT6, you should write: +LDSCRIPT = ld/stm32f042k.ld +LIBNAME = opencm3_stm32f0 +DEFS = -DSTM32F0 -DSTM32F042x6 -DEBUG + +OBJDIR = mk +INDEPENDENT_HEADERS= + +FP_FLAGS ?= -msoft-float +ARCH_FLAGS = -mthumb -mcpu=cortex-m0 $(FP_FLAGS) + +############################################################################### +# Executables +PREFIX ?= arm-none-eabi + +RM := rm -f +RMDIR := rmdir +CC := $(PREFIX)-gcc +LD := $(PREFIX)-gcc +AR := $(PREFIX)-ar +AS := $(PREFIX)-as +OBJCOPY := $(PREFIX)-objcopy +OBJDUMP := $(PREFIX)-objdump +GDB := $(PREFIX)-gdb +STFLASH = $(shell which st-flash) +STBOOT = $(shell which stm32flash) + +############################################################################### +# Source files +LDSCRIPT ?= $(BINARY).ld +SRC = $(wildcard *.c) +OBJS = $(addprefix $(OBJDIR)/, $(SRC:%.c=%.o)) + +ifeq ($(strip $(OPENCM3_DIR)),) +OPENCM3_DIR := /usr/local/arm-none-eabi +$(info Using $(OPENCM3_DIR) path to library) +endif + +INCLUDE_DIR = $(OPENCM3_DIR)/include +LIB_DIR = $(OPENCM3_DIR)/lib +SCRIPT_DIR = $(OPENCM3_DIR)/scripts + +############################################################################### +# C flags +CFLAGS += -O2 -g -MD +CFLAGS += -Wall -Werror -Wextra -Wshadow -Wimplicit-function-declaration +CFLAGS += -Wredundant-decls -I.. -I$(INCLUDE_DIR) +# -Wmissing-prototypes -Wstrict-prototypes +CFLAGS += -fno-common -ffunction-sections -fdata-sections + +############################################################################### +# Linker flags +LDFLAGS += --static -nostartfiles +LDFLAGS += -L$(LIB_DIR) +LDFLAGS += -T$(LDSCRIPT) +LDFLAGS += -Wl,-Map=$(*).map +LDFLAGS += -Wl,--gc-sections + +############################################################################### +# Used libraries +LDLIBS += -l$(LIBNAME) +LDLIBS += -Wl,--start-group -lc -lgcc -Wl,--end-group + +.SUFFIXES: .elf .bin .hex .srec .list .map .images +.SECONDEXPANSION: +.SECONDARY: + +ELF := $(OBJDIR)/$(BINARY).elf +LIST := $(OBJDIR)/$(BINARY).list +BIN := $(BINARY).bin +HEX := $(BINARY).hex + +all: list bin + +elf: $(ELF) +bin: $(BIN) +hex: $(HEX) +list: $(LIST) + +$(OBJDIR): + mkdir $(OBJDIR) + +$(OBJDIR)/%.o: %.c + @printf " CC $<\n" + $(CC) $(CFLAGS) $(DEFS) $(ARCH_FLAGS) -o $@ -c $< + +$(SRC) : %.c : %.h $(INDEPENDENT_HEADERS) + @touch $@ + +%.h: ; + +$(BIN): $(ELF) + @printf " OBJCOPY $(BIN)\n" + $(OBJCOPY) -Obinary $(ELF) $(BIN) + +$(HEX): $(ELF) + @printf " OBJCOPY $(HEX)\n" + $(OBJCOPY) -Oihex $(ELF) $(HEX) + +$(LIST): $(ELF) + @printf " OBJDUMP $(LIST)\n" + $(OBJDUMP) -S $(ELF) > $(LIST) + +$(ELF): $(OBJDIR) $(OBJS) $(LDSCRIPT) $(LIB_DIR)/lib$(LIBNAME).a + @printf " LD $(ELF)\n" + $(LD) $(LDFLAGS) $(ARCH_FLAGS) $(OBJS) $(LDLIBS) -o $(ELF) + +clean: + @printf " CLEAN\n" + $(RM) $(OBJS) $(OBJDIR)/*.d $(ELF) $(HEX) $(LIST) $(OBJDIR)/*.map + $(RMDIR) $(OBJDIR) + +flash: $(BIN) + @printf " FLASH $(BIN)\n" + $(STFLASH) write $(BIN) 0x8000000 + +boot: $(BIN) + @printf " LOAD $(BIN) through bootloader\n" + $(STBOOT) -b$(BOOTSPEED) $(BOOTPORT) -w $(BIN) + +.PHONY: clean elf hex list flash boot + +#-include $(OBJS:.o=.d) diff --git a/F0/uart/Readme b/F0/uart/Readme new file mode 100644 index 0000000..e8feb9b --- /dev/null +++ b/F0/uart/Readme @@ -0,0 +1,2 @@ +This is a simple USART over DMA application with hardware end of string control. +Speed 115200. Received string echoes back in reverce order. diff --git a/F0/uart/ld/stm32f042k.ld b/F0/uart/ld/stm32f042k.ld new file mode 100644 index 0000000..d4d8dd8 --- /dev/null +++ b/F0/uart/ld/stm32f042k.ld @@ -0,0 +1,31 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2012 Karl Palsson + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for STM32F100x4, 16K flash, 4K RAM. */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 32K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 6K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32f0.ld + diff --git a/F0/uart/main.c b/F0/uart/main.c new file mode 100644 index 0000000..23fdf5c --- /dev/null +++ b/F0/uart/main.c @@ -0,0 +1,114 @@ +/* + * main.c + * + * Copyright 2017 Edward V. Emelianoff + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include "stm32f0.h" +#include "usart.h" + +static uint32_t stk_ctr = 0; + +/* Called when systick fires */ +void sys_tick_handler(void){ + pin_toggle(GPIOB, GPIO3); + ++stk_ctr; +} + +/* + * Set up timer to fire every x milliseconds + * This is a unusual usage of systick, be very careful with the 24bit range + * of the systick counter! You can range from 1 to 2796ms with this. + */ +static void systick_setup(int xms){ + static int curms = 0; + if(curms == xms) return; + // 6MHz + systick_set_clocksource(STK_CSR_CLKSOURCE_EXT); + /* clear counter so it starts right away */ + STK_CVR = 0; + + systick_set_reload(6000 * xms); + systick_counter_enable(); + systick_interrupt_enable(); + curms = xms; +} + +/* set STM32 to clock by 48MHz from HSI oscillator */ +static void clock_setup(void){ + rcc_clock_setup_in_hsi_out_48mhz(); + //rcc_clock_setup_in_hse_8mhz_out_48mhz(); + + /* Enable clocks to the GPIO subsystems (A&B) */ + RCC_AHBENR = RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN; +} + +static void gpio_setup(void){ + // Set green led (PB3) as output + gpio_mode_setup(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO3); +} + +/** + * reverce line + */ +char *rline(char *in, int L){ + static char out[UARTBUFSZ]; + if(L > UARTBUFSZ - 1) return in; + char *optr = out, *iptr = &in[L-1]; + for(; L > 0; --L) *optr++ = *iptr--; + *optr = '\n'; + return out; +} + + +int main(void){ + clock_setup(); + gpio_setup(); + usart2_setup(); + + systick_setup(500); // 1Hz in normal mode + + const char dummy[] = "dummy text\n", err[] = "Error!\n"; + char *txt; + int L = 0, dummyctr = 0; + uint32_t ostctr = 0; + + while (1){ + if(L){ // there's something to send + if(ALL_OK == usart2_send(txt, L)){ + L = 0; + }else + usart2_send_blocking(err, sizeof(err)); + }else if(ostctr != stk_ctr){ + ostctr = stk_ctr; + if(++dummyctr > 10){ // once per 5 seconds + dummyctr = 0; + usart2_send(dummy, sizeof(dummy)); + } + } + if(usart2rx()){ // data received + L = usart2_getline(&txt); + if(L){ + txt = rline(txt, L); + ++L; // for trailing '\n' + } + } + } + return 0; +} + diff --git a/F0/uart/uart.bin b/F0/uart/uart.bin new file mode 100755 index 0000000000000000000000000000000000000000..b61477160f5e88704a9ec5a6fc274c6677fc28f3 GIT binary patch literal 2284 zcma)7eP|rV6@Rn4r`y|=bf-JX-CK?1y$?wzSr-0~k#XX5btkWMH?kYtw4~UfQ`aR+ zsN0K4!0kUdYD!6Jf^4S{+i99V9Y`EQeS}gSC?73^m~dAUu!;>E5;y{JyKQOpVUz2d zy>r1(LJod2Z{EClZ|3*jy#)iToncV7BgPT%6+dBfe_P1!K>Lh1_jT}Z`Rmzzm9Ot9 z5Bdl*H9hF|(F=uyJ*emfrKod-nJ(F7^78zO)33mSfj9$T`PGBYob!F>yN-YD`K#dn zYI84JN&iJM`OE@IL0Zj#|NBkwKV6?*+#Ipo6L2}-QwIzD(%l^l(k9=r0RF>w!G8=< zx;w%{uTL_KzThN2OuQ?f|2pKt4alxGA9cD@;O|+7ksD3(5+li*4z!6CiYTyYGv zwGiUW-fC=7q8Yb`5(~CTb;+uX!|lB_y$Bhd>kEB!f@V1QpKL}LNhe*f&K8n9T|F%f~i#tdMVSLW<&iWYc05+&w1= zAH(dmbUbA8Ay!Wx29r~)H(E5goo?UXWH#E;;C@EQzzrSYwaCbyrP}GN+)|NX0H$P*d=a@oMS8m;N3)QaR-sl0amV>ZY6U` zGV_^wqV!z0&y6qWg?KT+bvIu6%#5@6A<7f(6L288hr{U9caG+d&i1l?KID@0Zwf>F zICLL`WR6!X5mVfxT)Y1k@n#WAykFF!g%I%t4^ZvOc~<`@}xj+dsNVe9OUertl6=Gf!(jIQ%cBPN3-GxU2DD7LfgQ5+p?`!tHf%p zEw#k>L@-`!0m`2T<6lt&{?qHPSrxgp_CZTy8hVy3%{F;!?b()$mib`3&r{ZKvD9tuCfd3*LW=X>-_o+;z{ojFIIxo`TWL>=pymd>o3puvRCq(H+nj& z?qJng`{O;mqtOD5hrJj*2fa8efhE@e4-&(Mpoz?4f52WYGf}}Y`)olC@*P8+ggwq| z|5xVQYx2Ol5K&5(;F7U59kWUL7nnPI)hFOVb!$#ujb;q2BH?+(AZ1af{daKhGw}Rs zbYkDCPUXTY;fERt8hdCkLUs?5>qdz7x>Nj+`xE}6dxd}G#&-UV$M&4b|Em!ldCwTI zapQ;q6J7O-M(ULK-jQkK zvEeUE%j$eLl_^a8qp{YMX*a#ooS>DtpyQ7uB`Xc1nVla>W1$DciM;%C+~KRWe{ U1_w@_{OQS*1_X(P_|F6U7Z?Oyj{pDw literal 0 HcmV?d00001 diff --git a/F0/uart/uart.geanypro b/F0/uart/uart.geanypro new file mode 100644 index 0000000..d9d78ea --- /dev/null +++ b/F0/uart/uart.geanypro @@ -0,0 +1,47 @@ +[editor] +line_wrapping=true +line_break_column=100 +auto_continue_multiline=true + +[file_prefs] +final_new_line=true +ensure_convert_new_lines=true +strip_trailing_spaces=true +replace_tabs=true + +[indentation] +indent_width=4 +indent_type=0 +indent_hard_tab_width=4 +detect_indent=false +detect_indent_width=false +indent_mode=3 + +[project] +name=uart +base_path=/Big/Data/00__Electronics/STM32/src/stm32F0/uart/ +description= +file_patterns=*.c;*.h; + +[long line marker] +long_line_behaviour=1 +long_line_column=100 + +[files] +current_page=3 +FILE_NAME_0=2329;C;0;EUTF-8;0;1;1;%2FBig%2FData%2F00__Electronics%2FSTM32%2Fsrc%2Fstm32F0%2Fstm32f0.h;0;4 +FILE_NAME_1=1600;C;0;EUTF-8;0;1;1;%2FBig%2FData%2F00__Electronics%2FSTM32%2Fsrc%2Fstm32F0%2Fuart%2Fmain.c;0;4 +FILE_NAME_2=1095;C;0;EUTF-8;0;1;1;%2FBig%2FData%2F00__Electronics%2FSTM32%2Fsrc%2Fstm32F0%2Fuart%2Fusart.h;0;4 +FILE_NAME_3=2933;C;0;EUTF-8;0;1;1;%2FBig%2FData%2F00__Electronics%2FSTM32%2Fsrc%2Fstm32F0%2Fuart%2Fusart.c;0;4 +FILE_NAME_4=11024;C;0;EUTF-8;0;1;1;%2FBig%2FData%2F00__Electronics%2FSTM32%2FSTM32F0xx_Snippets_Package_V1.2.0%2FProjects%2FUSART%2F06_CommunicationUsingDMA%2Fmain.c;0;4 +FILE_NAME_5=977;C;0;EUTF-8;0;1;1;%2FBig%2FData%2F00__Electronics%2FSTM32%2Flibopencm3-examples%2Fexamples%2Fstm32%2Ff0%2Fstm32f0-discovery%2Fusart%2Fusart.c;0;4 +FILE_NAME_6=1858;C;0;EUTF-8;0;1;1;%2FBig%2FData%2F00__Electronics%2FSTM32%2Flibopencm3_stable_old%2Finclude%2Flibopencm3%2Fstm32%2Ff0%2Fusart.h;0;4 +FILE_NAME_7=3760;C;0;EUTF-8;0;1;1;%2FBig%2FData%2F00__Electronics%2FSTM32%2Flibopencm3_stable_old%2Flib%2Fstm32%2Fcommon%2Fgpio_common_f0234.c;0;4 +FILE_NAME_8=5290;C;0;EUTF-8;0;1;1;%2FBig%2FData%2F00__Electronics%2FSTM32%2Flibopencm3_stable_old%2Finclude%2Flibopencm3%2Fstm32%2Fcommon%2Fgpio_common_f234.h;0;4 +FILE_NAME_9=1682;C;0;EUTF-8;0;1;1;%2FBig%2FData%2F00__Electronics%2FSTM32%2Flibopencm3_stable_old%2Finclude%2Flibopencm3%2Fstm32%2Fcommon%2Fdma_common_l1f013.h;0;4 +FILE_NAME_10=4782;C;0;EUTF-8;0;1;1;%2FBig%2FData%2F00__Electronics%2FSTM32%2Flibopencm3_stable_old%2Finclude%2Flibopencm3%2Fcm3%2Fnvic.h;0;4 +FILE_NAME_11=2220;C;0;EUTF-8;0;1;1;%2FBig%2FData%2F00__Electronics%2FSTM32%2Flibopencm3_stable_old%2Finclude%2Flibopencm3%2Fstm32%2Ff0%2Fnvic.h;0;4 +FILE_NAME_12=13469;C;0;EUTF-8;0;1;1;%2FBig%2FData%2F00__Electronics%2FSTM32%2Flibopencm3_stable_old%2Flib%2Fstm32%2Ff0%2Frcc.c;0;4 + +[VTE] +last_dir=/home/eddy diff --git a/F0/uart/usart.c b/F0/uart/usart.c new file mode 100644 index 0000000..5663b21 --- /dev/null +++ b/F0/uart/usart.c @@ -0,0 +1,137 @@ +/*us + * usart.c + * + * Copyright 2017 Edward V. Emelianoff + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ +#include "stm32f0.h" +#include "usart.h" +#include + +int linerdy = 0, // received data line length + bufovr = 0, // input buffer overfull + txrdy = 1 // transmission done +; + + +int rbufno = 0; // current rbuf number +static char rbuf[UARTBUFSZ][2], tbuf[UARTBUFSZ]; // receive & transmit buffers +static char *recvdata = NULL; + +/** + * return length of received data (without trailing zero + */ +int usart2_getline(char **line){ + if(bufovr){ + bufovr = 0; + linerdy = 0; + return 0; + } + int L = linerdy; + *line = recvdata; + linerdy = 0; + return L; +} + +TXstatus usart2_send(const char *str, int len){ + if(!txrdy) return LINE_BUSY; + if(len > UARTBUFSZ) return STR_TOO_LONG; + txrdy = 0; + bufovr = 0; + memcpy(tbuf, str, len); + DMA1_CCR4 &= ~DMA_CCR_EN; + DMA1_CNDTR4 = len; + DMA1_CCR4 |= DMA_CCR_EN; // start transmission + return ALL_OK; +} + +TXstatus usart2_send_blocking(const char *str, int len){ + if(!txrdy) return LINE_BUSY; + int i; + bufovr = 0; + for(i = 0; i < len; ++i){ + USART2_TDR = *str++; + while(!(USART2_ISR & USART_ISR_TXE)); + } + txrdy = 1; + return ALL_OK; +} + + +// Nucleo's USART2 connected to VCP proxy of st-link +void usart2_setup(){ + // setup pins: PA2 (Tx - AF1), PA15 (Rx - AF1) + RCC_AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_DMAEN; + //RCC_APB2ENR |= RCC_APB2ENR_SYSCFGEN; // turn on syscfg + // AF mode + gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2|GPIO15); + gpio_set_af(GPIOA, GPIO_AF1, GPIO2|GPIO15); + // DMA: Tx - Ch4 + DMA1_CPAR4 = (uint32_t) &USART2_TDR; // periph + DMA1_CMAR4 = (uint32_t) tbuf; // mem + DMA1_CCR4 |= DMA_CCR_MINC | DMA_CCR_DIR | DMA_CCR_TCIE; // 8bit, mem++, mem->per, transcompl irq + // Tx CNDTR set @ each transmission due to data size + // DMA: Rx - Ch5 + DMA1_CPAR5 = (uint32_t) &USART2_RDR; + DMA1_CMAR5 = (uint32_t) rbuf[rbufno]; + DMA1_CNDTR5 = UARTBUFSZ; + DMA1_CCR5 |= DMA_CCR_MINC | DMA_CCR_TCIE | DMA_CCR_EN; // 8bit, mem++, per->mem, transcompl irq, enable receiver + nvic_set_priority(NVIC_DMA1_CHANNEL4_5_IRQ, 3); + nvic_enable_irq(NVIC_DMA1_CHANNEL4_5_IRQ); + nvic_set_priority(NVIC_USART2_IRQ, 0); + // setup usart2 + RCC_APB1ENR |= RCC_APB1ENR_USART2EN; // clock + USART2_CR2 = USART_CR2_ADD_VAL('\n'); // set newline as 'character match' interrupt + // oversampling by16, 115200bps (fck=48mHz) + //USART2_BRR = 0x1a1; // 48000000 / 115200 + USART2_BRR = 480000 / 1152; + USART2_CR3 = USART_CR3_DMAR | USART_CR3_DMAT; // enable DMA Tx/Rx + while(!(USART2_ISR & USART_ISR_TC)); // polling idle frame Transmission + USART2_ICR |= USART_ICR_TCCF; // clear TC flag + USART2_CR1 = USART_CR1_CMIE | USART_CR1_TE | USART_CR1_RE | USART_CR1_UE; // 1start,8data,nstop; enable Rx,Tx,USART, CM interrupt + nvic_enable_irq(NVIC_USART2_IRQ); +} + + +void dma1_channel4_5_isr(){ + if(DMA1_ISR & DMA_ISR_TCIF4){ // Tx + DMA1_IFCR |= DMA_IFCR_CIF4; // clear TC flag + txrdy = 1; + } + if(DMA1_ISR & DMA_ISR_TCIF5){ // Rx + DMA1_IFCR |= DMA_IFCR_CIF5; // clear TC flag + DMA1_CCR5 &= ~DMA_CCR_EN; // turn off Rx DMA to refresh CNDTR + DMA1_CNDTR5 = UARTBUFSZ; + DMA1_CCR5 |= DMA_CCR_EN; + bufovr = 1; // set buffer overrun flag: Rx buffer full, but no '\n' met! + } +} + +void usart2_isr(){ + if(USART2_ISR & USART_ISR_CMF){ // character match + USART2_ICR |= USART_ISR_CMF; // clear flag + linerdy = UARTBUFSZ - DMA1_CNDTR5 - 1; // set 'line ready' flag to data lenght + recvdata = rbuf[rbufno]; + recvdata[linerdy] = 0; + rbufno = !rbufno; + // reload DMA Rx + DMA1_CCR5 &= ~DMA_CCR_EN; + DMA1_CMAR5 = (uint32_t) rbuf[rbufno]; + DMA1_CNDTR5 = UARTBUFSZ; + DMA1_CCR5 |= DMA_CCR_EN; + } +} diff --git a/F0/uart/usart.h b/F0/uart/usart.h new file mode 100644 index 0000000..1ab4b50 --- /dev/null +++ b/F0/uart/usart.h @@ -0,0 +1,43 @@ +/* + * usart.h + * + * Copyright 2017 Edward V. Emelianoff + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ +#pragma once +#ifndef __USART_H__ +#define __USART_H__ + +#define UARTBUFSZ (128) + +typedef enum{ + ALL_OK, + LINE_BUSY, + STR_TOO_LONG +} TXstatus; + +#define usart2rx() (linerdy) +#define usart2ovr() (bufovr) + +extern int linerdy, bufovr, txrdy; + +void usart2_setup(); +int usart2_getline(char **line); +TXstatus usart2_send(const char *str, int len); +TXstatus usart2_send_blocking(const char *str, int len); + +#endif // __USART_H__