Skip to content

Commit b40dab8

Browse files
committed
Added support for ANALOGOUT
1 parent 698ab6a commit b40dab8

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2015, 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+
// These variables are used for the "free" function
39+
static int pa4_used = 0;
40+
static int pa5_used = 0;
41+
42+
#if STATIC_PINMAP_READY
43+
#define ANALOGOUT_INIT_DIRECT analogout_init_direct
44+
void analogout_init_direct(dac_t *obj, const PinMap *pinmap)
45+
#else
46+
#define ANALOGOUT_INIT_DIRECT _analogout_init_direct
47+
static void _analogout_init_direct(dac_t *obj, const PinMap *pinmap)
48+
#endif
49+
{
50+
DAC_ChannelConfTypeDef sConfig = {0};
51+
52+
// Get the peripheral name from the pin and assign it to the object
53+
obj->dac = (DACName)pinmap->peripheral;
54+
MBED_ASSERT(obj->dac != (DACName)NC);
55+
56+
// Get the pin function and assign the used channel to the object
57+
uint32_t function = (uint32_t)pinmap->function;
58+
MBED_ASSERT(function != (uint32_t)NC);
59+
60+
// Save the channel for the write and read functions
61+
switch (STM_PIN_CHANNEL(function)) {
62+
case 1:
63+
obj->channel = DAC_CHANNEL_1;
64+
break;
65+
#if defined(DAC_CHANNEL_2)
66+
case 2:
67+
obj->channel = DAC_CHANNEL_2;
68+
break;
69+
#endif
70+
default:
71+
error("Unknown DAC channel");
72+
break;
73+
}
74+
75+
// Configure GPIO
76+
pin_function(pinmap->pin, pinmap->function);
77+
pin_mode(pinmap->pin, PullNone);
78+
79+
// Save the pin for future use
80+
obj->pin = pinmap->pin;
81+
82+
// Enable DAC clock
83+
if (obj->dac == DAC_1) {
84+
__HAL_RCC_DAC1_CLK_ENABLE();
85+
}
86+
#if defined(DAC2)
87+
if (obj->dac == DAC_2) {
88+
__HAL_RCC_DAC2_CLK_ENABLE();
89+
}
90+
#endif
91+
92+
// Configure DAC
93+
obj->handle.Instance = (DAC_TypeDef *)(obj->dac);
94+
obj->handle.State = HAL_DAC_STATE_RESET;
95+
96+
if (HAL_DAC_Init(&obj->handle) != HAL_OK) {
97+
error("HAL_DAC_Init failed");
98+
}
99+
100+
/* Enable both Buffer and Switch in the configuration,
101+
* letting HAL layer in charge of selecting either one
102+
* or the other depending on the actual DAC instance and
103+
* channel being configured.
104+
*/
105+
sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
106+
sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
107+
#if defined(DAC_OUTPUTSWITCH_ENABLE)
108+
sConfig.DAC_OutputSwitch = DAC_OUTPUTSWITCH_ENABLE;
109+
#endif
110+
111+
if (pinmap->pin == PA_4) {
112+
pa4_used = 1;
113+
}
114+
115+
if (pinmap->pin == PA_5) {
116+
pa5_used = 1;
117+
}
118+
119+
if (HAL_DAC_ConfigChannel(&obj->handle, &sConfig, obj->channel) != HAL_OK) {
120+
error("HAL_DAC_ConfigChannel failed");
121+
}
122+
123+
analogout_write_u16(obj, 0);
124+
}
125+
126+
void analogout_init(dac_t *obj, PinName pin)
127+
{
128+
int peripheral = (int)pinmap_peripheral(pin, PinMap_DAC);
129+
int function = (int)pinmap_find_function(pin, PinMap_DAC);
130+
131+
const PinMap static_pinmap = {pin, peripheral, function};
132+
133+
ANALOGOUT_INIT_DIRECT(obj, &static_pinmap);
134+
}
135+
136+
void analogout_free(dac_t *obj)
137+
{
138+
// Reset DAC and disable clock
139+
if (obj->pin == PA_4) {
140+
pa4_used = 0;
141+
}
142+
if (obj->pin == PA_5) {
143+
pa5_used = 0;
144+
}
145+
146+
if ((pa4_used == 0) && (pa5_used == 0)) {
147+
__HAL_RCC_DAC1_FORCE_RESET();
148+
__HAL_RCC_DAC1_RELEASE_RESET();
149+
__HAL_RCC_DAC1_CLK_DISABLE();
150+
}
151+
152+
#if defined(DAC2)
153+
if (obj->pin == PA_6) {
154+
__HAL_RCC_DAC2_FORCE_RESET();
155+
__HAL_RCC_DAC2_RELEASE_RESET();
156+
__HAL_RCC_DAC2_CLK_DISABLE();
157+
}
158+
#endif
159+
160+
// Configure GPIO
161+
pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
162+
}
163+
164+
const PinMap *analogout_pinmap()
165+
{
166+
return PinMap_DAC;
167+
}
168+
169+
#endif // DEVICE_ANALOGOUT

targets/targets.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15048,6 +15048,7 @@
1504815048
"lpticker_delay_ticks": 0
1504915049
},
1505015050
"device_has_add": [
15051+
"ANALOGOUT",
1505115052
"FLASH",
1505215053
"MPU"
1505315054
],

0 commit comments

Comments
 (0)