mbed library sources. Supersedes mbed-src. GR-PEACH runs on RAM.
Fork of mbed-dev by
targets/hal/TARGET_NXP/TARGET_LPC23XX/analogout_api.c@103:493a29d2d4d7, 2016-03-30 (annotated)
- Committer:
- 1050186
- Date:
- Wed Mar 30 11:41:25 2016 +0000
- Revision:
- 103:493a29d2d4d7
- Parent:
- 0:9b334a45a8ff
GR-PEACH runs on RAM.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 0:9b334a45a8ff | 1 | /* mbed Microcontroller Library |
bogdanm | 0:9b334a45a8ff | 2 | * Copyright (c) 2006-2013 ARM Limited |
bogdanm | 0:9b334a45a8ff | 3 | * |
bogdanm | 0:9b334a45a8ff | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
bogdanm | 0:9b334a45a8ff | 5 | * you may not use this file except in compliance with the License. |
bogdanm | 0:9b334a45a8ff | 6 | * You may obtain a copy of the License at |
bogdanm | 0:9b334a45a8ff | 7 | * |
bogdanm | 0:9b334a45a8ff | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bogdanm | 0:9b334a45a8ff | 9 | * |
bogdanm | 0:9b334a45a8ff | 10 | * Unless required by applicable law or agreed to in writing, software |
bogdanm | 0:9b334a45a8ff | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
bogdanm | 0:9b334a45a8ff | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bogdanm | 0:9b334a45a8ff | 13 | * See the License for the specific language governing permissions and |
bogdanm | 0:9b334a45a8ff | 14 | * limitations under the License. |
bogdanm | 0:9b334a45a8ff | 15 | */ |
bogdanm | 0:9b334a45a8ff | 16 | #include "mbed_assert.h" |
bogdanm | 0:9b334a45a8ff | 17 | #include "analogout_api.h" |
bogdanm | 0:9b334a45a8ff | 18 | #include "cmsis.h" |
bogdanm | 0:9b334a45a8ff | 19 | #include "pinmap.h" |
bogdanm | 0:9b334a45a8ff | 20 | |
bogdanm | 0:9b334a45a8ff | 21 | static const PinMap PinMap_DAC[] = { |
bogdanm | 0:9b334a45a8ff | 22 | {P0_26, DAC_0, 2}, |
bogdanm | 0:9b334a45a8ff | 23 | {NC , NC , 0} |
bogdanm | 0:9b334a45a8ff | 24 | }; |
bogdanm | 0:9b334a45a8ff | 25 | |
bogdanm | 0:9b334a45a8ff | 26 | void analogout_init(dac_t *obj, PinName pin) { |
bogdanm | 0:9b334a45a8ff | 27 | obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); |
bogdanm | 0:9b334a45a8ff | 28 | MBED_ASSERT(obj->dac != (DACName)NC); |
bogdanm | 0:9b334a45a8ff | 29 | |
bogdanm | 0:9b334a45a8ff | 30 | // power is on by default, set DAC clk divider is /4 |
bogdanm | 0:9b334a45a8ff | 31 | LPC_SC->PCLKSEL0 &= ~(0x3 << 22); |
bogdanm | 0:9b334a45a8ff | 32 | |
bogdanm | 0:9b334a45a8ff | 33 | // map out (must be done before accessing registers) |
bogdanm | 0:9b334a45a8ff | 34 | pinmap_pinout(pin, PinMap_DAC); |
bogdanm | 0:9b334a45a8ff | 35 | |
bogdanm | 0:9b334a45a8ff | 36 | analogout_write_u16(obj, 0); |
bogdanm | 0:9b334a45a8ff | 37 | } |
bogdanm | 0:9b334a45a8ff | 38 | |
bogdanm | 0:9b334a45a8ff | 39 | void analogout_free(dac_t *obj) {} |
bogdanm | 0:9b334a45a8ff | 40 | |
bogdanm | 0:9b334a45a8ff | 41 | static inline void dac_write(int value) { |
bogdanm | 0:9b334a45a8ff | 42 | value &= 0x3FF; // 10-bit |
bogdanm | 0:9b334a45a8ff | 43 | |
bogdanm | 0:9b334a45a8ff | 44 | // Set the DAC output |
bogdanm | 0:9b334a45a8ff | 45 | LPC_DAC->DACR = (0 << 16) // bias = 0 |
bogdanm | 0:9b334a45a8ff | 46 | | (value << 6); |
bogdanm | 0:9b334a45a8ff | 47 | } |
bogdanm | 0:9b334a45a8ff | 48 | |
bogdanm | 0:9b334a45a8ff | 49 | static inline int dac_read() { |
bogdanm | 0:9b334a45a8ff | 50 | return (LPC_DAC->DACR >> 6) & 0x3FF; |
bogdanm | 0:9b334a45a8ff | 51 | } |
bogdanm | 0:9b334a45a8ff | 52 | |
bogdanm | 0:9b334a45a8ff | 53 | void analogout_write(dac_t *obj, float value) { |
bogdanm | 0:9b334a45a8ff | 54 | if (value < 0.0f) { |
bogdanm | 0:9b334a45a8ff | 55 | dac_write(0); |
bogdanm | 0:9b334a45a8ff | 56 | } else if (value > 1.0f) { |
bogdanm | 0:9b334a45a8ff | 57 | dac_write(0x3FF); |
bogdanm | 0:9b334a45a8ff | 58 | } else { |
bogdanm | 0:9b334a45a8ff | 59 | dac_write(value * (float)0x3FF); |
bogdanm | 0:9b334a45a8ff | 60 | } |
bogdanm | 0:9b334a45a8ff | 61 | } |
bogdanm | 0:9b334a45a8ff | 62 | |
bogdanm | 0:9b334a45a8ff | 63 | void analogout_write_u16(dac_t *obj, uint16_t value) { |
bogdanm | 0:9b334a45a8ff | 64 | dac_write(value >> 6); // 10-bit |
bogdanm | 0:9b334a45a8ff | 65 | } |
bogdanm | 0:9b334a45a8ff | 66 | |
bogdanm | 0:9b334a45a8ff | 67 | float analogout_read(dac_t *obj) { |
bogdanm | 0:9b334a45a8ff | 68 | uint32_t value = dac_read(); |
bogdanm | 0:9b334a45a8ff | 69 | return (float)value * (1.0f / (float)0x3FF); |
bogdanm | 0:9b334a45a8ff | 70 | } |
bogdanm | 0:9b334a45a8ff | 71 | |
bogdanm | 0:9b334a45a8ff | 72 | uint16_t analogout_read_u16(dac_t *obj) { |
bogdanm | 0:9b334a45a8ff | 73 | uint32_t value = dac_read(); // 10-bit |
bogdanm | 0:9b334a45a8ff | 74 | return (value << 6) | ((value >> 4) & 0x003F); |
bogdanm | 0:9b334a45a8ff | 75 | } |