add SSI->SPI on NUCLEO042

This commit is contained in:
2022-07-08 14:48:53 +03:00
parent 6f29257a28
commit 52236f2cf1
20 changed files with 8112 additions and 4 deletions

View File

@@ -0,0 +1,137 @@
BINARY = usart
BOOTPORT ?= /dev/ttyUSB0
BOOTSPEED ?= 115200
# MCU FAMILY
FAMILY = F0
# MCU code
MCU = F042x6
DEFS += -DEBUG
# change this linking script depending on particular MCU model,
# for example, if you have STM32F103VBT6, you should write:
LDSCRIPT = stm32f042k.ld
INDEPENDENT_HEADERS=
FP_FLAGS ?= -msoft-float
ASM_FLAGS = -mthumb -mcpu=cortex-m0 -march=armv6-m -mtune=cortex-m0
ARCH_FLAGS = $(ASM_FLAGS) $(FP_FLAGS)
###############################################################################
# Executables
PREFIX ?= /opt/bin/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
OBJDIR = mk
LDSCRIPT ?= $(BINARY).ld
SRC := $(wildcard *.c)
OBJS := $(addprefix $(OBJDIR)/, $(SRC:%.c=%.o))
STARTUP = $(OBJDIR)/startup.o
OBJS += $(STARTUP)
DEPS := $(OBJS:.o=.d)
INC_DIR ?= ../inc
INCLUDE := -I$(INC_DIR)/F0 -I$(INC_DIR)/cm
LIB_DIR := $(INC_DIR)/ld
###############################################################################
# C flags
CFLAGS += -O2 -g -MD -D__thumb2__=1
CFLAGS += -Wall -Werror -Wextra -Wshadow -Wimplicit-function-declaration
CFLAGS += -Wredundant-decls $(INCLUDE)
# -Wmissing-prototypes -Wstrict-prototypes
CFLAGS += -fno-common -ffunction-sections -fdata-sections
###############################################################################
# Linker flags
LDFLAGS += --static -nostartfiles
#--specs=nano.specs
LDFLAGS += -L$(LIB_DIR)
LDFLAGS += -T$(LDSCRIPT)
LDFLAGS += -Wl,-Map=$(OBJDIR)/$(BINARY).map
LDFLAGS += -Wl,--gc-sections
###############################################################################
# Used libraries
LDLIBS += -Wl,--start-group -lc -lgcc -Wl,--end-group
LDLIBS += $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)
DEFS += -DSTM32$(FAMILY) -DSTM32$(MCU)
#.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: bin list
elf: $(ELF)
bin: $(BIN)
hex: $(HEX)
list: $(LIST)
ifneq ($(MAKECMDGOALS),clean)
-include $(DEPS)
endif
$(OBJDIR):
mkdir $(OBJDIR)
$(STARTUP): $(INC_DIR)/startup/vector.c
$(CC) $(CFLAGS) $(DEFS) $(INCLUDE) $(ARCH_FLAGS) -o $@ -c $<
$(OBJDIR)/%.o: %.c
@echo " CC $<"
$(CC) $(CFLAGS) $(DEFS) $(INCLUDE) $(ARCH_FLAGS) -o $@ -c $<
#$(OBJDIR)/%.d: %.c $(OBJDIR)
# $(CC) -MM -MG $< | sed -e 's,^\([^:]*\)\.o[ ]*:,$(@D)/\1.o $(@D)/\1.d:,' >$@
$(BIN): $(ELF)
@echo " OBJCOPY $(BIN)"
$(OBJCOPY) -Obinary $(ELF) $(BIN)
$(HEX): $(ELF)
@echo " OBJCOPY $(HEX)"
$(OBJCOPY) -Oihex $(ELF) $(HEX)
$(LIST): $(ELF)
@echo " OBJDUMP $(LIST)"
$(OBJDUMP) -S $(ELF) > $(LIST)
$(ELF): $(OBJDIR) $(OBJS)
@echo " LD $(ELF)"
$(LD) $(LDFLAGS) $(ARCH_FLAGS) $(OBJS) $(LDLIBS) -o $(ELF)
clean:
@echo " CLEAN"
$(RM) $(OBJS) $(DEPS) $(ELF) $(HEX) $(LIST) $(OBJDIR)/*.map
@rmdir $(OBJDIR) 2>/dev/null || true
flash: $(BIN)
@echo " FLASH $(BIN)"
$(STFLASH) write $(BIN) 0x8000000
boot: $(BIN)
@echo " LOAD $(BIN) through bootloader"
$(STBOOT) -b$(BOOTSPEED) $(BOOTPORT) -w $(BIN)
.PHONY: clean flash boot

View File

@@ -0,0 +1,9 @@
This is a simple USART over DMA application with software end of string control.
Speed 115200. Received string echoes back.
run
DEFS=-DCHECK_TMOUT make
to add timeout 1.5s for each letter
DEFS="-DCHECK_TMOUT -DTIMEOUT_MS=xx" make
to make another timeout

View File

@@ -0,0 +1,101 @@
/*
* 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 "spi.h"
#include "usart.h"
volatile uint32_t Tms = 0;
/* Called when systick fires */
void sys_tick_handler(void){
++Tms;
}
static void gpio_setup(void){
// Set green led (PB3) as output - no, PB3 is SCK!
//RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
//GPIOB->MODER = GPIO_MODER_MODER3_O;
}
static void printuhex(uint8_t *arr, uint8_t l){
if(l > 32 || l == 0) return;
char buf[70] = "0x";
int8_t i, j, bidx = 2;
for(i = 0; i < l; ++i, ++arr){
for(j = 1; j > -1; --j){
uint8_t half = (*arr >> (4*j)) & 0x0f;
if(half < 10) buf[bidx++] = half + '0';
else buf[bidx++] = half - 10 + 'a';
}
}
buf[bidx++] = '\n';
SEND(buf, bidx);
}
static void print14bit(uint8_t *arr){
uint8_t _16[2];
_16[0] = (arr[0]>>4)&3; _16[1] = (arr[0]<<4) | (arr[1] >> 4);
printuhex(_16, 2);
//SEND("\n\n", 2);
}
int main(void){
uint32_t /*lastT = 0,*/ dctr = 0;
int16_t L = 0;
uint8_t buf[SPIBUFSZ], len;
char *txt;
sysreset();
SysTick_Config(6000, 1);
gpio_setup();
spi_setup();
usart2_setup();
while (1){
/* if(Tms - lastT > 499){
pin_toggle(GPIOB, 1<<3); // blink by onboard LED once per second
lastT = Tms;
}*/
if(usart2rx()){ // usart1 received data, store in in buffer
L = usart2_getline(&txt);
// do something with received data
}
if(L){ // text waits for sending
if(ALL_OK == usart2_send(txt, L)){
L = 0;
}
}
len = SPIBUFSZ;
uint8_t a = SPI_getdata(buf, &len);
if(a){
//printuhex(buf, len);
print14bit(buf);
}
if(Tms - dctr > 999){// once per 1 second
dctr = Tms;
//SEND("pre\n", 4);
SPI_prep_receive();
}
}
return 0;
}

