Skip to content

Commit 3354327

Browse files
Merge pull request #5188 from bcostm/fix_dac_f207zg
NUCLEO_F207ZG: Analogout improvement
2 parents 656aa82 + 02598d5 commit 3354327

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

targets/TARGET_STM/TARGET_STM32F2/analogout_device.c

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* mbed Microcontroller Library
2-
* Copyright (c) 2016, STMicroelectronics
2+
* Copyright (c) 2017, STMicroelectronics
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -25,26 +25,28 @@
2525
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2626
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2727
*/
28+
#include "mbed_assert.h"
2829
#include "analogout_api.h"
2930

3031
#if DEVICE_ANALOGOUT
3132

3233
#include "cmsis.h"
3334
#include "pinmap.h"
3435
#include "mbed_error.h"
35-
#include "stm32f2xx_hal.h"
3636
#include "PeripheralPins.h"
3737

3838
void analogout_init(dac_t *obj, PinName pin)
3939
{
40-
DAC_ChannelConfTypeDef sConfig;
40+
DAC_ChannelConfTypeDef sConfig = {0};
4141

42-
// Get the peripheral name (DAC_1, ...) from the pin and assign it to the object
42+
// Get the peripheral name from the pin and assign it to the object
4343
obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
44-
// Get the functions (dac channel) from the pin and assign it to the object
44+
MBED_ASSERT(obj->dac != (DACName)NC);
45+
46+
// Get the pin function and assign the used channel to the object
4547
uint32_t function = pinmap_function(pin, PinMap_DAC);
4648
MBED_ASSERT(function != (uint32_t)NC);
47-
// Save the channel for the write and read functions
49+
4850
switch (STM_PIN_CHANNEL(function)) {
4951
case 1:
5052
obj->channel = DAC_CHANNEL_1;
@@ -59,18 +61,19 @@ void analogout_init(dac_t *obj, PinName pin)
5961
break;
6062
}
6163

62-
if (obj->dac == (DACName)NC) {
63-
error("DAC pin mapping failed");
64-
}
65-
6664
// Configure GPIO
6765
pinmap_pinout(pin, PinMap_DAC);
6866

69-
__GPIOA_CLK_ENABLE();
67+
// Save the pin for future use
68+
obj->pin = pin;
69+
70+
// Enable DAC clock
71+
__HAL_RCC_DAC_CLK_ENABLE();
7072

71-
__DAC_CLK_ENABLE();
73+
// Configure DAC
74+
obj->handle.Instance = (DAC_TypeDef *)(obj->dac);
75+
obj->handle.State = HAL_DAC_STATE_RESET;
7276

73-
obj->handle.Instance = DAC;
7477
if (HAL_DAC_Init(&obj->handle) != HAL_OK ) {
7578
error("HAL_DAC_Init failed");
7679
}
@@ -87,8 +90,13 @@ void analogout_init(dac_t *obj, PinName pin)
8790

8891
void analogout_free(dac_t *obj)
8992
{
90-
}
91-
93+
// Reset DAC and disable clock
94+
__HAL_RCC_DAC_FORCE_RESET();
95+
__HAL_RCC_DAC_RELEASE_RESET();
96+
__HAL_RCC_DAC_CLK_DISABLE();
9297

98+
// Configure GPIO
99+
pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
100+
}
93101

94102
#endif // DEVICE_ANALOGOUT

targets/TARGET_STM/TARGET_STM32F2/objects.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ struct analogin_s {
6262

6363
struct dac_s {
6464
DACName dac;
65-
uint8_t channel;
65+
PinName pin;
66+
uint32_t channel;
6667
DAC_HandleTypeDef handle;
6768
};
6869

0 commit comments

Comments
 (0)