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:
511:532f83b66a7f
l053

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 285:31249416b6f9 34 #include "mbed_error.h"
mbed_official 141:706f8dea204a 35 #include "stm32f4xx_hal.h"
mbed_official 428:4ddf7f7eabbb 36 #include "PeripheralPins.h"
mbed_official 141:706f8dea204a 37
mbed_official 141:706f8dea204a 38 #define RANGE_12BIT (0xFFF)
mbed_official 141:706f8dea204a 39
mbed_official 141:706f8dea204a 40 DAC_HandleTypeDef DacHandle;
mbed_official 141:706f8dea204a 41 static DAC_ChannelConfTypeDef sConfig;
mbed_official 141:706f8dea204a 42
mbed_official 141:706f8dea204a 43 void analogout_init(dac_t *obj, PinName pin)
mbed_official 141:706f8dea204a 44 {
mbed_official 141:706f8dea204a 45 uint32_t channel ;
mbed_official 141:706f8dea204a 46 HAL_StatusTypeDef status;
mbed_official 141:706f8dea204a 47
mbed_official 141:706f8dea204a 48 // Get the peripheral name (DAC_1, ...) from the pin and assign it to the object
mbed_official 141:706f8dea204a 49 obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
mbed_official 511:532f83b66a7f 50 // Get the functions (dac channel) from the pin and assign it to the object
mbed_official 511:532f83b66a7f 51 uint32_t function = pinmap_function(pin, PinMap_DAC);
mbed_official 511:532f83b66a7f 52 MBED_ASSERT(function != (uint32_t)NC);
mbed_official 511:532f83b66a7f 53 // Save the channel for the write and read functions
mbed_official 511:532f83b66a7f 54 obj->channel = STM_PIN_CHANNEL(function);
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 __GPIOA_CLK_ENABLE();
mbed_official 141:706f8dea204a 64
mbed_official 141:706f8dea204a 65 __DAC_CLK_ENABLE();
mbed_official 141:706f8dea204a 66
mbed_official 141:706f8dea204a 67 DacHandle.Instance = DAC;
mbed_official 141:706f8dea204a 68
mbed_official 141:706f8dea204a 69 status = HAL_DAC_Init(&DacHandle);
mbed_official 141:706f8dea204a 70 if ( status != HAL_OK ) {
mbed_official 141:706f8dea204a 71 error("HAL_DAC_Init failed");
mbed_official 141:706f8dea204a 72 }
mbed_official 141:706f8dea204a 73
mbed_official 141:706f8dea204a 74 sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
mbed_official 141:706f8dea204a 75 sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
mbed_official 141:706f8dea204a 76
mbed_official 511:532f83b66a7f 77 if (obj->channel == 1) {
mbed_official 141:706f8dea204a 78 channel = DAC_CHANNEL_1;
mbed_official 141:706f8dea204a 79 } else {
mbed_official 141:706f8dea204a 80 channel = DAC_CHANNEL_2;
mbed_official 141:706f8dea204a 81 }
mbed_official 141:706f8dea204a 82
mbed_official 141:706f8dea204a 83 if (HAL_DAC_ConfigChannel(&DacHandle, &sConfig, channel) != HAL_OK) {
mbed_official 141:706f8dea204a 84 error("HAL_DAC_ConfigChannel failed");
mbed_official 141:706f8dea204a 85 }
mbed_official 141:706f8dea204a 86
mbed_official 141:706f8dea204a 87 if (HAL_DAC_Start(&DacHandle, channel) != HAL_OK) {
mbed_official 141:706f8dea204a 88 error("HAL_DAC_Start failed");
mbed_official 141:706f8dea204a 89 }
mbed_official 141:706f8dea204a 90
mbed_official 141:706f8dea204a 91 if (HAL_DAC_SetValue(&DacHandle, channel, DAC_ALIGN_12B_R, 0x000) != HAL_OK) {
mbed_official 141:706f8dea204a 92 error("HAL_DAC_SetValue failed");
mbed_official 141:706f8dea204a 93 }
mbed_official 141:706f8dea204a 94
mbed_official 141:706f8dea204a 95 }
mbed_official 141:706f8dea204a 96
mbed_official 141:706f8dea204a 97 void analogout_free(dac_t *obj)
mbed_official 141:706f8dea204a 98 {
mbed_official 141:706f8dea204a 99 }
mbed_official 141:706f8dea204a 100
mbed_official 141:706f8dea204a 101 static inline void dac_write(dac_t *obj, uint16_t value)
mbed_official 141:706f8dea204a 102 {
mbed_official 242:7074e42da0b2 103 HAL_StatusTypeDef status = HAL_ERROR;
mbed_official 141:706f8dea204a 104
mbed_official 511:532f83b66a7f 105 if (obj->channel == 1) {
mbed_official 141:706f8dea204a 106 status = HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, value);
mbed_official 511:532f83b66a7f 107 } else if (obj->channel == 2) {
mbed_official 141:706f8dea204a 108 status = HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value);
mbed_official 141:706f8dea204a 109 }
mbed_official 141:706f8dea204a 110
mbed_official 141:706f8dea204a 111 if ( status != HAL_OK ) {
mbed_official 511:532f83b66a7f 112 error("DAC pin mapping failed");
mbed_official 141:706f8dea204a 113 }
mbed_official 141:706f8dea204a 114 }
mbed_official 141:706f8dea204a 115
mbed_official 141:706f8dea204a 116 static inline int dac_read(dac_t *obj)
mbed_official 141:706f8dea204a 117 {
mbed_official 511:532f83b66a7f 118 if (obj->channel == 1) {
mbed_official 141:706f8dea204a 119 return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1);
mbed_official 511:532f83b66a7f 120 } else if (obj->channel == 2) {
mbed_official 141:706f8dea204a 121 return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_2);
mbed_official 141:706f8dea204a 122 }
mbed_official 242:7074e42da0b2 123 return 0; /* Just silented warning */
mbed_official 141:706f8dea204a 124 }
mbed_official 141:706f8dea204a 125
mbed_official 141:706f8dea204a 126 void analogout_write(dac_t *obj, float value)
mbed_official 141:706f8dea204a 127 {
mbed_official 141:706f8dea204a 128 if (value < 0.0f) {
mbed_official 141:706f8dea204a 129 dac_write(obj, 0); // Min value
mbed_official 141:706f8dea204a 130 } else if (value > 1.0f) {
mbed_official 141:706f8dea204a 131 dac_write(obj, (uint16_t)RANGE_12BIT); // Max value
mbed_official 141:706f8dea204a 132 } else {
mbed_official 141:706f8dea204a 133 dac_write(obj, (uint16_t)(value * (float)RANGE_12BIT));
mbed_official 141:706f8dea204a 134 }
mbed_official 141:706f8dea204a 135 }
mbed_official 141:706f8dea204a 136
mbed_official 141:706f8dea204a 137 void analogout_write_u16(dac_t *obj, uint16_t value)
mbed_official 141:706f8dea204a 138 {
mbed_official 141:706f8dea204a 139 if (value > (uint16_t)RANGE_12BIT) {
mbed_official 141:706f8dea204a 140 value = (uint16_t)RANGE_12BIT; // Max value
mbed_official 141:706f8dea204a 141 }
mbed_official 141:706f8dea204a 142
mbed_official 141:706f8dea204a 143 dac_write(obj, value);
mbed_official 141:706f8dea204a 144 }
mbed_official 141:706f8dea204a 145
mbed_official 141:706f8dea204a 146 float analogout_read(dac_t *obj)
mbed_official 141:706f8dea204a 147 {
mbed_official 141:706f8dea204a 148
mbed_official 141:706f8dea204a 149 uint32_t value = dac_read(obj);
mbed_official 141:706f8dea204a 150 return (float)value * (1.0f / (float)RANGE_12BIT);
mbed_official 141:706f8dea204a 151 }
mbed_official 141:706f8dea204a 152
mbed_official 141:706f8dea204a 153 uint16_t analogout_read_u16(dac_t *obj)
mbed_official 141:706f8dea204a 154 {
mbed_official 141:706f8dea204a 155 return (uint16_t)dac_read(obj);
mbed_official 141:706f8dea204a 156 }
mbed_official 141:706f8dea204a 157
mbed_official 141:706f8dea204a 158 #endif // DEVICE_ANALOGOUT