]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F3/analogout_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_STM / TARGET_STM32F3 / analogout_api.c
1 /* mbed Microcontroller Library
2  * Copyright (c) 2014, STMicroelectronics
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
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.
16  *
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.
27  */
28 #include "mbed_assert.h"
29 #include "analogout_api.h"
30
31 #if DEVICE_ANALOGOUT
32
33 #include "cmsis.h"
34 #include "pinmap.h"
35 #include "mbed_error.h"
36 #include "PeripheralPins.h"
37
38 #define DAC_RANGE (0xFFF) // 12 bits
39
40 static DAC_HandleTypeDef DacHandle;
41
42 // These variables are used for the "free" function
43 static int pa4_used = 0;
44 static int pa5_used = 0;
45
46 void analogout_init(dac_t *obj, PinName pin)
47 {
48     DAC_ChannelConfTypeDef sConfig;
49
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);
53
54     // Configure GPIO
55     pinmap_pinout(pin, PinMap_DAC);
56
57     // Save the pin for future use
58     obj->pin = pin;
59
60     // Enable DAC clock
61     if (obj->dac == DAC_1) {
62         __DAC1_CLK_ENABLE();
63     }
64 #if defined(__DAC2_FORCE_RESET)
65     if (obj->dac == DAC_2) {
66         __DAC2_CLK_ENABLE();
67     }
68 #endif
69
70     // Configure DAC
71     DacHandle.Instance = (DAC_TypeDef *)(obj->dac);
72
73     sConfig.DAC_Trigger      = DAC_TRIGGER_NONE;
74     sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE;
75
76     if (pin == PA_4) {
77         HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1);
78         pa4_used = 1;
79     }
80
81 #if defined(DAC_CHANNEL_2)
82     if (pin == PA_5) {
83         HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_2);
84         pa5_used = 1;
85     }
86 #endif
87
88     if (pin == PA_6) {
89         HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1);
90     }
91
92     analogout_write_u16(obj, 0);
93 }
94
95 void analogout_free(dac_t *obj)
96 {
97     // Reset DAC and disable clock
98     if (obj->pin == PA_4) pa4_used = 0;
99     if (obj->pin == PA_5) pa5_used = 0;
100
101     if ((pa4_used == 0) && (pa5_used == 0)) {
102         __DAC1_FORCE_RESET();
103         __DAC1_RELEASE_RESET();
104         __DAC1_CLK_DISABLE();
105     }
106
107 #if defined(__DAC2_FORCE_RESET)
108     if (obj->pin == PA_6) {
109         __DAC2_FORCE_RESET();
110         __DAC2_RELEASE_RESET();
111         __DAC2_CLK_DISABLE();
112     }
113 #endif
114
115     // Configure GPIO
116     pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
117 }
118
119 static inline void dac_write(dac_t *obj, uint16_t value)
120 {
121     if ((obj->pin == PA_4) || (obj->pin == PA_6)) {
122         HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, value);
123         HAL_DAC_Start(&DacHandle, DAC_CHANNEL_1);
124     }
125
126 #if defined(DAC_CHANNEL_2)
127     if (obj->pin == PA_5) {
128         HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value);
129         HAL_DAC_Start(&DacHandle, DAC_CHANNEL_2);
130     }
131 #endif
132 }
133
134 static inline int dac_read(dac_t *obj)
135 {
136     if ((obj->pin == PA_4) || (obj->pin == PA_6)) {
137         return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1);
138 #if defined(DAC_CHANNEL_2)
139     } else if (obj->pin == PA_5) {
140         return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_2);
141 #endif
142     } else {
143         return 0;
144     }
145 }
146
147 void analogout_write(dac_t *obj, float value)
148 {
149     if (value < 0.0f) {
150         dac_write(obj, 0); // Min value
151     } else if (value > 1.0f) {
152         dac_write(obj, (uint16_t)DAC_RANGE); // Max value
153     } else {
154         dac_write(obj, (uint16_t)(value * (float)DAC_RANGE));
155     }
156 }
157
158 void analogout_write_u16(dac_t *obj, uint16_t value)
159 {
160     if (value > (uint16_t)DAC_RANGE) {
161         dac_write(obj, (uint16_t)DAC_RANGE); // Max value
162     } else {
163         dac_write(obj, value);
164     }
165 }
166
167 float analogout_read(dac_t *obj)
168 {
169     uint32_t value = dac_read(obj);
170     return (float)((float)value * (1.0f / (float)DAC_RANGE));
171 }
172
173 uint16_t analogout_read_u16(dac_t *obj)
174 {
175     return (uint16_t)dac_read(obj);
176 }
177
178 #endif // DEVICE_ANALOGOUT