Skip to content

Commit 7ab31e8

Browse files
committed
Add new STM32F2 family (HAL)
1 parent 680a1c2 commit 7ab31e8

20 files changed

+5272
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* mbed Microcontroller Library
2+
*******************************************************************************
3+
* Copyright (c) 2016, STMicroelectronics
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright notice,
10+
* this list of conditions and the following disclaimer.
11+
* 2. Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
* 3. Neither the name of STMicroelectronics nor the names of its contributors
15+
* may be used to endorse or promote products derived from this software
16+
* without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
*******************************************************************************
29+
*/
30+
31+
#ifndef MBED_PERIPHERALPINS_H
32+
#define MBED_PERIPHERALPINS_H
33+
34+
#include "pinmap.h"
35+
#include "PeripheralNames.h"
36+
37+
//*** ADC ***
38+
39+
extern const PinMap PinMap_ADC[];
40+
41+
//*** DAC ***
42+
43+
extern const PinMap PinMap_DAC[];
44+
45+
//*** I2C ***
46+
47+
extern const PinMap PinMap_I2C_SDA[];
48+
extern const PinMap PinMap_I2C_SCL[];
49+
50+
//*** PWM ***
51+
52+
extern const PinMap PinMap_PWM[];
53+
54+
//*** SERIAL ***
55+
56+
extern const PinMap PinMap_UART_TX[];
57+
extern const PinMap PinMap_UART_RX[];
58+
extern const PinMap PinMap_UART_RTS[];
59+
extern const PinMap PinMap_UART_CTS[];
60+
61+
//*** SPI ***
62+
63+
extern const PinMap PinMap_SPI_MOSI[];
64+
extern const PinMap PinMap_SPI_MISO[];
65+
extern const PinMap PinMap_SPI_SCLK[];
66+
extern const PinMap PinMap_SPI_SSEL[];
67+
68+
//*** CAN ***
69+
extern const PinMap PinMap_CAN_RD[];
70+
extern const PinMap PinMap_CAN_TD[];
71+
72+
#endif
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* mbed Microcontroller Library
2+
*******************************************************************************
3+
* Copyright (c) 2016, STMicroelectronics
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright notice,
10+
* this list of conditions and the following disclaimer.
11+
* 2. Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
* 3. Neither the name of STMicroelectronics nor the names of its contributors
15+
* may be used to endorse or promote products derived from this software
16+
* without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
*******************************************************************************
29+
*/
30+
#ifndef MBED_PORTNAMES_H
31+
#define MBED_PORTNAMES_H
32+
33+
#ifdef __cplusplus
34+
extern "C" {
35+
#endif
36+
37+
typedef enum {
38+
PortA = 0,
39+
PortB = 1,
40+
PortC = 2,
41+
PortD = 3,
42+
PortE = 4,
43+
PortF = 5,
44+
PortG = 6,
45+
PortH = 7,
46+
PortI = 8
47+
} PortName;
48+
49+
#ifdef __cplusplus
50+
}
51+
#endif
52+
#endif
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2016, STMicroelectronics
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
* 3. Neither the name of STMicroelectronics nor the names of its contributors
14+
* may be used to endorse or promote products derived from this software
15+
* without specific prior written permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
#include "mbed_assert.h"
29+
#include "analogin_api.h"
30+
31+
#if DEVICE_ANALOGIN
32+
33+
#include "wait_api.h"
34+
#include "cmsis.h"
35+
#include "pinmap.h"
36+
#include "mbed_error.h"
37+
#include "PeripheralPins.h"
38+
39+
ADC_HandleTypeDef AdcHandle;
40+
41+
void analogin_init(analogin_t *obj, PinName pin)
42+
{
43+
#if defined(ADC1)
44+
static int adc1_inited = 0;
45+
#endif
46+
#if defined(ADC2)
47+
static int adc2_inited = 0;
48+
#endif
49+
#if defined(ADC3)
50+
static int adc3_inited = 0;
51+
#endif
52+
// Get the peripheral name from the pin and assign it to the object
53+
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
54+
MBED_ASSERT(obj->adc != (ADCName)NC);
55+
56+
// Get the functions (adc channel) from the pin and assign it to the object
57+
uint32_t function = pinmap_function(pin, PinMap_ADC);
58+
MBED_ASSERT(function != (uint32_t)NC);
59+
obj->channel = STM_PIN_CHANNEL(function);
60+
61+
// Configure GPIO
62+
pinmap_pinout(pin, PinMap_ADC);
63+
64+
// Save pin number for the read function
65+
obj->pin = pin;
66+
67+
// Check if ADC is already initialized
68+
// Enable ADC clock
69+
#if defined(ADC1)
70+
if ((obj->adc == ADC_1) && adc1_inited) return;
71+
if (obj->adc == ADC_1) {
72+
__ADC1_CLK_ENABLE();
73+
adc1_inited = 1;
74+
}
75+
#endif
76+
#if defined(ADC2)
77+
if ((obj->adc == ADC_2) && adc2_inited) return;
78+
if (obj->adc == ADC_2) {
79+
__ADC2_CLK_ENABLE();
80+
adc2_inited = 1;
81+
}
82+
#endif
83+
#if defined(ADC3)
84+
if ((obj->adc == ADC_3) && adc3_inited) return;
85+
if (obj->adc == ADC_3) {
86+
__ADC3_CLK_ENABLE();
87+
adc3_inited = 1;
88+
}
89+
#endif
90+
// Configure ADC
91+
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
92+
AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
93+
AdcHandle.Init.Resolution = ADC_RESOLUTION12b;
94+
AdcHandle.Init.ScanConvMode = DISABLE;
95+
AdcHandle.Init.ContinuousConvMode = DISABLE;
96+
AdcHandle.Init.DiscontinuousConvMode = DISABLE;
97+
AdcHandle.Init.NbrOfDiscConversion = 0;
98+
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
99+
AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
100+
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
101+
AdcHandle.Init.NbrOfConversion = 1;
102+
AdcHandle.Init.DMAContinuousRequests = DISABLE;
103+
AdcHandle.Init.EOCSelection = DISABLE;
104+
if (HAL_ADC_Init(&AdcHandle) != HAL_OK) {
105+
error("Cannot initialize ADC\n");
106+
}
107+
}
108+
109+
static inline uint16_t adc_read(analogin_t *obj)
110+
{
111+
ADC_ChannelConfTypeDef sConfig = {0};
112+
113+
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
114+
115+
// Configure ADC channel
116+
sConfig.Rank = 1;
117+
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
118+
sConfig.Offset = 0;
119+
120+
switch (obj->channel) {
121+
case 0:
122+
sConfig.Channel = ADC_CHANNEL_0;
123+
break;
124+
case 1:
125+
sConfig.Channel = ADC_CHANNEL_1;
126+
break;
127+
case 2:
128+
sConfig.Channel = ADC_CHANNEL_2;
129+
break;
130+
case 3:
131+
sConfig.Channel = ADC_CHANNEL_3;
132+
break;
133+
case 4:
134+
sConfig.Channel = ADC_CHANNEL_4;
135+
break;
136+
case 5:
137+
sConfig.Channel = ADC_CHANNEL_5;
138+
break;
139+
case 6:
140+
sConfig.Channel = ADC_CHANNEL_6;
141+
break;
142+
case 7:
143+
sConfig.Channel = ADC_CHANNEL_7;
144+
break;
145+
case 8:
146+
sConfig.Channel = ADC_CHANNEL_8;
147+
break;
148+
case 9:
149+
sConfig.Channel = ADC_CHANNEL_9;
150+
break;
151+
case 10:
152+
sConfig.Channel = ADC_CHANNEL_10;
153+
break;
154+
case 11:
155+
sConfig.Channel = ADC_CHANNEL_11;
156+
break;
157+
case 12:
158+
sConfig.Channel = ADC_CHANNEL_12;
159+
break;
160+
case 13:
161+
sConfig.Channel = ADC_CHANNEL_13;
162+
break;
163+
case 14:
164+
sConfig.Channel = ADC_CHANNEL_14;
165+
break;
166+
case 15:
167+
sConfig.Channel = ADC_CHANNEL_15;
168+
break;
169+
default:
170+
return 0;
171+
}
172+
173+
HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
174+
175+
HAL_ADC_Start(&AdcHandle); // Start conversion
176+
177+
// Wait end of conversion and get value
178+
if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
179+
return (HAL_ADC_GetValue(&AdcHandle));
180+
} else {
181+
return 0;
182+
}
183+
}
184+
185+
uint16_t analogin_read_u16(analogin_t *obj)
186+
{
187+
uint16_t value = adc_read(obj);
188+
// 12-bit to 16-bit conversion
189+
value = ((value << 4) & (uint16_t)0xFFF0) | ((value >> 8) & (uint16_t)0x000F);
190+
return value;
191+
}
192+
193+
float analogin_read(analogin_t *obj)
194+
{
195+
uint16_t value = adc_read(obj);
196+
return (float)value * (1.0f / (float)0xFFF); // 12 bits range
197+
}
198+
199+
#endif

0 commit comments

Comments
 (0)