]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KLXX/analogout_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_Freescale / TARGET_KLXX / analogout_api.c
1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "mbed_assert.h"
17 #include "analogout_api.h"
18
19 #include "cmsis.h"
20 #include "pinmap.h"
21 #include "PeripheralPins.h"
22
23 #define RANGE_12BIT 0xFFF
24
25 void analogout_init(dac_t *obj, PinName pin) {
26 obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
27 MBED_ASSERT(obj->dac != (DACName)NC);
28
29 SIM->SCGC6 |= SIM_SCGC6_DAC0_MASK;
30
31 uint32_t port = (uint32_t)pin >> PORT_SHIFT;
32 SIM->SCGC5 |= 1 << (SIM_SCGC5_PORTA_SHIFT + port);
33
34 DAC0->DAT[obj->dac].DATH = 0;
35 DAC0->DAT[obj->dac].DATL = 0;
36
37 DAC0->C1 = DAC_C1_DACBFMD_MASK; // One-Time Scan Mode
38
39 DAC0->C0 = DAC_C0_DACEN_MASK // Enable
40 | DAC_C0_DACSWTRG_MASK; // Software Trigger
41
42 pinmap_pinout(pin, PinMap_DAC);
43
44 analogout_write_u16(obj, 0);
45 }
46
47 void analogout_free(dac_t *obj) {}
48
49 static inline void dac_write(dac_t *obj, int value) {
50 DAC0->DAT[obj->dac].DATL = (uint8_t)( value & 0xFF);
51 DAC0->DAT[obj->dac].DATH = (uint8_t)((value >> 8) & 0xFF);
52 }
53
54 static inline int dac_read(dac_t *obj) {
55 return ((DAC0->DAT[obj->dac].DATH << 8) | DAC0->DAT[obj->dac].DATL);
56 }
57
58 void analogout_write(dac_t *obj, float value) {
59 if (value < 0.0) {
60 dac_write(obj, 0);
61 } else if (value > 1.0) {
62 dac_write(obj, RANGE_12BIT);
63 } else {
64 dac_write(obj, value * (float)RANGE_12BIT);
65 }
66 }
67
68 void analogout_write_u16(dac_t *obj, uint16_t value) {
69 dac_write(obj, value >> 4); // 12-bit
70 }
71
72 float analogout_read(dac_t *obj) {
73 uint32_t value = dac_read(obj);
74 return (float)value * (1.0f / (float)RANGE_12BIT);
75 }
76
77 uint16_t analogout_read_u16(dac_t *obj) {
78 uint32_t value = dac_read(obj); // 12-bit
79 return (value << 4) | ((value >> 8) & 0x003F);
80 }
Imprint / Impressum