mbed library sources modified for open wear
Dependents: openwear-lifelogger-example
Fork of mbed-src by
targets/hal/TARGET_Freescale/TARGET_KLXX/analogout_api.c@82:0b31dbcd4769, 2014-01-31 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Jan 31 10:00:06 2014 +0000
- Revision:
- 82:0b31dbcd4769
- Child:
- 227:7bd0639b8911
Synchronized with git revision 74409cbd593d1daab530a57baaa563f30b04b018
Full URL: https://github.com/mbedmicro/mbed/commit/74409cbd593d1daab530a57baaa563f30b04b018/
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 82:0b31dbcd4769 | 1 | /* mbed Microcontroller Library |
mbed_official | 82:0b31dbcd4769 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbed_official | 82:0b31dbcd4769 | 3 | * |
mbed_official | 82:0b31dbcd4769 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 82:0b31dbcd4769 | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 82:0b31dbcd4769 | 6 | * You may obtain a copy of the License at |
mbed_official | 82:0b31dbcd4769 | 7 | * |
mbed_official | 82:0b31dbcd4769 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 82:0b31dbcd4769 | 9 | * |
mbed_official | 82:0b31dbcd4769 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 82:0b31dbcd4769 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 82:0b31dbcd4769 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 82:0b31dbcd4769 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 82:0b31dbcd4769 | 14 | * limitations under the License. |
mbed_official | 82:0b31dbcd4769 | 15 | */ |
mbed_official | 82:0b31dbcd4769 | 16 | #include "analogout_api.h" |
mbed_official | 82:0b31dbcd4769 | 17 | |
mbed_official | 82:0b31dbcd4769 | 18 | #include "cmsis.h" |
mbed_official | 82:0b31dbcd4769 | 19 | #include "pinmap.h" |
mbed_official | 82:0b31dbcd4769 | 20 | #include "error.h" |
mbed_official | 82:0b31dbcd4769 | 21 | #include "PeripheralPins.h" |
mbed_official | 82:0b31dbcd4769 | 22 | |
mbed_official | 82:0b31dbcd4769 | 23 | #define RANGE_12BIT 0xFFF |
mbed_official | 82:0b31dbcd4769 | 24 | |
mbed_official | 82:0b31dbcd4769 | 25 | |
mbed_official | 82:0b31dbcd4769 | 26 | void analogout_init(dac_t *obj, PinName pin) { |
mbed_official | 82:0b31dbcd4769 | 27 | obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); |
mbed_official | 82:0b31dbcd4769 | 28 | if (obj->dac == (DACName)NC) { |
mbed_official | 82:0b31dbcd4769 | 29 | error("DAC pin mapping failed"); |
mbed_official | 82:0b31dbcd4769 | 30 | } |
mbed_official | 82:0b31dbcd4769 | 31 | |
mbed_official | 82:0b31dbcd4769 | 32 | SIM->SCGC6 |= SIM_SCGC6_DAC0_MASK; |
mbed_official | 82:0b31dbcd4769 | 33 | |
mbed_official | 82:0b31dbcd4769 | 34 | uint32_t port = (uint32_t)pin >> PORT_SHIFT; |
mbed_official | 82:0b31dbcd4769 | 35 | SIM->SCGC5 |= 1 << (SIM_SCGC5_PORTA_SHIFT + port); |
mbed_official | 82:0b31dbcd4769 | 36 | |
mbed_official | 82:0b31dbcd4769 | 37 | DAC0->DAT[obj->dac].DATH = 0; |
mbed_official | 82:0b31dbcd4769 | 38 | DAC0->DAT[obj->dac].DATL = 0; |
mbed_official | 82:0b31dbcd4769 | 39 | |
mbed_official | 82:0b31dbcd4769 | 40 | DAC0->C1 = DAC_C1_DACBFMD_MASK; // One-Time Scan Mode |
mbed_official | 82:0b31dbcd4769 | 41 | |
mbed_official | 82:0b31dbcd4769 | 42 | DAC0->C0 = DAC_C0_DACEN_MASK // Enable |
mbed_official | 82:0b31dbcd4769 | 43 | | DAC_C0_DACSWTRG_MASK; // Software Trigger |
mbed_official | 82:0b31dbcd4769 | 44 | |
mbed_official | 82:0b31dbcd4769 | 45 | pinmap_pinout(pin, PinMap_DAC); |
mbed_official | 82:0b31dbcd4769 | 46 | |
mbed_official | 82:0b31dbcd4769 | 47 | analogout_write_u16(obj, 0); |
mbed_official | 82:0b31dbcd4769 | 48 | } |
mbed_official | 82:0b31dbcd4769 | 49 | |
mbed_official | 82:0b31dbcd4769 | 50 | void analogout_free(dac_t *obj) {} |
mbed_official | 82:0b31dbcd4769 | 51 | |
mbed_official | 82:0b31dbcd4769 | 52 | static inline void dac_write(dac_t *obj, int value) { |
mbed_official | 82:0b31dbcd4769 | 53 | DAC0->DAT[obj->dac].DATL = (uint8_t)( value & 0xFF); |
mbed_official | 82:0b31dbcd4769 | 54 | DAC0->DAT[obj->dac].DATH = (uint8_t)((value >> 8) & 0xFF); |
mbed_official | 82:0b31dbcd4769 | 55 | } |
mbed_official | 82:0b31dbcd4769 | 56 | |
mbed_official | 82:0b31dbcd4769 | 57 | static inline int dac_read(dac_t *obj) { |
mbed_official | 82:0b31dbcd4769 | 58 | return ((DAC0->DAT[obj->dac].DATH << 8) | DAC0->DAT[obj->dac].DATL); |
mbed_official | 82:0b31dbcd4769 | 59 | } |
mbed_official | 82:0b31dbcd4769 | 60 | |
mbed_official | 82:0b31dbcd4769 | 61 | void analogout_write(dac_t *obj, float value) { |
mbed_official | 82:0b31dbcd4769 | 62 | if (value < 0.0) { |
mbed_official | 82:0b31dbcd4769 | 63 | dac_write(obj, 0); |
mbed_official | 82:0b31dbcd4769 | 64 | } else if (value > 1.0) { |
mbed_official | 82:0b31dbcd4769 | 65 | dac_write(obj, RANGE_12BIT); |
mbed_official | 82:0b31dbcd4769 | 66 | } else { |
mbed_official | 82:0b31dbcd4769 | 67 | dac_write(obj, value * (float)RANGE_12BIT); |
mbed_official | 82:0b31dbcd4769 | 68 | } |
mbed_official | 82:0b31dbcd4769 | 69 | } |
mbed_official | 82:0b31dbcd4769 | 70 | |
mbed_official | 82:0b31dbcd4769 | 71 | void analogout_write_u16(dac_t *obj, uint16_t value) { |
mbed_official | 82:0b31dbcd4769 | 72 | dac_write(obj, value >> 4); // 12-bit |
mbed_official | 82:0b31dbcd4769 | 73 | } |
mbed_official | 82:0b31dbcd4769 | 74 | |
mbed_official | 82:0b31dbcd4769 | 75 | float analogout_read(dac_t *obj) { |
mbed_official | 82:0b31dbcd4769 | 76 | uint32_t value = dac_read(obj); |
mbed_official | 82:0b31dbcd4769 | 77 | return (float)value * (1.0f / (float)RANGE_12BIT); |
mbed_official | 82:0b31dbcd4769 | 78 | } |
mbed_official | 82:0b31dbcd4769 | 79 | |
mbed_official | 82:0b31dbcd4769 | 80 | uint16_t analogout_read_u16(dac_t *obj) { |
mbed_official | 82:0b31dbcd4769 | 81 | uint32_t value = dac_read(obj); // 12-bit |
mbed_official | 82:0b31dbcd4769 | 82 | return (value << 4) | ((value >> 8) & 0x003F); |
mbed_official | 82:0b31dbcd4769 | 83 | } |