Thomas Byrne / mbed-src-stm32f030k6

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Mon Dec 08 07:30:07 2014 +0000
Revision:
428:4ddf7f7eabbb
Parent:
296:ec1b66a3d094
Synchronized with git revision 3413e21e59b8b0d03c0addac95b1ead87f0b7965

Full URL: https://github.com/mbedmicro/mbed/commit/3413e21e59b8b0d03c0addac95b1ead87f0b7965/

Gitignore - Ignore cscope and vim swap files

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 141:706f8dea204a 50
mbed_official 141:706f8dea204a 51 if (obj->dac == (DACName)NC) {
mbed_official 141:706f8dea204a 52 error("DAC pin mapping failed");
mbed_official 141:706f8dea204a 53 }
mbed_official 141:706f8dea204a 54
mbed_official 141:706f8dea204a 55 // Configure GPIO
mbed_official 141:706f8dea204a 56 pinmap_pinout(pin, PinMap_DAC);
mbed_official 141:706f8dea204a 57
mbed_official 141:706f8dea204a 58 // Save the channel for the write and read functions
mbed_official 141:706f8dea204a 59 obj->channel = pin;
mbed_official 141:706f8dea204a 60
mbed_official 141:706f8dea204a 61 __GPIOA_CLK_ENABLE();
mbed_official 141:706f8dea204a 62
mbed_official 141:706f8dea204a 63 __DAC_CLK_ENABLE();
mbed_official 141:706f8dea204a 64
mbed_official 141:706f8dea204a 65 DacHandle.Instance = DAC;
mbed_official 141:706f8dea204a 66
mbed_official 141:706f8dea204a 67 status = HAL_DAC_Init(&DacHandle);
mbed_official 141:706f8dea204a 68 if ( status != HAL_OK ) {
mbed_official 141:706f8dea204a 69 error("HAL_DAC_Init failed");
mbed_official 141:706f8dea204a 70 }
mbed_official 141:706f8dea204a 71
mbed_official 141:706f8dea204a 72 sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
mbed_official 141:706f8dea204a 73 sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
mbed_official 141:706f8dea204a 74
mbed_official 141:706f8dea204a 75 if (obj->channel == PA_4) {
mbed_official 141:706f8dea204a 76 channel = DAC_CHANNEL_1;
mbed_official 141:706f8dea204a 77 } else {
mbed_official 141:706f8dea204a 78 channel = DAC_CHANNEL_2;
mbed_official 141:706f8dea204a 79 }
mbed_official 141:706f8dea204a 80
mbed_official 141:706f8dea204a 81 if (HAL_DAC_ConfigChannel(&DacHandle, &sConfig, channel) != HAL_OK) {
mbed_official 141:706f8dea204a 82 error("HAL_DAC_ConfigChannel failed");
mbed_official 141:706f8dea204a 83 }
mbed_official 141:706f8dea204a 84
mbed_official 141:706f8dea204a 85 if (HAL_DAC_Start(&DacHandle, channel) != HAL_OK) {
mbed_official 141:706f8dea204a 86 error("HAL_DAC_Start failed");
mbed_official 141:706f8dea204a 87 }
mbed_official 141:706f8dea204a 88
mbed_official 141:706f8dea204a 89 if (HAL_DAC_SetValue(&DacHandle, channel, DAC_ALIGN_12B_R, 0x000) != HAL_OK) {
mbed_official 141:706f8dea204a 90 error("HAL_DAC_SetValue failed");
mbed_official 141:706f8dea204a 91 }
mbed_official 141:706f8dea204a 92
mbed_official 141:706f8dea204a 93 }
mbed_official 141:706f8dea204a 94
mbed_official 141:706f8dea204a 95 void analogout_free(dac_t *obj)
mbed_official 141:706f8dea204a 96 {
mbed_official 141:706f8dea204a 97 }
mbed_official 141:706f8dea204a 98
mbed_official 141:706f8dea204a 99 static inline void dac_write(dac_t *obj, uint16_t value)
mbed_official 141:706f8dea204a 100 {
mbed_official 242:7074e42da0b2 101 HAL_StatusTypeDef status = HAL_ERROR;
mbed_official 141:706f8dea204a 102
mbed_official 141:706f8dea204a 103 if (obj->channel == PA_4) {
mbed_official 141:706f8dea204a 104 status = HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, value);
mbed_official 141:706f8dea204a 105 } else if (obj->channel == PA_5) {
mbed_official 141:706f8dea204a 106 status = HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value);
mbed_official 141:706f8dea204a 107 }
mbed_official 141:706f8dea204a 108
mbed_official 141:706f8dea204a 109 if ( status != HAL_OK ) {
mbed_official 141:706f8dea204a 110 error("ADC pin mapping failed");
mbed_official 141:706f8dea204a 111 }
mbed_official 141:706f8dea204a 112 }
mbed_official 141:706f8dea204a 113
mbed_official 141:706f8dea204a 114 static inline int dac_read(dac_t *obj)
mbed_official 141:706f8dea204a 115 {
mbed_official 141:706f8dea204a 116 if (obj->channel == PA_4) {
mbed_official 141:706f8dea204a 117 return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1);
mbed_official 141:706f8dea204a 118 } else if (obj->channel == PA_5) {
mbed_official 141:706f8dea204a 119 return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_2);
mbed_official 141:706f8dea204a 120 }
mbed_official 242:7074e42da0b2 121 return 0; /* Just silented warning */
mbed_official 141:706f8dea204a 122 }
mbed_official 141:706f8dea204a 123
mbed_official 141:706f8dea204a 124 void analogout_write(dac_t *obj, float value)
mbed_official 141:706f8dea204a 125 {
mbed_official 141:706f8dea204a 126 if (value < 0.0f) {
mbed_official 141:706f8dea204a 127 dac_write(obj, 0); // Min value
mbed_official 141:706f8dea204a 128 } else if (value > 1.0f) {
mbed_official 141:706f8dea204a 129 dac_write(obj, (uint16_t)RANGE_12BIT); // Max value
mbed_official 141:706f8dea204a 130 } else {
mbed_official 141:706f8dea204a 131 dac_write(obj, (uint16_t)(value * (float)RANGE_12BIT));
mbed_official 141:706f8dea204a 132 }
mbed_official 141:706f8dea204a 133 }
mbed_official 141:706f8dea204a 134
mbed_official 141:706f8dea204a 135 void analogout_write_u16(dac_t *obj, uint16_t value)
mbed_official 141:706f8dea204a 136 {
mbed_official 141:706f8dea204a 137 if (value > (uint16_t)RANGE_12BIT) {
mbed_official 141:706f8dea204a 138 value = (uint16_t)RANGE_12BIT; // Max value
mbed_official 141:706f8dea204a 139 }
mbed_official 141:706f8dea204a 140
mbed_official 141:706f8dea204a 141 dac_write(obj, value);
mbed_official 141:706f8dea204a 142 }
mbed_official 141:706f8dea204a 143
mbed_official 141:706f8dea204a 144 float analogout_read(dac_t *obj)
mbed_official 141:706f8dea204a 145 {
mbed_official 141:706f8dea204a 146
mbed_official 141:706f8dea204a 147 uint32_t value = dac_read(obj);
mbed_official 141:706f8dea204a 148 return (float)value * (1.0f / (float)RANGE_12BIT);
mbed_official 141:706f8dea204a 149 }
mbed_official 141:706f8dea204a 150
mbed_official 141:706f8dea204a 151 uint16_t analogout_read_u16(dac_t *obj)
mbed_official 141:706f8dea204a 152 {
mbed_official 141:706f8dea204a 153 return (uint16_t)dac_read(obj);
mbed_official 141:706f8dea204a 154 }
mbed_official 141:706f8dea204a 155
mbed_official 141:706f8dea204a 156 #endif // DEVICE_ANALOGOUT