added prescaler for 16 bit pwm in LPC1347 target
Fork of mbed-dev by
targets/hal/TARGET_Silicon_Labs/TARGET_EFM32/analogout_api.c@0:9b334a45a8ff, 2015-10-01 (annotated)
- Committer:
- bogdanm
- Date:
- Thu Oct 01 15:25:22 2015 +0300
- Revision:
- 0:9b334a45a8ff
- Child:
- 50:a417edff4437
Initial commit on mbed-dev
Replaces mbed-src (now inactive)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 0:9b334a45a8ff | 1 | /***************************************************************************//** |
bogdanm | 0:9b334a45a8ff | 2 | * @file analogout_aoi.c |
bogdanm | 0:9b334a45a8ff | 3 | ******************************************************************************* |
bogdanm | 0:9b334a45a8ff | 4 | * @section License |
bogdanm | 0:9b334a45a8ff | 5 | * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b> |
bogdanm | 0:9b334a45a8ff | 6 | ******************************************************************************* |
bogdanm | 0:9b334a45a8ff | 7 | * |
bogdanm | 0:9b334a45a8ff | 8 | * Permission is granted to anyone to use this software for any purpose, |
bogdanm | 0:9b334a45a8ff | 9 | * including commercial applications, and to alter it and redistribute it |
bogdanm | 0:9b334a45a8ff | 10 | * freely, subject to the following restrictions: |
bogdanm | 0:9b334a45a8ff | 11 | * |
bogdanm | 0:9b334a45a8ff | 12 | * 1. The origin of this software must not be misrepresented; you must not |
bogdanm | 0:9b334a45a8ff | 13 | * claim that you wrote the original software. |
bogdanm | 0:9b334a45a8ff | 14 | * 2. Altered source versions must be plainly marked as such, and must not be |
bogdanm | 0:9b334a45a8ff | 15 | * misrepresented as being the original software. |
bogdanm | 0:9b334a45a8ff | 16 | * 3. This notice may not be removed or altered from any source distribution. |
bogdanm | 0:9b334a45a8ff | 17 | * |
bogdanm | 0:9b334a45a8ff | 18 | * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no |
bogdanm | 0:9b334a45a8ff | 19 | * obligation to support this Software. Silicon Labs is providing the |
bogdanm | 0:9b334a45a8ff | 20 | * Software "AS IS", with no express or implied warranties of any kind, |
bogdanm | 0:9b334a45a8ff | 21 | * including, but not limited to, any implied warranties of merchantability |
bogdanm | 0:9b334a45a8ff | 22 | * or fitness for any particular purpose or warranties against infringement |
bogdanm | 0:9b334a45a8ff | 23 | * of any proprietary rights of a third party. |
bogdanm | 0:9b334a45a8ff | 24 | * |
bogdanm | 0:9b334a45a8ff | 25 | * Silicon Labs will not be liable for any consequential, incidental, or |
bogdanm | 0:9b334a45a8ff | 26 | * special damages, or any other relief, or for any claim by any third party, |
bogdanm | 0:9b334a45a8ff | 27 | * arising from your use of this Software. |
bogdanm | 0:9b334a45a8ff | 28 | * |
bogdanm | 0:9b334a45a8ff | 29 | ******************************************************************************/ |
bogdanm | 0:9b334a45a8ff | 30 | |
bogdanm | 0:9b334a45a8ff | 31 | #include "device.h" |
bogdanm | 0:9b334a45a8ff | 32 | #if DEVICE_ANALOGOUT |
bogdanm | 0:9b334a45a8ff | 33 | |
bogdanm | 0:9b334a45a8ff | 34 | #include "mbed_assert.h" |
bogdanm | 0:9b334a45a8ff | 35 | #include "error.h" |
bogdanm | 0:9b334a45a8ff | 36 | #include "analogout_api.h" |
bogdanm | 0:9b334a45a8ff | 37 | #include "pinmap.h" |
bogdanm | 0:9b334a45a8ff | 38 | #include "pinmap_function.h" |
bogdanm | 0:9b334a45a8ff | 39 | #include "PeripheralPins.h" |
bogdanm | 0:9b334a45a8ff | 40 | #include "clocking.h" |
bogdanm | 0:9b334a45a8ff | 41 | |
bogdanm | 0:9b334a45a8ff | 42 | #include "em_dac.h" |
bogdanm | 0:9b334a45a8ff | 43 | #include "em_cmu.h" |
bogdanm | 0:9b334a45a8ff | 44 | |
bogdanm | 0:9b334a45a8ff | 45 | uint8_t analogout_get_index(dac_t *obj) |
bogdanm | 0:9b334a45a8ff | 46 | { |
bogdanm | 0:9b334a45a8ff | 47 | return 0; |
bogdanm | 0:9b334a45a8ff | 48 | } |
bogdanm | 0:9b334a45a8ff | 49 | |
bogdanm | 0:9b334a45a8ff | 50 | void analogout_preinit(dac_t *obj, PinName pin) |
bogdanm | 0:9b334a45a8ff | 51 | { |
bogdanm | 0:9b334a45a8ff | 52 | obj->dac = (DAC_TypeDef *) pinmap_peripheral(pin, PinMap_DAC); |
bogdanm | 0:9b334a45a8ff | 53 | MBED_ASSERT((int) obj->dac != NC); |
bogdanm | 0:9b334a45a8ff | 54 | |
bogdanm | 0:9b334a45a8ff | 55 | obj->channel = pin_location(pin, PinMap_DAC); |
bogdanm | 0:9b334a45a8ff | 56 | MBED_ASSERT((int) obj->channel != NC); |
bogdanm | 0:9b334a45a8ff | 57 | } |
bogdanm | 0:9b334a45a8ff | 58 | |
bogdanm | 0:9b334a45a8ff | 59 | void analogout_init(dac_t *obj, PinName pin) |
bogdanm | 0:9b334a45a8ff | 60 | { |
bogdanm | 0:9b334a45a8ff | 61 | static uint8_t dac_initialized = 0; |
bogdanm | 0:9b334a45a8ff | 62 | |
bogdanm | 0:9b334a45a8ff | 63 | /* init in-memory structure */ |
bogdanm | 0:9b334a45a8ff | 64 | analogout_preinit(obj, pin); |
bogdanm | 0:9b334a45a8ff | 65 | |
bogdanm | 0:9b334a45a8ff | 66 | if (!dac_initialized) { |
bogdanm | 0:9b334a45a8ff | 67 | /* Initialize the DAC. Will disable both DAC channels, so should only be done once */ |
bogdanm | 0:9b334a45a8ff | 68 | /* Use default settings */ |
bogdanm | 0:9b334a45a8ff | 69 | CMU_ClockEnable(cmuClock_DAC0, true); |
bogdanm | 0:9b334a45a8ff | 70 | |
bogdanm | 0:9b334a45a8ff | 71 | DAC_Init_TypeDef init = DAC_INIT_DEFAULT; |
bogdanm | 0:9b334a45a8ff | 72 | |
bogdanm | 0:9b334a45a8ff | 73 | /* Calculate the DAC clock prescaler value that will result in a DAC clock |
bogdanm | 0:9b334a45a8ff | 74 | * close to 500kHz. Second parameter is zero. This uses the current HFPERCLK |
bogdanm | 0:9b334a45a8ff | 75 | * frequency instead of setting a new one. */ |
bogdanm | 0:9b334a45a8ff | 76 | init.prescale = DAC_PrescaleCalc(500000, REFERENCE_FREQUENCY); |
bogdanm | 0:9b334a45a8ff | 77 | |
bogdanm | 0:9b334a45a8ff | 78 | /* Set reference voltage to VDD */ |
bogdanm | 0:9b334a45a8ff | 79 | init.reference = dacRefVDD; |
bogdanm | 0:9b334a45a8ff | 80 | |
bogdanm | 0:9b334a45a8ff | 81 | DAC_Init(obj->dac, &init); |
bogdanm | 0:9b334a45a8ff | 82 | dac_initialized = 1; |
bogdanm | 0:9b334a45a8ff | 83 | } |
bogdanm | 0:9b334a45a8ff | 84 | /* Use default channel settings */ |
bogdanm | 0:9b334a45a8ff | 85 | DAC_InitChannel_TypeDef initChannel = DAC_INITCHANNEL_DEFAULT; |
bogdanm | 0:9b334a45a8ff | 86 | DAC_InitChannel(obj->dac, &initChannel, obj->channel); |
bogdanm | 0:9b334a45a8ff | 87 | |
bogdanm | 0:9b334a45a8ff | 88 | |
bogdanm | 0:9b334a45a8ff | 89 | } |
bogdanm | 0:9b334a45a8ff | 90 | |
bogdanm | 0:9b334a45a8ff | 91 | void analogout_enable(dac_t *obj, uint8_t enable) |
bogdanm | 0:9b334a45a8ff | 92 | { |
bogdanm | 0:9b334a45a8ff | 93 | DAC_Enable(obj->dac, obj->channel, enable); |
bogdanm | 0:9b334a45a8ff | 94 | } |
bogdanm | 0:9b334a45a8ff | 95 | |
bogdanm | 0:9b334a45a8ff | 96 | void analogout_pins_enable(dac_t *obj, uint8_t enable) |
bogdanm | 0:9b334a45a8ff | 97 | { |
bogdanm | 0:9b334a45a8ff | 98 | //not avail for EFM32 |
bogdanm | 0:9b334a45a8ff | 99 | } |
bogdanm | 0:9b334a45a8ff | 100 | |
bogdanm | 0:9b334a45a8ff | 101 | static inline void dac_write(dac_t *obj, int value) |
bogdanm | 0:9b334a45a8ff | 102 | { |
bogdanm | 0:9b334a45a8ff | 103 | switch (obj->channel) { |
bogdanm | 0:9b334a45a8ff | 104 | case 0: |
bogdanm | 0:9b334a45a8ff | 105 | obj->dac->CH0DATA = value; |
bogdanm | 0:9b334a45a8ff | 106 | break; |
bogdanm | 0:9b334a45a8ff | 107 | case 1: |
bogdanm | 0:9b334a45a8ff | 108 | obj->dac->CH1DATA = value; |
bogdanm | 0:9b334a45a8ff | 109 | break; |
bogdanm | 0:9b334a45a8ff | 110 | } |
bogdanm | 0:9b334a45a8ff | 111 | } |
bogdanm | 0:9b334a45a8ff | 112 | |
bogdanm | 0:9b334a45a8ff | 113 | static inline int dac_read(dac_t *obj) |
bogdanm | 0:9b334a45a8ff | 114 | { |
bogdanm | 0:9b334a45a8ff | 115 | switch (obj->channel) { |
bogdanm | 0:9b334a45a8ff | 116 | case 0: |
bogdanm | 0:9b334a45a8ff | 117 | return obj->dac->CH0DATA; |
bogdanm | 0:9b334a45a8ff | 118 | break; |
bogdanm | 0:9b334a45a8ff | 119 | case 1: |
bogdanm | 0:9b334a45a8ff | 120 | return obj->dac->CH1DATA; |
bogdanm | 0:9b334a45a8ff | 121 | break; |
bogdanm | 0:9b334a45a8ff | 122 | default: |
bogdanm | 0:9b334a45a8ff | 123 | error("AnalogOut pin error. Invalid channel"); |
bogdanm | 0:9b334a45a8ff | 124 | return -1; |
bogdanm | 0:9b334a45a8ff | 125 | break; |
bogdanm | 0:9b334a45a8ff | 126 | } |
bogdanm | 0:9b334a45a8ff | 127 | } |
bogdanm | 0:9b334a45a8ff | 128 | |
bogdanm | 0:9b334a45a8ff | 129 | void analogout_write(dac_t *obj, float value) |
bogdanm | 0:9b334a45a8ff | 130 | { |
bogdanm | 0:9b334a45a8ff | 131 | /* We multiply the float value with 0xFFF because the DAC has 12-bit resolution. |
bogdanm | 0:9b334a45a8ff | 132 | * Ie. accepts values between 0 and 0xFFF (4096). */ |
bogdanm | 0:9b334a45a8ff | 133 | dac_write(obj, value*0xFFF); |
bogdanm | 0:9b334a45a8ff | 134 | } |
bogdanm | 0:9b334a45a8ff | 135 | |
bogdanm | 0:9b334a45a8ff | 136 | void analogout_write_u16(dac_t *obj, uint16_t value) |
bogdanm | 0:9b334a45a8ff | 137 | { |
bogdanm | 0:9b334a45a8ff | 138 | /* The DAC has 12 bit resolution, so we remove the 4 least significant bits */ |
bogdanm | 0:9b334a45a8ff | 139 | dac_write(obj, value >> 4); |
bogdanm | 0:9b334a45a8ff | 140 | } |
bogdanm | 0:9b334a45a8ff | 141 | |
bogdanm | 0:9b334a45a8ff | 142 | float analogout_read(dac_t *obj) |
bogdanm | 0:9b334a45a8ff | 143 | { |
bogdanm | 0:9b334a45a8ff | 144 | /* dac_read returns a number between 0 and 0xFFF. Division gives us a float between 0 and 1 */ |
bogdanm | 0:9b334a45a8ff | 145 | return dac_read(obj)/(float)0xFFF; |
bogdanm | 0:9b334a45a8ff | 146 | } |
bogdanm | 0:9b334a45a8ff | 147 | |
bogdanm | 0:9b334a45a8ff | 148 | uint16_t analogout_read_u16(dac_t *obj) |
bogdanm | 0:9b334a45a8ff | 149 | { |
bogdanm | 0:9b334a45a8ff | 150 | /* dac_read returns a number with 12 significant digits, |
bogdanm | 0:9b334a45a8ff | 151 | * so we shift in 0s from right to make it a 16 bit number */ |
bogdanm | 0:9b334a45a8ff | 152 | return dac_read(obj) << 4; |
bogdanm | 0:9b334a45a8ff | 153 | } |
bogdanm | 0:9b334a45a8ff | 154 | |
bogdanm | 0:9b334a45a8ff | 155 | #endif |