mbed library sources

Dependents:   FRDM-KL46Z_LCD_Test FRDM-KL46Z_LCD_Test FRDM-KL46Z_Plantilla FRDM-KL46Z_Plantilla ... more

Committer:
ebrus
Date:
Thu Jul 28 15:56:34 2016 +0000
Revision:
0:6bc4ac881c8e
1;

Who changed what in which revision?

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