Skip to content

Commit

Permalink
make firmware compile for stm32g4
Browse files Browse the repository at this point in the history
  • Loading branch information
bkleiner committed May 26, 2024
1 parent 8aa40cc commit 57fd957
Show file tree
Hide file tree
Showing 40 changed files with 1,828 additions and 225 deletions.
2 changes: 1 addition & 1 deletion boards/STM32F405RG.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@
]
},
"url": "https://www.st.com/en/microcontrollers-microprocessors/stm32f405rg.html",
"vendor": "Generic"
"vendor": "ST"
}
2 changes: 1 addition & 1 deletion boards/STM32F411RE.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@
]
},
"url": "https://www.st.com/en/microcontrollers-microprocessors/stm32f411re.html",
"vendor": "Generic"
"vendor": "ST"
}
2 changes: 1 addition & 1 deletion boards/STM32F722RE.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@
]
},
"url": "https://www.st.com/en/microcontrollers-microprocessors/stm32f722re.html",
"vendor": "Generic"
"vendor": "ST"
}
2 changes: 1 addition & 1 deletion boards/STM32F745XG.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@
]
},
"url": "https://www.st.com/en/microcontrollers-microprocessors/stm32f745xg.html",
"vendor": "Generic"
"vendor": "ST"
}
2 changes: 1 addition & 1 deletion boards/STM32F765VI.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@
]
},
"url": "https://www.st.com/en/microcontrollers-microprocessors/stm32f765vi.html",
"vendor": "Generic"
"vendor": "ST"
}
37 changes: 37 additions & 0 deletions boards/STM32G473CE.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"build": {
"core": "stm32",
"cpu": "cortex-m4",
"extra_flags": "-DSTM32G473xx -DSTM32G473 -DSTM32G4xx -DSTM32G4 -DSTM32 -DMCU_NAME=stm32g473 -DHSE_VALUE=8000000U -DUSE_FAST_RAM -DUSB_PMASIZE=0x400",
"f_cpu": "170000000L",
"mcu": "stm32g473",
"product_line": "STM32G473xx",
"variant": "Generic_G473"
},
"debug": {
"jlink_device": "STM32G473CB",
"openocd_target": "stm32g4x",
"svd_path": "STM32G473xx.svd"
},
"frameworks": [
"arduino",
"cmsis",
"libopencm3",
"stm32cube"
],
"name": "STM32G473CE (128k RAM. 512k Flash)",
"upload": {
"maximum_ram_size": 131072,
"maximum_size": 524288,
"protocol": "stlink",
"protocols": [
"stlink",
"jlink",
"cmsis-dap",
"blackmagic",
"mbed"
]
},
"url": "https://www.st.com/en/microcontrollers-microprocessors/stm32g473ce.html",
"vendor": "ST"
}
2 changes: 1 addition & 1 deletion boards/STM32H743VI.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@
]
},
"url": "https://www.st.com/en/microcontrollers-microprocessors/stm32h743vi.html",
"vendor": "Generic"
"vendor": "ST"
}
2 changes: 1 addition & 1 deletion lib/libusb_stm32/src/usbd_stm32l433_devfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ static int32_t ep_read(uint8_t ep, void *buf, uint16_t blen) {
}

