From eb67d2a9063d2c94b180219c2f8771da65fdfdf7 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Fri, 26 Jul 2024 18:19:56 +1000 Subject: [PATCH] building f415 and f421 bootloaders --- Makefile | 4 ++-- Mcu/f415/Inc/util.h | 43 ++++++++++++++++++++++++++++++++++++ Mcu/f421/Inc/util.h | 33 ++++++++++++++++++++++++++++ bootloader/main.c | 53 ++++++++++++++++++++------------------------- f415makefile.mk | 4 +++- f421makefile.mk | 4 +++- 6 files changed, 107 insertions(+), 34 deletions(-) create mode 100644 Mcu/f415/Inc/util.h create mode 100644 Mcu/f421/Inc/util.h diff --git a/Makefile b/Makefile index 029fd4f2..e77db678 100644 --- a/Makefile +++ b/Makefile @@ -141,14 +141,14 @@ $(BIN_DIR)/$(call BOOTLOADER_BASENAME_VER,$(1),$(2)).elf: $$(SRC_BL) $$(QUIET)$$(MKDIR) -p $(OBJ) $$(QUIET)echo BUILT $(1) pin $(2) > $$@ $$(QUIET)echo Compiling $(notdir $$@) - $$(CC) $$(CFLAGS_BL) $$(LDFLAGS_BL) -o $$(@) $$(SRC_BL) $$(SRC_$(1)_BL) -Os + $$(QUIET)$$(CC) $$(CFLAGS_BL) $$(LDFLAGS_BL) -o $$(@) $$(SRC_BL) $$(SRC_$(1)_BL) -Os $$(QUIET)$$(CP) -f $$@ $$(OBJ)$$(DSEP)debug.elf $$(QUIET)$$(CP) -f Mcu$(DSEP)$(call lc,$(1))$(DSEP)openocd.cfg $$(OBJ)$$(DSEP)openocd.cfg > $$(NUL) # Generate bin and hex files $(BIN_DIR)/$(call BOOTLOADER_BASENAME_VER,$(1),$(2)).hex: $(BIN_DIR)/$(call BOOTLOADER_BASENAME_VER,$(1),$(2)).elf $$(QUIET)echo Generating $(notdir $$@) - $$(QUIET)$$(OBJCOPY) -O binary $$(<) $$@ + $$(QUIET)$$(OBJCOPY) -O binary $$(<) $$(@:.hex=.bin) $$(QUIET)$$(OBJCOPY) $$(<) -O ihex $$(@:.bin=.hex) $(call BOOTLOADER_BASENAME,$(1),$(2)): $(BIN_DIR)/$(call BOOTLOADER_BASENAME_VER,$(1),$(2)).hex diff --git a/Mcu/f415/Inc/util.h b/Mcu/f415/Inc/util.h new file mode 100644 index 00000000..47c27c50 --- /dev/null +++ b/Mcu/f415/Inc/util.h @@ -0,0 +1,43 @@ +/* + MCU specific utility functions + */ +#pragma once + +static inline void gpio_mode_set_input(uint32_t pin, uint32_t pull_up_down) +{ + if (pin < 8) { + const uint32_t pin4 = (pin*pin*pin*pin); + input_port->cfglr &= (input_port->cfglr & ~(pin4*0xfU)) | (pin4*(GPIO_MODE_INPUT | pull_up_down)); + } else { + pin = (pin-8); + const uint32_t pin4 = (pin*pin*pin*pin); + input_port->cfghr &= (input_port->cfglr & ~(pin4*0xfU)) | (pin4*(GPIO_MODE_INPUT | pull_up_down)); + } +} + +static inline void gpio_mode_set_output(uint32_t pin, uint32_t output_mode) +{ + if (pin < 8) { + const uint32_t pin4 = (pin*pin*pin*pin); + input_port->cfglr &= (input_port->cfglr & ~(pin4*0xfU)) | (pin4*(GPIO_MODE_OUTPUT | output_mode)); + } else { + pin = (pin-8); + const uint32_t pin4 = (pin*pin*pin*pin); + input_port->cfghr &= (input_port->cfghr & ~(pin4*0xfU)) | (pin4*(GPIO_MODE_OUTPUT | output_mode)); + } +} + +static inline void gpio_set(uint32_t pin) +{ + input_port->scr |= pin; +} + +static inline void gpio_clear(uint32_t pin) +{ + input_port->clr |= pin; +} + +static inline bool gpio_read(uint32_t pin) +{ + return (input_port->idt & pin) != 0; +} diff --git a/Mcu/f421/Inc/util.h b/Mcu/f421/Inc/util.h new file mode 100644 index 00000000..d45bca58 --- /dev/null +++ b/Mcu/f421/Inc/util.h @@ -0,0 +1,33 @@ +/* + MCU specific utility functions + */ +#pragma once + +static inline void gpio_mode_set_input(uint32_t pin, uint32_t pull_up_down) +{ + const uint32_t pinsq = (pin*pin); + input_port->cfgr = (input_port->cfgr & ~(pinsq * 0x3U)) | (pinsq * GPIO_MODE_INPUT); + input_port->pull = (input_port->pull & ~(pinsq * 0x3U)) | (pinsq * pull_up_down); +} + +static inline void gpio_mode_set_output(uint32_t pin, uint32_t output_mode) +{ + const uint32_t pinsq = (pin*pin); + input_port->cfgr &= (input_port->cfgr & ~(pinsq * 0x3U)) | (pinsq * GPIO_MODE_OUTPUT); + input_port->omode = (input_port->omode & ~pin) | (output_mode | pin); +} + +static inline void gpio_set(uint32_t pin) +{ + input_port->scr |= pin; +} + +static inline void gpio_clear(uint32_t pin) +{ + input_port->clr |= pin; +} + +static inline bool gpio_read(uint32_t pin) +{ + return (input_port->idt & pin) != 0; +} diff --git a/bootloader/main.c b/bootloader/main.c index 9ee79fd0..1d979ea0 100644 --- a/bootloader/main.c +++ b/bootloader/main.c @@ -67,6 +67,8 @@ typedef void (*pFunction)(void); #define PORT_LETTER 1 #endif +#include + static uint16_t low_pin_count; static char receviedByte; static int count; @@ -125,18 +127,11 @@ static void recieveBuffer(); #define BITTIME 1000000/BAUDRATE #define HALFBITTIME 500000/BAUDRATE -static void gpio_mode_set(uint32_t mode, uint32_t pull_up_down, uint32_t pin) -{ - input_port->cfgr = (((((input_port->cfgr))) & (~(((pin * pin) * (0x3UL << (0U)))))) | (((pin * pin) * mode))); - input_port->pull = ((((((input_port->pull))) & (~(((pin * pin) * (0x3UL << (0U)))))) | (((pin * pin) * pull_up_down)))); -} - static void delayMicroseconds(uint32_t micros) { - TMR3->cval = 0; - while (TMR3->cval< micros){ - - } + TMR3->cval = 0; + while (TMR3->cval< micros) { + } } static void jump() @@ -199,17 +194,14 @@ static char checkCrc(uint8_t* pBuff, uint16_t length) static void setReceive() { - - input_port->cfgr = (((((input_port->cfgr))) & (~(((input_pin * input_pin) * (0x3UL << (0U)))))) | (((input_pin * input_pin) * GPIO_MODE_INPUT))); + gpio_mode_set_input(input_pin, GPIO_PULL_NONE); received = 0; } static void setTransmit() { - //gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, GPIO_PIN_4); - input_port->scr = input_pin; - input_port->cfgr = (((((input_port->cfgr))) & (~(((input_pin * input_pin) * (0x3UL << (0U)))))) | (((input_pin * input_pin) * GPIO_MODE_OUTPUT))); - //gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_4); // set as reciever // clear bits and set receive bits.. + gpio_set(input_pin); + gpio_mode_set_output(input_pin, GPIO_OUTPUT_PUSH_PULL); } @@ -438,7 +430,7 @@ static void decodeInput() static void serialreadChar() { rxbyte=0; - while(!(input_port->idt & input_pin)){ // wait for rx to go high + while(!gpio_read(input_pin)){ // wait for rx to go high if(TMR3->cval > 20000){ invalid_command = 101; return; @@ -446,7 +438,7 @@ static void serialreadChar() } - while((input_port->idt & input_pin)){ // wait for it go go low + while(gpio_read(input_pin)){ // wait for it go go low if(TMR3->cval > 250 && messagereceived){ return; } @@ -457,7 +449,7 @@ static void serialreadChar() int bits_to_read = 0; while (bits_to_read < 8) { delayMicroseconds(BITTIME); - rxbyte = rxbyte | ((input_port->idt & input_pin) >> PIN_NUMBER) << bits_to_read; + rxbyte = rxbyte | (gpio_read(input_pin) >> PIN_NUMBER) << bits_to_read; bits_to_read++; } @@ -470,23 +462,23 @@ static void serialreadChar() void serialwriteChar(char data) { - input_port->clr = input_pin; + gpio_clear(input_pin); char bits_to_read = 0; while (bits_to_read < 8) { delayMicroseconds(BITTIME); if (data & 0x01) { //GPIO_BOP(input_port) = input_pin; - input_port->scr = input_pin; + gpio_set(input_pin); }else{ // GPIO_BC(input_port) = input_pin; - input_port->clr = input_pin; + gpio_clear(input_pin); } bits_to_read++; data = data >> 1; } delayMicroseconds(BITTIME); - input_port->scr = input_pin; + gpio_set(input_pin); } @@ -549,12 +541,12 @@ static void update_EEPROM() static void checkForSignal() { - gpio_mode_set(GPIO_MODE_INPUT, GPIO_PULL_DOWN, input_pin); + gpio_mode_set_input(input_pin, GPIO_PULL_DOWN); delayMicroseconds(500); for(int i = 0 ; i < 500; i ++){ - if(!(input_port->idt & input_pin)){ + if(!gpio_read(input_pin)){ low_pin_count++; }else{ @@ -569,12 +561,12 @@ static void checkForSignal() } - gpio_mode_set(GPIO_MODE_INPUT, GPIO_PULL_UP, input_pin); + gpio_mode_set_input(input_pin, GPIO_PULL_UP); delayMicroseconds(500); for(int i = 0 ; i < 500; i ++){ - if( !((input_port->idt & input_pin))){ + if( !(gpio_read(input_pin))){ low_pin_count++; }else{ @@ -587,11 +579,12 @@ static void checkForSignal() low_pin_count = 0; - gpio_mode_set(GPIO_MODE_INPUT, GPIO_PULL_NONE, input_pin); + gpio_mode_set_input(input_pin, GPIO_PULL_NONE); + delayMicroseconds(500); for(int i = 0 ; i < 500; i ++){ - if( !((input_port->idt & input_pin))){ + if( !(gpio_read(input_pin))){ low_pin_count++; } @@ -672,7 +665,7 @@ int main(void) checkForSignal(); - gpio_mode_set( GPIO_MODE_INPUT, GPIO_PULL_NONE, input_pin); + gpio_mode_set_input(input_pin, GPIO_PULL_NONE); #ifdef USE_ADC_INPUT // go right to application jump(); diff --git a/f415makefile.mk b/f415makefile.mk index 7abaadcd..b57df013 100644 --- a/f415makefile.mk +++ b/f415makefile.mk @@ -27,4 +27,6 @@ CFLAGS_$(MCU) += \ -DUSE_STDPERIPH_DRIVER SRC_$(MCU) := $(foreach dir,$(SRC_DIR_$(MCU)),$(wildcard $(dir)/*.[cs])) -SRC_$(MCU)_BL := $(foreach dir,$(SRC_BASE_DIR_$(MCU)),$(wildcard $(dir)/*.[cs])) $(wildcard $(HAL_FOLDER_$(MCU))/*_it.c) +SRC_$(MCU)_BL := $(foreach dir,$(SRC_BASE_DIR_$(MCU)),$(wildcard $(dir)/*.[cs])) \ + $(HAL_FOLDER_$(MCU))/Src/flash.c \ + $(wildcard $(HAL_FOLDER_$(MCU))/*_it.c) diff --git a/f421makefile.mk b/f421makefile.mk index 03b4cd5e..745a5b51 100644 --- a/f421makefile.mk +++ b/f421makefile.mk @@ -27,4 +27,6 @@ CFLAGS_$(MCU) += \ -DUSE_STDPERIPH_DRIVER SRC_$(MCU) := $(foreach dir,$(SRC_DIR_$(MCU)),$(wildcard $(dir)/*.[cs])) -SRC_$(MCU)_BL := $(foreach dir,$(SRC_BASE_DIR_$(MCU)),$(wildcard $(dir)/*.[cs])) $(HAL_FOLDER_$(MCU))/Src/flash.c $(wildcard $(HAL_FOLDER_$(MCU))/*_it.c) +SRC_$(MCU)_BL := $(foreach dir,$(SRC_BASE_DIR_$(MCU)),$(wildcard $(dir)/*.[cs])) \ + $(HAL_FOLDER_$(MCU))/Src/flash.c \ + $(wildcard $(HAL_FOLDER_$(MCU))/*_it.c)