mbed library sources

Fork of mbed-src by mbed official

Committer:
lzbpli
Date:
Thu Jul 07 06:48:59 2016 +0000
Revision:
636:b0d178e9fa10
Parent:
544:1af5f1c39e80
l053

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 508:4f5903e025e6 1 /* mbed Microcontroller Library
mbed_official 544:1af5f1c39e80 2 * Copyright (c) 2015, STMicroelectronics
mbed_official 508:4f5903e025e6 3 * All rights reserved.
mbed_official 508:4f5903e025e6 4 *
mbed_official 508:4f5903e025e6 5 * Redistribution and use in source and binary forms, with or without
mbed_official 508:4f5903e025e6 6 * modification, are permitted provided that the following conditions are met:
mbed_official 508:4f5903e025e6 7 *
mbed_official 508:4f5903e025e6 8 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 508:4f5903e025e6 9 * this list of conditions and the following disclaimer.
mbed_official 508:4f5903e025e6 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 508:4f5903e025e6 11 * this list of conditions and the following disclaimer in the documentation
mbed_official 508:4f5903e025e6 12 * and/or other materials provided with the distribution.
mbed_official 508:4f5903e025e6 13 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 508:4f5903e025e6 14 * may be used to endorse or promote products derived from this software
mbed_official 508:4f5903e025e6 15 * without specific prior written permission.
mbed_official 508:4f5903e025e6 16 *
mbed_official 508:4f5903e025e6 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 508:4f5903e025e6 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 508:4f5903e025e6 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 508:4f5903e025e6 20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 508:4f5903e025e6 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 508:4f5903e025e6 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 508:4f5903e025e6 23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 508:4f5903e025e6 24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 508:4f5903e025e6 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 508:4f5903e025e6 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 508:4f5903e025e6 27 */
mbed_official 508:4f5903e025e6 28 #include "mbed_assert.h"
mbed_official 508:4f5903e025e6 29 #include "analogout_api.h"
mbed_official 508:4f5903e025e6 30
mbed_official 508:4f5903e025e6 31 #if DEVICE_ANALOGOUT
mbed_official 508:4f5903e025e6 32
mbed_official 508:4f5903e025e6 33 #include "cmsis.h"
mbed_official 508:4f5903e025e6 34 #include "pinmap.h"
mbed_official 508:4f5903e025e6 35 #include "mbed_error.h"
mbed_official 508:4f5903e025e6 36 #include "PeripheralPins.h"
mbed_official 508:4f5903e025e6 37
mbed_official 508:4f5903e025e6 38 #define DAC_RANGE (0xFFF) // 12 bits
mbed_official 508:4f5903e025e6 39
mbed_official 508:4f5903e025e6 40 static DAC_HandleTypeDef DacHandle;
mbed_official 508:4f5903e025e6 41
mbed_official 508:4f5903e025e6 42 // These variables are used for the "free" function
mbed_official 508:4f5903e025e6 43 static int pa4_used = 0;
mbed_official 508:4f5903e025e6 44 static int pa5_used = 0;
mbed_official 508:4f5903e025e6 45
mbed_official 508:4f5903e025e6 46 void analogout_init(dac_t *obj, PinName pin)
mbed_official 508:4f5903e025e6 47 {
mbed_official 508:4f5903e025e6 48 DAC_ChannelConfTypeDef sConfig;
mbed_official 508:4f5903e025e6 49
mbed_official 508:4f5903e025e6 50 // Get the peripheral name from the pin and assign it to the object
mbed_official 508:4f5903e025e6 51 obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
mbed_official 508:4f5903e025e6 52 MBED_ASSERT(obj->dac != (DACName)NC);
mbed_official 508:4f5903e025e6 53
mbed_official 544:1af5f1c39e80 54 // Get the pin function and assign the used channel to the object
mbed_official 544:1af5f1c39e80 55 uint32_t function = pinmap_function(pin, PinMap_DAC);
mbed_official 544:1af5f1c39e80 56 MBED_ASSERT(function != (uint32_t)NC);
mbed_official 544:1af5f1c39e80 57 obj->channel = STM_PIN_CHANNEL(function);
mbed_official 544:1af5f1c39e80 58
mbed_official 508:4f5903e025e6 59 // Configure GPIO
mbed_official 508:4f5903e025e6 60 pinmap_pinout(pin, PinMap_DAC);
mbed_official 508:4f5903e025e6 61
mbed_official 508:4f5903e025e6 62 // Save the pin for future use
mbed_official 508:4f5903e025e6 63 obj->pin = pin;
mbed_official 508:4f5903e025e6 64
mbed_official 508:4f5903e025e6 65 // Enable DAC clock
mbed_official 508:4f5903e025e6 66 if (obj->dac == DAC_1) {
mbed_official 508:4f5903e025e6 67 __DAC1_CLK_ENABLE();
mbed_official 508:4f5903e025e6 68 }
mbed_official 544:1af5f1c39e80 69 #if defined(DAC2)
mbed_official 508:4f5903e025e6 70 if (obj->dac == DAC_2) {
mbed_official 508:4f5903e025e6 71 __DAC2_CLK_ENABLE();
mbed_official 508:4f5903e025e6 72 }
mbed_official 508:4f5903e025e6 73 #endif
mbed_official 508:4f5903e025e6 74
mbed_official 508:4f5903e025e6 75 // Configure DAC
mbed_official 508:4f5903e025e6 76 DacHandle.Instance = (DAC_TypeDef *)(obj->dac);
mbed_official 508:4f5903e025e6 77
mbed_official 508:4f5903e025e6 78 sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
mbed_official 508:4f5903e025e6 79 sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE;
mbed_official 508:4f5903e025e6 80
mbed_official 508:4f5903e025e6 81 if (pin == PA_4) {
mbed_official 508:4f5903e025e6 82 HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1);
mbed_official 508:4f5903e025e6 83 pa4_used = 1;
mbed_official 508:4f5903e025e6 84 }
mbed_official 508:4f5903e025e6 85
mbed_official 508:4f5903e025e6 86 #if defined(DAC_CHANNEL_2)
mbed_official 508:4f5903e025e6 87 if (pin == PA_5) {
mbed_official 508:4f5903e025e6 88 HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_2);
mbed_official 508:4f5903e025e6 89 pa5_used = 1;
mbed_official 508:4f5903e025e6 90 }
mbed_official 508:4f5903e025e6 91 #endif
mbed_official 508:4f5903e025e6 92
mbed_official 508:4f5903e025e6 93 if (pin == PA_6) {
mbed_official 508:4f5903e025e6 94 HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1);
mbed_official 508:4f5903e025e6 95 }
mbed_official 508:4f5903e025e6 96
mbed_official 508:4f5903e025e6 97 analogout_write_u16(obj, 0);
mbed_official 508:4f5903e025e6 98 }
mbed_official 508:4f5903e025e6 99
mbed_official 508:4f5903e025e6 100 void analogout_free(dac_t *obj)
mbed_official 508:4f5903e025e6 101 {
mbed_official 508:4f5903e025e6 102 // Reset DAC and disable clock
mbed_official 508:4f5903e025e6 103 if (obj->pin == PA_4) pa4_used = 0;
mbed_official 508:4f5903e025e6 104 if (obj->pin == PA_5) pa5_used = 0;
mbed_official 508:4f5903e025e6 105
mbed_official 508:4f5903e025e6 106 if ((pa4_used == 0) && (pa5_used == 0)) {
mbed_official 508:4f5903e025e6 107 __DAC1_FORCE_RESET();
mbed_official 508:4f5903e025e6 108 __DAC1_RELEASE_RESET();
mbed_official 508:4f5903e025e6 109 __DAC1_CLK_DISABLE();
mbed_official 508:4f5903e025e6 110 }
mbed_official 508:4f5903e025e6 111
mbed_official 544:1af5f1c39e80 112 #if defined(DAC2)
mbed_official 508:4f5903e025e6 113 if (obj->pin == PA_6) {
mbed_official 508:4f5903e025e6 114 __DAC2_FORCE_RESET();
mbed_official 508:4f5903e025e6 115 __DAC2_RELEASE_RESET();
mbed_official 508:4f5903e025e6 116 __DAC2_CLK_DISABLE();
mbed_official 508:4f5903e025e6 117 }
mbed_official 508:4f5903e025e6 118 #endif
mbed_official 508:4f5903e025e6 119
mbed_official 508:4f5903e025e6 120 // Configure GPIO
mbed_official 508:4f5903e025e6 121 pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 508:4f5903e025e6 122 }
mbed_official 508:4f5903e025e6 123
mbed_official 508:4f5903e025e6 124 static inline void dac_write(dac_t *obj, uint16_t value)
mbed_official 508:4f5903e025e6 125 {
mbed_official 544:1af5f1c39e80 126 if (obj->channel == 1) {
mbed_official 508:4f5903e025e6 127 HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, value);
mbed_official 508:4f5903e025e6 128 HAL_DAC_Start(&DacHandle, DAC_CHANNEL_1);
mbed_official 508:4f5903e025e6 129 }
mbed_official 508:4f5903e025e6 130 #if defined(DAC_CHANNEL_2)
mbed_official 544:1af5f1c39e80 131 if (obj->channel == 2) {
mbed_official 508:4f5903e025e6 132 HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value);
mbed_official 508:4f5903e025e6 133 HAL_DAC_Start(&DacHandle, DAC_CHANNEL_2);
mbed_official 508:4f5903e025e6 134 }
mbed_official 508:4f5903e025e6 135 #endif
mbed_official 508:4f5903e025e6 136 }
mbed_official 508:4f5903e025e6 137
mbed_official 508:4f5903e025e6 138 static inline int dac_read(dac_t *obj)
mbed_official 508:4f5903e025e6 139 {
mbed_official 544:1af5f1c39e80 140 if (obj->channel == 1) {
mbed_official 508:4f5903e025e6 141 return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1);
mbed_official 544:1af5f1c39e80 142 }
mbed_official 508:4f5903e025e6 143 #if defined(DAC_CHANNEL_2)
mbed_official 544:1af5f1c39e80 144 if (obj->channel == 2) {
mbed_official 508:4f5903e025e6 145 return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_2);
mbed_official 544:1af5f1c39e80 146 }
mbed_official 508:4f5903e025e6 147 #endif
mbed_official 544:1af5f1c39e80 148 return 0;
mbed_official 508:4f5903e025e6 149 }
mbed_official 508:4f5903e025e6 150
mbed_official 508:4f5903e025e6 151 void analogout_write(dac_t *obj, float value)
mbed_official 508:4f5903e025e6 152 {
mbed_official 508:4f5903e025e6 153 if (value < 0.0f) {
mbed_official 508:4f5903e025e6 154 dac_write(obj, 0); // Min value
mbed_official 508:4f5903e025e6 155 } else if (value > 1.0f) {
mbed_official 508:4f5903e025e6 156 dac_write(obj, (uint16_t)DAC_RANGE); // Max value
mbed_official 508:4f5903e025e6 157 } else {
mbed_official 508:4f5903e025e6 158 dac_write(obj, (uint16_t)(value * (float)DAC_RANGE));
mbed_official 508:4f5903e025e6 159 }
mbed_official 508:4f5903e025e6 160 }
mbed_official 508:4f5903e025e6 161
mbed_official 508:4f5903e025e6 162 void analogout_write_u16(dac_t *obj, uint16_t value)
mbed_official 508:4f5903e025e6 163 {
mbed_official 508:4f5903e025e6 164 if (value > (uint16_t)DAC_RANGE) {
mbed_official 508:4f5903e025e6 165 dac_write(obj, (uint16_t)DAC_RANGE); // Max value
mbed_official 508:4f5903e025e6 166 } else {
mbed_official 508:4f5903e025e6 167 dac_write(obj, value);
mbed_official 508:4f5903e025e6 168 }
mbed_official 508:4f5903e025e6 169 }
mbed_official 508:4f5903e025e6 170
mbed_official 508:4f5903e025e6 171 float analogout_read(dac_t *obj)
mbed_official 508:4f5903e025e6 172 {
mbed_official 508:4f5903e025e6 173 uint32_t value = dac_read(obj);
mbed_official 508:4f5903e025e6 174 return (float)((float)value * (1.0f / (float)DAC_RANGE));
mbed_official 508:4f5903e025e6 175 }
mbed_official 508:4f5903e025e6 176
mbed_official 508:4f5903e025e6 177 uint16_t analogout_read_u16(dac_t *obj)
mbed_official 508:4f5903e025e6 178 {
mbed_official 508:4f5903e025e6 179 return (uint16_t)dac_read(obj);
mbed_official 508:4f5903e025e6 180 }
mbed_official 508:4f5903e025e6 181
mbed_official 508:4f5903e025e6 182 #endif // DEVICE_ANALOGOUT