mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_NXP/TARGET_LPC43XX/analogout_api.c@227:7bd0639b8911, 2014-06-11 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Jun 11 16:00:09 2014 +0100
- Revision:
- 227:7bd0639b8911
- Parent:
- 20:4263a77256ae
- Child:
- 256:76fd9a263045
Synchronized with git revision d58d532ebc0e0a96f4fffb8edefc082b71b964af
Full URL: https://github.com/mbedmicro/mbed/commit/d58d532ebc0e0a96f4fffb8edefc082b71b964af/
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" |
bogdanm | 20:4263a77256ae | 20 | #include "cmsis.h" |
bogdanm | 20:4263a77256ae | 21 | #include "pinmap.h" |
bogdanm | 20:4263a77256ae | 22 | |
bogdanm | 20:4263a77256ae | 23 | static const PinMap PinMap_DAC[] = { |
bogdanm | 20:4263a77256ae | 24 | {P_DAC0 , DAC_0, 0x0}, |
bogdanm | 20:4263a77256ae | 25 | {NC , NC , 0} |
bogdanm | 20:4263a77256ae | 26 | }; |
bogdanm | 20:4263a77256ae | 27 | |
bogdanm | 20:4263a77256ae | 28 | void analogout_init(dac_t *obj, PinName pin) { |
bogdanm | 20:4263a77256ae | 29 | obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); |
mbed_official | 227:7bd0639b8911 | 30 | MBED_ASSERT(obj->dac != (DACName)NC); |
bogdanm | 20:4263a77256ae | 31 | |
bogdanm | 20:4263a77256ae | 32 | // Configure the pin as GPIO input |
bogdanm | 20:4263a77256ae | 33 | pin_function(pin, (SCU_PINIO_PULLNONE | 0x0)); |
bogdanm | 20:4263a77256ae | 34 | pin_mode(pin, PullNone); |
bogdanm | 20:4263a77256ae | 35 | // Select DAC on analog function select register in SCU |
bogdanm | 20:4263a77256ae | 36 | LPC_SCU->ENAIO[2] |= 1; // Sets pin P4_4 as DAC |
bogdanm | 20:4263a77256ae | 37 | |
bogdanm | 20:4263a77256ae | 38 | // Set Maximum update rate for DAC */ |
bogdanm | 20:4263a77256ae | 39 | LPC_DAC->CR &= ~DAC_BIAS_EN; |
bogdanm | 20:4263a77256ae | 40 | |
bogdanm | 20:4263a77256ae | 41 | analogout_write_u16(obj, 0); |
bogdanm | 20:4263a77256ae | 42 | } |
bogdanm | 20:4263a77256ae | 43 | |
bogdanm | 20:4263a77256ae | 44 | void analogout_free(dac_t *obj) {} |
bogdanm | 20:4263a77256ae | 45 | |
bogdanm | 20:4263a77256ae | 46 | static inline void dac_write(int value) { |
bogdanm | 20:4263a77256ae | 47 | uint32_t tmp; |
bogdanm | 20:4263a77256ae | 48 | |
bogdanm | 20:4263a77256ae | 49 | // Set the DAC output |
bogdanm | 20:4263a77256ae | 50 | tmp = LPC_DAC->CR & DAC_BIAS_EN; |
bogdanm | 20:4263a77256ae | 51 | tmp |= DAC_VALUE(value); |
bogdanm | 20:4263a77256ae | 52 | LPC_DAC->CR = tmp; |
bogdanm | 20:4263a77256ae | 53 | } |
bogdanm | 20:4263a77256ae | 54 | |
bogdanm | 20:4263a77256ae | 55 | static inline int dac_read() { |
bogdanm | 20:4263a77256ae | 56 | return (DAC_VALUE(LPC_DAC->CR)); |
bogdanm | 20:4263a77256ae | 57 | } |
bogdanm | 20:4263a77256ae | 58 | |
bogdanm | 20:4263a77256ae | 59 | void analogout_write(dac_t *obj, float value) { |
bogdanm | 20:4263a77256ae | 60 | if (value < 0.0f) { |
bogdanm | 20:4263a77256ae | 61 | dac_write(0); |
bogdanm | 20:4263a77256ae | 62 | } else if (value > 1.0f) { |
bogdanm | 20:4263a77256ae | 63 | dac_write(DAC_RANGE); |
bogdanm | 20:4263a77256ae | 64 | } else { |
bogdanm | 20:4263a77256ae | 65 | dac_write(value * (float)DAC_RANGE); |
bogdanm | 20:4263a77256ae | 66 | } |
bogdanm | 20:4263a77256ae | 67 | } |
bogdanm | 20:4263a77256ae | 68 | |
bogdanm | 20:4263a77256ae | 69 | void analogout_write_u16(dac_t *obj, uint16_t value) { |
bogdanm | 20:4263a77256ae | 70 | dac_write(value >> 6); // 10-bit |
bogdanm | 20:4263a77256ae | 71 | } |
bogdanm | 20:4263a77256ae | 72 | |
bogdanm | 20:4263a77256ae | 73 | float analogout_read(dac_t *obj) { |
bogdanm | 20:4263a77256ae | 74 | uint32_t value = dac_read(); |
bogdanm | 20:4263a77256ae | 75 | return (float)value * (1.0f / (float)DAC_RANGE); |
bogdanm | 20:4263a77256ae | 76 | } |
bogdanm | 20:4263a77256ae | 77 | |
bogdanm | 20:4263a77256ae | 78 | uint16_t analogout_read_u16(dac_t *obj) { |
bogdanm | 20:4263a77256ae | 79 | uint32_t value = dac_read(); // 10-bit |
bogdanm | 20:4263a77256ae | 80 | return (value << 6) | ((value >> 4) & 0x003F); |
bogdanm | 20:4263a77256ae | 81 | } |