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

146
F3:F303/usart1/Makefile Normal file
View File

@@ -0,0 +1,146 @@
BINARY = usart1
BOOTPORT ?= /dev/ttyUSB0
BOOTSPEED ?= 115200
# MCU FAMILY
FAMILY ?= F3
# MCU code
MCU ?= F303xb
# or __ARM_ARCH_7EM__
ARMARCH = __ARM_ARCH_7M__
# change this linking script depending on particular MCU model,
LDSCRIPT ?= stm32f303xB.ld
# debug
#DEFS = -DEBUG
INDEPENDENT_HEADERS=
FP_FLAGS ?= -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant
ASM_FLAGS ?= -mthumb -mcpu=cortex-m4
ARCH_FLAGS = $(ASM_FLAGS) $(FP_FLAGS) -D $(ARMARCH)
###############################################################################
# Executables
#PREFIX ?= arm-none-eabi
# gcc from arm web site
PREFIX ?= /opt/bin/arm-none-eabi
TOOLCHLIB ?= /opt/arm-none-eabi/lib
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
MAP := $(OBJDIR)/$(BINARY).map
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 += -g -gdwarf-2
CFLAGS += -O2 -D__thumb2__=1 -MD
CFLAGS += -Wall -Werror -Wextra -Wshadow
CFLAGS += -fshort-enums -ffunction-sections -fdata-sections
#CFLAGS += -fno-common -ffunction-sections -fdata-sections -fno-stack-protector
CFLAGS += $(ARCH_FLAGS)
###############################################################################
# Linker flags
#LDFLAGS += -nostartfiles --static -nostdlib -specs=nosys.specs -specs=nano.specs
LDFLAGS += $(ARCH_FLAGS)
LDFLAGS += -specs=nosys.specs -specs=nano.specs
LDFLAGS += -L$(LIB_DIR)
#LDFLAGS += -L$(TOOLCHLIB)
LDFLAGS += -T$(LDSCRIPT)
LDFLAGS += -Wl,-Map=$(MAP),--cref -Wl,--gc-sections
###############################################################################
# Used libraries
#LDLIBS += -lc $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)
DEFS += -DSTM32$(FAMILY) -DSTM32$(MCU)
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) -o $@ -c $<
$(OBJDIR)/%.o: %.c
@echo " CC $<"
$(CC) $(CFLAGS) $(DEFS) $(INCLUDE) -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)"
$(CC) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $(ELF)
size: $(ELF)
$(SIZE) $(ELF)
clean:
@echo " CLEAN"
$(RM) $(OBJS) $(DEPS) $(ELF) $(HEX) $(LIST) $(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)
dfuboot: $(BIN)
@echo " LOAD $(BIN) THROUGH DFU"
$(DFUUTIL) -a0 -D $(BIN) -s 0x08000000
.PHONY: clean flash boot

32
F3:F303/usart1/hardware.c Normal file
View File

