mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
targets/TARGET_STM/analogout_api.c@180:d79f997829d6, 2017-12-18 (annotated)
- Committer:
- Anythingconnected
- Date:
- Mon Dec 18 10:14:27 2017 +0000
- Revision:
- 180:d79f997829d6
- Parent:
- 168:9672193075cf
Getting byte by byte read to work
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AnnaBridge | 168:9672193075cf | 1 | /* mbed Microcontroller Library |
AnnaBridge | 168:9672193075cf | 2 | * Copyright (c) 2017, STMicroelectronics |
AnnaBridge | 168:9672193075cf | 3 | * All rights reserved. |
AnnaBridge | 168:9672193075cf | 4 | * |
AnnaBridge | 168:9672193075cf | 5 | * Redistribution and use in source and binary forms, with or without |
AnnaBridge | 168:9672193075cf | 6 | * modification, are permitted provided that the following conditions are met: |
AnnaBridge | 168:9672193075cf | 7 | * |
AnnaBridge | 168:9672193075cf | 8 | * 1. Redistributions of source code must retain the above copyright notice, |
AnnaBridge | 168:9672193075cf | 9 | * this list of conditions and the following disclaimer. |
AnnaBridge | 168:9672193075cf | 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
AnnaBridge | 168:9672193075cf | 11 | * this list of conditions and the following disclaimer in the documentation |
AnnaBridge | 168:9672193075cf | 12 | * and/or other materials provided with the distribution. |
AnnaBridge | 168:9672193075cf | 13 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
AnnaBridge | 168:9672193075cf | 14 | * may be used to endorse or promote products derived from this software |
AnnaBridge | 168:9672193075cf | 15 | * without specific prior written permission. |
AnnaBridge | 168:9672193075cf | 16 | * |
AnnaBridge | 168:9672193075cf | 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
AnnaBridge | 168:9672193075cf | 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
AnnaBridge | 168:9672193075cf | 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
AnnaBridge | 168:9672193075cf | 20 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
AnnaBridge | 168:9672193075cf | 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
AnnaBridge | 168:9672193075cf | 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
AnnaBridge | 168:9672193075cf | 23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
AnnaBridge | 168:9672193075cf | 24 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
AnnaBridge | 168:9672193075cf | 25 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
AnnaBridge | 168:9672193075cf | 26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
AnnaBridge | 168:9672193075cf | 27 | */ |
AnnaBridge | 168:9672193075cf | 28 | #include "mbed_assert.h" |
AnnaBridge | 168:9672193075cf | 29 | #include "analogout_api.h" |
AnnaBridge | 168:9672193075cf | 30 | |
AnnaBridge | 168:9672193075cf | 31 | #if DEVICE_ANALOGOUT |
AnnaBridge | 168:9672193075cf | 32 | |
AnnaBridge | 168:9672193075cf | 33 | #include "cmsis.h" |
AnnaBridge | 168:9672193075cf | 34 | #include "pinmap.h" |
AnnaBridge | 168:9672193075cf | 35 | #include "mbed_error.h" |
AnnaBridge | 168:9672193075cf | 36 | #include "PeripheralPins.h" |
AnnaBridge | 168:9672193075cf | 37 | |
AnnaBridge | 168:9672193075cf | 38 | #define DAC_RANGE (0xFFF) // 12 bits |
AnnaBridge | 168:9672193075cf | 39 | #define DAC_NB_BITS (12) |
AnnaBridge | 168:9672193075cf | 40 | |
AnnaBridge | 168:9672193075cf | 41 | static inline void dac_write(dac_t *obj, int value) |
AnnaBridge | 168:9672193075cf | 42 | { |
AnnaBridge | 168:9672193075cf | 43 | HAL_DAC_SetValue(&obj->handle, obj->channel, DAC_ALIGN_12B_R, (value & DAC_RANGE)); |
AnnaBridge | 168:9672193075cf | 44 | HAL_DAC_Start(&obj->handle, obj->channel); |
AnnaBridge | 168:9672193075cf | 45 | } |
AnnaBridge | 168:9672193075cf | 46 | |
AnnaBridge | 168:9672193075cf | 47 | static inline int dac_read(dac_t *obj) |
AnnaBridge | 168:9672193075cf | 48 | { |
AnnaBridge | 168:9672193075cf | 49 | return (int)HAL_DAC_GetValue(&obj->handle, obj->channel); |
AnnaBridge | 168:9672193075cf | 50 | } |
AnnaBridge | 168:9672193075cf | 51 | |
AnnaBridge | 168:9672193075cf | 52 | void analogout_write(dac_t *obj, float value) |
AnnaBridge | 168:9672193075cf | 53 | { |
AnnaBridge | 168:9672193075cf | 54 | if (value < 0.0f) { |
AnnaBridge | 168:9672193075cf | 55 | dac_write(obj, 0); // Min value |
AnnaBridge | 168:9672193075cf | 56 | } else if (value > 1.0f) { |
AnnaBridge | 168:9672193075cf | 57 | dac_write(obj, (int)DAC_RANGE); // Max value |
AnnaBridge | 168:9672193075cf | 58 | } else { |
AnnaBridge | 168:9672193075cf | 59 | dac_write(obj, (int)(value * (float)DAC_RANGE)); |
AnnaBridge | 168:9672193075cf | 60 | } |
AnnaBridge | 168:9672193075cf | 61 | } |
AnnaBridge | 168:9672193075cf | 62 | |
AnnaBridge | 168:9672193075cf | 63 | void analogout_write_u16(dac_t *obj, uint16_t value) |
AnnaBridge | 168:9672193075cf | 64 | { |
AnnaBridge | 168:9672193075cf | 65 | dac_write(obj, value >> (16 - DAC_NB_BITS)); |
AnnaBridge | 168:9672193075cf | 66 | } |
AnnaBridge | 168:9672193075cf | 67 | |
AnnaBridge | 168:9672193075cf | 68 | float analogout_read(dac_t *obj) |
AnnaBridge | 168:9672193075cf | 69 | { |
AnnaBridge | 168:9672193075cf | 70 | uint32_t value = dac_read(obj); |
AnnaBridge | 168:9672193075cf | 71 | return (float)value * (1.0f / (float)DAC_RANGE); |
AnnaBridge | 168:9672193075cf | 72 | } |
AnnaBridge | 168:9672193075cf | 73 | |
AnnaBridge | 168:9672193075cf | 74 | uint16_t analogout_read_u16(dac_t *obj) |
AnnaBridge | 168:9672193075cf | 75 | { |
AnnaBridge | 168:9672193075cf | 76 | uint32_t value = dac_read(obj); |
AnnaBridge | 168:9672193075cf | 77 | return (value << 4) | ((value >> 8) & 0x000F); // Conversion from 12 to 16 bits |
AnnaBridge | 168:9672193075cf | 78 | } |
AnnaBridge | 168:9672193075cf | 79 | |
AnnaBridge | 168:9672193075cf | 80 | #endif // DEVICE_ANALOGOUT |