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 Dec 16 08:15:08 2014 +0000
Revision:
440:8a0b45cd594f
Parent:
381:5460fc57b6e4
Synchronized with git revision 67fbbf0b635d0c0d93fbe433306c537c2ad206aa

Full URL: https://github.com/mbedmicro/mbed/commit/67fbbf0b635d0c0d93fbe433306c537c2ad206aa/

Targets: nrf51 - updating app_timer.c from Norid'c SDKv7.1.0

Who changed what in which revision?

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