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:
Tue Jul 08 11:15:08 2014 +0100
Revision:
250:a49055e7a707
Parent:
239:8cadf13dff33
Child:
251:de9a1e4ffd79
Synchronized with git revision 3197042b65f8d28e856e1a7812d45e2fbe80e3f1

Full URL: https://github.com/mbedmicro/mbed/commit/3197042b65f8d28e856e1a7812d45e2fbe80e3f1/

error.h -> mbed_error.h

Who changed what in which revision?

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