forked from ARMmbed/mbed-os
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathanalogout_api.c
188 lines (144 loc) · 5.21 KB
/
analogout_api.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Copyright (c) 2018, Nuvoton Technology Corporation
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "analogout_api.h"
#if DEVICE_ANALOGOUT
#include "cmsis.h"
#include "pinmap.h"
#include "PeripheralPins.h"
#include "gpio_api.h"
#include "nu_modutil.h"
/* Maximum DAC modules */
#define NU_DACMOD_MAXNUM 1
/* Maximum DAC channels per module */
#define NU_DACCHN_MAXNUM 1
static uint32_t dac_modinit_mask[NU_DACMOD_MAXNUM];
static const struct nu_modinit_s dac_modinit_tab[] = {
{DAC_0_0, DAC_MODULE, 0, 0, DAC_RST, DAC_IRQn, NULL}
};
void analogout_init(dac_t *obj, PinName pin)
{
obj->dac = (DACName) pinmap_peripheral(pin, PinMap_DAC);
MBED_ASSERT(obj->dac != (DACName) NC);
const struct nu_modinit_s *modinit = get_modinit(obj->dac, dac_modinit_tab);
MBED_ASSERT(modinit != NULL);
MBED_ASSERT(modinit->modname == obj->dac);
/* Module index */
uint32_t modidx = NU_MODINDEX(obj->dac);
MBED_ASSERT(modidx < NU_DACMOD_MAXNUM);
/* Module subindex (aka channel) */
uint32_t chn = NU_MODSUBINDEX(obj->dac);
MBED_ASSERT(chn < NU_DACCHN_MAXNUM);
obj->pin = pin;
/* Wire pinout */
pinmap_pinout(pin, PinMap_DAC);
DAC_T *dac_base = (DAC_T *) NU_MODBASE(obj->dac);
/* Module-level setup from here */
if (! dac_modinit_mask[modidx]) {
/* Select IP clock source and clock divider */
CLK_SetModuleClock(modinit->clkidx, modinit->clksrc, modinit->clkdiv);
/* Enable IP clock */
CLK_EnableModuleClock(modinit->clkidx);
/* Reset IP */
SYS_ResetModule(modinit->rsetidx);
/* The conversion settling time is 8us when 12-bit input code transition from
* lowest code (0x000) to highest code (0xFFF). */
DAC_SetDelayTime(dac_base, 8);
/* Configure DAT data format to left-aligned
* Effective 12-bits are aligned to left of 16-bit DAC_DAT. */
DAC_ENABLE_LEFT_ALIGN(dac_base);
}
/* Channel-level setup from here: */
/* Set the software trigger, enable DAC event trigger mode and enable D/A converter */
DAC_Open(dac_base, chn, DAC_SOFTWARE_TRIGGER);
/* Mark channel allocated */
dac_modinit_mask[modidx] |= 1 << chn;
}
void analogout_free(dac_t *obj)
{
const struct nu_modinit_s *modinit = get_modinit(obj->dac, dac_modinit_tab);
MBED_ASSERT(modinit != NULL);
MBED_ASSERT(modinit->modname == obj->dac);
/* Module index */
uint32_t modidx = NU_MODINDEX(obj->dac);
MBED_ASSERT(modidx < NU_DACMOD_MAXNUM);
/* Module subindex (aka channel) */
uint32_t chn = NU_MODSUBINDEX(obj->dac);
MBED_ASSERT(chn < NU_DACCHN_MAXNUM);
DAC_T *dac_base = (DAC_T *) NU_MODBASE(obj->dac);
/* Channel-level windup from here */
/* Mark channel free */
dac_modinit_mask[modidx] &= ~(1 << chn);
/* Close channel */
DAC_Close(dac_base, chn);
/* Module-level windup from here: */
if (! dac_modinit_mask[modidx]) {
/* Disable IP clock */
CLK_DisableModuleClock(modinit->clkidx);
}
/* Free up pin */
gpio_set(obj->pin);
obj->pin = NC;
}
void analogout_write(dac_t *obj, float value)
{
if (value <= 0.0f) {
analogout_write_u16(obj, 0);
} else if (value >= 1.0f) {
analogout_write_u16(obj, 0xFFFF);
} else {
analogout_write_u16(obj, (uint16_t) (value * ((float) 0xFFFF)));
}
}
void analogout_write_u16(dac_t *obj, uint16_t value)
{
DAC_T *dac_base = (DAC_T *) NU_MODBASE(obj->dac);
uint32_t chn = NU_MODSUBINDEX(obj->dac);
/* We should have configured DAC data format to left-aligned */
MBED_ASSERT(dac_base->CTL & DAC_CTL_LALIGN_Msk);
DAC_WRITE_DATA(dac_base, chn, value);
/* Clear the DAC conversion complete finish flag for safe */
DAC_CLR_INT_FLAG(dac_base, chn);
/* Start A/D conversion */
DAC_START_CONV(dac_base);
/* Wait for completed */
while (DAC_IS_BUSY(dac_base, chn));
}
float analogout_read(dac_t *obj)
{
uint32_t value = analogout_read_u16(obj);
return (float) value * (1.0f / (float) 0xFFFF);
}
uint16_t analogout_read_u16(dac_t *obj)
{
DAC_T *dac_base = (DAC_T *) NU_MODBASE(obj->dac);
uint32_t chn = NU_MODSUBINDEX(obj->dac);
/* We should have configured DAC data format to left-aligned */
MBED_ASSERT(dac_base->CTL & DAC_CTL_LALIGN_Msk);
uint16_t dat12_4 = DAC_READ_DATA(dac_base, chn);
/* Just 12 bits are effective. Convert to 16 bits.
*
* dat12_4 : b11b10b9b8 b7b6b5b4 b3b2b1b0 0000
* dat16 : b11b10b9b8 b7b6b5b4 b3b2b1b0 b11b10b9b8
*/
uint16_t dat16 = (dat12_4 & 0xFFF0) | (dat12_4 >> 12);
return dat16;
}
const PinMap *analogout_pinmap()
{
return PinMap_DAC;
}
#endif