1 /* mbed Microcontroller Library
2 * Copyright (c) 2014, STMicroelectronics
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #include "mbed_assert.h"
29 #include "analogout_api.h"
35 #include "mbed_error.h"
36 #include "PeripheralPins.h"
38 #define DAC_RANGE (0xFFF) // 12 bits
40 static DAC_HandleTypeDef DacHandle
;
42 // These variables are used for the "free" function
43 static int pa4_used
= 0;
44 static int pa5_used
= 0;
46 void analogout_init(dac_t
*obj
, PinName pin
)
48 DAC_ChannelConfTypeDef sConfig
;
50 // Get the peripheral name from the pin and assign it to the object
51 obj
->dac
= (DACName
)pinmap_peripheral(pin
, PinMap_DAC
);
52 MBED_ASSERT(obj
->dac
!= (DACName
)NC
);
55 pinmap_pinout(pin
, PinMap_DAC
);
57 // Save the pin for future use
64 DacHandle
.Instance
= DAC
;
66 sConfig
.DAC_Trigger
= DAC_TRIGGER_NONE
;
67 sConfig
.DAC_OutputBuffer
= DAC_OUTPUTBUFFER_DISABLE
;
70 HAL_DAC_ConfigChannel(&DacHandle
, &sConfig
, DAC_CHANNEL_1
);
74 #if defined(DAC_CHANNEL_2)
76 HAL_DAC_ConfigChannel(&DacHandle
, &sConfig
, DAC_CHANNEL_2
);
81 analogout_write_u16(obj
, 0);
84 void analogout_free(dac_t
*obj
)
86 // Reset DAC and disable clock
87 if (obj
->pin
== PA_4
) pa4_used
= 0;
88 if (obj
->pin
== PA_5
) pa5_used
= 0;
90 if ((pa4_used
== 0) && (pa5_used
== 0)) {
92 __DAC_RELEASE_RESET();
97 pin_function(obj
->pin
, STM_PIN_DATA(STM_MODE_INPUT
, GPIO_NOPULL
, 0));
100 static inline void dac_write(dac_t
*obj
, uint16_t value
)
102 if (obj
->pin
== PA_4
) {
103 HAL_DAC_SetValue(&DacHandle
, DAC_CHANNEL_1
, DAC_ALIGN_12B_R
, value
);
104 HAL_DAC_Start(&DacHandle
, DAC_CHANNEL_1
);
107 #if defined(DAC_CHANNEL_2)
108 if (obj
->pin
== PA_5
) {
109 HAL_DAC_SetValue(&DacHandle
, DAC_CHANNEL_2
, DAC_ALIGN_12B_R
, value
);
110 HAL_DAC_Start(&DacHandle
, DAC_CHANNEL_2
);
115 static inline int dac_read(dac_t
*obj
)
117 if (obj
->pin
== PA_4
) {
118 return (int)HAL_DAC_GetValue(&DacHandle
, DAC_CHANNEL_1
);
120 #if defined(DAC_CHANNEL_2)
121 else if (obj
->pin
== PA_5
) {
122 return (int)HAL_DAC_GetValue(&DacHandle
, DAC_CHANNEL_2
);
130 void analogout_write(dac_t
*obj
, float value
)
133 dac_write(obj
, 0); // Min value
134 } else if (value
> 1.0f
) {
135 dac_write(obj
, (uint16_t)DAC_RANGE
); // Max value
137 dac_write(obj
, (uint16_t)(value
* (float)DAC_RANGE
));
141 void analogout_write_u16(dac_t
*obj
, uint16_t value
)
143 if (value
> (uint16_t)DAC_RANGE
) {
144 dac_write(obj
, (uint16_t)DAC_RANGE
); // Max value
146 dac_write(obj
, value
);
150 float analogout_read(dac_t
*obj
)
152 uint32_t value
= dac_read(obj
);
153 return (float)((float)value
* (1.0f
/ (float)DAC_RANGE
));
156 uint16_t analogout_read_u16(dac_t
*obj
)
158 return (uint16_t)dac_read(obj
);
161 #endif // DEVICE_ANALOGOUT