mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_NXP/TARGET_LPC43XX/analogout_api.c@323:9e901b0a5aa1, 2014-09-13 (annotated)
- Committer:
- shaoziyang
- Date:
- Sat Sep 13 14:25:46 2014 +0000
- Revision:
- 323:9e901b0a5aa1
- Parent:
- 315:1d806f4588f1
test with CLOCK_SETUP = 0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 20:4263a77256ae | 1 | /* mbed Microcontroller Library |
bogdanm | 20:4263a77256ae | 2 | * Copyright (c) 2006-2013 ARM Limited |
bogdanm | 20:4263a77256ae | 3 | * |
bogdanm | 20:4263a77256ae | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
bogdanm | 20:4263a77256ae | 5 | * you may not use this file except in compliance with the License. |
bogdanm | 20:4263a77256ae | 6 | * You may obtain a copy of the License at |
bogdanm | 20:4263a77256ae | 7 | * |
bogdanm | 20:4263a77256ae | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bogdanm | 20:4263a77256ae | 9 | * |
bogdanm | 20:4263a77256ae | 10 | * Unless required by applicable law or agreed to in writing, software |
bogdanm | 20:4263a77256ae | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
bogdanm | 20:4263a77256ae | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bogdanm | 20:4263a77256ae | 13 | * See the License for the specific language governing permissions and |
bogdanm | 20:4263a77256ae | 14 | * limitations under the License. |
bogdanm | 20:4263a77256ae | 15 | * |
bogdanm | 20:4263a77256ae | 16 | * Ported to NXP LPC43XX by Micromint USA <support@micromint.com> |
bogdanm | 20:4263a77256ae | 17 | */ |
mbed_official | 227:7bd0639b8911 | 18 | #include "mbed_assert.h" |
bogdanm | 20:4263a77256ae | 19 | #include "analogout_api.h" |
mbed_official | 256:76fd9a263045 | 20 | |
bogdanm | 20:4263a77256ae | 21 | #include "cmsis.h" |
bogdanm | 20:4263a77256ae | 22 | #include "pinmap.h" |
mbed_official | 285:31249416b6f9 | 23 | #include "mbed_error.h" |
mbed_official | 256:76fd9a263045 | 24 | #include "gpio_api.h" |
bogdanm | 20:4263a77256ae | 25 | |
bogdanm | 20:4263a77256ae | 26 | static const PinMap PinMap_DAC[] = { |
mbed_official | 315:1d806f4588f1 | 27 | #ifdef TARGET_LPC4337 |
mbed_official | 315:1d806f4588f1 | 28 | {P4_3, DAC_0, 0}, |
mbed_official | 315:1d806f4588f1 | 29 | #else |
mbed_official | 256:76fd9a263045 | 30 | {P4_4, DAC_0, 0}, |
mbed_official | 315:1d806f4588f1 | 31 | #endif |
mbed_official | 256:76fd9a263045 | 32 | {NC, NC, 0} |
bogdanm | 20:4263a77256ae | 33 | }; |
bogdanm | 20:4263a77256ae | 34 | |
bogdanm | 20:4263a77256ae | 35 | void analogout_init(dac_t *obj, PinName pin) { |
bogdanm | 20:4263a77256ae | 36 | obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); |
mbed_official | 227:7bd0639b8911 | 37 | MBED_ASSERT(obj->dac != (DACName)NC); |
bogdanm | 20:4263a77256ae | 38 | |
mbed_official | 256:76fd9a263045 | 39 | // Reset pin function to GPIO |
mbed_official | 256:76fd9a263045 | 40 | gpio_set(pin); |
bogdanm | 20:4263a77256ae | 41 | // Select DAC on analog function select register in SCU |
mbed_official | 256:76fd9a263045 | 42 | LPC_SCU->ENAIO[2] |= 1; // Sets pin as DAC |
bogdanm | 20:4263a77256ae | 43 | |
mbed_official | 256:76fd9a263045 | 44 | // Set bias=0 for maximum DAC update rate (1 MHz) |
bogdanm | 20:4263a77256ae | 45 | LPC_DAC->CR &= ~DAC_BIAS_EN; |
mbed_official | 256:76fd9a263045 | 46 | // Enable DAC and DMA |
mbed_official | 256:76fd9a263045 | 47 | LPC_DAC->CTRL |= DAC_DMA_ENA; |
bogdanm | 20:4263a77256ae | 48 | |
bogdanm | 20:4263a77256ae | 49 | analogout_write_u16(obj, 0); |
bogdanm | 20:4263a77256ae | 50 | } |
bogdanm | 20:4263a77256ae | 51 | |
bogdanm | 20:4263a77256ae | 52 | void analogout_free(dac_t *obj) {} |
bogdanm | 20:4263a77256ae | 53 | |
bogdanm | 20:4263a77256ae | 54 | static inline void dac_write(int value) { |
bogdanm | 20:4263a77256ae | 55 | |
bogdanm | 20:4263a77256ae | 56 | // Set the DAC output |
mbed_official | 256:76fd9a263045 | 57 | LPC_DAC->CR = DAC_SET(value); |
bogdanm | 20:4263a77256ae | 58 | } |
bogdanm | 20:4263a77256ae | 59 | |
bogdanm | 20:4263a77256ae | 60 | static inline int dac_read() { |
mbed_official | 256:76fd9a263045 | 61 | return (DAC_GET(LPC_DAC->CR)); |
bogdanm | 20:4263a77256ae | 62 | } |
bogdanm | 20:4263a77256ae | 63 | |
bogdanm | 20:4263a77256ae | 64 | void analogout_write(dac_t *obj, float value) { |
bogdanm | 20:4263a77256ae | 65 | if (value < 0.0f) { |
bogdanm | 20:4263a77256ae | 66 | dac_write(0); |
bogdanm | 20:4263a77256ae | 67 | } else if (value > 1.0f) { |
bogdanm | 20:4263a77256ae | 68 | dac_write(DAC_RANGE); |
bogdanm | 20:4263a77256ae | 69 | } else { |
bogdanm | 20:4263a77256ae | 70 | dac_write(value * (float)DAC_RANGE); |
bogdanm | 20:4263a77256ae | 71 | } |
bogdanm | 20:4263a77256ae | 72 | } |
bogdanm | 20:4263a77256ae | 73 | |
bogdanm | 20:4263a77256ae | 74 | void analogout_write_u16(dac_t *obj, uint16_t value) { |
bogdanm | 20:4263a77256ae | 75 | dac_write(value >> 6); // 10-bit |
bogdanm | 20:4263a77256ae | 76 | } |
bogdanm | 20:4263a77256ae | 77 | |
bogdanm | 20:4263a77256ae | 78 | float analogout_read(dac_t *obj) { |
bogdanm | 20:4263a77256ae | 79 | uint32_t value = dac_read(); |
bogdanm | 20:4263a77256ae | 80 | return (float)value * (1.0f / (float)DAC_RANGE); |
bogdanm | 20:4263a77256ae | 81 | } |
bogdanm | 20:4263a77256ae | 82 | |
bogdanm | 20:4263a77256ae | 83 | uint16_t analogout_read_u16(dac_t *obj) { |
bogdanm | 20:4263a77256ae | 84 | uint32_t value = dac_read(); // 10-bit |
bogdanm | 20:4263a77256ae | 85 | return (value << 6) | ((value >> 4) & 0x003F); |
bogdanm | 20:4263a77256ae | 86 | } |