mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Wed Mar 12 10:30:07 2014 +0000
Revision:
117:e0a7df0a9a56
Child:
227:7bd0639b8911
Synchronized with git revision 6b2f3120cfef5765bc1bade48c495873f627f9bc

Full URL: https://github.com/mbedmicro/mbed/commit/6b2f3120cfef5765bc1bade48c495873f627f9bc/

[LPC1549] Added AnalogOut API and PWM improvement

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 117:e0a7df0a9a56 16 #include "analogout_api.h"
mbed_official 117:e0a7df0a9a56 17 #include "cmsis.h"
mbed_official 117:e0a7df0a9a56 18 #include "pinmap.h"
mbed_official 117:e0a7df0a9a56 19 #include "error.h"
mbed_official 117:e0a7df0a9a56 20
mbed_official 117:e0a7df0a9a56 21 void analogout_init(dac_t *obj, PinName pin) {
mbed_official 117:e0a7df0a9a56 22 if (pin != P0_12) {
mbed_official 117:e0a7df0a9a56 23 error("DAC pin mapping failed");
mbed_official 117:e0a7df0a9a56 24 }
mbed_official 117:e0a7df0a9a56 25
mbed_official 117:e0a7df0a9a56 26 LPC_SYSCON->SYSAHBCLKCTRL0 |= (1 << 29);
mbed_official 117:e0a7df0a9a56 27 LPC_SYSCON->PDRUNCFG &= ~(1 << 12);
mbed_official 117:e0a7df0a9a56 28 LPC_IOCON->PIO0_12 = 0;
mbed_official 117:e0a7df0a9a56 29 LPC_SWM->PINENABLE0 &= ~(1 << 24);
mbed_official 117:e0a7df0a9a56 30 LPC_DAC->CTRL = 0;
mbed_official 117:e0a7df0a9a56 31
mbed_official 117:e0a7df0a9a56 32 analogout_write_u16(obj, 0);
mbed_official 117:e0a7df0a9a56 33 }
mbed_official 117:e0a7df0a9a56 34
mbed_official 117:e0a7df0a9a56 35 void analogout_free(dac_t *obj)
mbed_official 117:e0a7df0a9a56 36 {
mbed_official 117:e0a7df0a9a56 37 LPC_SYSCON->SYSAHBCLKCTRL0 &= ~(1 << 29);
mbed_official 117:e0a7df0a9a56 38 LPC_SWM->PINENABLE0 |= (1 << 24);
mbed_official 117:e0a7df0a9a56 39 }
mbed_official 117:e0a7df0a9a56 40
mbed_official 117:e0a7df0a9a56 41 static inline void dac_write(int value) {
mbed_official 117:e0a7df0a9a56 42 value &= 0xFFF; // 12-bit
mbed_official 117:e0a7df0a9a56 43
mbed_official 117:e0a7df0a9a56 44 // Set the DAC output
mbed_official 117:e0a7df0a9a56 45 LPC_DAC->VAL = (value << 4);
mbed_official 117:e0a7df0a9a56 46 }
mbed_official 117:e0a7df0a9a56 47
mbed_official 117:e0a7df0a9a56 48 static inline int dac_read() {
mbed_official 117:e0a7df0a9a56 49 return ((LPC_DAC->VAL >> 4) & 0xFFF);
mbed_official 117:e0a7df0a9a56 50 }
mbed_official 117:e0a7df0a9a56 51
mbed_official 117:e0a7df0a9a56 52 void analogout_write(dac_t *obj, float value) {
mbed_official 117:e0a7df0a9a56 53 if (value < 0.0f) {
mbed_official 117:e0a7df0a9a56 54 dac_write(0);
mbed_official 117:e0a7df0a9a56 55 } else if (value > 1.0f) {
mbed_official 117:e0a7df0a9a56 56 dac_write(0xFFF);
mbed_official 117:e0a7df0a9a56 57 } else {
mbed_official 117:e0a7df0a9a56 58 dac_write((uint32_t)(value * (float)0xFFF));
mbed_official 117:e0a7df0a9a56 59 }
mbed_official 117:e0a7df0a9a56 60 }
mbed_official 117:e0a7df0a9a56 61
mbed_official 117:e0a7df0a9a56 62 void analogout_write_u16(dac_t *obj, uint16_t value) {
mbed_official 117:e0a7df0a9a56 63 dac_write(value);
mbed_official 117:e0a7df0a9a56 64 }
mbed_official 117:e0a7df0a9a56 65
mbed_official 117:e0a7df0a9a56 66 float analogout_read(dac_t *obj) {
mbed_official 117:e0a7df0a9a56 67 uint32_t value = dac_read();
mbed_official 117:e0a7df0a9a56 68 return (float)value * (1.0f / (float)0xFFF);
mbed_official 117:e0a7df0a9a56 69 }
mbed_official 117:e0a7df0a9a56 70
mbed_official 117:e0a7df0a9a56 71 uint16_t analogout_read_u16(dac_t *obj) {
mbed_official 117:e0a7df0a9a56 72 return (uint16_t)dac_read();
mbed_official 117:e0a7df0a9a56 73 }