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
- void analogout_init (dac_t * obj , PinName pin )
39
- {
40
- DAC_ChannelConfTypeDef sConfig ;
38
+ void analogout_init (dac_t * obj , PinName pin ) {
39
+ DAC_ChannelConfTypeDef sConfig = {0 };
41
40
42
- // Get the peripheral name (DAC_1, ...) from the pin and assign it to the object
41
+ // Get the peripheral name from the pin and assign it to the object
43
42
obj -> dac = (DACName )pinmap_peripheral (pin , PinMap_DAC );
44
- // Get the functions (dac channel) from the pin and assign it to the object
43
+ MBED_ASSERT (obj -> dac != (DACName )NC );
44
+
45
+ // Get the pin function and assign the used channel to the object
45
46
uint32_t function = pinmap_function (pin , PinMap_DAC );
46
47
MBED_ASSERT (function != (uint32_t )NC );
47
- // Save the channel for the write and read functions
48
+
48
49
switch (STM_PIN_CHANNEL (function )) {
49
50
case 1 :
50
51
obj -> channel = DAC_CHANNEL_1 ;
@@ -59,18 +60,17 @@ void analogout_init(dac_t *obj, PinName pin)
59
60
break ;
60
61
}
61
62
62
- if (obj -> dac == (DACName )NC ) {
63
- error ("DAC pin mapping failed" );
64
- }
65
-
66
63
// Configure GPIO
67
64
pinmap_pinout (pin , PinMap_DAC );
68
65
69
- __GPIOA_CLK_ENABLE ();
66
+ // Save the pin for future use
67
+ obj -> pin = pin ;
70
68
71
- __DAC_CLK_ENABLE ();
69
+ // Enable DAC clock
70
+ __HAL_RCC_DAC_CLK_ENABLE ();
72
71
73
- obj -> handle .Instance = DAC ;
72
+ // Configure DAC
73
+ obj -> handle .Instance = (DAC_TypeDef * )(obj -> dac );
74
74
if (HAL_DAC_Init (& obj -> handle ) != HAL_OK ) {
75
75
error ("HAL_DAC_Init failed" );
76
76
}
@@ -85,10 +85,14 @@ void analogout_init(dac_t *obj, PinName pin)
85
85
analogout_write_u16 (obj , 0 );
86
86
}
87
87
88
- void analogout_free (dac_t * obj )
89
- {
90
- }
91
-
88
+ void analogout_free (dac_t * obj ) {
89
+ // Reset DAC and disable clock
90
+ __HAL_RCC_DAC_FORCE_RESET ();
91
+ __HAL_RCC_DAC_RELEASE_RESET ();
92
+ __HAL_RCC_DAC_CLK_DISABLE ();
92
93
94
+ // Configure GPIO
95
+ pin_function (obj -> pin , STM_PIN_DATA (STM_MODE_INPUT , GPIO_NOPULL , 0 ));
96
+ }
93
97
94
98
#endif // DEVICE_ANALOGOUT
0 commit comments