restructuring

This commit is contained in:
2022-03-10 11:04:14 +03:00
parent 29560b7c0c
commit 733dbd75d2
1758 changed files with 14 additions and 26855 deletions

137
F1:F103/uart/Makefile Normal file
View File

@@ -0,0 +1,137 @@
BINARY = uart
BOOTPORT ?= /dev/ttyUSB0
BOOTSPEED ?= 115200
# MCU FAMILY
FAMILY ?= F1
# MCU code
MCU ?= F103xB
# density (stm32f10x.h, lines 70-84)
DENSITY ?= MD
# change this linking script depending on particular MCU model,
LDSCRIPT ?= stm32f103xB.ld
# debug
DEFS = -DEBUG
INDEPENDENT_HEADERS=
FP_FLAGS ?= -msoft-float
ASM_FLAGS ?= -mthumb -mcpu=cortex-m3
ARCH_FLAGS = $(ASM_FLAGS) $(FP_FLAGS)
###############################################################################
# Executables
#PREFIX ?= arm-none-eabi
# gcc from arm web site
PREFIX ?= /opt/bin/arm-none-eabi
RM := rm -f
RMDIR := rmdir
CC := $(PREFIX)-gcc
# don't replace ld with gcc: the binary size would be much greater!!
LD := $(PREFIX)-ld
AR := $(PREFIX)-ar
AS := $(PREFIX)-as
SIZE := $(PREFIX)-size
OBJCOPY := $(PREFIX)-objcopy
OBJDUMP := $(PREFIX)-objdump
GDB := $(PREFIX)-gdb
STFLASH := $(shell which st-flash)
STBOOT := $(shell which stm32flash)
DFUUTIL := $(shell which dfu-util)
###############################################################################
# Source files
OBJDIR = mk
SRC := $(wildcard *.c)
OBJS := $(addprefix $(OBJDIR)/, $(SRC:%.c=%.o))
STARTUP = $(OBJDIR)/startup.o
OBJS += $(STARTUP)
# dependencies: we need them to recompile files if their headers-dependencies changed
DEPS := $(OBJS:.o=.d)
INC_DIR ?= ../inc
INCLUDE := -I$(INC_DIR)/Fx -I$(INC_DIR)/cm
LIB_DIR := $(INC_DIR)/ld
###############################################################################
# C flags
CFLAGS += -O2 -g -D__thumb2__=1 -MD
CFLAGS += -Wall -Werror -Wextra -Wshadow
CFLAGS += -fno-common -ffunction-sections -fdata-sections
###############################################################################
# Linker flags
LDFLAGS += -nostartfiles --static
LDFLAGS += -L$(LIB_DIR)
LDFLAGS += -T$(LDSCRIPT)
###############################################################################
# Used libraries
LDLIBS += $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)
DEFS += -DSTM32$(FAMILY) -DSTM32$(MCU) -DSTM32F10X_$(DENSITY)
ELF := $(OBJDIR)/$(BINARY).elf
LIST := $(OBJDIR)/$(BINARY).list
BIN := $(BINARY).bin
HEX := $(BINARY).hex
all: bin list size
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 $<
$(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) $(OBJS) $(LDLIBS) -o $(ELF)
size: $(ELF)
$(SIZE) $(ELF)
clean:
@echo " CLEAN"
$(RM) $(OBJS) $(DEPS) $(ELF) $(HEX) $(LIST)
@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)
dfuboot: $(BIN)
@echo " LOAD $(BIN) THROUGH DFU"
$(DFUUTIL) -a0 -D $(BIN) -s 0x08000000
.PHONY: clean flash boot

4
F1:F103/uart/README Normal file
View File

@@ -0,0 +1,4 @@
Toggle LED PB9 on STM32F103 development board once per second
Allow to turn on/off LED PB8 depending on user commands over USART (0/1) and check its state (s)
Show information in USART console about S2/S3 buttons pressed
USART: 115200 8n1

195
F1:F103/uart/main.c Normal file
View File

