mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Thu Mar 20 11:45:07 2014 +0000
Revision:
129:0182c99221bc
Parent:
95:7f2fbdc5e413
Child:
174:8bb9f3a33240
Synchronized with git revision 93e44fb5a5aa010177943aaaec0126ed8b8e014d

Full URL: https://github.com/mbedmicro/mbed/commit/93e44fb5a5aa010177943aaaec0126ed8b8e014d/

[NUCLEO_F302R8] and [NUCLEO_L152RE] updates

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 129:0182c99221bc 40 {PA_5, DAC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // DAC_OUT2
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 129:0182c99221bc 64 DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
mbed_official 129:0182c99221bc 65 DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
mbed_official 95:7f2fbdc5e413 66 DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0;
mbed_official 129:0182c99221bc 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 129:0182c99221bc 70 DAC_Init(DAC_Channel_1, &DAC_InitStructure);
mbed_official 129:0182c99221bc 71 DAC_Cmd(DAC_Channel_1, ENABLE);
mbed_official 76:aeb1df146756 72 }
mbed_official 129:0182c99221bc 73 if (obj->channel == PA_5) {
mbed_official 129:0182c99221bc 74 DAC_Init(DAC_Channel_2, &DAC_InitStructure);
mbed_official 129:0182c99221bc 75 DAC_Cmd(DAC_Channel_2, ENABLE);
mbed_official 129:0182c99221bc 76 }
mbed_official 129:0182c99221bc 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 129:0182c99221bc 86 DAC_SetChannel1Data(DAC_Align_12b_R, value);
mbed_official 76:aeb1df146756 87 }
mbed_official 129:0182c99221bc 88 if (obj->channel == PA_5) {
mbed_official 129:0182c99221bc 89 DAC_SetChannel2Data(DAC_Align_12b_R, value);
mbed_official 129:0182c99221bc 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 129:0182c99221bc 95 return (int)DAC_GetDataOutputValue(DAC_Channel_1);
mbed_official 76:aeb1df146756 96 }
mbed_official 129:0182c99221bc 97 if (obj->channel == PA_5) {
mbed_official 129:0182c99221bc 98 return (int)DAC_GetDataOutputValue(DAC_Channel_2);
mbed_official 129:0182c99221bc 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