]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC408X/analogout_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_NXP / TARGET_LPC408X / 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 #include "mbed_assert.h"
17 #include "analogout_api.h"
18 #include "cmsis.h"
19 #include "pinmap.h"
20
21 static const PinMap PinMap_DAC[] = {
22 {P0_26, DAC_0, 2},
23 {NC , NC , 0}
24 };
25
26 void analogout_init(dac_t *obj, PinName pin) {
27 obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
28 MBED_ASSERT(obj->dac != (DACName)NC);
29
30 // DAC enable bit must be set
31 LPC_IOCON->P0_26 |= (1 << 16); // DACEN
32
33 // map out (must be done before accessing registers)
34 pinmap_pinout(pin, PinMap_DAC);
35
36 analogout_write_u16(obj, 0);
37 }
38
39 void analogout_free(dac_t *obj) {}
40
41 static inline void dac_write(int value) {
42 value &= 0x3FF; // 10-bit
43
44 // Set the DAC output
45 LPC_DAC->CR = (0 << 16) // bias = 0
46 | (value << 6);
47 }
48
49 static inline int dac_read() {
50 return (LPC_DAC->CR >> 6) & 0x3FF;
51 }
52
53 void analogout_write(dac_t *obj, float value) {
54 if (value < 0.0f) {
55 dac_write(0);
56 } else if (value > 1.0f) {
57 dac_write(0x3FF);
58 } else {
59 dac_write(value * (float)0x3FF);
60 }
61 }
62
63 void analogout_write_u16(dac_t *obj, uint16_t value) {
64 dac_write(value >> 6); // 10-bit
65 }
66
67 float analogout_read(dac_t *obj) {
68 uint32_t value = dac_read();
69 return (float)value * (1.0f / (float)0x3FF);
70 }
71
72 uint16_t analogout_read_u16(dac_t *obj) {
73 uint32_t value = dac_read(); // 10-bit
74 return (value << 6) | ((value >> 4) & 0x003F);
75 }
Imprint / Impressum