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