View File

@@ -0,0 +1,119 @@
/*
* This file is part of the F0testbrd project.
* Copyright 2021 Edward V. Emelianov <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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <string.h> // memcpy
#include "usart.h"
#include "spi.h"
// buffers for DMA rx/tx
static uint8_t inbuff[SPIBUFSZ], outbuff[SPIBUFSZ];
static uint8_t rxrdy = 0;
// SPI1 (AF0): PB3 - SCK, PB4 - MISO, PB5 - MOSI; RxDMA - ch2
void spi_setup(){
RCC->AHBENR |= RCC_AHBENR_GPIOBEN; // uncomment in common case
/* (1) Select AF mode on pins */
/* (2) AF0 for SPI1 signals */
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER4)) |
GPIO_MODER_MODER3_AF | GPIO_MODER_MODER4_AF; /* (1) */
GPIOB->AFR[0] = (GPIOB->AFR[0] & ~(GPIO_AFRL_AFRL3 | GPIO_AFRL_AFRL4)); /* (2) */
// enable clocking
RCC->AHBENR |= RCC_AHBENR_DMA1EN;
RCC->APB2ENR |= RCC_APB2ENR_SPI1EN;
// Configure DMA SPI
/* SPI_RX DMA config */
/* (1) Peripheral address */
/* (2) Memory address */
/* (3) Data size */
/* (4) Memory increment */
/* Peripheral to memory */
/* 8-bit transfer */
/* Overflow IR */
DMA1_Channel2->CCR &= ~DMA_CCR_EN;
DMA1_Channel2->CPAR = (uint32_t)&(SPI1->DR); /* (1) */
DMA1_Channel2->CMAR = (uint32_t)inbuff; /* (2) */
DMA1_Channel2->CNDTR = SPIBUFSZ; /* (3) */
DMA1_Channel2->CCR |= DMA_CCR_MINC | DMA_CCR_EN; /* (4) */
/* (5) Peripheral address */
/* (6) Memory address */
/* (7) Memory increment */
/* Memory to peripheral*/
/* 8-bit transfer */
/* Transfer complete IT */
DMA1_Channel3->CCR &= ~DMA_CCR_EN;
DMA1_Channel3->CPAR = (uint32_t)&(SPI1->DR); /* (5) */
DMA1_Channel3->CMAR = (uint32_t)outbuff; /* (6) */
DMA1_Channel3->CCR |= DMA_CCR_MINC | DMA_CCR_TCIE | DMA_CCR_DIR; /* (7) */
NVIC_SetPriority(DMA1_Channel2_3_IRQn, 0);
NVIC_EnableIRQ(DMA1_Channel2_3_IRQn);
/* Configure SPI */
/* (1) Master selection, BR: Fpclk/256 CPOL and CPHA at zero (rising first edge) */
/* (1a) software slave management (SSI inactive) */
/* (2) TX and RX with DMA, 8-bit Rx fifo */
/* (3) Enable SPI */
SPI1->CR1 = SPI_CR1_MSTR | SPI_CR1_BR | SPI_CR1_SSM | SPI_CR1_SSI; /* (1) */
SPI1->CR2 = SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN | SPI_CR2_FRXTH | SPI_CR2_DS_2 | SPI_CR2_DS_1 | SPI_CR2_DS_0; /* (2) */
SPI1->CR1 |= SPI_CR1_SPE; /* (3) */
SPI_prep_receive();
}
// prepare to receive data
void SPI_prep_receive(){
DMA1_Channel3->CCR &= ~DMA_CCR_EN;
DMA1_Channel2->CCR &= ~DMA_CCR_EN;
rxrdy = 0;
(void)SPI1->DR; // read DR and SR to clear OVR flag
(void)SPI1->SR;
DMA1_Channel2->CNDTR = SPIBUFSZ;
DMA1_Channel3->CNDTR = SPIBUFSZ;
DMA1_Channel2->CCR |= DMA_CCR_EN;
DMA1_Channel3->CCR |= DMA_CCR_EN;
}
/**
* @brief SPI_getdata - get data received by DMA & reload receiver
* @param buf - buffer for data (with length maxlen) or NULL
* @param maxlen - (I) - amount of received bytes (or 0 if buffer is empty),
* (O) - amount of real bytes amount in buffer (could be > maxlen if maxlen < SPIBUFSZ)
* @return 1 if got data
*/
uint8_t SPI_getdata(uint8_t *buf, uint8_t *maxlen){
if(!rxrdy) return 0;
//SEND("def\n", 4);
uint8_t remain = DMA1_Channel2->CNDTR;
if(remain) return 0;
rxrdy = 0;
if(maxlen){
if(buf && *maxlen) memcpy(buf, inbuff, *maxlen);
*maxlen = SPIBUFSZ; // bytes received
}
return 1;
}
void dma1_channel2_3_isr(){
if(DMA1->ISR & DMA_ISR_TCIF3){
DMA1->IFCR |= DMA_IFCR_CTCIF3;
DMA1_Channel3->CCR &= ~DMA_CCR_EN;
rxrdy = 1;
}
if(DMA1->ISR & DMA_ISR_TEIF2){ // receiver overflow
DMA1->IFCR |= DMA_IFCR_CTEIF2;
DMA1_Channel2->CCR &= ~DMA_CCR_EN;
}
}

