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@310:6188e0254baa, 2014-09-16 (annotated)
- Committer:
- janekm
- Date:
- Tue Sep 16 22:42:01 2014 +0000
- Revision:
- 310:6188e0254baa
- Parent:
- 227:7bd0639b8911
N/A
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 | 227:7bd0639b8911 | 16 | #include "mbed_assert.h" |
mbed_official | 82:0b31dbcd4769 | 17 | #include "analogout_api.h" |
mbed_official | 82:0b31dbcd4769 | 18 | |
mbed_official | 82:0b31dbcd4769 | 19 | #include "cmsis.h" |
mbed_official | 82:0b31dbcd4769 | 20 | #include "pinmap.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 | void analogout_init(dac_t *obj, PinName pin) { |
mbed_official | 82:0b31dbcd4769 | 26 | obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); |
mbed_official | 227:7bd0639b8911 | 27 | MBED_ASSERT(obj->dac != (DACName)NC); |
mbed_official | 82:0b31dbcd4769 | 28 | |
mbed_official | 82:0b31dbcd4769 | 29 | SIM->SCGC6 |= SIM_SCGC6_DAC0_MASK; |
mbed_official | 82:0b31dbcd4769 | 30 | |
mbed_official | 82:0b31dbcd4769 | 31 | uint32_t port = (uint32_t)pin >> PORT_SHIFT; |
mbed_official | 82:0b31dbcd4769 | 32 | SIM->SCGC5 |= 1 << (SIM_SCGC5_PORTA_SHIFT + port); |
mbed_official | 82:0b31dbcd4769 | 33 | |
mbed_official | 82:0b31dbcd4769 | 34 | DAC0->DAT[obj->dac].DATH = 0; |
mbed_official | 82:0b31dbcd4769 | 35 | DAC0->DAT[obj->dac].DATL = 0; |
mbed_official | 82:0b31dbcd4769 | 36 | |
mbed_official | 82:0b31dbcd4769 | 37 | DAC0->C1 = DAC_C1_DACBFMD_MASK; // One-Time Scan Mode |
mbed_official | 82:0b31dbcd4769 | 38 | |
mbed_official | 82:0b31dbcd4769 | 39 | DAC0->C0 = DAC_C0_DACEN_MASK // Enable |
mbed_official | 82:0b31dbcd4769 | 40 | | DAC_C0_DACSWTRG_MASK; // Software Trigger |
mbed_official | 82:0b31dbcd4769 | 41 | |
mbed_official | 82:0b31dbcd4769 | 42 | pinmap_pinout(pin, PinMap_DAC); |
mbed_official | 82:0b31dbcd4769 | 43 | |
mbed_official | 82:0b31dbcd4769 | 44 | analogout_write_u16(obj, 0); |
mbed_official | 82:0b31dbcd4769 | 45 | } |
mbed_official | 82:0b31dbcd4769 | 46 | |
mbed_official | 82:0b31dbcd4769 | 47 | void analogout_free(dac_t *obj) {} |
mbed_official | 82:0b31dbcd4769 | 48 | |
mbed_official | 82:0b31dbcd4769 | 49 | static inline void dac_write(dac_t *obj, int value) { |
mbed_official | 82:0b31dbcd4769 | 50 | DAC0->DAT[obj->dac].DATL = (uint8_t)( value & 0xFF); |
mbed_official | 82:0b31dbcd4769 | 51 | DAC0->DAT[obj->dac].DATH = (uint8_t)((value >> 8) & 0xFF); |
mbed_official | 82:0b31dbcd4769 | 52 | } |
mbed_official | 82:0b31dbcd4769 | 53 | |
mbed_official | 82:0b31dbcd4769 | 54 | static inline int dac_read(dac_t *obj) { |
mbed_official | 82:0b31dbcd4769 | 55 | return ((DAC0->DAT[obj->dac].DATH << 8) | DAC0->DAT[obj->dac].DATL); |
mbed_official | 82:0b31dbcd4769 | 56 | } |
mbed_official | 82:0b31dbcd4769 | 57 | |
mbed_official | 82:0b31dbcd4769 | 58 | void analogout_write(dac_t *obj, float value) { |
mbed_official | 82:0b31dbcd4769 | 59 | if (value < 0.0) { |
mbed_official | 82:0b31dbcd4769 | 60 | dac_write(obj, 0); |
mbed_official | 82:0b31dbcd4769 | 61 | } else if (value > 1.0) { |
mbed_official | 82:0b31dbcd4769 | 62 | dac_write(obj, RANGE_12BIT); |
mbed_official | 82:0b31dbcd4769 | 63 | } else { |
mbed_official | 82:0b31dbcd4769 | 64 | dac_write(obj, value * (float)RANGE_12BIT); |
mbed_official | 82:0b31dbcd4769 | 65 | } |
mbed_official | 82:0b31dbcd4769 | 66 | } |
mbed_official | 82:0b31dbcd4769 | 67 | |
mbed_official | 82:0b31dbcd4769 | 68 | void analogout_write_u16(dac_t *obj, uint16_t value) { |
mbed_official | 82:0b31dbcd4769 | 69 | dac_write(obj, value >> 4); // 12-bit |
mbed_official | 82:0b31dbcd4769 | 70 | } |
mbed_official | 82:0b31dbcd4769 | 71 | |
mbed_official | 82:0b31dbcd4769 | 72 | float analogout_read(dac_t *obj) { |
mbed_official | 82:0b31dbcd4769 | 73 | uint32_t value = dac_read(obj); |
mbed_official | 82:0b31dbcd4769 | 74 | return (float)value * (1.0f / (float)RANGE_12BIT); |
mbed_official | 82:0b31dbcd4769 | 75 | } |
mbed_official | 82:0b31dbcd4769 | 76 | |
mbed_official | 82:0b31dbcd4769 | 77 | uint16_t analogout_read_u16(dac_t *obj) { |
mbed_official | 82:0b31dbcd4769 | 78 | uint32_t value = dac_read(obj); // 12-bit |
mbed_official | 82:0b31dbcd4769 | 79 | return (value << 4) | ((value >> 8) & 0x003F); |
mbed_official | 82:0b31dbcd4769 | 80 | } |