Skip to content

Commit bee5d44

Browse files
committed
STM32L5: add API L5 family files
1 parent 5d59c99 commit bee5d44

18 files changed

+2336
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/* mbed Microcontroller Library
2+
* SPDX-License-Identifier: BSD-3-Clause
3+
******************************************************************************
4+
*
5+
* Copyright (c) 2017 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_2 = (int)ADC2_BASE
28+
} ADCName;
29+
30+
typedef enum {
31+
DAC_1 = (int)DAC_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+
} QSPIName;
75+
76+
typedef enum {
77+
USB_FS = (int)USB_BASE,
78+
} USBName;
79+
80+
#ifdef __cplusplus
81+
}
82+
#endif
83+
84+
#endif
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
/* mbed Microcontroller Library
2+
* SPDX-License-Identifier: BSD-3-Clause
3+
******************************************************************************
4+
*
5+
* Copyright (c) 2015 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+
// Configure ADC object structures
60+
obj->handle.State = HAL_ADC_STATE_RESET;
61+
obj->handle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV2; // Asynchronous clock mode, input ADC clock
62+
obj->handle.Init.Resolution = ADC_RESOLUTION_12B;
63+
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
64+
obj->handle.Init.ScanConvMode = DISABLE; // Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1)
65+
obj->handle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; // On STM32L1xx ADC, overrun detection is enabled only if EOC selection is set to each conversion (or transfer by DMA enabled, this is not the case in this example).
66+
obj->handle.Init.LowPowerAutoWait = DISABLE;
67+
obj->handle.Init.ContinuousConvMode = DISABLE; // Continuous mode disabled to have only 1 conversion at each conversion trig
68+
obj->handle.Init.NbrOfConversion = 1; // Parameter discarded because sequencer is disabled
69+
obj->handle.Init.DiscontinuousConvMode = DISABLE; // Parameter discarded because sequencer is disabled
70+
obj->handle.Init.NbrOfDiscConversion = 1; // Parameter discarded because sequencer is disabled
71+
obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START; // Software start to trig the 1st conversion manually, without external event
72+
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
73+
obj->handle.Init.DMAContinuousRequests = DISABLE;
74+
obj->handle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; // DR register is overwritten with the last conversion result in case of overrun
75+
obj->handle.Init.OversamplingMode = DISABLE; // No oversampling
76+
#if defined(ADC_CFGR_DFSDMCFG) &&defined(DFSDM1_Channel0)
77+
obj->handle.Init.DFSDMConfig = 0;
78+
#endif
79+
80+
// Enable ADC clock
81+
__HAL_RCC_ADC_CLK_ENABLE();
82+
__HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_SYSCLK);
83+
84+
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
85+
error("Cannot initialize ADC\n");
86+
}
87+
88+
// ADC calibration is done only once
89+
if (!HAL_ADCEx_Calibration_GetValue(&obj->handle, ADC_SINGLE_ENDED)) {
90+
HAL_ADCEx_Calibration_Start(&obj->handle, ADC_SINGLE_ENDED);
91+
}
92+
}
93+
94+
void analogin_init(analogin_t *obj, PinName pin)
95+
{
96+
int peripheral;
97+
int function;
98+
99+
if ((pin < 0xF0) || (pin >= 0x100)) {
100+
peripheral = (int)pinmap_peripheral(pin, PinMap_ADC);
101+
function = (int)pinmap_find_function(pin, PinMap_ADC);
102+
} else {
103+
peripheral = (int)pinmap_peripheral(pin, PinMap_ADC_Internal);
104+
function = (int)pinmap_find_function(pin, PinMap_ADC_Internal);
105+
}
106+
107+
const PinMap static_pinmap = {pin, peripheral, function};
108+
109+
ANALOGIN_INIT_DIRECT(obj, &static_pinmap);
110+
}
111+
112+
113+
uint16_t adc_read(analogin_t *obj)
114+
{
115+
ADC_ChannelConfTypeDef sConfig = {0};
116+
117+
// Configure ADC channel
118+
sConfig.Rank = ADC_REGULAR_RANK_1;
119+
sConfig.SamplingTime = ADC_SAMPLETIME_47CYCLES_5; // default value (1.5 us for 80MHz clock)
120+
sConfig.SingleDiff = ADC_SINGLE_ENDED;
121+
sConfig.OffsetNumber = ADC_OFFSET_NONE;
122+
sConfig.Offset = 0;
123+
124+
switch (obj->channel) {
125+
case 0:
126+
sConfig.Channel = ADC_CHANNEL_VREFINT;
127+
sConfig.SamplingTime = ADC_SAMPLETIME_247CYCLES_5; // Minimum ADC sampling time when reading the internal reference voltage is 4us
128+
break;
129+
case 1:
130+
sConfig.Channel = ADC_CHANNEL_1;
131+
break;
132+
case 2:
133+
sConfig.Channel = ADC_CHANNEL_2;
134+
break;
135+
case 3:
136+
sConfig.Channel = ADC_CHANNEL_3;
137+
break;
138+
case 4:
139+
sConfig.Channel = ADC_CHANNEL_4;
140+
break;
141+
case 5:
142+
sConfig.Channel = ADC_CHANNEL_5;
143+
break;
144+
case 6:
145+
sConfig.Channel = ADC_CHANNEL_6;
146+
break;
147+
case 7:
148+
sConfig.Channel = ADC_CHANNEL_7;
149+
break;
150+
case 8:
151+
sConfig.Channel = ADC_CHANNEL_8;
152+
break;
153+
case 9:
154+
sConfig.Channel = ADC_CHANNEL_9;
155+
break;
156+
case 10:
157+
sConfig.Channel = ADC_CHANNEL_10;
158+
break;
159+
case 11:
160+
sConfig.Channel = ADC_CHANNEL_11;
161+
break;
162+
case 12:
163+
sConfig.Channel = ADC_CHANNEL_12;
164+
break;
165+
case 13:
166+
sConfig.Channel = ADC_CHANNEL_13;
167+
break;
168+
case 14:
169+
sConfig.Channel = ADC_CHANNEL_14;
170+
break;
171+
case 15:
172+
sConfig.Channel = ADC_CHANNEL_15;
173+
break;
174+
case 16:
175+
sConfig.Channel = ADC_CHANNEL_16;
176+
break;
177+
case 17:
178+
sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
179+
sConfig.SamplingTime = ADC_SAMPLETIME_247CYCLES_5; // Minimum ADC sampling time when reading the temperature is 5us
180+
break;
181+
case 18:
182+
sConfig.Channel = ADC_CHANNEL_VBAT;
183+
sConfig.SamplingTime = ADC_SAMPLETIME_640CYCLES_5; // Minimum ADC sampling time when reading the VBAT is 12us
184+
break;
185+
default:
186+
return 0;
187+
}
188+
189+
HAL_ADC_ConfigChannel(&obj->handle, &sConfig);
190+
191+
HAL_ADC_Start(&obj->handle); // Start conversion
192+
193+
// Wait end of conversion and get value
194+
uint16_t adcValue = 0;
195+
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
196+
adcValue = (uint16_t)HAL_ADC_GetValue(&obj->handle);
197+
}
198+
LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE((&obj->handle)->Instance), LL_ADC_PATH_INTERNAL_NONE);
199+
return adcValue;
200+
}
201+
202+
const PinMap *analogin_pinmap()
203+
{
204+
return PinMap_ADC;
205+
}
206+
207+
#endif

0 commit comments

Comments
 (0)