mbed library sources

Dependents:   Nucleo_blink_led

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Mar 24 09:00:08 2015 +0000
Revision:
496:543871686697
Parent:
targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/analogout_api.c@414:4ec4c5b614b0
Synchronized with git revision ea01d61fa18430564b78226045b196bb6bf6b66a

Full URL: https://github.com/mbedmicro/mbed/commit/ea01d61fa18430564b78226045b196bb6bf6b66a/

NUCLEO_L152RE - reorg hal

Who changed what in which revision?

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