mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Feb 18 14:15:05 2014 +0000
Revision:
95:7f2fbdc5e413
Parent:
94:6519f69185ce
Child:
129:0182c99221bc
Synchronized with git revision a8f17f5426dd14412a34d93e1222fd148089dc47

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

Who changed what in which revision?

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