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