Skip to content

Commit 010012c

Browse files
AsensioLdpgeorge
authored andcommitted
stm32: Add support for G0 MCUs.
This commit adds support for the STM32G0 series of MCUs. Signed-off-by: Asensio Lorenzo Sempere <asensio.aerospace@gmail.com>
1 parent 3587d8e commit 010012c

26 files changed

Lines changed: 719 additions & 110 deletions

‎ports/stm32/Makefile‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -324,14 +324,12 @@ SRC_CXX += \
324324
SRC_O += \
325325
$(STARTUP_FILE) \
326326
$(SYSTEM_FILE)
327-
328-
ifeq ($(MCU_SERIES),f0)
329-
SRC_O += \
330-
resethandler_m0.o \
331-
shared/runtime/gchelper_m0.o
332-
else
327+
333328
ifeq ($(MCU_SERIES),l0)
334329
CSUPEROPT = -Os # save some code space
330+
endif
331+
332+
ifeq ($(MCU_SERIES),$(filter $(MCU_SERIES),f0 g0 l0))
335333
SRC_O += \
336334
resethandler_m0.o \
337335
shared/runtime/gchelper_m0.o
@@ -341,7 +339,6 @@ SRC_O += \
341339
resethandler.o \
342340
shared/runtime/gchelper_m3.o
343341
endif
344-
endif
345342

