mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Wed Jun 11 16:00:09 2014 +0100
Revision:
227:7bd0639b8911
Parent:
216:577900467c9e
Synchronized with git revision d58d532ebc0e0a96f4fffb8edefc082b71b964af

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

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 125:23cc3068a9e4 1 /* mbed Microcontroller Library
mbed_official 125:23cc3068a9e4 2 * Copyright (c) 2014, STMicroelectronics
mbed_official 125:23cc3068a9e4 3 * All rights reserved.
mbed_official 125:23cc3068a9e4 4 *
mbed_official 125:23cc3068a9e4 5 * Redistribution and use in source and binary forms, with or without
mbed_official 125:23cc3068a9e4 6 * modification, are permitted provided that the following conditions are met:
mbed_official 125:23cc3068a9e4 7 *
mbed_official 125:23cc3068a9e4 8 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 125:23cc3068a9e4 9 * this list of conditions and the following disclaimer.
mbed_official 125:23cc3068a9e4 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 125:23cc3068a9e4 11 * this list of conditions and the following disclaimer in the documentation
mbed_official 125:23cc3068a9e4 12 * and/or other materials provided with the distribution.
mbed_official 125:23cc3068a9e4 13 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 125:23cc3068a9e4 14 * may be used to endorse or promote products derived from this software
mbed_official 125:23cc3068a9e4 15 * without specific prior written permission.
mbed_official 125:23cc3068a9e4 16 *
mbed_official 125:23cc3068a9e4 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 125:23cc3068a9e4 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 125:23cc3068a9e4 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 125:23cc3068a9e4 20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 125:23cc3068a9e4 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 125:23cc3068a9e4 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 125:23cc3068a9e4 23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 125:23cc3068a9e4 24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 125:23cc3068a9e4 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 125:23cc3068a9e4 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 125:23cc3068a9e4 27 */
mbed_official 227:7bd0639b8911 28 #include "mbed_assert.h"
mbed_official 125:23cc3068a9e4 29 #include "analogout_api.h"
mbed_official 125:23cc3068a9e4 30
mbed_official 125:23cc3068a9e4 31 #if DEVICE_ANALOGOUT
mbed_official 125:23cc3068a9e4 32
mbed_official 125:23cc3068a9e4 33 #include "cmsis.h"
mbed_official 125:23cc3068a9e4 34 #include "pinmap.h"
mbed_official 125:23cc3068a9e4 35
mbed_official 159:3b23f6d9ecb9 36 #define DAC_RANGE (0xFFF) // 12 bits
mbed_official 125:23cc3068a9e4 37
mbed_official 125:23cc3068a9e4 38 static const PinMap PinMap_DAC[] = {
mbed_official 125:23cc3068a9e4 39 {PA_4, DAC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // DAC_OUT1
mbed_official 125:23cc3068a9e4 40 {NC, NC, 0}
mbed_official 125:23cc3068a9e4 41 };
mbed_official 125:23cc3068a9e4 42
mbed_official 125:23cc3068a9e4 43 void analogout_init(dac_t *obj, PinName pin) {
mbed_official 125:23cc3068a9e4 44 DAC_TypeDef *dac;
mbed_official 125:23cc3068a9e4 45 DAC_InitTypeDef DAC_InitStructure;
mbed_official 125:23cc3068a9e4 46
mbed_official 125:23cc3068a9e4 47 // Get the peripheral name (DAC_1, ...) from the pin and assign it to the object
mbed_official 125:23cc3068a9e4 48 obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
mbed_official 227:7bd0639b8911 49 MBED_ASSERT(obj->dac != (DACName)NC);
mbed_official 125:23cc3068a9e4 50
mbed_official 125:23cc3068a9e4 51 dac = (DAC_TypeDef *)(obj->dac);
mbed_official 125:23cc3068a9e4 52
mbed_official 125:23cc3068a9e4 53 // Configure GPIO
mbed_official 125:23cc3068a9e4 54 pinmap_pinout(pin, PinMap_DAC);
mbed_official 125:23cc3068a9e4 55
mbed_official 159:3b23f6d9ecb9 56 // Save the channel for future use
mbed_official 216:577900467c9e 57 obj->pin = pin;
mbed_official 125:23cc3068a9e4 58
mbed_official 125:23cc3068a9e4 59 // Enable DAC clock
mbed_official 125:23cc3068a9e4 60 RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
mbed_official 125:23cc3068a9e4 61
mbed_official 125:23cc3068a9e4 62 // Configure and enable DAC channel
mbed_official 135:067cc8ba23da 63 DAC_StructInit(&DAC_InitStructure);
mbed_official 125:23cc3068a9e4 64 DAC_Init(dac, DAC_Channel_1, &DAC_InitStructure);
mbed_official 125:23cc3068a9e4 65 DAC_Cmd(dac, DAC_Channel_1, ENABLE);
mbed_official 125:23cc3068a9e4 66
mbed_official 125:23cc3068a9e4 67 analogout_write_u16(obj, 0);
mbed_official 125:23cc3068a9e4 68 }
mbed_official 125:23cc3068a9e4 69
mbed_official 125:23cc3068a9e4 70 void analogout_free(dac_t *obj) {
mbed_official 159:3b23f6d9ecb9 71 DAC_TypeDef *dac = (DAC_TypeDef *)(obj->dac);
mbed_official 159:3b23f6d9ecb9 72 // Disable DAC
mbed_official 159:3b23f6d9ecb9 73 DAC_DeInit(dac);
mbed_official 159:3b23f6d9ecb9 74 RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, DISABLE);
mbed_official 159:3b23f6d9ecb9 75 // Configure GPIO
mbed_official 216:577900467c9e 76 pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
mbed_official 125:23cc3068a9e4 77 }
mbed_official 125:23cc3068a9e4 78
mbed_official 125:23cc3068a9e4 79 static inline void dac_write(dac_t *obj, uint16_t value) {
mbed_official 125:23cc3068a9e4 80 DAC_TypeDef *dac = (DAC_TypeDef *)(obj->dac);
mbed_official 125:23cc3068a9e4 81 DAC_SetChannel1Data(dac, DAC_Align_12b_R, value);
mbed_official 125:23cc3068a9e4 82 }
mbed_official 125:23cc3068a9e4 83
mbed_official 125:23cc3068a9e4 84 static inline int dac_read(dac_t *obj) {
mbed_official 125:23cc3068a9e4 85 DAC_TypeDef *dac = (DAC_TypeDef *)(obj->dac);
mbed_official 125:23cc3068a9e4 86 return (int)DAC_GetDataOutputValue(dac, DAC_Channel_1);
mbed_official 125:23cc3068a9e4 87 }
mbed_official 125:23cc3068a9e4 88
mbed_official 125:23cc3068a9e4 89 void analogout_write(dac_t *obj, float value) {
mbed_official 125:23cc3068a9e4 90 if (value < 0.0f) {
mbed_official 125:23cc3068a9e4 91 dac_write(obj, 0); // Min value
mbed_official 125:23cc3068a9e4 92 } else if (value > 1.0f) {
mbed_official 159:3b23f6d9ecb9 93 dac_write(obj, (uint16_t)DAC_RANGE); // Max value
mbed_official 125:23cc3068a9e4 94 } else {
mbed_official 159:3b23f6d9ecb9 95 dac_write(obj, (uint16_t)(value * (float)DAC_RANGE));
mbed_official 125:23cc3068a9e4 96 }
mbed_official 125:23cc3068a9e4 97 }
mbed_official 125:23cc3068a9e4 98
mbed_official 125:23cc3068a9e4 99 void analogout_write_u16(dac_t *obj, uint16_t value) {
mbed_official 159:3b23f6d9ecb9 100 if (value > (uint16_t)DAC_RANGE) {
mbed_official 159:3b23f6d9ecb9 101 dac_write(obj, (uint16_t)DAC_RANGE); // Max value
mbed_official 135:067cc8ba23da 102 } else {
mbed_official 135:067cc8ba23da 103 dac_write(obj, value);
mbed_official 125:23cc3068a9e4 104 }
mbed_official 125:23cc3068a9e4 105 }
mbed_official 125:23cc3068a9e4 106
mbed_official 125:23cc3068a9e4 107 float analogout_read(dac_t *obj) {
mbed_official 125:23cc3068a9e4 108 uint32_t value = dac_read(obj);
mbed_official 159:3b23f6d9ecb9 109 return (float)((float)value * (1.0f / (float)DAC_RANGE));
mbed_official 125:23cc3068a9e4 110 }
mbed_official 125:23cc3068a9e4 111
mbed_official 125:23cc3068a9e4 112 uint16_t analogout_read_u16(dac_t *obj) {
mbed_official 125:23cc3068a9e4 113 return (uint16_t)dac_read(obj);
mbed_official 125:23cc3068a9e4 114 }
mbed_official 125:23cc3068a9e4 115
mbed_official 125:23cc3068a9e4 116 #endif // DEVICE_ANALOGOUT