Skip to content

Commit 6433521

Browse files
committed
[STM32_L0] hal improvements
- pins settings - check STM32Cube HAL_Init return value
1 parent 7b6a5ea commit 6433521

File tree

15 files changed

+116
-91
lines changed

15 files changed

+116
-91
lines changed

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32L0/PeripheralPins.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* mbed Microcontroller Library
22
*******************************************************************************
3-
* Copyright (c) 2014, STMicroelectronics
3+
* Copyright (c) 2015, STMicroelectronics
44
* All rights reserved.
55
*
66
* Redistribution and use in source and binary forms, with or without

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32L0/analogin_api.c

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* mbed Microcontroller Library
2-
* Copyright (c) 2014, STMicroelectronics
2+
* Copyright (c) 2015, STMicroelectronics
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -33,6 +33,7 @@
3333
#include "wait_api.h"
3434
#include "cmsis.h"
3535
#include "pinmap.h"
36+
#include "mbed_error.h"
3637
#include "PeripheralPins.h"
3738

3839
ADC_HandleTypeDef AdcHandle;
@@ -45,6 +46,11 @@ void analogin_init(analogin_t *obj, PinName pin)
4546
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
4647
MBED_ASSERT(obj->adc != (ADCName)NC);
4748

49+
// Get the pin function and assign the used channel to the object
50+
uint32_t function = pinmap_function(pin, PinMap_ADC);
51+
MBED_ASSERT(function != (uint32_t)NC);
52+
obj->channel = STM_PIN_CHANNEL(function);
53+
4854
// Configure GPIO
4955
pinmap_pinout(pin, PinMap_ADC);
5056

@@ -77,7 +83,10 @@ void analogin_init(analogin_t *obj, PinName pin)
7783
AdcHandle.Init.LowPowerAutoWait = ENABLE;
7884
AdcHandle.Init.LowPowerFrequencyMode = DISABLE; // To be enabled only if ADC clock < 2.8 MHz
7985
AdcHandle.Init.LowPowerAutoPowerOff = DISABLE;
80-
HAL_ADC_Init(&AdcHandle);
86+
87+
if (HAL_ADC_Init(&AdcHandle) != HAL_OK) {
88+
error("Cannot initialize ADC");
89+
}
8190

8291
// Calibration
8392
HAL_ADCEx_Calibration_Start(&AdcHandle, ADC_SINGLE_ENDED);
@@ -88,58 +97,58 @@ void analogin_init(analogin_t *obj, PinName pin)
8897