346343
HAL_SRC_C += $(addprefix $(STM32LIB_HAL_BASE)/Src/stm32$(MCU_SERIES)xx_,\
347344
hal.c \
@@ -366,7 +363,7 @@ HAL_SRC_C += $(addprefix $(STM32LIB_HAL_BASE)/Src/stm32$(MCU_SERIES)xx_,\
366363
ll_utils.c \
367364
)
368365

369-
ifeq ($(MCU_SERIES),$(filter $(MCU_SERIES),f4 f7 g4 h7 l0 l4 wb))
366+
ifeq ($(MCU_SERIES),$(filter $(MCU_SERIES),f4 f7 g0 g4 h7 l0 l4 wb))
370367
HAL_SRC_C += $(addprefix $(STM32LIB_HAL_BASE)/Src/stm32$(MCU_SERIES)xx_,\
371368
hal_pcd.c \
372369
hal_pcd_ex.c \
@@ -386,7 +383,6 @@ ifeq ($(MCU_SERIES),$(filter $(MCU_SERIES),f4 f7 h7))
386383
HAL_SRC_C += $(addprefix $(STM32LIB_HAL_BASE)/Src/stm32$(MCU_SERIES)xx_,\
387384
hal_mmc.c \
388385
hal_sdram.c \
389-
hal_dma_ex.c \
390386
hal_dcmi.c \
391387
)
392388
ifeq ($(MCU_SERIES),$(filter $(MCU_SERIES),f4))
@@ -396,7 +392,13 @@ $(BUILD)/$(STM32LIB_HAL_BASE)/Src/stm32$(MCU_SERIES)xx_hal_mmc.o: CFLAGS += -Wno
396392
endif
397393
endif
398394

399-
ifeq ($(MCU_SERIES),$(filter $(MCU_SERIES),g4))
395+
ifeq ($(MCU_SERIES),$(filter $(MCU_SERIES),f4 f7 g0 g4 h7))
396+
HAL_SRC_C += $(addprefix $(STM32LIB_HAL_BASE)/Src/stm32$(MCU_SERIES)xx_,\
397+
hal_dma_ex.c \
398+
)
399+
endif
400+
401+
ifeq ($(MCU_SERIES),$(filter $(MCU_SERIES),g0 g4))
400402
HAL_SRC_C += $(addprefix $(STM32LIB_HAL_BASE)/Src/stm32$(MCU_SERIES)xx_, hal_fdcan.c)
401403
endif
402404

@@ -413,7 +415,7 @@ endif
413415
endif
414416
endif
415417

416-
ifeq ($(MCU_SERIES),$(filter $(MCU_SERIES),f0 f4 f7 l0))
418+
ifeq ($(MCU_SERIES),$(filter $(MCU_SERIES),f0 f4 f7 g0 l0))
417419
HAL_SRC_C += $(addprefix $(STM32LIB_HAL_BASE)/Src/stm32$(MCU_SERIES)xx_,\
418420
hal_i2s.c \
419421
)

‎ports/stm32/adc.c‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,13 @@
102102
#define ADC_CAL2 ((uint16_t *)(ADC_CAL_ADDRESS + 4))
103103
#define ADC_CAL_BITS (12)
104104

105-
#elif defined(STM32G4)
105+
#elif defined(STM32G0) || defined(STM32G4)
106106

107107
#define ADC_SCALE_V (((float)VREFINT_CAL_VREF) / 1000.0f)
108108
#define ADC_CAL_ADDRESS VREFINT_CAL_ADDR
109109
#define ADC_CAL1 TEMPSENSOR_CAL1_ADDR
110110
#define ADC_CAL2 TEMPSENSOR_CAL2_ADDR
111-
#define ADC_CAL_BITS (12) // UM2570, __HAL_ADC_CALC_TEMPERATURE: 'corresponds to a resolution of 12 bits'
111+
#define ADC_CAL_BITS (12) // UM2319/UM2570, __HAL_ADC_CALC_TEMPERATURE: 'corresponds to a resolution of 12 bits'
112112

113113
#elif defined(STM32H7)
114114

@@ -150,7 +150,7 @@
150150
defined(STM32F746xx) || defined(STM32F765xx) || \
151151
defined(STM32F767xx) || defined(STM32F769xx)
152152
#define VBAT_DIV (4)
153-
#elif defined(STM32G4)
153+
#elif defined(STM32G0) || defined(STM32G4)
154154
#define VBAT_DIV (3)
155155
#elif defined(STM32H743xx) || defined(STM32H747xx) || \
156156
defined(STM32H7A3xx) || defined(STM32H7A3xxQ) || \
@@ -210,7 +210,7 @@ STATIC bool is_adcx_channel(int channel) {
210210
return IS_ADC_CHANNEL(channel) || channel == ADC_CHANNEL_TEMPSENSOR;
211211
#elif defined(STM32F0) || defined(STM32F4) || defined(STM32F7)
212212
return IS_ADC_CHANNEL(channel);
213-
#elif defined(STM32H7)
213+
#elif defined(STM32G0) || defined(STM32H7)
214214
return __HAL_ADC_IS_CHANNEL_INTERNAL(channel)
215215
|| IS_ADC_CHANNEL(__HAL_ADC_DECIMAL_NB_TO_CHANNEL(channel));
216216
#elif defined(STM32G4) || defined(STM32L4) || defined(STM32WB)
@@ -227,7 +227,7 @@ STATIC void adc_wait_for_eoc_or_timeout(ADC_HandleTypeDef *adcHandle, int32_t ti
227227
uint32_t tickstart = HAL_GetTick();
228228
#if defined(STM32F4) || defined(STM32F7)
229229
while ((adcHandle->Instance->SR & ADC_FLAG_EOC) != ADC_FLAG_EOC) {
230-
#elif defined(STM32F0) || defined(STM32G4) || defined(STM32H7) || defined(STM32L4) || defined(STM32WB)
230+
#elif defined(STM32F0) || defined(STM32G0) || defined(STM32G4) || defined(STM32H7) || defined(STM32L4) || defined(STM32WB)
231231
while (READ_BIT(adcHandle->Instance->ISR, ADC_FLAG_EOC) != ADC_FLAG_EOC) {
232232
#else
233233
#error Unsupported processor
@@ -244,6 +244,8 @@ STATIC void adcx_clock_enable(ADC_HandleTypeDef *adch) {
244244
#elif defined(STM32H7A3xx) || defined(STM32H7A3xxQ) || defined(STM32H7B3xx) || defined(STM32H7B3xxQ)
245245
__HAL_RCC_ADC12_CLK_ENABLE();
246246
__HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_CLKP);
247+
#elif defined(STM32G0)
248+
__HAL_RCC_ADC_CLK_ENABLE();
247249
#elif defined(STM32G4)
248250
__HAL_RCC_ADC12_CLK_ENABLE();
249251
#elif defined(STM32H7)
@@ -269,8 +271,10 @@ STATIC void adcx_init_periph(ADC_HandleTypeDef *adch, uint32_t resolution) {
269271
adch->Init.Resolution = resolution;
270272
adch->Init.ContinuousConvMode = DISABLE;
271273
adch->Init.DiscontinuousConvMode = DISABLE;
272-
#if !defined(STM32F0)
274+
#if !defined(STM32F0) && !defined(STM32G0)
273275
adch->Init.NbrOfDiscConversion = 0;
276+
#endif
277+
#if !defined(STM32F0)
274278
adch->Init.NbrOfConversion = 1;
275279
#endif
276280
adch->Init.EOCSelection = ADC_EOC_SINGLE_CONV;
@@ -295,7 +299,7 @@ STATIC void adcx_init_periph(ADC_HandleTypeDef *adch, uint32_t resolution) {
295299
adch->Init.OversamplingMode = DISABLE;
296300
adch->Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE;
297301
adch->Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR;
298-
#elif defined(STM32G4) || defined(STM32L4) || defined(STM32WB)
302+
#elif defined(STM32G0) || defined(STM32G4) || defined(STM32L4) || defined(STM32WB)
299303
adch->Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
300304
adch->Init.ScanConvMode = ADC_SCAN_DISABLE;
301305
adch->Init.LowPowerAutoWait = DISABLE;
@@ -312,7 +316,9 @@ STATIC void adcx_init_periph(ADC_HandleTypeDef *adch, uint32_t resolution) {
312316
#if defined(STM32H7)
313317
HAL_ADCEx_Calibration_Start(adch, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED);
314318
#endif
315-
#if defined(STM32G4) || defined(STM32L4) || defined(STM32WB)
319+
#if defined(STM32G0)
320+
HAL_ADCEx_Calibration_Start(adch);
321+
#elif defined(STM32G4) || defined(STM32L4) || defined(STM32WB)
316322
HAL_ADCEx_Calibration_Start(adch, ADC_SINGLE_ENDED);
317323
#endif
318324
}
@@ -333,7 +339,7 @@ STATIC void adc_init_single(pyb_obj_adc_t *adc_obj) {
333339
STATIC void adc_config_channel(ADC_HandleTypeDef *adc_handle, uint32_t channel) {
334340
ADC_ChannelConfTypeDef sConfig;
335341

336-
#if defined(STM32G4) || defined(STM32H7) || defined(STM32L4) || defined(STM32WB)
342+
#if defined(STM32G0) || defined(STM32G4) || defined(STM32H7) || defined(STM32L4) || defined(STM32WB)
337343
sConfig.Rank = ADC_REGULAR_RANK_1;
338344
if (__HAL_ADC_IS_CHANNEL_INTERNAL(channel) == 0) {
339345
channel = __HAL_ADC_DECIMAL_NB_TO_CHANNEL(channel);
@@ -361,6 +367,12 @@ STATIC void adc_config_channel(ADC_HandleTypeDef *adc_handle, uint32_t channel)
361367
sConfig.OffsetNumber = ADC_OFFSET_NONE;
362368
sConfig.OffsetRightShift = DISABLE;
363369
sConfig.OffsetSignedSaturation = DISABLE;
370+
#elif defined(STM32G0)
371+
if (__HAL_ADC_IS_CHANNEL_INTERNAL(channel)) {
372+
sConfig.SamplingTime = ADC_SAMPLETIME_160CYCLES_5;
373+
} else {
374+
sConfig.SamplingTime = ADC_SAMPLETIME_12CYCLES_5;
375+
}
364376
#elif defined(STM32G4) || defined(STM32L4) || defined(STM32WB)
365377
if (__HAL_ADC_IS_CHANNEL_INTERNAL(channel)) {
366378
sConfig.SamplingTime = ADC_SAMPLETIME_247CYCLES_5;
@@ -545,7 +557,7 @@ STATIC mp_obj_t adc_read_timed(mp_obj_t self_in, mp_obj_t buf_in, mp_obj_t freq_
545557
// for subsequent samples we can just set the "start sample" bit
546558
#if defined(STM32F4) || defined(STM32F7)
547559
self->handle.Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;
548-
#elif defined(STM32F0) || defined(STM32G4) || defined(STM32H7) || defined(STM32L4) || defined(STM32WB)
560+
#elif defined(STM32F0) || defined(STM32G0) || defined(STM32G4) || defined(STM32H7) || defined(STM32L4) || defined(STM32WB)
549561
SET_BIT(self->handle.Instance->CR, ADC_CR_ADSTART);
550562
#else
551563
#error Unsupported processor
@@ -655,7 +667,7 @@ STATIC mp_obj_t adc_read_timed_multi(mp_obj_t adc_array_in, mp_obj_t buf_array_i
655667
// ADC is started: set the "start sample" bit
656668
#if defined(STM32F4) || defined(STM32F7)
657669
adc->handle.Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;
658-
#elif defined(STM32F0) || defined(STM32G4) || defined(STM32H7) || defined(STM32L4) || defined(STM32WB)
670+
#elif defined(STM32F0) || defined(STM32G0) || defined(STM32G4) || defined(STM32H7) || defined(STM32L4) || defined(STM32WB)
659671
SET_BIT(adc->handle.Instance->CR, ADC_CR_ADSTART);
660672
#else
661673
#error Unsupported processor

‎ports/stm32/adc.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static inline void adc_deselect_vbat(ADC_TypeDef *adc, uint32_t channel) {
4242
if (channel == ADC_CHANNEL_VBAT) {
4343
ADC_Common_TypeDef *adc_common;
4444

45-
#if defined(STM32F0) || defined(STM32WB)
45+
#if defined(STM32F0) || defined(STM32G0) || defined(STM32WB)
4646
adc_common = ADC1_COMMON;
4747
#elif defined(STM32F4)
4848
adc_common = ADC_COMMON_REGISTER(0);
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_STM32G0XX_HAL_CONF_BASE_H
28+
#define MICROPY_INCLUDED_STM32G0XX_HAL_CONF_BASE_H
29+
30+
// Oscillator values in Hz
31+
// These must come before the HAL headers because stm32g0xx_ll_rcc.h will define HSI_VALUE unless already defined
32+
#define HSI_VALUE (16000000)
33+
#define LSI_VALUE (32000)
34+
#if defined(STM32G0C1xx) || defined(STM32G0B1xx) || defined(STM32G0B0xx)
35+
#define HSI48_VALUE 48000000
36+
#endif
37+
38+
// Include various HAL modules for convenience
39+
40+
#include "stm32g0xx_hal_rcc.h"
41+
#include "stm32g0xx_hal_gpio.h"
42+
#include "stm32g0xx_hal_dma.h"
43+
#include "stm32g0xx_hal_cortex.h"
44+
#include "stm32g0xx_hal_adc.h"
45+
#include "stm32g0xx_hal_adc_ex.h"
46+
#include "stm32g0xx_hal_cec.h"
47+
#include "stm32g0xx_hal_comp.h"
48+
#include "stm32g0xx_hal_crc.h"
49+
#include "stm32g0xx_hal_cryp.h"
50+
#include "stm32g0xx_hal_dac.h"
51+
#include "stm32g0xx_hal_exti.h"
52+
#include "stm32g0xx_hal_fdcan.h"
53+
#include "stm32g0xx_hal_flash.h"
54+
#include "stm32g0xx_hal_i2c.h"
55+
#include "stm32g0xx_hal_i2s.h"
56+
#include "stm32g0xx_hal_irda.h"
57+
#include "stm32g0xx_hal_iwdg.h"
58+
#include "stm32g0xx_hal_lptim.h"
59+
#include "stm32g0xx_hal_pcd.h"
60+
#include "stm32g0xx_hal_hcd.h"
61+
#include "stm32g0xx_hal_pwr.h"
62+
#include "stm32g0xx_hal_rng.h"
63+
#include "stm32g0xx_hal_rtc.h"
64+
#include "stm32g0xx_hal_smartcard.h"
65+
#include "stm32g0xx_hal_smbus.h"
66+
#include "stm32g0xx_hal_spi.h"
67+
#include "stm32g0xx_hal_tim.h"
68+
#include "stm32g0xx_hal_uart.h"
69+
#include "stm32g0xx_hal_usart.h"
70+
#include "stm32g0xx_hal_wwdg.h"
71+
72+
#include "stm32g0xx_ll_lpuart.h"
73+
#include "stm32g0xx_ll_rtc.h"
74+
#include "stm32g0xx_ll_usart.h"
75+
76+
// Enable various HAL modules
77+
#define HAL_MODULE_ENABLED
78+
#define HAL_ADC_MODULE_ENABLED
79+
#define HAL_CORTEX_MODULE_ENABLED
80+
#define HAL_DMA_MODULE_ENABLED
81+
#define HAL_EXTI_MODULE_ENABLED
82+
#define HAL_FLASH_MODULE_ENABLED
83+
#define HAL_GPIO_MODULE_ENABLED
84+
#define HAL_I2C_MODULE_ENABLED
85+
#define HAL_PWR_MODULE_ENABLED
86+
#define HAL_RCC_MODULE_ENABLED
87+
#define HAL_RTC_MODULE_ENABLED
88+
#define HAL_SPI_MODULE_ENABLED
89+
#define HAL_TIM_MODULE_ENABLED
90+
#define HAL_UART_MODULE_ENABLED
91+
#define HAL_USART_MODULE_ENABLED
92+
93+
// SysTick has the highest priority
94+
#define TICK_INT_PRIORITY (0x00)
95+
96+
// Miscellaneous HAL settings
97+
#define USE_RTOS 0
98+
#define PREFETCH_ENABLE 1
99+
#define INSTRUCTION_CACHE_ENABLE 1
100+
#define USE_SPI_CRC 1
101+
#define USE_HAL_CRYP_SUSPEND_RESUME 1
102+
103+
// HAL parameter assertions are disabled
104+
#define assert_param(expr) ((void)0)
105+
106+
#endif // MICROPY_INCLUDED_STM32G0XX_HAL_CONF_BASE_H

‎ports/stm32/dac.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ STATIC uint32_t TIMx_Config(mp_obj_t timer) {
124124

125125
STATIC void dac_deinit(uint32_t dac_channel) {
126126
DAC->CR &= ~(DAC_CR_EN1 << dac_channel);
127-
#if defined(STM32G4) || defined(STM32H7) || defined(STM32L4)
127+
#if defined(STM32G0) || defined(STM32G4) || defined(STM32H7) || defined(STM32L4)
128128
DAC->MCR = (DAC->MCR & ~(DAC_MCR_MODE1_Msk << dac_channel)) | (DAC_OUTPUTBUFFER_DISABLE << dac_channel);
129129
#else
130130
DAC->CR |= DAC_CR_BOFF1 << dac_channel;
@@ -142,7 +142,7 @@ STATIC void dac_config_channel(uint32_t dac_channel, uint32_t trig, uint32_t out
142142
DAC->CR &= ~(DAC_CR_EN1 << dac_channel);
143143
uint32_t cr_off = DAC_CR_DMAEN1 | DAC_CR_MAMP1 | DAC_CR_WAVE1 | DAC_CR_TSEL1 | DAC_CR_TEN1;
144144
uint32_t cr_on = trig;
145-
#if defined(STM32G4) || defined(STM32H7) || defined(STM32L4)
145+
#if defined(STM32G0) || defined(STM32G4) || defined(STM32H7) || defined(STM32L4)
146146
DAC->MCR = (DAC->MCR & ~(DAC_MCR_MODE1_Msk << dac_channel)) | (outbuf << dac_channel);
147147
#else
148148
cr_off |= DAC_CR_BOFF1;
@@ -259,7 +259,7 @@ STATIC mp_obj_t pyb_dac_init_helper(pyb_dac_obj_t *self, size_t n_args, const mp
259259
__DAC_CLK_ENABLE();
260260
#elif defined(STM32H7)
261261
__HAL_RCC_DAC12_CLK_ENABLE();
262-
#elif defined(STM32F0) || defined(STM32G4) || defined(STM32L4)
262+
#elif defined(STM32F0) || defined(STM32G0) || defined(STM32G4) || defined(STM32L4)
263263
__HAL_RCC_DAC1_CLK_ENABLE();
264264
#else
265265
#error Unsupported Processor

0 commit comments

Comments
 (0)