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:
Wed Mar 19 10:15:22 2014 +0000
Revision:
125:23cc3068a9e4
Child:
135:067cc8ba23da
Synchronized with git revision ace35dfba3748c7cdc102eb38ec6b9e1067c3252

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

[NUCLEO_F302R8] Add cmsis and hal files + change F401RE clock to 84MHz

Who changed what in which revision?

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