Mouse code for the MacroRat
mbed-dev/targets/TARGET_Silicon_Labs/TARGET_EFM32/analogout_api.c@18:6a4db94011d3, 2017-05-14 (annotated)
- Committer:
- sahilmgandhi
- Date:
- Sun May 14 23:18:57 2017 +0000
- Revision:
- 18:6a4db94011d3
Publishing again
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sahilmgandhi | 18:6a4db94011d3 | 1 | /***************************************************************************//** |
sahilmgandhi | 18:6a4db94011d3 | 2 | * @file analogout_aoi.c |
sahilmgandhi | 18:6a4db94011d3 | 3 | ******************************************************************************* |
sahilmgandhi | 18:6a4db94011d3 | 4 | * @section License |
sahilmgandhi | 18:6a4db94011d3 | 5 | * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b> |
sahilmgandhi | 18:6a4db94011d3 | 6 | ******************************************************************************* |
sahilmgandhi | 18:6a4db94011d3 | 7 | * |
sahilmgandhi | 18:6a4db94011d3 | 8 | * SPDX-License-Identifier: Apache-2.0 |
sahilmgandhi | 18:6a4db94011d3 | 9 | * |
sahilmgandhi | 18:6a4db94011d3 | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
sahilmgandhi | 18:6a4db94011d3 | 11 | * not use this file except in compliance with the License. |
sahilmgandhi | 18:6a4db94011d3 | 12 | * You may obtain a copy of the License at |
sahilmgandhi | 18:6a4db94011d3 | 13 | * |
sahilmgandhi | 18:6a4db94011d3 | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
sahilmgandhi | 18:6a4db94011d3 | 15 | * |
sahilmgandhi | 18:6a4db94011d3 | 16 | * Unless required by applicable law or agreed to in writing, software |
sahilmgandhi | 18:6a4db94011d3 | 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
sahilmgandhi | 18:6a4db94011d3 | 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sahilmgandhi | 18:6a4db94011d3 | 19 | * See the License for the specific language governing permissions and |
sahilmgandhi | 18:6a4db94011d3 | 20 | * limitations under the License. |
sahilmgandhi | 18:6a4db94011d3 | 21 | * |
sahilmgandhi | 18:6a4db94011d3 | 22 | ******************************************************************************/ |
sahilmgandhi | 18:6a4db94011d3 | 23 | |
sahilmgandhi | 18:6a4db94011d3 | 24 | #include "device.h" |
sahilmgandhi | 18:6a4db94011d3 | 25 | #if DEVICE_ANALOGOUT |
sahilmgandhi | 18:6a4db94011d3 | 26 | |
sahilmgandhi | 18:6a4db94011d3 | 27 | #include "mbed_assert.h" |
sahilmgandhi | 18:6a4db94011d3 | 28 | #include "mbed_error.h" |
sahilmgandhi | 18:6a4db94011d3 | 29 | #include "analogout_api.h" |
sahilmgandhi | 18:6a4db94011d3 | 30 | #include "pinmap.h" |
sahilmgandhi | 18:6a4db94011d3 | 31 | #include "pinmap_function.h" |
sahilmgandhi | 18:6a4db94011d3 | 32 | #include "PeripheralPins.h" |
sahilmgandhi | 18:6a4db94011d3 | 33 | #include "clocking.h" |
sahilmgandhi | 18:6a4db94011d3 | 34 | |
sahilmgandhi | 18:6a4db94011d3 | 35 | #include "em_dac.h" |
sahilmgandhi | 18:6a4db94011d3 | 36 | #include "em_cmu.h" |
sahilmgandhi | 18:6a4db94011d3 | 37 | |
sahilmgandhi | 18:6a4db94011d3 | 38 | static uint8_t dac_initialized = 0; |
sahilmgandhi | 18:6a4db94011d3 | 39 | |
sahilmgandhi | 18:6a4db94011d3 | 40 | void analogout_init(dac_t *obj, PinName pin) |
sahilmgandhi | 18:6a4db94011d3 | 41 | { |
sahilmgandhi | 18:6a4db94011d3 | 42 | /* init in-memory structure */ |
sahilmgandhi | 18:6a4db94011d3 | 43 | obj->dac = (DAC_TypeDef *) pinmap_peripheral(pin, PinMap_DAC); |
sahilmgandhi | 18:6a4db94011d3 | 44 | MBED_ASSERT((int) obj->dac != NC); |
sahilmgandhi | 18:6a4db94011d3 | 45 | |
sahilmgandhi | 18:6a4db94011d3 | 46 | obj->channel = pin_location(pin, PinMap_DAC); |
sahilmgandhi | 18:6a4db94011d3 | 47 | MBED_ASSERT((int) obj->channel != NC); |
sahilmgandhi | 18:6a4db94011d3 | 48 | |
sahilmgandhi | 18:6a4db94011d3 | 49 | pin_mode(pin, Disabled); |
sahilmgandhi | 18:6a4db94011d3 | 50 | |
sahilmgandhi | 18:6a4db94011d3 | 51 | if (!dac_initialized) { |
sahilmgandhi | 18:6a4db94011d3 | 52 | /* Initialize the DAC. Will disable both DAC channels, so should only be done once */ |
sahilmgandhi | 18:6a4db94011d3 | 53 | /* Use default settings */ |
sahilmgandhi | 18:6a4db94011d3 | 54 | CMU_ClockEnable(cmuClock_DAC0, true); |
sahilmgandhi | 18:6a4db94011d3 | 55 | |
sahilmgandhi | 18:6a4db94011d3 | 56 | DAC_Init_TypeDef init = DAC_INIT_DEFAULT; |
sahilmgandhi | 18:6a4db94011d3 | 57 | |
sahilmgandhi | 18:6a4db94011d3 | 58 | /* Calculate the DAC clock prescaler value that will result in a DAC clock |
sahilmgandhi | 18:6a4db94011d3 | 59 | * close to 500kHz. Second parameter is zero. This uses the current HFPERCLK |
sahilmgandhi | 18:6a4db94011d3 | 60 | * frequency instead of setting a new one. */ |
sahilmgandhi | 18:6a4db94011d3 | 61 | init.prescale = DAC_PrescaleCalc(500000, REFERENCE_FREQUENCY); |
sahilmgandhi | 18:6a4db94011d3 | 62 | |
sahilmgandhi | 18:6a4db94011d3 | 63 | /* Set reference voltage to VDD */ |
sahilmgandhi | 18:6a4db94011d3 | 64 | init.reference = dacRefVDD; |
sahilmgandhi | 18:6a4db94011d3 | 65 | |
sahilmgandhi | 18:6a4db94011d3 | 66 | DAC_Init(obj->dac, &init); |
sahilmgandhi | 18:6a4db94011d3 | 67 | dac_initialized = 1; |
sahilmgandhi | 18:6a4db94011d3 | 68 | } |
sahilmgandhi | 18:6a4db94011d3 | 69 | /* Use default channel settings */ |
sahilmgandhi | 18:6a4db94011d3 | 70 | DAC_InitChannel_TypeDef initChannel = DAC_INITCHANNEL_DEFAULT; |
sahilmgandhi | 18:6a4db94011d3 | 71 | initChannel.enable = true; |
sahilmgandhi | 18:6a4db94011d3 | 72 | DAC_InitChannel(obj->dac, &initChannel, obj->channel); |
sahilmgandhi | 18:6a4db94011d3 | 73 | } |
sahilmgandhi | 18:6a4db94011d3 | 74 | |
sahilmgandhi | 18:6a4db94011d3 | 75 | void analogout_free(dac_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 76 | { |
sahilmgandhi | 18:6a4db94011d3 | 77 | //Reset channel by re-initializing |
sahilmgandhi | 18:6a4db94011d3 | 78 | DAC_InitChannel_TypeDef initChannel = DAC_INITCHANNEL_DEFAULT; |
sahilmgandhi | 18:6a4db94011d3 | 79 | initChannel.enable = false; |
sahilmgandhi | 18:6a4db94011d3 | 80 | DAC_InitChannel(obj->dac, &initChannel, obj->channel); |
sahilmgandhi | 18:6a4db94011d3 | 81 | |
sahilmgandhi | 18:6a4db94011d3 | 82 | //Check all channels to see if we can disable the DAC completely |
sahilmgandhi | 18:6a4db94011d3 | 83 | if((DAC0->CH0CTRL & DAC_CH0CTRL_EN) == 0 && (DAC0->CH1CTRL & DAC_CH1CTRL_EN) == 0) { |
sahilmgandhi | 18:6a4db94011d3 | 84 | CMU_ClockEnable(cmuClock_DAC0, false); |
sahilmgandhi | 18:6a4db94011d3 | 85 | dac_initialized = 0; |
sahilmgandhi | 18:6a4db94011d3 | 86 | } |
sahilmgandhi | 18:6a4db94011d3 | 87 | } |
sahilmgandhi | 18:6a4db94011d3 | 88 | |
sahilmgandhi | 18:6a4db94011d3 | 89 | static inline void dac_write(dac_t *obj, int value) |
sahilmgandhi | 18:6a4db94011d3 | 90 | { |
sahilmgandhi | 18:6a4db94011d3 | 91 | switch (obj->channel) { |
sahilmgandhi | 18:6a4db94011d3 | 92 | case 0: |
sahilmgandhi | 18:6a4db94011d3 | 93 | obj->dac->CH0DATA = value; |
sahilmgandhi | 18:6a4db94011d3 | 94 | break; |
sahilmgandhi | 18:6a4db94011d3 | 95 | case 1: |
sahilmgandhi | 18:6a4db94011d3 | 96 | obj->dac->CH1DATA = value; |
sahilmgandhi | 18:6a4db94011d3 | 97 | break; |
sahilmgandhi | 18:6a4db94011d3 | 98 | } |
sahilmgandhi | 18:6a4db94011d3 | 99 | } |
sahilmgandhi | 18:6a4db94011d3 | 100 | |
sahilmgandhi | 18:6a4db94011d3 | 101 | static inline int dac_read(dac_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 102 | { |
sahilmgandhi | 18:6a4db94011d3 | 103 | switch (obj->channel) { |
sahilmgandhi | 18:6a4db94011d3 | 104 | case 0: |
sahilmgandhi | 18:6a4db94011d3 | 105 | return obj->dac->CH0DATA; |
sahilmgandhi | 18:6a4db94011d3 | 106 | break; |
sahilmgandhi | 18:6a4db94011d3 | 107 | case 1: |
sahilmgandhi | 18:6a4db94011d3 | 108 | return obj->dac->CH1DATA; |
sahilmgandhi | 18:6a4db94011d3 | 109 | break; |
sahilmgandhi | 18:6a4db94011d3 | 110 | default: |
sahilmgandhi | 18:6a4db94011d3 | 111 | error("AnalogOut pin error. Invalid channel"); |
sahilmgandhi | 18:6a4db94011d3 | 112 | return -1; |
sahilmgandhi | 18:6a4db94011d3 | 113 | break; |
sahilmgandhi | 18:6a4db94011d3 | 114 | } |
sahilmgandhi | 18:6a4db94011d3 | 115 | } |
sahilmgandhi | 18:6a4db94011d3 | 116 | |
sahilmgandhi | 18:6a4db94011d3 | 117 | void analogout_write(dac_t *obj, float value) |
sahilmgandhi | 18:6a4db94011d3 | 118 | { |
sahilmgandhi | 18:6a4db94011d3 | 119 | /* We multiply the float value with 0xFFF because the DAC has 12-bit resolution. |
sahilmgandhi | 18:6a4db94011d3 | 120 | * Ie. accepts values between 0 and 0xFFF (4096). */ |
sahilmgandhi | 18:6a4db94011d3 | 121 | dac_write(obj, value*0xFFF); |
sahilmgandhi | 18:6a4db94011d3 | 122 | } |
sahilmgandhi | 18:6a4db94011d3 | 123 | |
sahilmgandhi | 18:6a4db94011d3 | 124 | void analogout_write_u16(dac_t *obj, uint16_t value) |
sahilmgandhi | 18:6a4db94011d3 | 125 | { |
sahilmgandhi | 18:6a4db94011d3 | 126 | /* The DAC has 12 bit resolution, so we remove the 4 least significant bits */ |
sahilmgandhi | 18:6a4db94011d3 | 127 | dac_write(obj, value >> 4); |
sahilmgandhi | 18:6a4db94011d3 | 128 | } |
sahilmgandhi | 18:6a4db94011d3 | 129 | |
sahilmgandhi | 18:6a4db94011d3 | 130 | float analogout_read(dac_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 131 | { |
sahilmgandhi | 18:6a4db94011d3 | 132 | /* dac_read returns a number between 0 and 0xFFF. Division gives us a float between 0 and 1 */ |
sahilmgandhi | 18:6a4db94011d3 | 133 | return dac_read(obj)/(float)0xFFF; |
sahilmgandhi | 18:6a4db94011d3 | 134 | } |
sahilmgandhi | 18:6a4db94011d3 | 135 | |
sahilmgandhi | 18:6a4db94011d3 | 136 | uint16_t analogout_read_u16(dac_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 137 | { |
sahilmgandhi | 18:6a4db94011d3 | 138 | /* dac_read returns a number with 12 significant digits, |
sahilmgandhi | 18:6a4db94011d3 | 139 | * so we shift in 0s from right to make it a 16 bit number */ |
sahilmgandhi | 18:6a4db94011d3 | 140 | return dac_read(obj) << 4; |
sahilmgandhi | 18:6a4db94011d3 | 141 | } |
sahilmgandhi | 18:6a4db94011d3 | 142 | |
sahilmgandhi | 18:6a4db94011d3 | 143 | #endif |