]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC43XX/analogout_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_NXP / TARGET_LPC43XX / analogout_api.c
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 ARM Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * Ported to NXP LPC43XX by Micromint USA <support@micromint.com>
17  */
18 #include "mbed_assert.h"
19 #include "analogout_api.h"
20
21 #include "cmsis.h"
22 #include "pinmap.h"
23 #include "mbed_error.h"
24 #include "gpio_api.h"
25
26 static const PinMap PinMap_DAC[] = {
27 #ifdef TARGET_LPC4337
28     {P4_3, DAC_0, 0},
29 #else
30     {P4_4, DAC_0, 0},
31 #endif
32     {NC,   NC,    0}
33 };
34
35 void analogout_init(dac_t *obj, PinName pin) {
36     obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
37     MBED_ASSERT(obj->dac != (DACName)NC);
38
39     // Reset pin function to GPIO
40     gpio_set(pin);
41     // Select DAC on analog function select register in SCU
42     LPC_SCU->ENAIO[2] |= 1; // Sets pin as DAC
43
44     // Set bias=0 for maximum DAC update rate (1 MHz)
45     LPC_DAC->CR &= ~DAC_BIAS_EN;
46     // Enable DAC and DMA
47     LPC_DAC->CTRL |= DAC_DMA_ENA;
48         
49     analogout_write_u16(obj, 0);
50 }
51
52 void analogout_free(dac_t *obj) {}
53
54 static inline void dac_write(int value) {
55     
56     // Set the DAC output
57     LPC_DAC->CR = DAC_SET(value);
58 }
59
60 static inline int dac_read() {
61     return (DAC_GET(LPC_DAC->CR));
62 }
63
64 void analogout_write(dac_t *obj, float value) {
65     if (value < 0.0f) {
66         dac_write(0);
67     } else if (value > 1.0f) {
68         dac_write(DAC_RANGE);
69     } else {
70         dac_write(value * (float)DAC_RANGE);
71     }
72 }
73
74 void analogout_write_u16(dac_t *obj, uint16_t value) {
75     dac_write(value >> 6); // 10-bit
76 }
77
78 float analogout_read(dac_t *obj) {
79     uint32_t value = dac_read();
80     return (float)value * (1.0f / (float)DAC_RANGE);
81 }
82
83 uint16_t analogout_read_u16(dac_t *obj) {
84     uint32_t value = dac_read(); // 10-bit
85     return (value << 6) | ((value >> 4) & 0x003F);
86 }