Skip to content

Commit f45b189

Browse files
committed
STM32U5 specific driver files
1 parent e2ca71d commit f45b189

19 files changed

+2294
-0
lines changed

targets/TARGET_STM/TARGET_STM32U5/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
add_subdirectory(TARGET_STM32U575xG EXCLUDE_FROM_ALL)
55
add_subdirectory(TARGET_STM32U575xI EXCLUDE_FROM_ALL)
66
add_subdirectory(TARGET_STM32U585xI EXCLUDE_FROM_ALL)
7+
78
add_subdirectory(STM32Cube_FW EXCLUDE_FROM_ALL)
89

910
add_library(mbed-stm32u5 INTERFACE)
@@ -15,6 +16,14 @@ target_include_directories(mbed-stm32u5
1516

1617
target_sources(mbed-stm32u5
1718
INTERFACE
19+
analogin_device.c
20+
analogout_device.c
21+
flash_api.c
22+
gpio_irq_device.c
23+
pwmout_device.c
24+
i2c_device.c
25+
serial_device.c
26+
spi_api.c
1827
)
1928

2029
target_link_libraries(mbed-stm32u5 INTERFACE mbed-stm mbed-stm32u5cube-fw)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/* mbed Microcontroller Library
2+
* SPDX-License-Identifier: BSD-3-Clause
3+
******************************************************************************
4+
*
5+
* Copyright (c) 2015-2021 STMicroelectronics.
6+
* All rights reserved.
7+
*
8+
* This software component is licensed by ST under BSD 3-Clause license,
9+
* the "License"; You may not use this file except in compliance with the
10+
* License. You may obtain a copy of the License at:
11+
* opensource.org/licenses/BSD-3-Clause
12+
*
13+
******************************************************************************
14+
*/
15+
16+
#ifndef MBED_PERIPHERALNAMES_H
17+
#define MBED_PERIPHERALNAMES_H
18+
19+
#include "cmsis.h"
20+
21+
#ifdef __cplusplus
22+
extern "C" {
23+
#endif
24+
25+
typedef enum {
26+
ADC_1 = (int)ADC1_BASE,
27+
ADC_4 = (int)ADC4_BASE
28+
} ADCName;
29+
30+
typedef enum {
31+
DAC_1 = (int)DAC1_BASE
32+
} DACName;
33+
34+
typedef enum {
35+
UART_1 = (int)USART1_BASE,
36+
UART_2 = (int)USART2_BASE,
37+
UART_3 = (int)USART3_BASE,
38+
UART_4 = (int)UART4_BASE,
39+
UART_5 = (int)UART5_BASE,
40+
LPUART_1 = (int)LPUART1_BASE
41+
} UARTName;
42+
43+
typedef enum {
44+
SPI_1 = (int)SPI1_BASE,
45+
SPI_2 = (int)SPI2_BASE,
46+
SPI_3 = (int)SPI3_BASE
47+
} SPIName;
48+
49+
typedef enum {
50+
I2C_1 = (int)I2C1_BASE,
51+
I2C_2 = (int)I2C2_BASE,
52+
I2C_3 = (int)I2C3_BASE,
53+
I2C_4 = (int)I2C4_BASE
54+
} I2CName;
55+
56+
typedef enum {
57+
PWM_1 = (int)TIM1_BASE,
58+
PWM_2 = (int)TIM2_BASE,
59+
PWM_3 = (int)TIM3_BASE,
60+
PWM_4 = (int)TIM4_BASE,
61+
PWM_5 = (int)TIM5_BASE,
62+
PWM_8 = (int)TIM8_BASE,
63+
PWM_15 = (int)TIM15_BASE,
64+
PWM_16 = (int)TIM16_BASE,
65+
PWM_17 = (int)TIM17_BASE
66+
} PWMName;
67+
68+
typedef enum {
69+
CAN_1 = (int)FDCAN1_BASE
70+
} CANName;
71+
72+
typedef enum {
73+
QSPI_1 = (int)OCTOSPI1_R_BASE,
74+
QSPI_2 = (int)OCTOSPI2_R_BASE
75+
} QSPIName;
76+
77+
typedef enum {
78+
OSPI_1 = (int)OCTOSPI1_R_BASE,
79+
OSPI_2 = (int)OCTOSPI2_R_BASE
80+
} OSPIName;
81+
82+
typedef enum {
83+
USB_FS = (int)USB_OTG_FS_BASE
84+
} USBName;
85+
86+
#ifdef __cplusplus
87+
}
88+
#endif
89+
90+
#endif
Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
/* mbed Microcontroller Library
2+
* SPDX-License-Identifier: BSD-3-Clause
3+
******************************************************************************
4+
*
5+
* Copyright (c) 2015-2021 STMicroelectronics.
6+
* All rights reserved.
7+
*
8+
* This software component is licensed by ST under BSD 3-Clause license,
9+
* the "License"; You may not use this file except in compliance with the
10+
* License. You may obtain a copy of the License at:
11+
* opensource.org/licenses/BSD-3-Clause
12+
*
13+
******************************************************************************
14+
*/
15+
16+
#include "mbed_assert.h"
17+
#include "analogin_api.h"
18+
19+
#if DEVICE_ANALOGIN
20+
21+
#include "mbed_wait_api.h"
22+
#include "cmsis.h"
23+
#include "pinmap.h"
24+
#include "mbed_error.h"
25+
#include "PeripheralPins.h"
26+
27+
#if STATIC_PINMAP_READY
28+
#define ANALOGIN_INIT_DIRECT analogin_init_direct
29+
void analogin_init_direct(analogin_t *obj, const PinMap *pinmap)
30+
#else
31+
#define ANALOGIN_INIT_DIRECT _analogin_init_direct
32+
static void _analogin_init_direct(analogin_t *obj, const PinMap *pinmap)
33+
#endif
34+
{
35+
uint32_t function = (uint32_t)pinmap->function;
36+
37+
// Get the peripheral name from the pin and assign it to the object
38+
obj->handle.Instance = (ADC_TypeDef *)pinmap->peripheral;
39+
40+
// ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...)
41+
// are described in PinNames.h and PeripheralPins.c
42+
// Pin value must be between 0xF0 and 0xFF
43+
if ((pinmap->pin < 0xF0) || (pinmap->pin >= 0x100)) {
44+
// Configure GPIO
45+
pin_function(pinmap->pin, pinmap->function);
46+
pin_mode(pinmap->pin, PullNone);
47+
} else {
48+
// Internal channels
49+
// No GPIO configuration for internal channels
50+
}
51+
MBED_ASSERT(obj->handle.Instance != (ADC_TypeDef *)NC);
52+
MBED_ASSERT(function != (uint32_t)NC);
53+
54+
obj->channel = STM_PIN_CHANNEL(function);
55+
56+
// Save pin number for the read function
57+
obj->pin = pinmap->pin;
58+
59+
if (obj->handle.Instance == ADC1) {
60+
__HAL_RCC_ADC1_CLK_ENABLE();
61+
} else if (obj->handle.Instance == ADC4) {
62+
__HAL_RCC_ADC4_CLK_ENABLE();
63+
} else {
64+
error("ADC instance error\n");
65+
}
66+
67+
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
68+
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADCDAC;
69+
PeriphClkInit.AdcDacClockSelection = RCC_ADCDACCLKSOURCE_HSI;
70+
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
71+
error("HAL_RCCEx_PeriphCLKConfig\n");
72+
}
73+
74+
// Configure ADC object structures
75+
obj->handle.State = HAL_ADC_STATE_RESET;
76+
obj->handle.DMA_Handle = NULL;
77+
obj->handle.Lock = HAL_UNLOCKED;
78+
79+
obj->handle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV4;
80+
obj->handle.Init.Resolution = ADC_RESOLUTION_12B;
81+
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
82+
obj->handle.Init.ScanConvMode = ADC_SCAN_DISABLE;
83+
obj->handle.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
84+
obj->handle.Init.LowPowerAutoWait = DISABLE;
85+
obj->handle.Init.LowPowerAutoPowerOff = ADC_LOW_POWER_NONE;
86+
obj->handle.Init.ContinuousConvMode = DISABLE;
87+
obj->handle.Init.NbrOfConversion = 1;
88+
obj->handle.Init.DiscontinuousConvMode = DISABLE;
89+
obj->handle.Init.DMAContinuousRequests = DISABLE;
90+
obj->handle.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
91+
obj->handle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
92+
obj->handle.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE;
93+
obj->handle.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR;
94+
obj->handle.Init.OversamplingMode = DISABLE;
95+
obj->handle.Init.GainCompensation = 0;
96+
obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
97+
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
98+
obj->handle.Init.VrefProtection = ADC_VREF_PPROT_NONE;
99+
100+
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
101+
error("Cannot initialize ADC\n");
102+
}
103+
104+
if (HAL_ADCEx_Calibration_Start(&obj->handle, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED) != HAL_OK) {
105+
error("HAL_ADCEx_Calibration_Start\n");
106+
}
107+
}
108+
109+
void analogin_init(analogin_t *obj, PinName pin)
110+
{
111+
int peripheral;
112+
int function;
113+
114+
if ((pin < 0xF0) || (pin >= 0x100)) {
115+
peripheral = (int)pinmap_peripheral(pin, PinMap_ADC);
116+
function = (int)pinmap_find_function(pin, PinMap_ADC);
117+
} else {
118+
peripheral = (int)pinmap_peripheral(pin, PinMap_ADC_Internal);
119+
function = (int)pinmap_find_function(pin, PinMap_ADC_Internal);
120+
}
121+
122+
const PinMap static_pinmap = {pin, peripheral, function};
123+
124+
ANALOGIN_INIT_DIRECT(obj, &static_pinmap);
125+
}
126+
127+
128+
uint16_t adc_read(analogin_t *obj)
129+
{
130+
ADC_ChannelConfTypeDef sConfig = {0};
131+
132+
// Configure ADC channel
133+
if (obj->handle.Instance == ADC1) {
134+
sConfig.Rank = ADC_REGULAR_RANK_1;
135+
sConfig.SamplingTime = ADC_SAMPLETIME_36CYCLES;
136+
} else if (obj->handle.Instance == ADC4) {
137+
sConfig.Rank = ADC4_REGULAR_RANK_1;
138+
sConfig.SamplingTime = ADC4_SAMPLETIME_12CYCLES_5;
139+
}
140+
sConfig.SingleDiff = ADC_SINGLE_ENDED;
141+
sConfig.OffsetNumber = ADC_OFFSET_NONE;
142+
sConfig.Offset = 0;
143+
sConfig.OffsetRightShift = DISABLE;
144+
sConfig.OffsetSignedSaturation = DISABLE;
145+
146+
switch (obj->channel) {
147+
case 0:
148+
sConfig.Channel = ADC_CHANNEL_VREFINT;
149+
sConfig.SamplingTime = ADC_SAMPLETIME_391CYCLES_5;
150+
break;
151+
case 1:
152+
sConfig.Channel = ADC_CHANNEL_1;
153+
break;
154+
case 2:
155+
sConfig.Channel = ADC_CHANNEL_2;
156+
break;
157+
case 3:
158+
sConfig.Channel = ADC_CHANNEL_3;
159+
break;
160+
case 4:
161+
sConfig.Channel = ADC_CHANNEL_4;
162+
break;
163+
case 5:
164+
sConfig.Channel = ADC_CHANNEL_5;
165+
break;
166+
case 6:
167+
sConfig.Channel = ADC_CHANNEL_6;
168+
break;
169+
case 7:
170+
sConfig.Channel = ADC_CHANNEL_7;
171+
break;
172+
case 8:
173+
sConfig.Channel = ADC_CHANNEL_8;
174+
break;
175+
case 9:
176+
sConfig.Channel = ADC_CHANNEL_9;
177+
break;
178+
case 10:
179+
sConfig.Channel = ADC_CHANNEL_10;
180+
break;
181+
case 11:
182+
sConfig.Channel = ADC_CHANNEL_11;
183+
break;
184+
case 12:
185+
sConfig.Channel = ADC_CHANNEL_12;
186+
break;
187+
case 13:
188+
sConfig.Channel = ADC_CHANNEL_13;
189+
break;
190+
case 14:
191+
sConfig.Channel = ADC_CHANNEL_14;
192+
break;
193+
case 15:
194+
sConfig.Channel = ADC_CHANNEL_15;
195+
break;
196+
case 16:
197+
sConfig.Channel = ADC_CHANNEL_16;
198+
break;
199+
case 17:
200+
sConfig.Channel = ADC_CHANNEL_17;
201+
break;
202+
case 18:
203+
sConfig.Channel = ADC_CHANNEL_VBAT;
204+
sConfig.SamplingTime = ADC_SAMPLETIME_814CYCLES;
205+
break;
206+
case 19:
207+
sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
208+
sConfig.SamplingTime = ADC_SAMPLETIME_391CYCLES_5;
209+
break;
210+
default:
211+
error("ADC channel not expected\n");
212+
}
213+
214+
if (HAL_ADC_ConfigChannel(&obj->handle, &sConfig) != HAL_OK) {
215+
error("HAL_ADC_ConfigChannel\n");
216+
}
217+
218+
if (HAL_ADC_Start(&obj->handle) != HAL_OK) {
219+
error("HAL_ADC_Start\n");
220+
}
221+
222+
// Wait end of conversion and get value
223+
uint16_t adcValue = 0;
224+
if (HAL_ADC_PollForConversion(&obj->handle, 10) != HAL_OK) {
225+
error("HAL_ADC_PollForConversion error 0x%x\n", obj->handle.State);
226+
} else {
227+
adcValue = (uint16_t)HAL_ADC_GetValue(&obj->handle);
228+
}
229+
230+
if (HAL_ADC_Stop(&obj->handle) != HAL_OK) {
231+
error("HAL_ADC_Stop\n");
232+
}
233+
234+
LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE((&obj->handle)->Instance), LL_ADC_PATH_INTERNAL_NONE);
235+
236+
return adcValue;
237+
}
238+
239+
const PinMap *analogin_pinmap()
240+
{
241+
return PinMap_ADC;
242+
}
243+
244+
#endif

0 commit comments

Comments
 (0)