1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * Ported to NXP LPC43XX by Micromint USA <support@micromint.com>
18 #include "mbed_assert.h"
19 #include "analogout_api.h"
23 #include "mbed_error.h"
26 static const PinMap PinMap_DAC[] = {
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);
39 // Reset pin function to GPIO
41 // Select DAC on analog function select register in SCU
42 LPC_SCU->ENAIO[2] |= 1; // Sets pin as DAC
44 // Set bias=0 for maximum DAC update rate (1 MHz)
45 LPC_DAC->CR &= ~DAC_BIAS_EN;
47 LPC_DAC->CTRL |= DAC_DMA_ENA;
49 analogout_write_u16(obj, 0);
52 void analogout_free(dac_t *obj) {}
54 static inline void dac_write(int value) {
57 LPC_DAC->CR = DAC_SET(value);
60 static inline int dac_read() {
61 return (DAC_GET(LPC_DAC->CR));
64 void analogout_write(dac_t *obj, float value) {
67 } else if (value > 1.0f) {
70 dac_write(value * (float)DAC_RANGE);
74 void analogout_write_u16(dac_t *obj, uint16_t value) {
75 dac_write(value >> 6); // 10-bit
78 float analogout_read(dac_t *obj) {
79 uint32_t value = dac_read();
80 return (float)value * (1.0f / (float)DAC_RANGE);
83 uint16_t analogout_read_u16(dac_t *obj) {
84 uint32_t value = dac_read(); // 10-bit
85 return (value << 6) | ((value >> 4) & 0x003F);