@@ -0,0 +1,195 @@
/*
* main.c
*
* Copyright 2018 Edward V. Emelianov <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 "stm32f1.h"
#include "usart.h"
// debounce pause (ms - 1)
#define DEBOUNCE_PAUSE (9)
static volatile uint32_t Tms = 0; // milliseconds from last reset
// Called when systick fires
void sys_tick_handler(void){
++Tms;
}
// setup
static void gpio_setup(void){
// Enable clocks to the GPIO subsystems
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN | RCC_APB2ENR_IOPCEN;
// Set leds (PB8/PB9) as opendrain output
GPIOB->CRH = CRH(8, CNF_ODOUTPUT|MODE_SLOW) | CRH(9, CNF_ODOUTPUT|MODE_SLOW);
// Set buttons (PC0/1) as inputs with weak pullups
GPIOC->ODR = 3; // pullups for PC0/1
GPIOC->CRL = CRL(0, CNF_PUDINPUT|MODE_INPUT) | CRL(1, CNF_PUDINPUT|MODE_INPUT);
}
typedef enum{
BTN_PRESSED
,BTN_RELEASED
,BTN_DEBOUNCE
,BTN_RELAX
} button_state;
button_state check_state(uint8_t N){
static uint32_t tlast[2] = {0,0};
static button_state oldstate[2] = {BTN_RELEASED, BTN_RELEASED};
button_state s = BTN_RELAX;
uint8_t b = pin_read(GPIOC, N+1); // == 1 if button released
if(b){
switch(oldstate[N]){
case BTN_PRESSED: // debounce pause for DEBOUNCE_PAUSE ms
s = oldstate[N] = BTN_DEBOUNCE;
tlast[N] = Tms;
break;
case BTN_DEBOUNCE: // check debounce pause
if(Tms - tlast[N] > DEBOUNCE_PAUSE){
s = oldstate[N] = BTN_RELEASED;
}
break;
default: // BTN_RELEASED - do nothing -> return BTN_RELAX
;
}
}else{
switch(oldstate[N]){
case BTN_RELEASED:
s = BTN_PRESSED; // button was just pressed
__attribute__((fallthrough)); // change oldstate too
case BTN_DEBOUNCE: // change old state to BTN_PRESSED again
oldstate[N] = BTN_PRESSED; // returned state still relax
break;
default: // BTN_PRESSED before - do nothing
;
}
}
return s;
}
int main(){
int L;
char *txt;
sysreset();
StartHSE();
gpio_setup();
usart_setup();
uint32_t Told = 0;
SysTick_Config(72000); // interrupt each ms
SEND("Greetings!"); newline();
pin_set(GPIOB, 3<<8); // turn off LEDs
/* Do nothing in main loop */
while (1){
if(Tms - Told > 499){ // blink LED1 every 1 second
pin_toggle(GPIOB, 1<<9);
Told = Tms;
transmit_tbuf();
}
if(usartrx()){ // usart1 received data, store in in buffer
L = usart_getline(&txt);
char _1st = txt[0];
if(L == 2 && txt[1] == '\n'){
L = 0;
switch(_1st){
case '0': // turn off LED2
SEND("LED2 off");
pin_set(GPIOB, 1<<8);
newline();
break;
case '1': // turn it on
SEND("LED2 on");
pin_clear(GPIOB, 1<<8);
newline();
break;
case 's':
case 'S':
SEND("LED2 state: ");
if(pin_read(GPIOB, 1<<8)) SEND("off");
else SEND("on");
newline();
break;
default: // help
SEND(
"'0' - turn off LED2\n"
"'1' - turn on LED2\n"
"'s' - test LED2 state\n"
);
break;
}
}
transmit_tbuf();
}
if(L){ // echo all other data
txt[L] = 0;
usart_send(txt);
transmit_tbuf();
L = 0;
}
for(int i = 0; i < 2; ++i){
const char *st = NULL;
switch(check_state(i)){
case BTN_PRESSED:
st = "pressed";
break;
case BTN_RELEASED:
st = "released";
break;
default:
; // nothing
}
if(st){
SEND("The button");
usart_putchar('2' + i);
SEND(" was ");
SEND(st);
newline();
}
}
/*
uint8_t
if(pin_read(GPIOC, 1) && pin_read(GPIOC, 2)){ // no buttons present - morze @ LED1 (PB9)
if(oldctr != blink_ctr){
uint32_t T = blink_ctr % 18;
systick_setup(L[T]);
if(T & 1) pin_set(GPIOB, 1<<9);
else pin_clear(GPIOB, 1<<9);
oldctr = blink_ctr;
}
}else{ // button pressed: turn ON given LED
if(pin_read(GPIOC, 1) == 0){ // PC0 pressed (button S2)
systick_setup(5);
if(blink_ctr - oldctr > 499){
pin_toggle(GPIOB, 1<<9);
oldctr = blink_ctr;
}
}else pin_set(GPIOB, 1<<9);
if(pin_read(GPIOC, 2) == 0){ // PC1 pressed (button S3)
systick_setup(1);
if(blink_ctr - oldctr > 499){
pin_toggle(GPIOB, 1<<8);
oldctr = blink_ctr;
}
}else pin_set(GPIOB, 1<<8);
}*/
}
}

