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 1, 2024
1 parent bc5da29 commit 6d8b4d9
Show file tree
Hide file tree
Showing 38 changed files with 1,742 additions and 218 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"
}
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
3 changes: 2 additions & 1 deletion 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",
"stm32g473ce",
"stm32f722re",
"stm32f745vg",
"stm32f765vi",
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
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
8 changes: 4 additions & 4 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,7 +141,7 @@ 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);
}
Expand Down
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
41 changes: 30 additions & 11 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_247CYCLES_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 @@ -117,7 +130,10 @@ static void adc_init_dev(adc_devices_t index) {

LL_ADC_InitTypeDef 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,7 +150,7 @@ 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);
Expand All @@ -148,7 +164,7 @@ static void adc_init_dev(adc_devices_t index) {

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 +181,18 @@ 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_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 +211,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,7 +230,7 @@ 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);
}
Expand Down
41 changes: 36 additions & 5 deletions src/driver/stm32/dma.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "driver/rcc.h"

#if !defined(STM32G4)
// DMA1 Stream0 SPI3_RX
// DMA1 Stream1
// DMA1 Stream2
Expand Down Expand Up @@ -32,8 +33,35 @@
DMA_STREAM(2, 6, 3, TIM1_CH1) \
DMA_STREAM(2, 6, 6, TIM1_CH3) \
DMA_STREAM(2, 6, 4, TIM1_CH4)
#else
#define DMA_STREAMS \
DMA_STREAM(1, 0, 1, SPI1_RX) \
DMA_STREAM(1, 0, 2, SPI1_TX) \
DMA_STREAM(1, 0, 3, SPI2_RX) \
DMA_STREAM(1, 0, 4, SPI2_TX) \
DMA_STREAM(1, 0, 5, SPI3_RX) \
DMA_STREAM(1, 0, 6, SPI3_TX) \
DMA_STREAM(1, 0, 7, SPI4_RX) \
DMA_STREAM(1, 0, 8, SPI4_TX) \
DMA_STREAM(2, 0, 1, TIM1_CH1) \
DMA_STREAM(2, 0, 2, TIM1_CH3) \
DMA_STREAM(2, 0, 3, TIM1_CH4)
#endif

#ifdef STM32H7
#if defined(STM32G4)
#define DMA_STREAM(_port, _chan, _stream, _dev) \
[DMA_DEVICE_##_dev] = { \
.device = DMA_DEVICE_##_dev, \
.port = DMA##_port, \
.port_index = _port, \
.channel = 0, \
.channel_index = 0, \
.request = LL_DMAMUX_REQ_##_dev, \
.stream = DMA##_port##_Channel##_stream, \
.stream_index = LL_DMA_CHANNEL_##_stream, \
.irq = DMA##_port##_Channel##_stream##_IRQn, \
},
#elif defined(STM32H7)
#define DMA_STREAM(_port, _chan, _stream, _dev) \
[DMA_DEVICE_##_dev] = { \
.device = DMA_DEVICE_##_dev, \
Expand Down Expand Up @@ -87,6 +115,10 @@ void dma_prepare_rx_memory(void *addr, uint32_t size) {
}

void dma_enable_rcc(dma_device_t dev) {
#ifdef STM32G4
rcc_enable(RCC_AHB1_GRP1(DMAMUX1));
#endif

const dma_stream_def_t *dma = &dma_stream_defs[dev];
switch (dma->port_index) {
case 1:
Expand Down Expand Up @@ -125,10 +157,9 @@ static void handle_dma_stream_isr(dma_device_t dev) {
}
}

#define DMA_STREAM(_port, _chan, _stream, _dev) \
void DMA##_port##_Stream##_stream##_IRQHandler() { \
handle_dma_stream_isr(DMA_DEVICE_##_dev); \
}
#define DMA_STREAM(_port, _chan, _stream, _dev) \
void DMA##_port##_Stream##_stream##_IRQHandler() { handle_dma_stream_isr(DMA_DEVICE_##_dev); } \
void DMA##_port##_Channel##_stream##_IRQHandler() { handle_dma_stream_isr(DMA_DEVICE_##_dev); }

DMA_STREAMS

Expand Down
Loading

0 comments on commit 6d8b4d9

Please sign in to comment.