View File

@@ -0,0 +1,41 @@
/*
* This file is part of the F0testbrd project.
* Copyright 2021 Edward V. Emelianov <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 3 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, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef SPI_H__
#define SPI_H__
#include <stm32f0.h>
// 5 bytes == 40 bits
#define SPIBUFSZ (2)
typedef enum{
SPI_NOTREADY,
SPI_READY,
SPI_BUSY
} spiStatus;
extern spiStatus SPI_status[];
extern volatile uint8_t SPIoverfl[];
void spi_setup();
void SPI_prep_receive();
uint8_t SPI_getdata(uint8_t *buf, uint8_t *maxlen);
#endif // SPI_H__

Binary file not shown.

View File

@@ -0,0 +1,153 @@
/*
* 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 <string.h>
#include "usart.h"
extern volatile uint32_t Tms;
static int datalen[2] = {0,0}; // received data line length (including '\n')
int linerdy = 0, // received data ready
dlen = 0, // length of data (including '\n') in current buffer
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;
}
*line = recvdata;
linerdy = 0;
return dlen;
}
TXstatus usart2_send(const char *str, int len){
if(!txrdy) return LINE_BUSY;
if(len > UARTBUFSZ) return STR_TOO_LONG;
txrdy = 0;
memcpy(tbuf, str, len);
DMA1_Channel4->CCR &= ~DMA_CCR_EN;
DMA1_Channel4->CNDTR = len;
DMA1_Channel4->CCR |= 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;
// AF mode (AF1)
GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER2|GPIO_MODER_MODER15))\
| (GPIO_MODER_MODER2_1 | GPIO_MODER_MODER15_1);
GPIOA->AFR[0] = (GPIOA->AFR[0] &~GPIO_AFRH_AFRH2) | 1 << (2 * 4); // PA2
GPIOA->AFR[1] = (GPIOA->AFR[1] &~GPIO_AFRH_AFRH7) | 1 << (7 * 4); // PA15
// DMA: Tx - Ch4
DMA1_Channel4->CPAR = (uint32_t) &USART2->TDR; // periph
DMA1_Channel4->CMAR = (uint32_t) tbuf; // mem
DMA1_Channel4->CCR |= DMA_CCR_MINC | DMA_CCR_DIR | DMA_CCR_TCIE; // 8bit, mem++, mem->per, transcompl irq
// Tx CNDTR set @ each transmission due to data size
NVIC_SetPriority(DMA1_Channel4_5_IRQn, 3);
NVIC_EnableIRQ(DMA1_Channel4_5_IRQn);
NVIC_SetPriority(USART2_IRQn, 0);
// setup usart2
RCC->APB1ENR |= RCC_APB1ENR_USART2EN; // clock
// oversampling by16, 115200bps (fck=48mHz)
//USART2_BRR = 0x1a1; // 48000000 / 115200
USART2->BRR = 480000 / 1152;
USART2->CR3 = USART_CR3_DMAT; // enable DMA Tx
USART2->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE; // 1start,8data,nstop; enable Rx,Tx,USART
while(!(USART2->ISR & USART_ISR_TC)); // polling idle frame Transmission
USART2->ICR |= USART_ICR_TCCF; // clear TC flag
USART2->CR1 |= USART_CR1_RXNEIE;
NVIC_EnableIRQ(USART2_IRQn);
}
void dma1_channel4_5_isr(){
if(DMA1->ISR & DMA_ISR_TCIF4){ // Tx
DMA1->IFCR |= DMA_IFCR_CTCIF4; // clear TC flag
txrdy = 1;
}
}
void usart2_isr(){
#ifdef CHECK_TMOUT
static uint32_t tmout = 0;
#endif
if(USART2->ISR & USART_ISR_RXNE){ // RX not emty - receive next char
#ifdef CHECK_TMOUT
if(tmout && Tms >= tmout){ // set overflow flag
bufovr = 1;
datalen[rbufno] = 0;
}
tmout = Tms + TIMEOUT_MS;
if(!tmout) tmout = 1; // prevent 0
#endif
// read RDR clears flag
uint8_t rb = USART2->RDR;
if(datalen[rbufno] < UARTBUFSZ){ // put next char into buf
rbuf[rbufno][datalen[rbufno]++] = rb;
if(rb == '\n'){ // got newline - line ready
linerdy = 1;
dlen = datalen[rbufno];
recvdata = rbuf[rbufno];
// prepare other buffer
rbufno = !rbufno;
datalen[rbufno] = 0;
#ifdef CHECK_TMOUT
// clear timeout at line end
tmout = 0;
#endif
}
}else{ // buffer overrun
bufovr = 1;
datalen[rbufno] = 0;
#ifdef CHECK_TMOUT
tmout = 0;
#endif
}
}
}

View File

@@ -0,0 +1,50 @@
/*
* 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__
// input and output buffers size
#define UARTBUFSZ (64)
// timeout between data bytes
#ifndef TIMEOUT_MS
#define TIMEOUT_MS (1500)
#endif
typedef enum{
ALL_OK,
LINE_BUSY,
STR_TOO_LONG
} TXstatus;
#define SEND(s, l) do{}while(ALL_OK != usart2_send(s, l))
#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__