mbed library sources modified for open wear
Dependents: openwear-lifelogger-example
Fork of mbed-src by
targets/hal/TARGET_NXP/TARGET_LPC408X/analogout_api.c@29:6ac4027eff2b, 2013-09-23 (annotated)
- Committer:
- mbed_official
- Date:
- Mon Sep 23 11:30:07 2013 +0100
- Revision:
- 29:6ac4027eff2b
- Parent:
- 15:4892fe388435
- Child:
- 227:7bd0639b8911
Synchronized with git revision 4e23b72a78e8adfcaf3e52831780eca6d7a7bafe
Who changed what in which revision?
User | Revision | Line number | New 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 | */ |
bogdanm | 15:4892fe388435 | 16 | #include "analogout_api.h" |
bogdanm | 15:4892fe388435 | 17 | #include "cmsis.h" |
bogdanm | 15:4892fe388435 | 18 | #include "pinmap.h" |
bogdanm | 15:4892fe388435 | 19 | #include "error.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 | 29:6ac4027eff2b | 28 | if (obj->dac == (DACName)NC) { |
bogdanm | 15:4892fe388435 | 29 | error("DAC pin mapping failed"); |
bogdanm | 15:4892fe388435 | 30 | } |
bogdanm | 15:4892fe388435 | 31 | |
bogdanm | 15:4892fe388435 | 32 | // DAC enable bit must be set |
bogdanm | 15:4892fe388435 | 33 | LPC_IOCON->P0_26 |= (1 << 16); // DACEN |
bogdanm | 15:4892fe388435 | 34 | |
bogdanm | 15:4892fe388435 | 35 | // map out (must be done before accessing registers) |
bogdanm | 15:4892fe388435 | 36 | pinmap_pinout(pin, PinMap_DAC); |
bogdanm | 15:4892fe388435 | 37 | |
bogdanm | 15:4892fe388435 | 38 | analogout_write_u16(obj, 0); |
bogdanm | 15:4892fe388435 | 39 | } |
bogdanm | 15:4892fe388435 | 40 | |
bogdanm | 15:4892fe388435 | 41 | void analogout_free(dac_t *obj) {} |
bogdanm | 15:4892fe388435 | 42 | |
bogdanm | 15:4892fe388435 | 43 | static inline void dac_write(int value) { |
bogdanm | 15:4892fe388435 | 44 | value &= 0x3FF; // 10-bit |
bogdanm | 15:4892fe388435 | 45 | |
bogdanm | 15:4892fe388435 | 46 | // Set the DAC output |
bogdanm | 15:4892fe388435 | 47 | LPC_DAC->CR = (0 << 16) // bias = 0 |
bogdanm | 15:4892fe388435 | 48 | | (value << 6); |
bogdanm | 15:4892fe388435 | 49 | } |
bogdanm | 15:4892fe388435 | 50 | |
bogdanm | 15:4892fe388435 | 51 | static inline int dac_read() { |
bogdanm | 15:4892fe388435 | 52 | return (LPC_DAC->CR >> 6) & 0x3FF; |
bogdanm | 15:4892fe388435 | 53 | } |
bogdanm | 15:4892fe388435 | 54 | |
bogdanm | 15:4892fe388435 | 55 | void analogout_write(dac_t *obj, float value) { |
bogdanm | 15:4892fe388435 | 56 | if (value < 0.0f) { |
bogdanm | 15:4892fe388435 | 57 | dac_write(0); |
bogdanm | 15:4892fe388435 | 58 | } else if (value > 1.0f) { |
bogdanm | 15:4892fe388435 | 59 | dac_write(0x3FF); |
bogdanm | 15:4892fe388435 | 60 | } else { |
bogdanm | 15:4892fe388435 | 61 | dac_write(value * (float)0x3FF); |
bogdanm | 15:4892fe388435 | 62 | } |
bogdanm | 15:4892fe388435 | 63 | } |
bogdanm | 15:4892fe388435 | 64 | |
bogdanm | 15:4892fe388435 | 65 | void analogout_write_u16(dac_t *obj, uint16_t value) { |
bogdanm | 15:4892fe388435 | 66 | dac_write(value >> 6); // 10-bit |
bogdanm | 15:4892fe388435 | 67 | } |
bogdanm | 15:4892fe388435 | 68 | |
bogdanm | 15:4892fe388435 | 69 | float analogout_read(dac_t *obj) { |
bogdanm | 15:4892fe388435 | 70 | uint32_t value = dac_read(); |
bogdanm | 15:4892fe388435 | 71 | return (float)value * (1.0f / (float)0x3FF); |
bogdanm | 15:4892fe388435 | 72 | } |
bogdanm | 15:4892fe388435 | 73 | |
bogdanm | 15:4892fe388435 | 74 | uint16_t analogout_read_u16(dac_t *obj) { |
bogdanm | 15:4892fe388435 | 75 | uint32_t value = dac_read(); // 10-bit |
bogdanm | 15:4892fe388435 | 76 | return (value << 6) | ((value >> 4) & 0x003F); |
bogdanm | 15:4892fe388435 | 77 | } |