BIN
F1:F103/uart/uart.bin Normal file

Binary file not shown.

233
F1:F103/uart/usart.c Normal file
View File

@@ -0,0 +1,233 @@
/*
* usart.c
*
* Copyright 2018 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 "stm32f1.h"
#include "usart.h"
extern volatile uint32_t Tms;
static volatile int idatalen[2] = {0,0}; // received data line length (including '\n')
static volatile int odatalen[2] = {0,0};
volatile 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, tbufno = 0; // current rbuf/tbuf numbers
static char rbuf[2][UARTBUFSZI], tbuf[2][UARTBUFSZO]; // receive & transmit buffers
static char *recvdata = NULL;
/**
* return length of received data (without trailing zero)
*/
int usart_getline(char **line){
if(bufovr){
bufovr = 0;
linerdy = 0;
return 0;
}
*line = recvdata;
linerdy = 0;
return dlen;
}
// transmit current tbuf and swap buffers
void transmit_tbuf(){
uint32_t tmout = 16000000;
while(!txrdy){if(--tmout == 0) return;}; // wait for previos buffer transmission
register int l = odatalen[tbufno];
if(!l) return;
txrdy = 0;
odatalen[tbufno] = 0;
DMA1_Channel4->CCR &= ~DMA_CCR_EN;
DMA1_Channel4->CMAR = (uint32_t) tbuf[tbufno]; // mem
DMA1_Channel4->CNDTR = l;
DMA1_Channel4->CCR |= DMA_CCR_EN;
tbufno = !tbufno;
}
void usart_putchar(const char ch){
if(odatalen[tbufno] == UARTBUFSZO) transmit_tbuf();
tbuf[tbufno][odatalen[tbufno]++] = ch;
}
void usart_send(const char *str){
uint32_t x = 512;
while(*str && --x){
if(odatalen[tbufno] == UARTBUFSZO) transmit_tbuf();
tbuf[tbufno][odatalen[tbufno]++] = *str++;
}
}
void newline(){
usart_putchar('\n');
transmit_tbuf();
}
/*
* USART speed: baudrate = Fck/(USARTDIV)
* USARTDIV stored in USART->BRR
*
* for 72MHz USARTDIV=72000/f(kboud); so for 115200 USARTDIV=72000/115.2=625 -> BRR=0x271
* 9600: BRR = 7500 (0x1D4C)
*/
void usart_setup(){
uint32_t tmout = 16000000;
// PA9 - Tx, PA10 - Rx
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_USART1EN | RCC_APB2ENR_AFIOEN;
RCC->AHBENR |= RCC_AHBENR_DMA1EN;
GPIOA->CRH = CRH(9, CNF_AFPP|MODE_NORMAL) | CRH(10, CNF_FLINPUT|MODE_INPUT);
// USART1 Tx DMA - Channel4 (Rx - channel 5)
DMA1_Channel4->CPAR = (uint32_t) &USART1->DR; // periph
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_IRQn, 3);
NVIC_EnableIRQ(DMA1_Channel4_IRQn);
NVIC_SetPriority(USART1_IRQn, 0);
// setup usart1
USART1->BRR = 72000000 / 115200;
USART1->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE; // 1start,8data,nstop; enable Rx,Tx,USART
while(!(USART1->SR & USART_SR_TC)){if(--tmout == 0) break;} // polling idle frame Transmission
USART1->SR = 0; // clear flags
USART1->CR1 |= USART_CR1_RXNEIE; // allow Rx IRQ
USART1->CR3 = USART_CR3_DMAT; // enable DMA Tx
NVIC_EnableIRQ(USART1_IRQn);
}
void usart1_isr(){
#ifdef CHECK_TMOUT
static uint32_t tmout = 0;
#endif
if(USART1->SR & USART_SR_RXNE){ // RX not emty - receive next char
#ifdef CHECK_TMOUT
if(tmout && Tms >= tmout){ // set overflow flag
bufovr = 1;
idatalen[rbufno] = 0;
}
tmout = Tms + TIMEOUT_MS;
if(!tmout) tmout = 1; // prevent 0
#endif
uint8_t rb = USART1->DR;
if(idatalen[rbufno] < UARTBUFSZI){ // put next char into buf
rbuf[rbufno][idatalen[rbufno]++] = rb;
if(rb == '\n'){ // got newline - line ready
linerdy = 1;
dlen = idatalen[rbufno];
recvdata = rbuf[rbufno];
// prepare other buffer
rbufno = !rbufno;
idatalen[rbufno] = 0;
#ifdef CHECK_TMOUT
// clear timeout at line end
tmout = 0;
#endif
}
}else{ // buffer overrun
bufovr = 1;
idatalen[rbufno] = 0;
#ifdef CHECK_TMOUT
tmout = 0;
#endif
}
}
}
// print 32bit unsigned int
void printu(uint32_t val){
char bufa[11], bufb[10];
int l = 0, bpos = 0;
if(!val){
bufa[0] = '0';
l = 1;
}else{
while(val){
bufb[l++] = val % 10 + '0';
val /= 10;
}
int i;
bpos += l;
for(i = 0; i < l; ++i){
bufa[--bpos] = bufb[i];
}
}
bufa[l + bpos] = 0;
usart_send(bufa);
}
// print 32bit unsigned int as hex
void printuhex(uint32_t val){
usart_send("0x");
uint8_t *ptr = (uint8_t*)&val + 3;
int i, j;
for(i = 0; i < 4; ++i, --ptr){
for(j = 1; j > -1; --j){
register uint8_t half = (*ptr >> (4*j)) & 0x0f;
if(half < 10) usart_putchar(half + '0');
else usart_putchar(half - 10 + 'a');
}
}
}
// dump memory buffer
void hexdump(uint8_t *arr, uint16_t len){
for(uint16_t l = 0; l < len; ++l, ++arr){
for(int16_t j = 1; j > -1; --j){
register uint8_t half = (*arr >> (4*j)) & 0x0f;
if(half < 10) usart_putchar(half + '0');
else usart_putchar(half - 10 + 'a');
}
if(l % 16 == 15) usart_putchar('\n');
else if(l & 1) usart_putchar(' ');
}
}
void dma1_channel4_isr(){
if(DMA1->ISR & DMA_ISR_TCIF4){ // Tx
DMA1->IFCR = DMA_IFCR_CTCIF4; // clear TC flag
txrdy = 1;
}
}
/*
#if USARTNUM == 2
void dma1_channel4_5_isr(){
if(DMA1->ISR & DMA_ISR_TCIF4){ // Tx
DMA1->IFCR |= DMA_IFCR_CTCIF4; // clear TC flag
txrdy = 1;
}
}
// USART1
#elif USARTNUM == 1
void dma1_channel2_3_isr(){
if(DMA1->ISR & DMA_ISR_TCIF2){ // Tx
DMA1->IFCR |= DMA_IFCR_CTCIF2; // clear TC flag
txrdy = 1;
}
}
#else
#error "Not implemented"
#endif
*/

58
F1:F103/uart/usart.h Normal file
View File

@@ -0,0 +1,58 @@
/*
* usart.h
*
* Copyright 2018 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 UARTBUFSZI (16)
#define UARTBUFSZO (32)
// timeout between data bytes
#ifndef TIMEOUT_MS
#define TIMEOUT_MS (1500)
#endif
// macro for static strings
#define SEND(str) usart_send(str)
#ifdef EBUG
#define MSG(str) do{SEND(__FILE__ " (L" STR(__LINE__) "): " str);}while(0)
#else
#define MSG(str)
#endif
#define usartrx() (linerdy)
#define usartovr() (bufovr)
extern volatile int linerdy, bufovr, txrdy;
void transmit_tbuf();
void usart_setup();
int usart_getline(char **line);
void usart_send(const char *str);
void newline();
void usart_putchar(const char ch);
void printu(uint32_t val);
void printuhex(uint32_t val);
void hexdump(uint8_t *arr, uint16_t len);
#endif // __USART_H__