static void pma_write(const uint8_t *buf, uint16_t blen, pma_rec *tx) {
uint16_t *pma = (void*)(USB_PMAADDR + tx->addr);
volatile uint16_t *pma = (void*)(USB_PMAADDR + tx->addr);
tx->cnt = blen;
while (blen > 1) {
*pma++ = buf[1] << 8 | buf[0];
Expand Down
20 changes: 19 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ build_flags =

[stm32]
extends = common
platform = ststm32@~17.0.0
platform = ststm32@~17.3.0
build_src_filter = ${common.build_src_filter} +<driver/stm32>
framework = stm32cube
board_build.stm32cube.custom_system_setup = yes
Expand Down Expand Up @@ -133,6 +133,24 @@ build_flags =
[env:stm32h743]
extends = stm32h743

[stm32g4]
extends = stm32
system_flags =
-mfloat-abi=hard
-mfpu=fpv4-sp-d16

[stm32g473]
extends = stm32g4
board = STM32G473CE
build_src_filter = ${stm32.build_src_filter} +<system/stm32g473>
board_build.ldscript = $PROJECT_DIR/src/system/stm32g473/flash_layout.ld
build_flags =
${stm32g4.build_flags}
-Isrc/system/stm32g473

[env:stm32g473]
extends = stm32g473

[at32]
extends = common
platform = https://github.com/ArteryTek/platform-arterytekat32.git#5729d36
Expand Down
5 changes: 3 additions & 2 deletions script/device_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def parse_all(self):
)

# roughly filter to supported devices
supported = ["stm32f4", "stm32f7", "stm32h7"]
supported = ["stm32f4", "stm32g4", "stm32f7", "stm32h7"]
device_file_names = [
dfn for dfn in device_file_names if any(s in dfn for s in supported)
]
Expand Down Expand Up @@ -109,6 +109,7 @@ def __getitem__(self, item) -> modm_devices.device.Device:
devices = [
"stm32f405rg",
"stm32f411re",
"stm32g473ceu6",
"stm32f722re",
"stm32f745vg",
"stm32f765vi",
Expand Down Expand Up @@ -176,7 +177,7 @@ def __getitem__(self, item) -> modm_devices.device.Device:

pins[f"P{pin['port']}{pin['pin']}".upper()] = funcs

with open(f"src/system/{device[:-2]}/gpio_pins.yaml", "w") as file:
with open(f"src/system/{device[:9]}/gpio_pins.yaml", "w") as file:
documents = yaml.dump(pins, file, sort_keys=False)

for filename in glob.glob("src/system/*/gpio_pins.yaml"):
Expand Down
2 changes: 2 additions & 0 deletions src/core/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ typedef enum {
SERIAL_PORT4,
SERIAL_PORT5,
#endif
#ifndef STM32G4
SERIAL_PORT6,
#endif
#if defined(STM32F7) || defined(STM32H7) || defined(AT32F4)
SERIAL_PORT7,
SERIAL_PORT8,
Expand Down
2 changes: 1 addition & 1 deletion src/driver/adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ uint16_t adc_array[ADC_CHAN_MAX];
adc_channel_t adc_pins[ADC_CHAN_MAX];

extern uint16_t adc_read_raw(adc_chan_t index);
extern float adc_convert_to_temp(float val);
extern float adc_convert_to_temp(uint16_t val);

static float adc_convert_to_mv(float value) {
const float vref = (float)(VREFINT_CAL * VREFINT_CAL_VREF) / (float)adc_read_raw(ADC_CHAN_VREF);
Expand Down
6 changes: 5 additions & 1 deletion src/driver/adc.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ typedef enum {
ADC_DEVICE2,
ADC_DEVICE3,
#endif
ADC_DEVICEMAX,
#ifdef STM32G473
ADC_DEVICE4,
ADC_DEVICE5,
#endif
ADC_DEVICE_MAX,
} adc_devices_t;

typedef enum {
Expand Down
10 changes: 5 additions & 5 deletions src/driver/at32/adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
extern uint16_t adc_array[ADC_CHAN_MAX];
extern adc_channel_t adc_pins[ADC_CHAN_MAX];

static adc_type *adc_devs[ADC_DEVICEMAX] = {
static adc_type *adc_devs[ADC_DEVICE_MAX] = {
ADC1,
ADC2,
ADC3,
Expand All @@ -23,7 +23,7 @@ static adc_type *adc_devs[ADC_DEVICEMAX] = {
static void adc_init_pin(adc_chan_t chan, gpio_pins_t pin) {
adc_array[chan] = 1;
adc_pins[chan].pin = PIN_NONE;
adc_pins[chan].dev = ADC_DEVICEMAX;
adc_pins[chan].dev = ADC_DEVICE_MAX;

switch (chan) {
case ADC_CHAN_VREF:
Expand Down Expand Up @@ -70,7 +70,7 @@ static void adc_init_dev() {
common_init.vbat_state = FALSE;
adc_common_config(&common_init);

for (uint32_t i = 0; i < ADC_DEVICEMAX; i++) {
for (uint32_t i = 0; i < ADC_DEVICE_MAX; i++) {
adc_base_config_type base_init;
base_init.sequence_mode = FALSE;
base_init.repeat_mode = FALSE;
Expand Down Expand Up @@ -141,14 +141,14 @@ uint16_t adc_read_raw(adc_chan_t index) {
do {
last_adc_chan = (last_adc_chan + 1) % ADC_CHAN_MAX;
// skip through all channels without a dev
} while (adc_pins[last_adc_chan].dev == ADC_DEVICEMAX);
} while (adc_pins[last_adc_chan].dev == ADC_DEVICE_MAX);

adc_start_conversion(last_adc_chan);
}

return adc_array[index];
}

float adc_convert_to_temp(float val) {
float adc_convert_to_temp(uint16_t val) {
return (ADC_TEMP_BASE - val * ADC_VREF / 4096) / ADC_TEMP_SLOPE + 25;
}
5 changes: 4 additions & 1 deletion src/driver/fmc.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

#include "util/util.h"

#if defined(STM32H7)
#if defined(STM32G4)
#define FLASH_WORD_SIZE 8
typedef uint64_t flash_word_t;
#elif defined(STM32H7)
#define FLASH_WORD_SIZE 32
typedef uint64_t flash_word_t;
#else
Expand Down
58 changes: 44 additions & 14 deletions src/driver/stm32/adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,25 @@ typedef struct {
ADC_Common_TypeDef *common;
} adc_dev_t;

#ifdef STM32H7
#if defined(STM32G4)
#define ADC_INTERNAL_CHANNEL ADC_DEVICE1
#define LL_ADC_CHANNEL_TEMPSENSOR LL_ADC_CHANNEL_TEMPSENSOR_ADC1
#define ADC_SAMPLINGTIME LL_ADC_SAMPLINGTIME_640CYCLES_5
#define READY_TO_CONVERT(dev) LL_ADC_IsActiveFlag_EOC(dev)

static const adc_dev_t adc_dev[ADC_DEVICE_MAX] = {
{.adc = ADC1, .common = ADC12_COMMON},
{.adc = ADC2, .common = ADC12_COMMON},
{.adc = ADC3, .common = ADC345_COMMON},
{.adc = ADC4, .common = ADC345_COMMON},
{.adc = ADC5, .common = ADC345_COMMON},
};
#elif defined(STM32H7)
#define ADC_INTERNAL_CHANNEL ADC_DEVICE3
#define ADC_SAMPLINGTIME LL_ADC_SAMPLINGTIME_387CYCLES_5
#define READY_TO_CONVERT(dev) LL_ADC_IsActiveFlag_EOC(dev)

static const adc_dev_t adc_dev[ADC_DEVICEMAX] = {
static const adc_dev_t adc_dev[ADC_DEVICE_MAX] = {
{.adc = ADC1, .common = ADC12_COMMON},
{.adc = ADC2, .common = ADC12_COMMON},
{.adc = ADC3, .common = ADC3_COMMON},
Expand All @@ -20,7 +33,7 @@ static const adc_dev_t adc_dev[ADC_DEVICEMAX] = {
#define ADC_SAMPLINGTIME LL_ADC_SAMPLINGTIME_480CYCLES
#define READY_TO_CONVERT(dev) LL_ADC_IsActiveFlag_EOCS(dev)

static const adc_dev_t adc_dev[ADC_DEVICEMAX] = {
static const adc_dev_t adc_dev[ADC_DEVICE_MAX] = {
{.adc = ADC1, .common = ADC},
};
#endif
Expand Down Expand Up @@ -56,7 +69,7 @@ static const uint32_t channel_map[] = {
static void adc_init_pin(adc_chan_t chan, gpio_pins_t pin) {
adc_array[chan] = 1;
adc_pins[chan].pin = PIN_NONE;
adc_pins[chan].dev = ADC_DEVICEMAX;
adc_pins[chan].dev = ADC_DEVICE_MAX;

switch (chan) {
case ADC_CHAN_VREF:
Expand Down Expand Up @@ -103,6 +116,7 @@ static void adc_init_dev(adc_devices_t index) {
}

LL_ADC_REG_InitTypeDef adc_reg_init;
LL_ADC_REG_StructInit(&adc_reg_init);
adc_reg_init.TriggerSource = LL_ADC_REG_TRIG_SOFTWARE;
adc_reg_init.SequencerLength = LL_ADC_REG_SEQ_SCAN_DISABLE;
adc_reg_init.SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE;
Expand All @@ -115,9 +129,18 @@ static void adc_init_dev(adc_devices_t index) {
#endif
LL_ADC_REG_Init(dev->adc, &adc_reg_init);

#if defined(STM32G4)
LL_ADC_SetGainCompensation(dev->adc, 0);
LL_ADC_SetOverSamplingScope(dev->adc, LL_ADC_OVS_DISABLE);
#endif

LL_ADC_InitTypeDef adc_init;
LL_ADC_StructInit(&adc_init);
adc_init.Resolution = LL_ADC_RESOLUTION_12B;
#ifdef STM32H7
#if defined(STM32G4)
adc_init.DataAlignment = LL_ADC_DATA_ALIGN_RIGHT;
adc_init.LowPowerMode = LL_ADC_LP_MODE_NONE;
#elif defined(STM32H7)
adc_init.LeftBitShift = LL_ADC_LEFT_BIT_SHIFT_NONE;
adc_init.LowPowerMode = LL_ADC_LP_MODE_NONE;
#else
Expand All @@ -134,21 +157,24 @@ static void adc_init_dev(adc_devices_t index) {
LL_ADC_SetCommonPathInternalCh(dev->common, LL_ADC_PATH_INTERNAL_TEMPSENSOR);
}

#ifdef STM32H7
#if defined(STM32H7) || defined(STM32G4)
LL_ADC_DisableDeepPowerDown(dev->adc);
LL_ADC_EnableInternalRegulator(dev->adc);
time_delay_us(LL_ADC_DELAY_INTERNAL_REGUL_STAB_US);
time_delay_us(LL_ADC_DELAY_TEMPSENSOR_STAB_US);

#if defined(STM32G4)
LL_ADC_StartCalibration(dev->adc, LL_ADC_SINGLE_ENDED);
#endif
while (LL_ADC_IsCalibrationOnGoing(dev->adc) != 0)
;

// should be cycles, but just use us to be sure
time_delay_us(LL_ADC_DELAY_CALIB_ENABLE_ADC_CYCLES);
time_delay_us(LL_ADC_DELAY_CALIB_ENABLE_ADC_CYCLES * 32);
#endif

LL_ADC_Enable(dev->adc);

#ifdef STM32H7
#if defined(STM32H7) || defined(STM32G4)
while (LL_ADC_IsActiveFlag_ADRDY(dev->adc) == 0)
;
#endif
Expand All @@ -165,15 +191,19 @@ static void adc_start_conversion(adc_chan_t index) {
LL_ADC_SetChannelSamplingTime(dev->adc, chan->channel, ADC_SAMPLINGTIME);
LL_ADC_REG_SetSequencerRanks(dev->adc, LL_ADC_REG_RANK_1, chan->channel);

#ifdef STM32H7
#if defined(STM32H7) || defined(STM32G4)
LL_ADC_SetChannelSingleDiff(dev->adc, chan->channel, LL_ADC_SINGLE_ENDED);
LL_ADC_REG_StartConversion(dev->adc);
#else
LL_ADC_REG_StartConversionSWStart(dev->adc);
#endif
}

void adc_init() {
#ifdef STM32H7
#if defined(STM32G4)
rcc_enable(RCC_AHB2_GRP1(ADC12));
rcc_enable(RCC_AHB2_GRP1(ADC345));
#elif defined(STM32H7)
rcc_enable(RCC_AHB1_GRP1(ADC12));
rcc_enable(RCC_AHB4_GRP1(ADC3));
#else
Expand All @@ -192,7 +222,7 @@ void adc_init() {
adc_init_pin(ADC_CHAN_IBAT, target.ibat);
}

for (uint32_t i = 0; i < ADC_DEVICEMAX; i++) {
for (uint32_t i = 0; i < ADC_DEVICE_MAX; i++) {
adc_init_dev(i);
}

Expand All @@ -211,15 +241,15 @@ uint16_t adc_read_raw(adc_chan_t index) {
do {
last_adc_chan = (last_adc_chan + 1) % ADC_CHAN_MAX;
// skip through all channels without a dev
} while (adc_pins[last_adc_chan].dev == ADC_DEVICEMAX);
} while (adc_pins[last_adc_chan].dev == ADC_DEVICE_MAX);

adc_start_conversion(last_adc_chan);
}

return adc_array[index];
}

float adc_convert_to_temp(float val) {
float adc_convert_to_temp(uint16_t val) {
#ifdef STM32H7
// adc cal is 16bit on h7, shift by 4bit left
val *= 16;
Expand Down
Loading

0 comments on commit 57fd957

Please sign in to comment.