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