mbed os with nrf51 internal bandgap enabled to read battery level
Dependents: BLE_file_test BLE_Blink ExternalEncoder
targets/TARGET_Freescale/TARGET_KLXX/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-2013 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 | #include "mbed_assert.h" |
elessair | 0:f269e3021894 | 17 | #include "analogout_api.h" |
elessair | 0:f269e3021894 | 18 | |
elessair | 0:f269e3021894 | 19 | #include "cmsis.h" |
elessair | 0:f269e3021894 | 20 | #include "pinmap.h" |
elessair | 0:f269e3021894 | 21 | #include "PeripheralPins.h" |
elessair | 0:f269e3021894 | 22 | |
elessair | 0:f269e3021894 | 23 | #define RANGE_12BIT 0xFFF |
elessair | 0:f269e3021894 | 24 | |
elessair | 0:f269e3021894 | 25 | void analogout_init(dac_t *obj, PinName pin) { |
elessair | 0:f269e3021894 | 26 | obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); |
elessair | 0:f269e3021894 | 27 | MBED_ASSERT(obj->dac != (DACName)NC); |
elessair | 0:f269e3021894 | 28 | |
elessair | 0:f269e3021894 | 29 | SIM->SCGC6 |= SIM_SCGC6_DAC0_MASK; |
elessair | 0:f269e3021894 | 30 | |
elessair | 0:f269e3021894 | 31 | uint32_t port = (uint32_t)pin >> PORT_SHIFT; |
elessair | 0:f269e3021894 | 32 | SIM->SCGC5 |= 1 << (SIM_SCGC5_PORTA_SHIFT + port); |
elessair | 0:f269e3021894 | 33 | |
elessair | 0:f269e3021894 | 34 | DAC0->DAT[obj->dac].DATH = 0; |
elessair | 0:f269e3021894 | 35 | DAC0->DAT[obj->dac].DATL = 0; |
elessair | 0:f269e3021894 | 36 | |
elessair | 0:f269e3021894 | 37 | DAC0->C1 = DAC_C1_DACBFMD_MASK; // One-Time Scan Mode |
elessair | 0:f269e3021894 | 38 | |
elessair | 0:f269e3021894 | 39 | DAC0->C0 = DAC_C0_DACEN_MASK // Enable |
elessair | 0:f269e3021894 | 40 | | DAC_C0_DACSWTRG_MASK; // Software Trigger |
elessair | 0:f269e3021894 | 41 | |
elessair | 0:f269e3021894 | 42 | pinmap_pinout(pin, PinMap_DAC); |
elessair | 0:f269e3021894 | 43 | |
elessair | 0:f269e3021894 | 44 | analogout_write_u16(obj, 0); |
elessair | 0:f269e3021894 | 45 | } |
elessair | 0:f269e3021894 | 46 | |
elessair | 0:f269e3021894 | 47 | void analogout_free(dac_t *obj) {} |
elessair | 0:f269e3021894 | 48 | |
elessair | 0:f269e3021894 | 49 | static inline void dac_write(dac_t *obj, int value) { |
elessair | 0:f269e3021894 | 50 | DAC0->DAT[obj->dac].DATL = (uint8_t)( value & 0xFF); |
elessair | 0:f269e3021894 | 51 | DAC0->DAT[obj->dac].DATH = (uint8_t)((value >> 8) & 0xFF); |
elessair | 0:f269e3021894 | 52 | } |
elessair | 0:f269e3021894 | 53 | |
elessair | 0:f269e3021894 | 54 | static inline int dac_read(dac_t *obj) { |
elessair | 0:f269e3021894 | 55 | return ((DAC0->DAT[obj->dac].DATH << 8) | DAC0->DAT[obj->dac].DATL); |
elessair | 0:f269e3021894 | 56 | } |
elessair | 0:f269e3021894 | 57 | |
elessair | 0:f269e3021894 | 58 | void analogout_write(dac_t *obj, float value) { |
elessair | 0:f269e3021894 | 59 | if (value < 0.0) { |
elessair | 0:f269e3021894 | 60 | dac_write(obj, 0); |
elessair | 0:f269e3021894 | 61 | } else if (value > 1.0) { |
elessair | 0:f269e3021894 | 62 | dac_write(obj, RANGE_12BIT); |
elessair | 0:f269e3021894 | 63 | } else { |
elessair | 0:f269e3021894 | 64 | dac_write(obj, value * (float)RANGE_12BIT); |
elessair | 0:f269e3021894 | 65 | } |
elessair | 0:f269e3021894 | 66 | } |
elessair | 0:f269e3021894 | 67 | |
elessair | 0:f269e3021894 | 68 | void analogout_write_u16(dac_t *obj, uint16_t value) { |
elessair | 0:f269e3021894 | 69 | dac_write(obj, value >> 4); // 12-bit |
elessair | 0:f269e3021894 | 70 | } |
elessair | 0:f269e3021894 | 71 | |
elessair | 0:f269e3021894 | 72 | float analogout_read(dac_t *obj) { |
elessair | 0:f269e3021894 | 73 | uint32_t value = dac_read(obj); |
elessair | 0:f269e3021894 | 74 | return (float)value * (1.0f / (float)RANGE_12BIT); |
elessair | 0:f269e3021894 | 75 | } |
elessair | 0:f269e3021894 | 76 | |
elessair | 0:f269e3021894 | 77 | uint16_t analogout_read_u16(dac_t *obj) { |
elessair | 0:f269e3021894 | 78 | uint32_t value = dac_read(obj); // 12-bit |
elessair | 0:f269e3021894 | 79 | return (value << 4) | ((value >> 8) & 0x003F); |
elessair | 0:f269e3021894 | 80 | } |