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