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