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 "mbed_assert.h"
ebrus 0:6bc4ac881c8e 29 #include "analogout_api.h"
ebrus 0:6bc4ac881c8e 30
ebrus 0:6bc4ac881c8e 31 #if DEVICE_ANALOGOUT
ebrus 0:6bc4ac881c8e 32
ebrus 0:6bc4ac881c8e 33 #include "cmsis.h"
ebrus 0:6bc4ac881c8e 34 #include "pinmap.h"
ebrus 0:6bc4ac881c8e 35
ebrus 0:6bc4ac881c8e 36 #define RANGE_12BIT (0xFFF)
ebrus 0:6bc4ac881c8e 37
ebrus 0:6bc4ac881c8e 38 static const PinMap PinMap_DAC[] = {
ebrus 0:6bc4ac881c8e 39 {PA_4, DAC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // DAC_OUT1
ebrus 0:6bc4ac881c8e 40 {PA_5, DAC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // DAC_OUT2
ebrus 0:6bc4ac881c8e 41 {NC, NC, 0}
ebrus 0:6bc4ac881c8e 42 };
ebrus 0:6bc4ac881c8e 43
ebrus 0:6bc4ac881c8e 44 void analogout_init(dac_t *obj, PinName pin) {
ebrus 0:6bc4ac881c8e 45 DAC_InitTypeDef DAC_InitStructure;
ebrus 0:6bc4ac881c8e 46
ebrus 0:6bc4ac881c8e 47 // Get the peripheral name (DAC_1, ...) from the pin and assign it to the object
ebrus 0:6bc4ac881c8e 48 obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
ebrus 0:6bc4ac881c8e 49 MBED_ASSERT(obj->dac != (DACName)NC);
ebrus 0:6bc4ac881c8e 50
ebrus 0:6bc4ac881c8e 51 // Configure GPIO
ebrus 0:6bc4ac881c8e 52 pinmap_pinout(pin, PinMap_DAC);
ebrus 0:6bc4ac881c8e 53
ebrus 0:6bc4ac881c8e 54 // Save the pin for future use
ebrus 0:6bc4ac881c8e 55 obj->pin = pin;
ebrus 0:6bc4ac881c8e 56
ebrus 0:6bc4ac881c8e 57 // Enable DAC clock
ebrus 0:6bc4ac881c8e 58 RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
ebrus 0:6bc4ac881c8e 59
ebrus 0:6bc4ac881c8e 60 // Configure and enable DAC channel
ebrus 0:6bc4ac881c8e 61 DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
ebrus 0:6bc4ac881c8e 62 DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
ebrus 0:6bc4ac881c8e 63 DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0;
ebrus 0:6bc4ac881c8e 64 DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
ebrus 0:6bc4ac881c8e 65
ebrus 0:6bc4ac881c8e 66 if (obj->pin == PA_4) {
ebrus 0:6bc4ac881c8e 67 DAC_Init(DAC_Channel_1, &DAC_InitStructure);
ebrus 0:6bc4ac881c8e 68 DAC_Cmd(DAC_Channel_1, ENABLE);
ebrus 0:6bc4ac881c8e 69 }
ebrus 0:6bc4ac881c8e 70 if (obj->pin == PA_5) {
ebrus 0:6bc4ac881c8e 71 DAC_Init(DAC_Channel_2, &DAC_InitStructure);
ebrus 0:6bc4ac881c8e 72 DAC_Cmd(DAC_Channel_2, ENABLE);
ebrus 0:6bc4ac881c8e 73 }
ebrus 0:6bc4ac881c8e 74
ebrus 0:6bc4ac881c8e 75 analogout_write_u16(obj, 0);
ebrus 0:6bc4ac881c8e 76 }
ebrus 0:6bc4ac881c8e 77
ebrus 0:6bc4ac881c8e 78 void analogout_free(dac_t *obj) {
ebrus 0:6bc4ac881c8e 79 // Configure GPIOs
ebrus 0:6bc4ac881c8e 80 pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
ebrus 0:6bc4ac881c8e 81 }
ebrus 0:6bc4ac881c8e 82
ebrus 0:6bc4ac881c8e 83 static inline void dac_write(dac_t *obj, uint16_t value) {
ebrus 0:6bc4ac881c8e 84 if (obj->pin == PA_4) {
ebrus 0:6bc4ac881c8e 85 DAC_SetChannel1Data(DAC_Align_12b_R, value);
ebrus 0:6bc4ac881c8e 86 }
ebrus 0:6bc4ac881c8e 87 if (obj->pin == PA_5) {
ebrus 0:6bc4ac881c8e 88 DAC_SetChannel2Data(DAC_Align_12b_R, value);
ebrus 0:6bc4ac881c8e 89 }
ebrus 0:6bc4ac881c8e 90 }
ebrus 0:6bc4ac881c8e 91
ebrus 0:6bc4ac881c8e 92 static inline int dac_read(dac_t *obj) {
ebrus 0:6bc4ac881c8e 93 if (obj->pin == PA_4) {
ebrus 0:6bc4ac881c8e 94 return (int)DAC_GetDataOutputValue(DAC_Channel_1);
ebrus 0:6bc4ac881c8e 95 }
ebrus 0:6bc4ac881c8e 96 if (obj->pin == PA_5) {
ebrus 0:6bc4ac881c8e 97 return (int)DAC_GetDataOutputValue(DAC_Channel_2);
ebrus 0:6bc4ac881c8e 98 }
ebrus 0:6bc4ac881c8e 99 return 0;
ebrus 0:6bc4ac881c8e 100 }
ebrus 0:6bc4ac881c8e 101
ebrus 0:6bc4ac881c8e 102 void analogout_write(dac_t *obj, float value) {
ebrus 0:6bc4ac881c8e 103 if (value < 0.0f) {
ebrus 0:6bc4ac881c8e 104 dac_write(obj, 0); // Min value
ebrus 0:6bc4ac881c8e 105 } else if (value > 1.0f) {
ebrus 0:6bc4ac881c8e 106 dac_write(obj, (uint16_t)RANGE_12BIT); // Max value
ebrus 0:6bc4ac881c8e 107 } else {
ebrus 0:6bc4ac881c8e 108 dac_write(obj, (uint16_t)(value * (float)RANGE_12BIT));
ebrus 0:6bc4ac881c8e 109 }
ebrus 0:6bc4ac881c8e 110 }
ebrus 0:6bc4ac881c8e 111
ebrus 0:6bc4ac881c8e 112 void analogout_write_u16(dac_t *obj, uint16_t value) {
ebrus 0:6bc4ac881c8e 113 if (value > (uint16_t)RANGE_12BIT) {
ebrus 0:6bc4ac881c8e 114 dac_write(obj, (uint16_t)RANGE_12BIT); // Max value
ebrus 0:6bc4ac881c8e 115 } else {
ebrus 0:6bc4ac881c8e 116 dac_write(obj, value);
ebrus 0:6bc4ac881c8e 117 }
ebrus 0:6bc4ac881c8e 118 }
ebrus 0:6bc4ac881c8e 119
ebrus 0:6bc4ac881c8e 120 float analogout_read(dac_t *obj) {
ebrus 0:6bc4ac881c8e 121 uint32_t value = dac_read(obj);
ebrus 0:6bc4ac881c8e 122 return (float)value * (1.0f / (float)RANGE_12BIT);
ebrus 0:6bc4ac881c8e 123 }
ebrus 0:6bc4ac881c8e 124
ebrus 0:6bc4ac881c8e 125 uint16_t analogout_read_u16(dac_t *obj) {
ebrus 0:6bc4ac881c8e 126 return (uint16_t)dac_read(obj);
ebrus 0:6bc4ac881c8e 127 }
ebrus 0:6bc4ac881c8e 128
ebrus 0:6bc4ac881c8e 129 #endif // DEVICE_ANALOGOUT