|
| 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 | +#include "stm32f4xx_hal.h" |
| 36 | + |
| 37 | +#define RANGE_12BIT (0xFFF) |
| 38 | + |
| 39 | +DAC_HandleTypeDef DacHandle; |
| 40 | +static DAC_ChannelConfTypeDef sConfig; |
| 41 | + |
| 42 | +static const PinMap PinMap_DAC[] = { |
| 43 | + {PA_4, DAC_0, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0xFF)}, // DAC_OUT1 |
| 44 | + {PA_5, DAC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0xFF)}, // DAC_OUT2 |
| 45 | + {NC, NC, 0} |
| 46 | +}; |
| 47 | + |
| 48 | +void analogout_init(dac_t *obj, PinName pin) |
| 49 | +{ |
| 50 | + uint32_t channel ; |
| 51 | + HAL_StatusTypeDef status; |
| 52 | + |
| 53 | + // Get the peripheral name (DAC_1, ...) from the pin and assign it to the object |
| 54 | + obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); |
| 55 | + |
| 56 | + if (obj->dac == (DACName)NC) { |
| 57 | + error("DAC pin mapping failed"); |
| 58 | + } |
| 59 | + |
| 60 | + // Configure GPIO |
| 61 | + pinmap_pinout(pin, PinMap_DAC); |
| 62 | + |
| 63 | + // Save the channel for the write and read functions |
| 64 | + obj->channel = pin; |
| 65 | + |
| 66 | + __GPIOA_CLK_ENABLE(); |
| 67 | + |
| 68 | + __DAC_CLK_ENABLE(); |
| 69 | + |
| 70 | + DacHandle.Instance = DAC; |
| 71 | + |
| 72 | + status = HAL_DAC_Init(&DacHandle); |
| 73 | + if ( status != HAL_OK ) { |
| 74 | + error("HAL_DAC_Init failed"); |
| 75 | + } |
| 76 | + |
| 77 | + sConfig.DAC_Trigger = DAC_TRIGGER_NONE; |
| 78 | + sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE; |
| 79 | + |
| 80 | + if (obj->channel == PA_4) { |
| 81 | + channel = DAC_CHANNEL_1; |
| 82 | + } else { |
| 83 | + channel = DAC_CHANNEL_2; |
| 84 | + } |
| 85 | + |
| 86 | + if (HAL_DAC_ConfigChannel(&DacHandle, &sConfig, channel) != HAL_OK) { |
| 87 | + error("HAL_DAC_ConfigChannel failed"); |
| 88 | + } |
| 89 | + |
| 90 | + if (HAL_DAC_Start(&DacHandle, channel) != HAL_OK) { |
| 91 | + error("HAL_DAC_Start failed"); |
| 92 | + } |
| 93 | + |
| 94 | + if (HAL_DAC_SetValue(&DacHandle, channel, DAC_ALIGN_12B_R, 0x000) != HAL_OK) { |
| 95 | + error("HAL_DAC_SetValue failed"); |
| 96 | + } |
| 97 | + |
| 98 | +} |
| 99 | + |
| 100 | +void analogout_free(dac_t *obj) |
| 101 | +{ |
| 102 | +} |
| 103 | + |
| 104 | +static inline void dac_write(dac_t *obj, uint16_t value) |
| 105 | +{ |
| 106 | + HAL_StatusTypeDef status; |
| 107 | + |
| 108 | + if (obj->channel == PA_4) { |
| 109 | + status = HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, value); |
| 110 | + } else if (obj->channel == PA_5) { |
| 111 | + status = HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value); |
| 112 | + } |
| 113 | + |
| 114 | + if ( status != HAL_OK ) { |
| 115 | + error("ADC pin mapping failed"); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +static inline int dac_read(dac_t *obj) |
| 120 | +{ |
| 121 | + if (obj->channel == PA_4) { |
| 122 | + return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1); |
| 123 | + } else if (obj->channel == PA_5) { |
| 124 | + return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_2); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +void analogout_write(dac_t *obj, float value) |
| 129 | +{ |
| 130 | + if (value < 0.0f) { |
| 131 | + dac_write(obj, 0); // Min value |
| 132 | + } else if (value > 1.0f) { |
| 133 | + dac_write(obj, (uint16_t)RANGE_12BIT); // Max value |
| 134 | + } else { |
| 135 | + dac_write(obj, (uint16_t)(value * (float)RANGE_12BIT)); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +void analogout_write_u16(dac_t *obj, uint16_t value) |
| 140 | +{ |
| 141 | + if (value > (uint16_t)RANGE_12BIT) { |
| 142 | + value = (uint16_t)RANGE_12BIT; // Max value |
| 143 | + } |
| 144 | + |
| 145 | + dac_write(obj, value); |
| 146 | +} |
| 147 | + |
| 148 | +float analogout_read(dac_t *obj) |
| 149 | +{ |
| 150 | + |
| 151 | + uint32_t value = dac_read(obj); |
| 152 | + return (float)value * (1.0f / (float)RANGE_12BIT); |
| 153 | +} |
| 154 | + |
| 155 | +uint16_t analogout_read_u16(dac_t *obj) |
| 156 | +{ |
| 157 | + return (uint16_t)dac_read(obj); |
| 158 | +} |
| 159 | + |
| 160 | +#endif // DEVICE_ANALOGOUT |
0 commit comments