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