Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-src by
targets/hal/TARGET_NXP/TARGET_LPC43XX/analogout_api.c@285:31249416b6f9, 2014-08-15 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Aug 15 16:30:08 2014 +0100
- Revision:
- 285:31249416b6f9
- Parent:
- 256:76fd9a263045
- Child:
- 315:1d806f4588f1
Synchronized with git revision 601712595f49bbd2a2771c52cf3e84c9c7a591af
Full URL: https://github.com/mbedmicro/mbed/commit/601712595f49bbd2a2771c52cf3e84c9c7a591af/
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 | 256:76fd9a263045 | 27 | {P4_4, DAC_0, 0}, |
mbed_official | 256:76fd9a263045 | 28 | {NC, NC, 0} |
bogdanm | 20:4263a77256ae | 29 | }; |
bogdanm | 20:4263a77256ae | 30 | |
bogdanm | 20:4263a77256ae | 31 | void analogout_init(dac_t *obj, PinName pin) { |
bogdanm | 20:4263a77256ae | 32 | obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); |
mbed_official | 227:7bd0639b8911 | 33 | MBED_ASSERT(obj->dac != (DACName)NC); |
bogdanm | 20:4263a77256ae | 34 | |
mbed_official | 256:76fd9a263045 | 35 | // Reset pin function to GPIO |
mbed_official | 256:76fd9a263045 | 36 | gpio_set(pin); |
bogdanm | 20:4263a77256ae | 37 | // Select DAC on analog function select register in SCU |
mbed_official | 256:76fd9a263045 | 38 | LPC_SCU->ENAIO[2] |= 1; // Sets pin as DAC |
bogdanm | 20:4263a77256ae | 39 | |
mbed_official | 256:76fd9a263045 | 40 | // Set bias=0 for maximum DAC update rate (1 MHz) |
bogdanm | 20:4263a77256ae | 41 | LPC_DAC->CR &= ~DAC_BIAS_EN; |
mbed_official | 256:76fd9a263045 | 42 | // Enable DAC and DMA |
mbed_official | 256:76fd9a263045 | 43 | LPC_DAC->CTRL |= DAC_DMA_ENA; |
bogdanm | 20:4263a77256ae | 44 | |
bogdanm | 20:4263a77256ae | 45 | analogout_write_u16(obj, 0); |
bogdanm | 20:4263a77256ae | 46 | } |
bogdanm | 20:4263a77256ae | 47 | |
bogdanm | 20:4263a77256ae | 48 | void analogout_free(dac_t *obj) {} |
bogdanm | 20:4263a77256ae | 49 | |
bogdanm | 20:4263a77256ae | 50 | static inline void dac_write(int value) { |
bogdanm | 20:4263a77256ae | 51 | |
bogdanm | 20:4263a77256ae | 52 | // Set the DAC output |
mbed_official | 256:76fd9a263045 | 53 | LPC_DAC->CR = DAC_SET(value); |
bogdanm | 20:4263a77256ae | 54 | } |
bogdanm | 20:4263a77256ae | 55 | |
bogdanm | 20:4263a77256ae | 56 | static inline int dac_read() { |
mbed_official | 256:76fd9a263045 | 57 | return (DAC_GET(LPC_DAC->CR)); |
bogdanm | 20:4263a77256ae | 58 | } |
bogdanm | 20:4263a77256ae | 59 | |
bogdanm | 20:4263a77256ae | 60 | void analogout_write(dac_t *obj, float value) { |
bogdanm | 20:4263a77256ae | 61 | if (value < 0.0f) { |
bogdanm | 20:4263a77256ae | 62 | dac_write(0); |
bogdanm | 20:4263a77256ae | 63 | } else if (value > 1.0f) { |
bogdanm | 20:4263a77256ae | 64 | dac_write(DAC_RANGE); |
bogdanm | 20:4263a77256ae | 65 | } else { |
bogdanm | 20:4263a77256ae | 66 | dac_write(value * (float)DAC_RANGE); |
bogdanm | 20:4263a77256ae | 67 | } |
bogdanm | 20:4263a77256ae | 68 | } |
bogdanm | 20:4263a77256ae | 69 | |
bogdanm | 20:4263a77256ae | 70 | void analogout_write_u16(dac_t *obj, uint16_t value) { |
bogdanm | 20:4263a77256ae | 71 | dac_write(value >> 6); // 10-bit |
bogdanm | 20:4263a77256ae | 72 | } |
bogdanm | 20:4263a77256ae | 73 | |
bogdanm | 20:4263a77256ae | 74 | float analogout_read(dac_t *obj) { |
bogdanm | 20:4263a77256ae | 75 | uint32_t value = dac_read(); |
bogdanm | 20:4263a77256ae | 76 | return (float)value * (1.0f / (float)DAC_RANGE); |
bogdanm | 20:4263a77256ae | 77 | } |
bogdanm | 20:4263a77256ae | 78 | |
bogdanm | 20:4263a77256ae | 79 | uint16_t analogout_read_u16(dac_t *obj) { |
bogdanm | 20:4263a77256ae | 80 | uint32_t value = dac_read(); // 10-bit |
bogdanm | 20:4263a77256ae | 81 | return (value << 6) | ((value >> 4) & 0x003F); |
bogdanm | 20:4263a77256ae | 82 | } |