8998
static inline uint16_t adc_read(analogin_t *obj)
9099
{
91-
ADC_ChannelConfTypeDef sConfig;
100+
ADC_ChannelConfTypeDef sConfig = {0};
92101

93102
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
94103

95104
// Configure ADC channel
96-
switch (obj->pin) {
97-
case PA_0:
105+
switch (obj->channel) {
106+
case 0:
98107
sConfig.Channel = ADC_CHANNEL_0;
99108
break;
100-
case PA_1:
109+
case 1:
101110
sConfig.Channel = ADC_CHANNEL_1;
102111
break;
103-
case PA_2:
112+
case 2:
104113
sConfig.Channel = ADC_CHANNEL_2;
105114
break;
106-
case PA_3:
115+
case 3:
107116
sConfig.Channel = ADC_CHANNEL_3;
108117
break;
109-
case PA_4:
118+
case 4:
110119
sConfig.Channel = ADC_CHANNEL_4;
111120
break;
112-
case PA_5:
121+
case 5:
113122
sConfig.Channel = ADC_CHANNEL_5;
114123
break;
115-
case PA_6:
124+
case 6:
116125
sConfig.Channel = ADC_CHANNEL_6;
117126
break;
118-
case PA_7:
127+
case 7:
119128
sConfig.Channel = ADC_CHANNEL_7;
120129
break;
121-
case PB_0:
130+
case 8:
122131
sConfig.Channel = ADC_CHANNEL_8;
123132
break;
124-
case PB_1:
133+
case 9:
125134
sConfig.Channel = ADC_CHANNEL_9;
126135
break;
127-
case PC_0:
136+
case 10:
128137
sConfig.Channel = ADC_CHANNEL_10;
129138
break;
130-
case PC_1:
139+
case 11:
131140
sConfig.Channel = ADC_CHANNEL_11;
132141
break;
133-
case PC_2:
142+
case 12:
134143
sConfig.Channel = ADC_CHANNEL_12;
135144
break;
136-
case PC_3:
145+
case 13:
137146
sConfig.Channel = ADC_CHANNEL_13;
138147
break;
139-
case PC_4:
148+
case 14:
140149
sConfig.Channel = ADC_CHANNEL_14;
141150
break;
142-
case PC_5:
151+
case 15:
143152
sConfig.Channel = ADC_CHANNEL_15;
144153
break;
145154
default:

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32L0/analogout_api.c

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* mbed Microcontroller Library
2-
* Copyright (c) 2014, STMicroelectronics
2+
* Copyright (c) 2015, STMicroelectronics
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -40,8 +40,8 @@
4040
static DAC_HandleTypeDef DacHandle;
4141

4242
// These variables are used for the "free" function
43-
static int pa4_used = 0;
44-
static int pa5_used = 0;
43+
static int channel1_used = 0;
44+
static int channel2_used = 0;
4545

4646
void analogout_init(dac_t *obj, PinName pin)
4747
{
@@ -51,6 +51,11 @@ void analogout_init(dac_t *obj, PinName pin)
5151
obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
5252
MBED_ASSERT(obj->dac != (DACName)NC);
5353

54+
// Get the pin function and assign the used channel to the object
55+
uint32_t function = pinmap_function(pin, PinMap_DAC);
56+
MBED_ASSERT(function != (uint32_t)NC);
57+
obj->channel = STM_PIN_CHANNEL(function);
58+
5459
// Configure GPIO
5560
pinmap_pinout(pin, PinMap_DAC);
5661

@@ -66,28 +71,33 @@ void analogout_init(dac_t *obj, PinName pin)
6671
sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
6772
sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE;
6873

69-
if (pin == PA_4) {
70-
HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1);
71-
pa4_used = 1;
72-
}
73-
7474
#if defined(DAC_CHANNEL_2)
75-
if (pin == PA_5) {
76-
HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_2);
77-
pa5_used = 1;
78-
}
75+
if (obj->channel == 2) {
76+
if (HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_2) != HAL_OK) {
77+
error("Cannot configure DAC channel 2");
78+
}
79+
channel2_used = 1;
80+
} else
7981
#endif
82+
{
83+
// channel 1 per default
84+
if (HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1) != HAL_OK) {
85+
error("Cannot configure DAC channel 1");
86+
}
87+
obj->channel = 1;
88+
channel1_used = 1;
89+
}
8090

8191
analogout_write_u16(obj, 0);
8292
}
8393