@@ -0,0 +1,32 @@
/*
* This file is part of the F303usart 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 "hardware.h"
#include "usart.h"
static inline void gpio_setup(){
RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN; // for USART and LEDs
for(int i = 0; i < 10000; ++i) nop();
GPIOB->MODER = GPIO_MODER_MODER0_O | GPIO_MODER_MODER1_O;
GPIOB->ODR = 1;
}
void hw_setup(){
gpio_setup();
}

27
F3:F303/usart1/hardware.h Normal file
View File

@@ -0,0 +1,27 @@
/*
* This file is part of the F303usart 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 __HARDWARE_H__
#define __HARDWARE_H__
#include "stm32f3.h"
void hw_setup();
#endif // __HARDWARE_H__

52
F3:F303/usart1/main.c Normal file
View File

@@ -0,0 +1,52 @@
/*
* This file is part of the F303usart 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 "stm32f3.h"
#include "hardware.h"
#include "usart.h"
volatile uint32_t Tms = 0;
void sys_tick_handler(void){
++Tms;
}
int main(void){
if(!StartHSE()) StartHSI();
SysTick_Config((uint32_t)72000); // 1ms
hw_setup();
usart_setup();
uint32_t ctr = Tms;
while(1){
if(Tms - ctr > 499){
ctr = Tms;
pin_toggle(GPIOB, 1 << 1 | 1 << 0); // toggle LED @ PB0
}
if(bufovr){
bufovr = 0;
usart_send("bufovr\n");
}
char *txt = NULL;
if(usart_getline(&txt)){
usart_send("Get by USART1: ");
usart_send(txt);
usart_putchar('\n');
}
}
}

106
F3:F303/usart1/usart.c Normal file
View File

@@ -0,0 +1,106 @@
/*
* This file is part of the F303usart 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 "stm32f3.h"
#include "hardware.h"
#include "usart.h"
#include <string.h>
static volatile int idatalen = 0; // received data line length (including '\n')
volatile int linerdy = 0, // received data ready
dlen = 0, // length of data (including '\n') in current buffer
bufovr = 0; // input buffer overfull
static volatile int rbufno = 0; // current rbuf number
static char rbuf[2][UARTBUFSZ]; // receive buffers
static volatile char *recvdata = NULL;
/**
* return length of received data (without trailing zero)
*/
int usart_getline(char **line){
if(bufovr){
bufovr = 0;
linerdy = 0;
return 0;
}
if(!linerdy) return 0;
*line = (char*)recvdata;
linerdy = 0;
return dlen;
}
void usart_putchar(const char ch){
while(!(USART1->ISR & USART_ISR_TXE));
USART1->TDR = ch;
}
void usart_sendn(const char *str, int L){
if(!str) return;
for(int i = 0; i < L; ++i){
usart_putchar(str[i]);
}
}
void usart_send(const char *str){
if(!str) return;
int L = 0;
const char *ptr = str;
while(*ptr++) ++L;
usart_sendn(str, L);
}
void usart_setup(){
// USART1: Rx - PA10 (AF7), Tx - PA9 (AF7)
// setup pins:
GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER9 | GPIO_MODER_MODER10)) |
GPIO_MODER_MODER9_AF | GPIO_MODER_MODER10_AF;
GPIOA->AFR[1] = (GPIOA->AFR[1] & ~(GPIO_AFRH_AFRH1 | GPIO_AFRH_AFRH2)) |
7 << (1 * 4) | 7 << (2 * 4); // PA9, PA10
// clock
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
USART1->ICR = 0xffffffff; // clear all flags
USART1->BRR = 720000 / 1152;
USART1->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE | USART_CR1_RXNEIE; // 1start,8data,nstop; enable Rx,Tx,USART
uint32_t tmout = 16000000;
while(!(USART1->ISR & USART_ISR_TC)){if(--tmout == 0) break;} // polling idle frame Transmission
USART1->ICR = 0xffffffff; // clear all flags again
NVIC_SetPriority(USART1_IRQn, 0);
NVIC_EnableIRQ(USART1_IRQn);
}
void usart1_exti25_isr(){
if(USART1->ISR & USART_ISR_RXNE){ // RX not emty - receive next char
// read RDR clears flag
uint8_t rb = USART1->RDR;
if(idatalen < UARTBUFSZ){ // put next char into buf
rbuf[rbufno][idatalen++] = rb;
if(rb == '\n'){ // got newline - line ready
linerdy = 1;
dlen = idatalen;
recvdata = rbuf[rbufno];
recvdata[dlen-1] = 0;
// prepare other buffer
rbufno = !rbufno;
idatalen = 0;
}
}else{ // buffer overrun
bufovr = 1;
idatalen = 0;
}
}
}

36
F3:F303/usart1/usart.h Normal file
View File

@@ -0,0 +1,36 @@
/*
* This file is part of the F303usart 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 __USART_H__
#define __USART_H__
#include "hardware.h"
// input buffers size
#define UARTBUFSZ (80)
extern volatile int linerdy, bufovr;
void usart_setup();
int usart_getline(char **line);
void usart_send(const char *str);
void usart_sendn(const char *str, int L);
void usart_putchar(const char ch);
#endif // __USART_H__

BIN
F3:F303/usart1/usart1.bin Normal file

Binary file not shown.