mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Wed Jun 11 16:00:09 2014 +0100
Revision:
227:7bd0639b8911
Parent:
29:6ac4027eff2b
Synchronized with git revision d58d532ebc0e0a96f4fffb8edefc082b71b964af

Full URL: https://github.com/mbedmicro/mbed/commit/d58d532ebc0e0a96f4fffb8edefc082b71b964af/

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 15:4892fe388435 1 /* mbed Microcontroller Library
bogdanm 15:4892fe388435 2 * Copyright (c) 2006-2013 ARM Limited
bogdanm 15:4892fe388435 3 *
bogdanm 15:4892fe388435 4 * Licensed under the Apache License, Version 2.0 (the "License");
bogdanm 15:4892fe388435 5 * you may not use this file except in compliance with the License.
bogdanm 15:4892fe388435 6 * You may obtain a copy of the License at
bogdanm 15:4892fe388435 7 *
bogdanm 15:4892fe388435 8 * http://www.apache.org/licenses/LICENSE-2.0
bogdanm 15:4892fe388435 9 *
bogdanm 15:4892fe388435 10 * Unless required by applicable law or agreed to in writing, software
bogdanm 15:4892fe388435 11 * distributed under the License is distributed on an "AS IS" BASIS,
bogdanm 15:4892fe388435 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bogdanm 15:4892fe388435 13 * See the License for the specific language governing permissions and
bogdanm 15:4892fe388435 14 * limitations under the License.
bogdanm 15:4892fe388435 15 */
mbed_official 227:7bd0639b8911 16 #include "mbed_assert.h"
bogdanm 15:4892fe388435 17 #include "analogout_api.h"
bogdanm 15:4892fe388435 18 #include "cmsis.h"
bogdanm 15:4892fe388435 19 #include "pinmap.h"
bogdanm 15:4892fe388435 20
bogdanm 15:4892fe388435 21 static const PinMap PinMap_DAC[] = {
bogdanm 15:4892fe388435 22 {P0_26, DAC_0, 2},
bogdanm 15:4892fe388435 23 {NC , NC , 0}
bogdanm 15:4892fe388435 24 };
bogdanm 15:4892fe388435 25
bogdanm 15:4892fe388435 26 void analogout_init(dac_t *obj, PinName pin) {
bogdanm 15:4892fe388435 27 obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
mbed_official 227:7bd0639b8911 28 MBED_ASSERT(obj->dac != (DACName)NC);
bogdanm 15:4892fe388435 29
bogdanm 15:4892fe388435 30 // DAC enable bit must be set
bogdanm 15:4892fe388435 31 LPC_IOCON->P0_26 |= (1 << 16); // DACEN
bogdanm 15:4892fe388435 32
bogdanm 15:4892fe388435 33 // map out (must be done before accessing registers)
bogdanm 15:4892fe388435 34 pinmap_pinout(pin, PinMap_DAC);
bogdanm 15:4892fe388435 35
bogdanm 15:4892fe388435 36 analogout_write_u16(obj, 0);
bogdanm 15:4892fe388435 37 }
bogdanm 15:4892fe388435 38
bogdanm 15:4892fe388435 39 void analogout_free(dac_t *obj) {}
bogdanm 15:4892fe388435 40
bogdanm 15:4892fe388435 41 static inline void dac_write(int value) {
bogdanm 15:4892fe388435 42 value &= 0x3FF; // 10-bit
bogdanm 15:4892fe388435 43
bogdanm 15:4892fe388435 44 // Set the DAC output
bogdanm 15:4892fe388435 45 LPC_DAC->CR = (0 << 16) // bias = 0
bogdanm 15:4892fe388435 46 | (value << 6);
bogdanm 15:4892fe388435 47 }
bogdanm 15:4892fe388435 48
bogdanm 15:4892fe388435 49 static inline int dac_read() {
bogdanm 15:4892fe388435 50 return (LPC_DAC->CR >> 6) & 0x3FF;
bogdanm 15:4892fe388435 51 }
bogdanm 15:4892fe388435 52
bogdanm 15:4892fe388435 53 void analogout_write(dac_t *obj, float value) {
bogdanm 15:4892fe388435 54 if (value < 0.0f) {
bogdanm 15:4892fe388435 55 dac_write(0);
bogdanm 15:4892fe388435 56 } else if (value > 1.0f) {
bogdanm 15:4892fe388435 57 dac_write(0x3FF);
bogdanm 15:4892fe388435 58 } else {
bogdanm 15:4892fe388435 59 dac_write(value * (float)0x3FF);
bogdanm 15:4892fe388435 60 }
bogdanm 15:4892fe388435 61 }
bogdanm 15:4892fe388435 62
bogdanm 15:4892fe388435 63 void analogout_write_u16(dac_t *obj, uint16_t value) {
bogdanm 15:4892fe388435 64 dac_write(value >> 6); // 10-bit
bogdanm 15:4892fe388435 65 }
bogdanm 15:4892fe388435 66
bogdanm 15:4892fe388435 67 float analogout_read(dac_t *obj) {
bogdanm 15:4892fe388435 68 uint32_t value = dac_read();
bogdanm 15:4892fe388435 69 return (float)value * (1.0f / (float)0x3FF);
bogdanm 15:4892fe388435 70 }
bogdanm 15:4892fe388435 71
bogdanm 15:4892fe388435 72 uint16_t analogout_read_u16(dac_t *obj) {
bogdanm 15:4892fe388435 73 uint32_t value = dac_read(); // 10-bit
bogdanm 15:4892fe388435 74 return (value << 6) | ((value >> 4) & 0x003F);
bogdanm 15:4892fe388435 75 }