mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Fri Jun 27 07:30:09 2014 +0100
Revision:
242:7074e42da0b2
Parent:
141:706f8dea204a
Child:
250:a49055e7a707
Synchronized with git revision 124ef5e3add9e74a3221347a3fbeea7c8b3cf353

Full URL: https://github.com/mbedmicro/mbed/commit/124ef5e3add9e74a3221347a3fbeea7c8b3cf353/

[DISCO_F407VG] HAL update.

Who changed what in which revision?

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