mirror of
https://github.com/eddyem/stm32samples.git
synced 2026-02-28 03:44:30 +03:00
restructuring
This commit is contained in:
146
F3:F303/floatPrintf/Makefile
Normal file
146
F3:F303/floatPrintf/Makefile
Normal 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=softfp -fsingle-precision-constant -mlittle-endian -DARM_MATH_CM4
|
||||
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 # debuggin symbols in listing
|
||||
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=nano.specs -specs=nosys.specs
|
||||
LDFLAGS += -L$(LIB_DIR)
|
||||
#-L$(TOOLCHLIB)
|
||||
LDFLAGS += -T$(LDSCRIPT)
|
||||
LDFLAGS += -Wl,-Map=$(MAP),--cref -Wl,--gc-sections -Wl,--print-memory-usage
|
||||
|
||||
###############################################################################
|
||||
# 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/floatPrintf/hardware.c
Normal file
32
F3:F303/floatPrintf/hardware.c
Normal 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/floatPrintf/hardware.h
Normal file
27
F3:F303/floatPrintf/hardware.h
Normal 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__
|
||||
131
F3:F303/floatPrintf/main.c
Normal file
131
F3:F303/floatPrintf/main.c
Normal file
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
|
||||
// be careful: if pow10 would be bigger you should change str[] size!
|
||||
static const float pwr10[] = {1., 10., 100., 1000., 10000.};
|
||||
static const float rounds[] = {0.5, 0.05, 0.005, 0.0005, 0.00005};
|
||||
#define P10L (sizeof(pwr10)/sizeof(uint32_t) - 1)
|
||||
static char * float2str(float x, uint8_t prec){
|
||||
if(prec > P10L) prec = P10L;
|
||||
static char str[16] = {0}; // -117.5494E-36\0 - 14 symbols max!
|
||||
char *s = str + 14; // go to end of buffer
|
||||
uint8_t minus = 0;
|
||||
if(x < 0){
|
||||
x = -x;
|
||||
minus = 1;
|
||||
}
|
||||
int pow = 0; // xxxEpow
|
||||
// now convert float to 1.xxxE3y
|
||||
while(x > 1000.f){
|
||||
x /= 1000.f;
|
||||
pow += 3;
|
||||
}
|
||||
if(x > 0.) while(x < 1.){
|
||||
x *= 1000.f;
|
||||
pow -= 3;
|
||||
}
|
||||
// print Eyy
|
||||
if(pow){
|
||||
uint8_t m = 0;
|
||||
if(pow < 0){pow = -pow; m = 1;}
|
||||
while(pow){
|
||||
register int p10 = pow/10;
|
||||
*s-- = '0' + (pow - 10*p10);
|
||||
pow = p10;
|
||||
}
|
||||
if(m) *s-- = '-';
|
||||
*s-- = 'E';
|
||||
}
|
||||
// now our number is in [1, 1000]
|
||||
uint32_t units;
|
||||
if(prec){
|
||||
units = (uint32_t) x;
|
||||
uint32_t decimals = (uint32_t)((x-units+rounds[prec])*pwr10[prec]);
|
||||
// print decimals
|
||||
while(prec){
|
||||
register int d10 = decimals / 10;
|
||||
*s-- = '0' + (decimals - 10*d10);
|
||||
decimals = d10;
|
||||
--prec;
|
||||
}
|
||||
// decimal point
|
||||
*s-- = '.';
|
||||
}else{ // without decimal part
|
||||
units = (uint32_t) (x + 0.5);
|
||||
}
|
||||
// print main units
|
||||
if(units == 0) *s-- = '0';
|
||||
else while(units){
|
||||
register uint32_t u10 = units / 10;
|
||||
*s-- = '0' + (units - 10*u10);
|
||||
units = u10;
|
||||
}
|
||||
if(minus) *s-- = '-';
|
||||
return s+1;
|
||||
}
|
||||
|
||||
static const float tests[] = {-1.23456789e-37, -3.14159265e-2, -1234.56789, -1.2345678, 0., 1e-40, 0.1234567, 123.456789, 2.473829e31};
|
||||
#define TESTN 9
|
||||
|
||||
int main(void){
|
||||
sysreset();
|
||||
if(!StartHSE()) StartHSI();
|
||||
SysTick_Config((uint32_t)72000); // 1ms
|
||||
hw_setup();
|
||||
usart_setup();
|
||||
usart_send("Start\n");
|
||||
uint32_t ctr = Tms;
|
||||
float x = 0.519, more = 5.123;
|
||||
while(1){
|
||||
if(Tms - ctr > 499){
|
||||
ctr = Tms;
|
||||
pin_toggle(GPIOB, 1 << 1 | 1 << 0); // toggle LED @ PB0
|
||||
/**/
|
||||
if((x += 0.519) > more){
|
||||
more += 5.123;
|
||||
}
|
||||
}
|
||||
if(bufovr){
|
||||
bufovr = 0;
|
||||
usart_send("bufovr\n");
|
||||
}
|
||||
char *txt = NULL;
|
||||
if(usart_getline(&txt)){
|
||||
if(*txt == '?'){
|
||||
for(int i = 0; i < TESTN; ++i) for(int j = 0; j <= (int)P10L; ++j){
|
||||
usart_send(float2str(tests[i], j));
|
||||
usart_putchar('\n');
|
||||
}
|
||||
}else{
|
||||
usart_send("Get by USART1: ");
|
||||
usart_send(txt);
|
||||
usart_putchar('\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
106
F3:F303/floatPrintf/usart.c
Normal file
106
F3:F303/floatPrintf/usart.c
Normal 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/floatPrintf/usart.h
Normal file
36
F3:F303/floatPrintf/usart.h
Normal 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/floatPrintf/usart1.bin
Normal file
BIN
F3:F303/floatPrintf/usart1.bin
Normal file
Binary file not shown.
Reference in New Issue
Block a user