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:
117:e0a7df0a9a56
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
mbed_official 117:e0a7df0a9a56 1 /* mbed Microcontroller Library
mbed_official 117:e0a7df0a9a56 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 117:e0a7df0a9a56 3 *
mbed_official 117:e0a7df0a9a56 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 117:e0a7df0a9a56 5 * you may not use this file except in compliance with the License.
mbed_official 117:e0a7df0a9a56 6 * You may obtain a copy of the License at
mbed_official 117:e0a7df0a9a56 7 *
mbed_official 117:e0a7df0a9a56 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 117:e0a7df0a9a56 9 *
mbed_official 117:e0a7df0a9a56 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 117:e0a7df0a9a56 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 117:e0a7df0a9a56 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 117:e0a7df0a9a56 13 * See the License for the specific language governing permissions and
mbed_official 117:e0a7df0a9a56 14 * limitations under the License.
mbed_official 117:e0a7df0a9a56 15 */
mbed_official 227:7bd0639b8911 16 #include "mbed_assert.h"
mbed_official 117:e0a7df0a9a56 17 #include "analogout_api.h"
mbed_official 117:e0a7df0a9a56 18 #include "cmsis.h"
mbed_official 117:e0a7df0a9a56 19 #include "pinmap.h"
mbed_official 117:e0a7df0a9a56 20
mbed_official 117:e0a7df0a9a56 21 void analogout_init(dac_t *obj, PinName pin) {
mbed_official 227:7bd0639b8911 22 MBED_ASSERT(pin == P0_12);
mbed_official 117:e0a7df0a9a56 23
mbed_official 117:e0a7df0a9a56 24 LPC_SYSCON->SYSAHBCLKCTRL0 |= (1 << 29);
mbed_official 117:e0a7df0a9a56 25 LPC_SYSCON->PDRUNCFG &= ~(1 << 12);
mbed_official 117:e0a7df0a9a56 26 LPC_IOCON->PIO0_12 = 0;
mbed_official 117:e0a7df0a9a56 27 LPC_SWM->PINENABLE0 &= ~(1 << 24);
mbed_official 117:e0a7df0a9a56 28 LPC_DAC->CTRL = 0;
mbed_official 117:e0a7df0a9a56 29
mbed_official 117:e0a7df0a9a56 30 analogout_write_u16(obj, 0);
mbed_official 117:e0a7df0a9a56 31 }
mbed_official 117:e0a7df0a9a56 32
mbed_official 117:e0a7df0a9a56 33 void analogout_free(dac_t *obj)
mbed_official 117:e0a7df0a9a56 34 {
mbed_official 117:e0a7df0a9a56 35 LPC_SYSCON->SYSAHBCLKCTRL0 &= ~(1 << 29);
mbed_official 117:e0a7df0a9a56 36 LPC_SWM->PINENABLE0 |= (1 << 24);
mbed_official 117:e0a7df0a9a56 37 }
mbed_official 117:e0a7df0a9a56 38
mbed_official 117:e0a7df0a9a56 39 static inline void dac_write(int value) {
mbed_official 117:e0a7df0a9a56 40 value &= 0xFFF; // 12-bit
mbed_official 117:e0a7df0a9a56 41
mbed_official 117:e0a7df0a9a56 42 // Set the DAC output
mbed_official 117:e0a7df0a9a56 43 LPC_DAC->VAL = (value << 4);
mbed_official 117:e0a7df0a9a56 44 }
mbed_official 117:e0a7df0a9a56 45
mbed_official 117:e0a7df0a9a56 46 static inline int dac_read() {
mbed_official 117:e0a7df0a9a56 47 return ((LPC_DAC->VAL >> 4) & 0xFFF);
mbed_official 117:e0a7df0a9a56 48 }
mbed_official 117:e0a7df0a9a56 49
mbed_official 117:e0a7df0a9a56 50 void analogout_write(dac_t *obj, float value) {
mbed_official 117:e0a7df0a9a56 51 if (value < 0.0f) {
mbed_official 117:e0a7df0a9a56 52 dac_write(0);
mbed_official 117:e0a7df0a9a56 53 } else if (value > 1.0f) {
mbed_official 117:e0a7df0a9a56 54 dac_write(0xFFF);
mbed_official 117:e0a7df0a9a56 55 } else {
mbed_official 117:e0a7df0a9a56 56 dac_write((uint32_t)(value * (float)0xFFF));
mbed_official 117:e0a7df0a9a56 57 }
mbed_official 117:e0a7df0a9a56 58 }
mbed_official 117:e0a7df0a9a56 59
mbed_official 117:e0a7df0a9a56 60 void analogout_write_u16(dac_t *obj, uint16_t value) {
mbed_official 117:e0a7df0a9a56 61 dac_write(value);
mbed_official 117:e0a7df0a9a56 62 }
mbed_official 117:e0a7df0a9a56 63
mbed_official 117:e0a7df0a9a56 64 float analogout_read(dac_t *obj) {
mbed_official 117:e0a7df0a9a56 65 uint32_t value = dac_read();
mbed_official 117:e0a7df0a9a56 66 return (float)value * (1.0f / (float)0xFFF);
mbed_official 117:e0a7df0a9a56 67 }
mbed_official 117:e0a7df0a9a56 68
mbed_official 117:e0a7df0a9a56 69 uint16_t analogout_read_u16(dac_t *obj) {
mbed_official 117:e0a7df0a9a56 70 return (uint16_t)dac_read();
mbed_official 117:e0a7df0a9a56 71 }