8494
void analogout_free(dac_t *obj)
8595
{
8696
// Reset DAC and disable clock
87-
if (obj->pin == PA_4) pa4_used = 0;
88-
if (obj->pin == PA_5) pa5_used = 0;
97+
if (obj->channel == 1) channel1_used = 0;
98+
if (obj->channel == 2) channel2_used = 0;
8999

90-
if ((pa4_used == 0) && (pa5_used == 0)) {
100+
if ((channel1_used == 0) && (channel2_used == 0)) {
91101
__DAC_FORCE_RESET();
92102
__DAC_RELEASE_RESET();
93103
__DAC_CLK_DISABLE();
@@ -99,13 +109,12 @@ void analogout_free(dac_t *obj)
99109

100110
static inline void dac_write(dac_t *obj, uint16_t value)
101111
{
102-
if (obj->pin == PA_4) {
112+
if (obj->channel == 1) {
103113
HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, value);
104114
HAL_DAC_Start(&DacHandle, DAC_CHANNEL_1);
105115
}
106-
107116
#if defined(DAC_CHANNEL_2)
108-
if (obj->pin == PA_5) {
117+
if (obj->channel == 2) {
109118
HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value);
110119
HAL_DAC_Start(&DacHandle, DAC_CHANNEL_2);
111120
}
@@ -114,17 +123,15 @@ static inline void dac_write(dac_t *obj, uint16_t value)
114123

115124
static inline int dac_read(dac_t *obj)
116125
{
117-
if (obj->pin == PA_4) {
126+
if (obj->channel == 1) {
118127
return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1);
119128
}
120129
#if defined(DAC_CHANNEL_2)
121-
else if (obj->pin == PA_5) {
130+
if (obj->channel == 2) {
122131
return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_2);
123132
}
124133
#endif
125-
else {
126-
return 0;
127-
}
134+
return 0;
128135
}
129136

130137
void analogout_write(dac_t *obj, float value)

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32L0/gpio_api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* mbed Microcontroller Library
22
*******************************************************************************
3-
* Copyright (c) 2014, STMicroelectronics
3+
* Copyright (c) 2015, STMicroelectronics
44
* All rights reserved.
55
*
66
* Redistribution and use in source and binary forms, with or without

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32L0/gpio_irq_api.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* mbed Microcontroller Library
22
*******************************************************************************
3-
* Copyright (c) 2014, STMicroelectronics
3+
* Copyright (c) 2015, STMicroelectronics
44
* All rights reserved.
55
*
66
* Redistribution and use in source and binary forms, with or without
@@ -76,8 +76,8 @@ static uint32_t pin_base_nr[16] = {
7676
7, // pin 11
7777
8, // pin 12
7878
9, // pin 13
79-
10, // pin 14
80-
11 // pin 15
79+
10, // pin 14
80+
11 // pin 15
8181
};
8282

8383
static gpio_irq_handler irq_handler;

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32L0/gpio_object.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* mbed Microcontroller Library
22
*******************************************************************************
3-
* Copyright (c) 2014, STMicroelectronics
3+
* Copyright (c) 2015, STMicroelectronics
44
* All rights reserved.
55
*
66
* Redistribution and use in source and binary forms, with or without
@@ -64,7 +64,8 @@ static inline int gpio_read(gpio_t *obj)
6464
return ((*obj->reg_in & obj->mask) ? 1 : 0);
6565
}
6666

67-
static inline int gpio_is_connected(const gpio_t *obj) {
67+
static inline int gpio_is_connected(const gpio_t *obj)
68+
{
6869
return obj->pin != (PinName)NC;
6970
}
7071

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32L0/i2c_api.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* mbed Microcontroller Library
22
*******************************************************************************
3-
* Copyright (c) 2014, STMicroelectronics
3+
* Copyright (c) 2015, STMicroelectronics
44
* All rights reserved.
55
*
66
* Redistribution and use in source and binary forms, with or without
@@ -34,6 +34,7 @@
3434

3535
#include "cmsis.h"
3636
#include "pinmap.h"
37+
#include "mbed_error.h"
3738
#include "PeripheralPins.h"
3839

3940
/* Timeout values for flags and events waiting loops. These timeouts are
@@ -135,7 +136,10 @@ void i2c_frequency(i2c_t *obj, int hz)
135136
I2cHandle.Init.OwnAddress1 = 0;
136137
I2cHandle.Init.OwnAddress2 = 0;
137138
I2cHandle.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
138-
HAL_I2C_Init(&I2cHandle);
139+
140+
if (HAL_I2C_Init(&I2cHandle) != HAL_OK) {
141+
error("Cannot initialize I2C");
142+
}
139143
}
140144

141145
inline int i2c_start(i2c_t *obj)

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32L0/mbed_overrides.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* mbed Microcontroller Library
2-
* Copyright (c) 2014, STMicroelectronics
2+
* Copyright (c) 2015, STMicroelectronics
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32L0/pinmap.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* mbed Microcontroller Library
22
*******************************************************************************
3-
* Copyright (c) 2014, STMicroelectronics
3+
* Copyright (c) 2015, STMicroelectronics
44
* All rights reserved.
55
*
66
* Redistribution and use in source and binary forms, with or without
@@ -33,6 +33,7 @@
3333
#include "mbed_error.h"
3434

3535
// GPIO mode look-up table
36+
// Warning: order must be the same as the one defined in PinNames.h !!!
3637
static const uint32_t gpio_mode[13] = {
3738
0x00000000, // 0 = GPIO_MODE_INPUT
3839
0x00000001, // 1 = GPIO_MODE_OUTPUT_PP
@@ -62,18 +63,24 @@ uint32_t Set_GPIO_Clock(uint32_t port_idx)
6263
gpio_add = GPIOB_BASE;
6364
__GPIOB_CLK_ENABLE();
6465
break;
66+
#if defined(GPIOC_BASE)
6567
case PortC:
6668
gpio_add = GPIOC_BASE;
6769
__GPIOC_CLK_ENABLE();
6870
break;
71+
#endif
72+
#if defined(GPIOD_BASE)
6973
case PortD:
7074
gpio_add = GPIOD_BASE;
7175
__GPIOD_CLK_ENABLE();
7276
break;
77+
#endif
78+
#if defined(GPIOH_BASE)
7379
case PortH:
7480
gpio_add = GPIOH_BASE;
7581
__GPIOH_CLK_ENABLE();
7682
break;
83+
#endif
7784
default:
7885
error("Pinmap error: wrong port number.");
7986
break;

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32L0/port_api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* mbed Microcontroller Library
22
*******************************************************************************
3-
* Copyright (c) 2014, STMicroelectronics
3+
* Copyright (c) 2015, STMicroelectronics
44
* All rights reserved.
55
*
66
* Redistribution and use in source and binary forms, with or without

0 commit comments

Comments
 (0)