mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_DISCO_F303VC/analogout_api.c@241:ffe41b0c8126, 2014-06-26 (annotated)
- Committer:
- mbed_official
- Date:
- Thu Jun 26 10:30:09 2014 +0100
- Revision:
- 241:ffe41b0c8126
- Parent:
- 157:90e3acc479a2
Synchronized with git revision 9fec469ec63095abac767a4581abba008ffddf14
Full URL: https://github.com/mbedmicro/mbed/commit/9fec469ec63095abac767a4581abba008ffddf14/
[DISCO-F303VC] Updated with F302R8 recent changes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 157:90e3acc479a2 | 1 | /* mbed Microcontroller Library |
mbed_official | 157:90e3acc479a2 | 2 | * Copyright (c) 2014, STMicroelectronics |
mbed_official | 157:90e3acc479a2 | 3 | * All rights reserved. |
mbed_official | 157:90e3acc479a2 | 4 | * |
mbed_official | 157:90e3acc479a2 | 5 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 157:90e3acc479a2 | 6 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 157:90e3acc479a2 | 7 | * |
mbed_official | 157:90e3acc479a2 | 8 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 157:90e3acc479a2 | 9 | * this list of conditions and the following disclaimer. |
mbed_official | 157:90e3acc479a2 | 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 157:90e3acc479a2 | 11 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 157:90e3acc479a2 | 12 | * and/or other materials provided with the distribution. |
mbed_official | 157:90e3acc479a2 | 13 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 157:90e3acc479a2 | 14 | * may be used to endorse or promote products derived from this software |
mbed_official | 157:90e3acc479a2 | 15 | * without specific prior written permission. |
mbed_official | 157:90e3acc479a2 | 16 | * |
mbed_official | 157:90e3acc479a2 | 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 157:90e3acc479a2 | 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 157:90e3acc479a2 | 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 157:90e3acc479a2 | 20 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 157:90e3acc479a2 | 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 157:90e3acc479a2 | 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 157:90e3acc479a2 | 23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 157:90e3acc479a2 | 24 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 157:90e3acc479a2 | 25 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 157:90e3acc479a2 | 26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 157:90e3acc479a2 | 27 | */ |
mbed_official | 241:ffe41b0c8126 | 28 | #include "mbed_assert.h" |
mbed_official | 157:90e3acc479a2 | 29 | #include "analogout_api.h" |
mbed_official | 157:90e3acc479a2 | 30 | |
mbed_official | 157:90e3acc479a2 | 31 | #if DEVICE_ANALOGOUT |
mbed_official | 157:90e3acc479a2 | 32 | |
mbed_official | 157:90e3acc479a2 | 33 | #include "cmsis.h" |
mbed_official | 157:90e3acc479a2 | 34 | #include "pinmap.h" |
mbed_official | 157:90e3acc479a2 | 35 | |
mbed_official | 241:ffe41b0c8126 | 36 | #define DAC_RANGE (0xFFF) // 12 bits |
mbed_official | 157:90e3acc479a2 | 37 | |
mbed_official | 157:90e3acc479a2 | 38 | static const PinMap PinMap_DAC[] = { |
mbed_official | 157:90e3acc479a2 | 39 | {PA_4, DAC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // DAC_OUT1 |
mbed_official | 157:90e3acc479a2 | 40 | {PA_5, DAC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // DAC_OUT2 |
mbed_official | 157:90e3acc479a2 | 41 | {NC, NC, 0} |
mbed_official | 157:90e3acc479a2 | 42 | }; |
mbed_official | 157:90e3acc479a2 | 43 | |
mbed_official | 157:90e3acc479a2 | 44 | void analogout_init(dac_t *obj, PinName pin) { |
mbed_official | 157:90e3acc479a2 | 45 | DAC_TypeDef *dac; |
mbed_official | 157:90e3acc479a2 | 46 | DAC_InitTypeDef DAC_InitStructure; |
mbed_official | 157:90e3acc479a2 | 47 | |
mbed_official | 157:90e3acc479a2 | 48 | // Get the peripheral name (DAC_1, ...) from the pin and assign it to the object |
mbed_official | 157:90e3acc479a2 | 49 | obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); |
mbed_official | 241:ffe41b0c8126 | 50 | MBED_ASSERT(obj->dac != (DACName)NC); |
mbed_official | 157:90e3acc479a2 | 51 | |
mbed_official | 157:90e3acc479a2 | 52 | dac = (DAC_TypeDef *)(obj->dac); |
mbed_official | 157:90e3acc479a2 | 53 | |
mbed_official | 157:90e3acc479a2 | 54 | // Configure GPIO |
mbed_official | 157:90e3acc479a2 | 55 | pinmap_pinout(pin, PinMap_DAC); |
mbed_official | 157:90e3acc479a2 | 56 | |
mbed_official | 157:90e3acc479a2 | 57 | // Save the channel for the write and read functions |
mbed_official | 157:90e3acc479a2 | 58 | obj->channel = pin; |
mbed_official | 157:90e3acc479a2 | 59 | |
mbed_official | 157:90e3acc479a2 | 60 | // Enable DAC clock |
mbed_official | 157:90e3acc479a2 | 61 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); |
mbed_official | 157:90e3acc479a2 | 62 | |
mbed_official | 157:90e3acc479a2 | 63 | // Configure and enable DAC channel |
mbed_official | 157:90e3acc479a2 | 64 | DAC_StructInit(&DAC_InitStructure); |
mbed_official | 157:90e3acc479a2 | 65 | |
mbed_official | 157:90e3acc479a2 | 66 | if (obj->channel == PA_4) { |
mbed_official | 157:90e3acc479a2 | 67 | DAC_Init(dac,DAC_Channel_1, &DAC_InitStructure); |
mbed_official | 157:90e3acc479a2 | 68 | DAC_Cmd(dac,DAC_Channel_1, ENABLE); |
mbed_official | 157:90e3acc479a2 | 69 | } |
mbed_official | 157:90e3acc479a2 | 70 | if (obj->channel == PA_5) { |
mbed_official | 157:90e3acc479a2 | 71 | DAC_Init(dac,DAC_Channel_2, &DAC_InitStructure); |
mbed_official | 157:90e3acc479a2 | 72 | DAC_Cmd(dac,DAC_Channel_2, ENABLE); |
mbed_official | 157:90e3acc479a2 | 73 | } |
mbed_official | 157:90e3acc479a2 | 74 | |
mbed_official | 157:90e3acc479a2 | 75 | analogout_write_u16(obj, 0); |
mbed_official | 157:90e3acc479a2 | 76 | } |
mbed_official | 157:90e3acc479a2 | 77 | |
mbed_official | 157:90e3acc479a2 | 78 | void analogout_free(dac_t *obj) { |
mbed_official | 241:ffe41b0c8126 | 79 | DAC_TypeDef *dac = (DAC_TypeDef *)(obj->dac); |
mbed_official | 241:ffe41b0c8126 | 80 | // Disable DAC |
mbed_official | 241:ffe41b0c8126 | 81 | DAC_DeInit(dac); |
mbed_official | 241:ffe41b0c8126 | 82 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, DISABLE); |
mbed_official | 241:ffe41b0c8126 | 83 | // Configure GPIO |
mbed_official | 241:ffe41b0c8126 | 84 | pin_function(obj->channel, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF)); |
mbed_official | 157:90e3acc479a2 | 85 | } |
mbed_official | 157:90e3acc479a2 | 86 | |
mbed_official | 157:90e3acc479a2 | 87 | static inline void dac_write(dac_t *obj, uint16_t value) { |
mbed_official | 157:90e3acc479a2 | 88 | DAC_TypeDef *dac = (DAC_TypeDef *)(obj->dac); |
mbed_official | 157:90e3acc479a2 | 89 | if (obj->channel == PA_4) { |
mbed_official | 157:90e3acc479a2 | 90 | DAC_SetChannel1Data(dac,DAC_Align_12b_R, value); |
mbed_official | 157:90e3acc479a2 | 91 | } |
mbed_official | 157:90e3acc479a2 | 92 | if (obj->channel == PA_5) { |
mbed_official | 157:90e3acc479a2 | 93 | DAC_SetChannel2Data(dac,DAC_Align_12b_R, value); |
mbed_official | 157:90e3acc479a2 | 94 | } |
mbed_official | 157:90e3acc479a2 | 95 | } |
mbed_official | 157:90e3acc479a2 | 96 | |
mbed_official | 157:90e3acc479a2 | 97 | static inline int dac_read(dac_t *obj) { |
mbed_official | 157:90e3acc479a2 | 98 | DAC_TypeDef *dac = (DAC_TypeDef *)(obj->dac); |
mbed_official | 157:90e3acc479a2 | 99 | if (obj->channel == PA_4) { |
mbed_official | 157:90e3acc479a2 | 100 | return (int)DAC_GetDataOutputValue(dac,DAC_Channel_1); |
mbed_official | 157:90e3acc479a2 | 101 | } |
mbed_official | 157:90e3acc479a2 | 102 | if (obj->channel == PA_5) { |
mbed_official | 157:90e3acc479a2 | 103 | return (int)DAC_GetDataOutputValue(dac,DAC_Channel_2); |
mbed_official | 157:90e3acc479a2 | 104 | } |
mbed_official | 157:90e3acc479a2 | 105 | return 0; |
mbed_official | 157:90e3acc479a2 | 106 | } |
mbed_official | 157:90e3acc479a2 | 107 | |
mbed_official | 157:90e3acc479a2 | 108 | void analogout_write(dac_t *obj, float value) { |
mbed_official | 157:90e3acc479a2 | 109 | if (value < 0.0f) { |
mbed_official | 157:90e3acc479a2 | 110 | dac_write(obj, 0); // Min value |
mbed_official | 157:90e3acc479a2 | 111 | } else if (value > 1.0f) { |
mbed_official | 241:ffe41b0c8126 | 112 | dac_write(obj, (uint16_t)DAC_RANGE); // Max value |
mbed_official | 157:90e3acc479a2 | 113 | } else { |
mbed_official | 241:ffe41b0c8126 | 114 | dac_write(obj, (uint16_t)(value * (float)DAC_RANGE)); |
mbed_official | 157:90e3acc479a2 | 115 | } |
mbed_official | 157:90e3acc479a2 | 116 | } |
mbed_official | 157:90e3acc479a2 | 117 | |
mbed_official | 157:90e3acc479a2 | 118 | void analogout_write_u16(dac_t *obj, uint16_t value) { |
mbed_official | 241:ffe41b0c8126 | 119 | if (value > (uint16_t)DAC_RANGE) { |
mbed_official | 241:ffe41b0c8126 | 120 | dac_write(obj, (uint16_t)DAC_RANGE); // Max value |
mbed_official | 241:ffe41b0c8126 | 121 | } else { |
mbed_official | 157:90e3acc479a2 | 122 | dac_write(obj, value); |
mbed_official | 157:90e3acc479a2 | 123 | } |
mbed_official | 157:90e3acc479a2 | 124 | } |
mbed_official | 157:90e3acc479a2 | 125 | |
mbed_official | 157:90e3acc479a2 | 126 | float analogout_read(dac_t *obj) { |
mbed_official | 157:90e3acc479a2 | 127 | uint32_t value = dac_read(obj); |
mbed_official | 241:ffe41b0c8126 | 128 | return (float)((float)value * (1.0f / (float)DAC_RANGE)); |
mbed_official | 157:90e3acc479a2 | 129 | } |
mbed_official | 157:90e3acc479a2 | 130 | |
mbed_official | 157:90e3acc479a2 | 131 | uint16_t analogout_read_u16(dac_t *obj) { |
mbed_official | 157:90e3acc479a2 | 132 | return (uint16_t)dac_read(obj); |
mbed_official | 157:90e3acc479a2 | 133 | } |
mbed_official | 157:90e3acc479a2 | 134 | |
mbed_official | 157:90e3acc479a2 | 135 | #endif // DEVICE_ANALOGOUT |