add USART over DMA

This commit is contained in:
eddyem 2017-01-15 10:01:48 +03:00
parent 715bfc9304
commit 8faa004882
9 changed files with 505 additions and 1 deletions

View File

@ -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

127
F0/uart/Makefile Normal file
View File

@ -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)

2
F0/uart/Readme Normal file
View File

@ -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.

31
F0/uart/ld/stm32f042k.ld Normal file
View File

@ -0,0 +1,31 @@
/*
* This file is part of the libopencm3 project.
*
* Copyright (C) 2012 Karl Palsson <karlp@tweak.net.au>
*
* 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 <http://www.gnu.org/licenses/>.
*/
/* 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

114
F0/uart/main.c Normal file
View File

@ -0,0 +1,114 @@
/*
* main.c
*
* Copyright 2017 Edward V. Emelianoff <eddy@sao.ru, edward.emelianoff@gmail.com>
*
* 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;
}

BIN
F0/uart/uart.bin Executable file

Binary file not shown.

47
F0/uart/uart.geanypro Normal file
View File

@ -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

137
F0/uart/usart.c Normal file
View File

@ -0,0 +1,137 @@
/*us
* usart.c
*
* Copyright 2017 Edward V. Emelianoff <eddy@sao.ru, edward.emelianoff@gmail.com>
*
* 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 <string.h>
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;
}
}

43
F0/uart/usart.h Normal file
View File

@ -0,0 +1,43 @@
/*
* usart.h
*
* Copyright 2017 Edward V. Emelianoff <eddy@sao.ru, edward.emelianoff@gmail.com>
*
* 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__