Skip to content

Commit

Permalink
building f415 and f421 bootloaders
Browse files Browse the repository at this point in the history
  • Loading branch information
tridge committed Jul 26, 2024
1 parent d61cecc commit eb67d2a
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 34 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions Mcu/f415/Inc/util.h
Original file line number Diff line number Diff line change
@@ -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;
}
33 changes: 33 additions & 0 deletions Mcu/f421/Inc/util.h
Original file line number Diff line number Diff line change
@@ -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;
}
53 changes: 23 additions & 30 deletions bootloader/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ typedef void (*pFunction)(void);
#define PORT_LETTER 1
#endif

#include <util.h>

static uint16_t low_pin_count;
static char receviedByte;
static int count;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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);
}


Expand Down Expand Up @@ -438,15 +430,15 @@ 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;
}
}


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;
}
Expand All @@ -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++;
}

Expand All @@ -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);
}


Expand Down Expand Up @@ -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{

Expand All @@ -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{

Expand All @@ -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++;
}

Expand Down Expand Up @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion f415makefile.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 3 additions & 1 deletion f421makefile.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit eb67d2a

Please sign in to comment.