diff --git a/.gitignore b/.gitignore index d56bbc9..94e81b1 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,7 @@ */mk/* .hg* .dropbox.attr +*.sublime-project +*.sublime-workspace +F1/client-term/client +*.bk diff --git a/F0-nolib/blink/Makefile b/F0-nolib/blink/Makefile index e85a52f..535bba3 100644 --- a/F0-nolib/blink/Makefile +++ b/F0-nolib/blink/Makefile @@ -8,7 +8,7 @@ MCU = F042x6 DEFS = -DEBUG # change this linking script depending on particular MCU model, # for example, if you have STM32F103VBT6, you should write: -LDSCRIPT = ld/stm32f042k.ld +LDSCRIPT = stm32f042k.ld INDEPENDENT_HEADERS= diff --git a/F0-nolib/canbus/src/Makefile b/F0-nolib/canbus/src/Makefile index 691a4d5..d95bc47 100644 --- a/F0-nolib/canbus/src/Makefile +++ b/F0-nolib/canbus/src/Makefile @@ -11,7 +11,7 @@ DEFS += -DUSARTNUM=1 DEFS += -DEBUG # change this linking script depending on particular MCU model, # for example, if you have STM32F103VBT6, you should write: -LDSCRIPT = ld/stm32f042k.ld +LDSCRIPT = stm32f042k.ld INDEPENDENT_HEADERS= diff --git a/F0-nolib/canbus/src/can.c b/F0-nolib/canbus/src/can.c index 49ff5f3..8cb6886 100644 --- a/F0-nolib/canbus/src/can.c +++ b/F0-nolib/canbus/src/can.c @@ -232,18 +232,25 @@ CAN_status can_send(uint8_t *msg, uint8_t len, uint16_t target_id){ switch(len){ case 8: hb |= (uint32_t)msg[7] << 24; + __attribute__((fallthrough)); case 7: hb |= (uint32_t)msg[6] << 16; + __attribute__((fallthrough)); case 6: hb |= (uint32_t)msg[5] << 8; + __attribute__((fallthrough)); case 5: hb |= (uint32_t)msg[4]; + __attribute__((fallthrough)); case 4: lb |= (uint32_t)msg[3] << 24; + __attribute__((fallthrough)); case 3: lb |= (uint32_t)msg[2] << 16; + __attribute__((fallthrough)); case 2: lb |= (uint32_t)msg[1] << 8; + __attribute__((fallthrough)); default: lb |= (uint32_t)msg[0]; } @@ -288,18 +295,25 @@ static void can_process_fifo(uint8_t fifo_num){ switch(len){ case 8: dat[7] = hb>>24; + __attribute__((fallthrough)); case 7: dat[6] = (hb>>16) & 0xff; + __attribute__((fallthrough)); case 6: dat[5] = (hb>>8) & 0xff; + __attribute__((fallthrough)); case 5: dat[4] = hb & 0xff; + __attribute__((fallthrough)); case 4: dat[3] = lb>>24; + __attribute__((fallthrough)); case 3: dat[2] = (lb>>16) & 0xff; + __attribute__((fallthrough)); case 2: dat[1] = (lb>>8) & 0xff; + __attribute__((fallthrough)); case 1: dat[0] = lb & 0xff; } diff --git a/F0-nolib/canbus/src/canbus.bin b/F0-nolib/canbus/src/canbus.bin index ef66d7b..a3509b1 100755 Binary files a/F0-nolib/canbus/src/canbus.bin and b/F0-nolib/canbus/src/canbus.bin differ diff --git a/F0-nolib/canbus/src/ld/stm32f042k.ld b/F0-nolib/canbus/src/ld/stm32f042k.ld deleted file mode 100644 index e747253..0000000 --- a/F0-nolib/canbus/src/ld/stm32f042k.ld +++ /dev/null @@ -1,12 +0,0 @@ -/* Linker script for STM32F042x6, 32K flash, 6K RAM. */ - -/* Define memory regions. */ -MEMORY -{ - rom (rx) : ORIGIN = 0x08000000, LENGTH = 32K - ram (rwx) : ORIGIN = 0x20000000, LENGTH = 6K -} - -/* Include the common ld script. */ -INCLUDE stm32f0.ld - diff --git a/F0-nolib/ch340/Makefile b/F0-nolib/ch340/Makefile index a72fdf8..2e59822 100644 --- a/F0-nolib/ch340/Makefile +++ b/F0-nolib/ch340/Makefile @@ -11,7 +11,7 @@ DEFS += -DUSARTNUM=1 #DEFS += -DEBUG # change this linking script depending on particular MCU model, # for example, if you have STM32F103VBT6, you should write: -LDSCRIPT = ld/stm32f042k.ld +LDSCRIPT = stm32f042k.ld INDEPENDENT_HEADERS= diff --git a/F0-nolib/ch340/ld/stm32f042k.ld b/F0-nolib/ch340/ld/stm32f042k.ld deleted file mode 100644 index e747253..0000000 --- a/F0-nolib/ch340/ld/stm32f042k.ld +++ /dev/null @@ -1,12 +0,0 @@ -/* Linker script for STM32F042x6, 32K flash, 6K RAM. */ - -/* Define memory regions. */ -MEMORY -{ - rom (rx) : ORIGIN = 0x08000000, LENGTH = 32K - ram (rwx) : ORIGIN = 0x20000000, LENGTH = 6K -} - -/* Include the common ld script. */ -INCLUDE stm32f0.ld - diff --git a/F0-nolib/htu21d_nucleo/Makefile b/F0-nolib/htu21d_nucleo/Makefile index 48a5249..103a408 100644 --- a/F0-nolib/htu21d_nucleo/Makefile +++ b/F0-nolib/htu21d_nucleo/Makefile @@ -8,7 +8,7 @@ MCU = F042x6 DEFS += -DEBUG # change this linking script depending on particular MCU model, # for example, if you have STM32F103VBT6, you should write: -LDSCRIPT = ld/stm32f042k.ld +LDSCRIPT = stm32f042k.ld INDEPENDENT_HEADERS= diff --git a/F0-nolib/htu21d_nucleo/ld/stm32f042k.ld b/F0-nolib/htu21d_nucleo/ld/stm32f042k.ld deleted file mode 100644 index e747253..0000000 --- a/F0-nolib/htu21d_nucleo/ld/stm32f042k.ld +++ /dev/null @@ -1,12 +0,0 @@ -/* Linker script for STM32F042x6, 32K flash, 6K RAM. */ - -/* Define memory regions. */ -MEMORY -{ - rom (rx) : ORIGIN = 0x08000000, LENGTH = 32K - ram (rwx) : ORIGIN = 0x20000000, LENGTH = 6K -} - -/* Include the common ld script. */ -INCLUDE stm32f0.ld - diff --git a/F0-nolib/morze/ld/stm32f030f.ld b/F0-nolib/inc/ld/stm32f030f.ld similarity index 100% rename from F0-nolib/morze/ld/stm32f030f.ld rename to F0-nolib/inc/ld/stm32f030f.ld diff --git a/F0-nolib/blink/ld/stm32f042k.ld b/F0-nolib/inc/ld/stm32f042k.ld similarity index 100% rename from F0-nolib/blink/ld/stm32f042k.ld rename to F0-nolib/inc/ld/stm32f042k.ld diff --git a/F0-nolib/morze/Makefile b/F0-nolib/morze/Makefile index f85ea34..0a1f97d 100644 --- a/F0-nolib/morze/Makefile +++ b/F0-nolib/morze/Makefile @@ -8,7 +8,7 @@ MCU = F030x4 DEFS = -DEBUG # change this linking script depending on particular MCU model, # for example, if you have STM32F103VBT6, you should write: -LDSCRIPT = ld/stm32f030f.ld +LDSCRIPT = stm32f030f.ld INDEPENDENT_HEADERS= diff --git a/F0-nolib/tsys01_nucleo/Makefile b/F0-nolib/tsys01_nucleo/Makefile index d2a8296..2aec3b5 100644 --- a/F0-nolib/tsys01_nucleo/Makefile +++ b/F0-nolib/tsys01_nucleo/Makefile @@ -8,7 +8,7 @@ MCU = F042x6 DEFS += -DEBUG # change this linking script depending on particular MCU model, # for example, if you have STM32F103VBT6, you should write: -LDSCRIPT = ld/stm32f042k.ld +LDSCRIPT = stm32f042k.ld INDEPENDENT_HEADERS= diff --git a/F0-nolib/tsys01_nucleo/ld/stm32f042k.ld b/F0-nolib/tsys01_nucleo/ld/stm32f042k.ld deleted file mode 100644 index e747253..0000000 --- a/F0-nolib/tsys01_nucleo/ld/stm32f042k.ld +++ /dev/null @@ -1,12 +0,0 @@ -/* Linker script for STM32F042x6, 32K flash, 6K RAM. */ - -/* Define memory regions. */ -MEMORY -{ - rom (rx) : ORIGIN = 0x08000000, LENGTH = 32K - ram (rwx) : ORIGIN = 0x20000000, LENGTH = 6K -} - -/* Include the common ld script. */ -INCLUDE stm32f0.ld - diff --git a/F0-nolib/tsys01_nucleo/tsys01.bin b/F0-nolib/tsys01_nucleo/tsys01.bin index 3c7a867..fbaa5fd 100755 Binary files a/F0-nolib/tsys01_nucleo/tsys01.bin and b/F0-nolib/tsys01_nucleo/tsys01.bin differ diff --git a/F0-nolib/uart_blink/Makefile b/F0-nolib/uart_blink/Makefile index f652305..acd9178 100644 --- a/F0-nolib/uart_blink/Makefile +++ b/F0-nolib/uart_blink/Makefile @@ -8,7 +8,7 @@ MCU = F030x4 DEFS = -DEBUG # change this linking script depending on particular MCU model, # for example, if you have STM32F103VBT6, you should write: -LDSCRIPT = ld/stm32f030f.ld +LDSCRIPT = stm32f030f.ld INDEPENDENT_HEADERS= diff --git a/F0-nolib/uart_blink/ld/stm32f030f.ld b/F0-nolib/uart_blink/ld/stm32f030f.ld deleted file mode 100644 index f8b61f1..0000000 --- a/F0-nolib/uart_blink/ld/stm32f030f.ld +++ /dev/null @@ -1,12 +0,0 @@ -/* Linker script for STM32F030f4, 16K flash, 4K RAM. */ - -/* Define memory regions. */ -MEMORY -{ - rom (rx) : ORIGIN = 0x08000000, LENGTH = 16K - ram (rwx) : ORIGIN = 0x20000000, LENGTH = 4K -} - -/* Include the common ld script. */ -INCLUDE stm32f0.ld - diff --git a/F0-nolib/uart_blink/uartblink.bin b/F0-nolib/uart_blink/uartblink.bin index 589e867..38ef083 100755 Binary files a/F0-nolib/uart_blink/uartblink.bin and b/F0-nolib/uart_blink/uartblink.bin differ diff --git a/F0-nolib/uart_blink_dma/Makefile b/F0-nolib/uart_blink_dma/Makefile index f652305..acd9178 100644 --- a/F0-nolib/uart_blink_dma/Makefile +++ b/F0-nolib/uart_blink_dma/Makefile @@ -8,7 +8,7 @@ MCU = F030x4 DEFS = -DEBUG # change this linking script depending on particular MCU model, # for example, if you have STM32F103VBT6, you should write: -LDSCRIPT = ld/stm32f030f.ld +LDSCRIPT = stm32f030f.ld INDEPENDENT_HEADERS= diff --git a/F0-nolib/uart_blink_dma/ld/stm32f030f.ld b/F0-nolib/uart_blink_dma/ld/stm32f030f.ld deleted file mode 100644 index f8b61f1..0000000 --- a/F0-nolib/uart_blink_dma/ld/stm32f030f.ld +++ /dev/null @@ -1,12 +0,0 @@ -/* Linker script for STM32F030f4, 16K flash, 4K RAM. */ - -/* Define memory regions. */ -MEMORY -{ - rom (rx) : ORIGIN = 0x08000000, LENGTH = 16K - ram (rwx) : ORIGIN = 0x20000000, LENGTH = 4K -} - -/* Include the common ld script. */ -INCLUDE stm32f0.ld - diff --git a/F0-nolib/uart_blink_dma/uartblink.bin b/F0-nolib/uart_blink_dma/uartblink.bin index 87da4f5..1ce79a8 100755 Binary files a/F0-nolib/uart_blink_dma/uartblink.bin and b/F0-nolib/uart_blink_dma/uartblink.bin differ diff --git a/F0-nolib/uart_nucleo/Makefile b/F0-nolib/uart_nucleo/Makefile index c581875..e9f44bd 100644 --- a/F0-nolib/uart_nucleo/Makefile +++ b/F0-nolib/uart_nucleo/Makefile @@ -8,7 +8,7 @@ MCU = F042x6 DEFS += -DEBUG # change this linking script depending on particular MCU model, # for example, if you have STM32F103VBT6, you should write: -LDSCRIPT = ld/stm32f042k.ld +LDSCRIPT = stm32f042k.ld INDEPENDENT_HEADERS= diff --git a/F0-nolib/uart_nucleo/ld/stm32f042k.ld b/F0-nolib/uart_nucleo/ld/stm32f042k.ld deleted file mode 100644 index e747253..0000000 --- a/F0-nolib/uart_nucleo/ld/stm32f042k.ld +++ /dev/null @@ -1,12 +0,0 @@ -/* Linker script for STM32F042x6, 32K flash, 6K RAM. */ - -/* Define memory regions. */ -MEMORY -{ - rom (rx) : ORIGIN = 0x08000000, LENGTH = 32K - ram (rwx) : ORIGIN = 0x20000000, LENGTH = 6K -} - -/* Include the common ld script. */ -INCLUDE stm32f0.ld - diff --git a/F0-nolib/uart_nucleo/usart.bin b/F0-nolib/uart_nucleo/usart.bin index 1445acf..4e04806 100755 Binary files a/F0-nolib/uart_nucleo/usart.bin and b/F0-nolib/uart_nucleo/usart.bin differ diff --git a/F0-nolib/usbcdc/Makefile b/F0-nolib/usbcdc/Makefile index ddba4e5..881c852 100644 --- a/F0-nolib/usbcdc/Makefile +++ b/F0-nolib/usbcdc/Makefile @@ -11,7 +11,7 @@ DEFS += -DUSARTNUM=1 #DEFS += -DEBUG # change this linking script depending on particular MCU model, # for example, if you have STM32F103VBT6, you should write: -LDSCRIPT = ld/stm32f042k.ld +LDSCRIPT = stm32f042k.ld INDEPENDENT_HEADERS= diff --git a/F0-nolib/usbcdc/ld/stm32f042k.ld b/F0-nolib/usbcdc/ld/stm32f042k.ld deleted file mode 100644 index e747253..0000000 --- a/F0-nolib/usbcdc/ld/stm32f042k.ld +++ /dev/null @@ -1,12 +0,0 @@ -/* Linker script for STM32F042x6, 32K flash, 6K RAM. */ - -/* Define memory regions. */ -MEMORY -{ - rom (rx) : ORIGIN = 0x08000000, LENGTH = 32K - ram (rwx) : ORIGIN = 0x20000000, LENGTH = 6K -} - -/* Include the common ld script. */ -INCLUDE stm32f0.ld - diff --git a/F0-nolib/usbcdc/usbcan.bin b/F0-nolib/usbcdc/usbcan.bin index 5f62f49..b8ecb64 100755 Binary files a/F0-nolib/usbcdc/usbcan.bin and b/F0-nolib/usbcdc/usbcan.bin differ diff --git a/F1-nolib/led_blink/ld/stm32f103x4.ld b/F1-nolib/inc/ld/stm32f103x4.ld similarity index 100% rename from F1-nolib/led_blink/ld/stm32f103x4.ld rename to F1-nolib/inc/ld/stm32f103x4.ld diff --git a/F1-nolib/led_blink/ld/stm32f103x6.ld b/F1-nolib/inc/ld/stm32f103x6.ld similarity index 100% rename from F1-nolib/led_blink/ld/stm32f103x6.ld rename to F1-nolib/inc/ld/stm32f103x6.ld diff --git a/F1-nolib/led_blink/ld/stm32f103x8.ld b/F1-nolib/inc/ld/stm32f103x8.ld similarity index 100% rename from F1-nolib/led_blink/ld/stm32f103x8.ld rename to F1-nolib/inc/ld/stm32f103x8.ld diff --git a/F1-nolib/led_blink/ld/stm32f103xB.ld b/F1-nolib/inc/ld/stm32f103xB.ld similarity index 100% rename from F1-nolib/led_blink/ld/stm32f103xB.ld rename to F1-nolib/inc/ld/stm32f103xB.ld diff --git a/F1-nolib/led_blink/ld/stm32f103xC.ld b/F1-nolib/inc/ld/stm32f103xC.ld similarity index 100% rename from F1-nolib/led_blink/ld/stm32f103xC.ld rename to F1-nolib/inc/ld/stm32f103xC.ld diff --git a/F1-nolib/led_blink/ld/stm32f103xD.ld b/F1-nolib/inc/ld/stm32f103xD.ld similarity index 100% rename from F1-nolib/led_blink/ld/stm32f103xD.ld rename to F1-nolib/inc/ld/stm32f103xD.ld diff --git a/F1-nolib/led_blink/ld/stm32f103xE.ld b/F1-nolib/inc/ld/stm32f103xE.ld similarity index 100% rename from F1-nolib/led_blink/ld/stm32f103xE.ld rename to F1-nolib/inc/ld/stm32f103xE.ld diff --git a/F1-nolib/led_blink/ld/stm32f103xF.ld b/F1-nolib/inc/ld/stm32f103xF.ld similarity index 100% rename from F1-nolib/led_blink/ld/stm32f103xF.ld rename to F1-nolib/inc/ld/stm32f103xF.ld diff --git a/F1-nolib/led_blink/ld/stm32f103xG.ld b/F1-nolib/inc/ld/stm32f103xG.ld similarity index 100% rename from F1-nolib/led_blink/ld/stm32f103xG.ld rename to F1-nolib/inc/ld/stm32f103xG.ld diff --git a/F1-nolib/led_blink/Makefile b/F1-nolib/led_blink/Makefile index c1879da..c7d76c2 100644 --- a/F1-nolib/led_blink/Makefile +++ b/F1-nolib/led_blink/Makefile @@ -8,7 +8,7 @@ MCU ?= F103xB # density (stm32f10x.h, lines 70-84) DENSITY ?= MD # change this linking script depending on particular MCU model, -LDSCRIPT ?= ld/stm32f103xB.ld +LDSCRIPT ?= stm32f103xB.ld # debug DEFS = -DEBUG diff --git a/F1-nolib/led_blink/ld/devices.data b/F1-nolib/led_blink/ld/devices.data deleted file mode 100644 index 7f29538..0000000 --- a/F1-nolib/led_blink/ld/devices.data +++ /dev/null @@ -1,9 +0,0 @@ -stm32f103?4* stm32f1 ROM=16K RAM=6K -stm32f103?6* stm32f1 ROM=32K RAM=10K -stm32f103?8* stm32f1 ROM=64K RAM=20K -stm32f103?b* stm32f1 ROM=128K RAM=20K -stm32f103?c* stm32f1 ROM=256K RAM=48K -stm32f103?d* stm32f1 ROM=384K RAM=64K -stm32f103?e* stm32f1 ROM=512K RAM=64K -stm32f103?f* stm32f1 ROM=768K RAM=96K -stm32f103?g* stm32f1 ROM=1024K RAM=96K diff --git a/F1-nolib/uart/Makefile b/F1-nolib/uart/Makefile new file mode 100644 index 0000000..5cd1511 --- /dev/null +++ b/F1-nolib/uart/Makefile @@ -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 diff --git a/F1-nolib/uart/README b/F1-nolib/uart/README new file mode 100644 index 0000000..35bb182 --- /dev/null +++ b/F1-nolib/uart/README @@ -0,0 +1,5 @@ +Toggle LEDs (PB8/PB9) on STM32F103 development board depending on buttons PC0,PC1: +- no buttons pressed == 'SOS' in Morze @ LED D1 +- Button S2 pressed - D1 blinks with period of 5s +- Button S1 pressed - D2 blinks with period of 1s + diff --git a/F1-nolib/uart/main.c b/F1-nolib/uart/main.c new file mode 100644 index 0000000..6132830 --- /dev/null +++ b/F1-nolib/uart/main.c @@ -0,0 +1,131 @@ +/* + * main.c + * + * Copyright 2018 Edward V. Emelianov + * + * 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" + +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); +} + +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; + } + + /* + 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); + + }*/ + } +} + diff --git a/F1-nolib/uart/uart.bin b/F1-nolib/uart/uart.bin new file mode 100755 index 0000000..1b5a8ce Binary files /dev/null and b/F1-nolib/uart/uart.bin differ diff --git a/F1-nolib/uart/usart.c b/F1-nolib/uart/usart.c new file mode 100644 index 0000000..25f41ea --- /dev/null +++ b/F1-nolib/uart/usart.c @@ -0,0 +1,242 @@ +/* + * usart.c + * + * Copyright 2018 Edward V. Emelianoff + * + * 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) break;}; // wait for previos buffer transmission + register int l = odatalen[tbufno]; + if(!l) return; +//txrdy = 0; + odatalen[tbufno] = 0; + char *buf = tbuf[tbufno]; + for(int i = 0; i < l; ++i){ + USART1->DR = *buf++; + tmout = 16000000; + while(!(USART1->SR & USART_SR_TXE)){if(--tmout == 0)break;}; + } + /* +#if USARTNUM == 2 + DMA1_Channel4->CCR &= ~DMA_CCR_EN; + DMA1_Channel4->CMAR = (uint32_t) tbuf[tbufno]; // mem + DMA1_Channel4->CNDTR = l; + DMA1_Channel4->CCR |= DMA_CCR_EN; // start transmission +#elif USARTNUM == 1 + DMA1_Channel2->CCR &= ~DMA_CCR_EN; + DMA1_Channel2->CMAR = (uint32_t) tbuf[tbufno]; // mem + DMA1_Channel2->CNDTR = l; + DMA1_Channel2->CCR |= DMA_CCR_EN; +#else +#error "Not implemented" +#endif +*/ + 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; + GPIOA->CRH = CRH(9, CNF_AFPP|MODE_NORMAL) | CRH(10, CNF_FLINPUT|MODE_INPUT); +/* + // USART1 Tx DMA - Channel2 (default value in SYSCFG_CFGR1) + DMA1_Channel2->CPAR = (uint32_t) &USART1->TDR; // periph + DMA1_Channel2->CMAR = (uint32_t) tbuf; // mem + DMA1_Channel2->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_Channel2_3_IRQn, 3); + NVIC_EnableIRQ(DMA1_Channel2_3_IRQn);*/ + //USART1->CR3 = USART_CR3_DMAT; // enable DMA Tx + 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 + 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(' '); + } +} +/* +#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 +*/ diff --git a/F1-nolib/uart/usart.h b/F1-nolib/uart/usart.h new file mode 100644 index 0000000..3ade072 --- /dev/null +++ b/F1-nolib/uart/usart.h @@ -0,0 +1,58 @@ +/* + * usart.h + * + * Copyright 2018 Edward V. Emelianoff + * + * 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 (32) +#define UARTBUFSZO (512) +// 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__ diff --git a/F1/client-term/client.c b/F1/client-term/client.c index 5f565e0..5ee52be 100644 --- a/F1/client-term/client.c +++ b/F1/client-term/client.c @@ -36,7 +36,7 @@ double t0; // start time FILE *fout = NULL; // file for messages duplicating -char *comdev = "/dev/ttyACM0"; +char *comdev = "/dev/ttyUSB0"; int BAUD_RATE = B115200; struct termio oldtty, tty; // TTY flags struct termios oldt, newt; // terminal flags