test

Committer:
elijahsj
Date:
Mon Nov 09 00:33:19 2020 -0500
Revision:
2:4364577b5ad8
Parent:
1:8a094db1347f
copied mbed library

Who changed what in which revision?

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