1
1
/* mbed Microcontroller Library
2
- * Copyright (c) 2016 , STMicroelectronics
2
+ * Copyright (c) 2017 , STMicroelectronics
3
3
* All rights reserved.
4
4
*
5
5
* Redistribution and use in source and binary forms, with or without
25
25
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
26
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
27
*/
28
+ #include "mbed_assert.h"
28
29
#include "analogout_api.h"
29
30
30
31
#if DEVICE_ANALOGOUT
31
32
32
33
#include "cmsis.h"
33
34
#include "pinmap.h"
34
35
#include "mbed_error.h"
35
- #include "stm32f2xx_hal.h"
36
36
#include "PeripheralPins.h"
37
37
38
38
void analogout_init (dac_t * obj , PinName pin )
39
39
{
40
- DAC_ChannelConfTypeDef sConfig ;
40
+ DAC_ChannelConfTypeDef sConfig = { 0 } ;
41
41
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
43
43
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
45
47
uint32_t function = pinmap_function (pin , PinMap_DAC );
46
48
MBED_ASSERT (function != (uint32_t )NC );
47
- // Save the channel for the write and read functions
49
+
48
50
switch (STM_PIN_CHANNEL (function )) {
49
51
case 1 :
50
52
obj -> channel = DAC_CHANNEL_1 ;
@@ -59,18 +61,19 @@ void analogout_init(dac_t *obj, PinName pin)
59
61
break ;
60
62
}
61
63
62
- if (obj -> dac == (DACName )NC ) {
63
- error ("DAC pin mapping failed" );
64
- }
65
-
66
64
// Configure GPIO
67
65
pinmap_pinout (pin , PinMap_DAC );
68
66
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 ();
70
72
71
- __DAC_CLK_ENABLE ();
73
+ // Configure DAC
74
+ obj -> handle .Instance = (DAC_TypeDef * )(obj -> dac );
75
+ obj -> handle .State = HAL_DAC_STATE_RESET ;
72
76
73
- obj -> handle .Instance = DAC ;
74
77
if (HAL_DAC_Init (& obj -> handle ) != HAL_OK ) {
75
78
error ("HAL_DAC_Init failed" );
76
79
}
@@ -87,8 +90,13 @@ void analogout_init(dac_t *obj, PinName pin)
87
90
88
91
void analogout_free (dac_t * obj )
89
92
{
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 ();
92
97
98
+ // Configure GPIO
99
+ pin_function (obj -> pin , STM_PIN_DATA (STM_MODE_INPUT , GPIO_NOPULL , 0 ));
100
+ }
93
101
94
102
#endif // DEVICE_ANALOGOUT
0 commit comments