fix for mbed lib issue 3 (i2c problem) see also https://mbed.org/users/mbed_official/code/mbed/issues/3 affected implementations: LPC812, LPC11U24, LPC1768, LPC2368, LPC4088

Fork of mbed-src by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers analogout_api.c Source File

analogout_api.c

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "analogout_api.h"
00017 #include "cmsis.h"
00018 #include "pinmap.h"
00019 #include "error.h"
00020 
00021 static const PinMap PinMap_DAC[] = {
00022     {P0_26, DAC_0, 2},
00023     {NC   , NC   , 0}
00024 };
00025 
00026 void analogout_init(dac_t *obj, PinName pin) {
00027     obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
00028     if (obj->dac == (uint32_t)NC) {
00029         error("DAC pin mapping failed");
00030     }
00031     
00032     // power is on by default, set DAC clk divider is /4
00033     LPC_SC->PCLKSEL0 &= ~(0x3 << 22);
00034     
00035     // map out (must be done before accessing registers)
00036     pinmap_pinout(pin, PinMap_DAC);
00037     
00038     analogout_write_u16(obj, 0);
00039 }
00040 
00041 void analogout_free(dac_t *obj) {}
00042 
00043 static inline void dac_write(int value) {
00044     value &= 0x3FF; // 10-bit
00045     
00046     // Set the DAC output
00047     LPC_DAC->DACR = (0 << 16)       // bias = 0
00048                   | (value << 6);
00049 }
00050 
00051 static inline int dac_read() {
00052     return (LPC_DAC->DACR >> 6) & 0x3FF;
00053 }
00054 
00055 void analogout_write(dac_t *obj, float value) {
00056     if (value < 0.0f) {
00057         dac_write(0);
00058     } else if (value > 1.0f) {
00059         dac_write(0x3FF);
00060     } else {
00061         dac_write(value * (float)0x3FF);
00062     }
00063 }
00064 
00065 void analogout_write_u16(dac_t *obj, uint16_t value) {
00066     dac_write(value >> 6); // 10-bit
00067 }
00068 
00069 float analogout_read(dac_t *obj) {
00070     uint32_t value = dac_read();
00071     return (float)value * (1.0f / (float)0x3FF);
00072 }
00073 
00074 uint16_t analogout_read_u16(dac_t *obj) {
00075     uint32_t value = dac_read(); // 10-bit
00076     return (value << 6) | ((value >> 4) & 0x003F);
00077 }