EVAL-AD7124 Mbed Example Program.
Dependencies: adi_console_menu platform_drivers
app/ad7124_support.c@7:3e1005bd4d41, 2021-08-03 (annotated)
- Committer:
- Kjansen
- Date:
- Tue Aug 03 12:05:42 2021 +0100
- Revision:
- 7:3e1005bd4d41
- Parent:
- 3:779bb1e55f1a
No-OS Adoption Changes:
* Updated the .lib files for adoption of no-OS repository as-is.
* Replaced platform_drivers.h with required header files.
* Added designated initializers for nanodac_init_params as the latest
spi_init_param in no-OS repository include a new field member platform_ops.
* Added Support Macros.
* Updated the copyright year of the nanodac_console_app.c file
Mbed OS update changes:
1) Added the mbed_app.json file with custom parameters
2) Updated the mbed-os version to 6.8.0
Updated the readme file.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mahphalke | 3:779bb1e55f1a | 1 | /*! |
mahphalke | 3:779bb1e55f1a | 2 | ***************************************************************************** |
mahphalke | 3:779bb1e55f1a | 3 | @file: ad7124_support.c |
mahphalke | 3:779bb1e55f1a | 4 | |
mahphalke | 3:779bb1e55f1a | 5 | @brief: Provides useful support functions for the AD7124 NoOS driver |
mahphalke | 3:779bb1e55f1a | 6 | |
mahphalke | 3:779bb1e55f1a | 7 | @details: |
mahphalke | 3:779bb1e55f1a | 8 | ----------------------------------------------------------------------------- |
mahphalke | 3:779bb1e55f1a | 9 | Copyright (c) 2019, 2020 Analog Devices, Inc. |
mahphalke | 3:779bb1e55f1a | 10 | All rights reserved. |
mahphalke | 3:779bb1e55f1a | 11 | |
mahphalke | 3:779bb1e55f1a | 12 | This software is proprietary to Analog Devices, Inc. and its licensors. |
mahphalke | 3:779bb1e55f1a | 13 | By using this software you agree to the terms of the associated |
mahphalke | 3:779bb1e55f1a | 14 | Analog Devices Software License Agreement. |
mahphalke | 3:779bb1e55f1a | 15 | *****************************************************************************/ |
mahphalke | 3:779bb1e55f1a | 16 | |
mahphalke | 3:779bb1e55f1a | 17 | #include <stdbool.h> |
mahphalke | 3:779bb1e55f1a | 18 | #include "ad7124_support.h" |
mahphalke | 3:779bb1e55f1a | 19 | |
mahphalke | 3:779bb1e55f1a | 20 | // Public Functions |
mahphalke | 3:779bb1e55f1a | 21 | /* |
mahphalke | 3:779bb1e55f1a | 22 | * @brief helper function get the setup setting for an ADC channel |
mahphalke | 3:779bb1e55f1a | 23 | * |
mahphalke | 3:779bb1e55f1a | 24 | * @param dev The device structure. |
mahphalke | 3:779bb1e55f1a | 25 | * |
mahphalke | 3:779bb1e55f1a | 26 | * @param channel ADC channel to get Setup for. |
mahphalke | 3:779bb1e55f1a | 27 | * |
mahphalke | 3:779bb1e55f1a | 28 | * @return value of setup field in channel configuration. |
mahphalke | 3:779bb1e55f1a | 29 | */ |
mahphalke | 3:779bb1e55f1a | 30 | uint8_t ad7124_get_channel_setup(struct ad7124_dev *dev, uint8_t channel) |
mahphalke | 3:779bb1e55f1a | 31 | { |
mahphalke | 3:779bb1e55f1a | 32 | return (dev->regs[AD7124_Channel_0 + channel].value >> 12) & 0x7; |
mahphalke | 3:779bb1e55f1a | 33 | } |
mahphalke | 3:779bb1e55f1a | 34 | |
mahphalke | 3:779bb1e55f1a | 35 | |
mahphalke | 3:779bb1e55f1a | 36 | /* |
mahphalke | 3:779bb1e55f1a | 37 | * @brief helper function get the PGA setting for an ADC channel |
mahphalke | 3:779bb1e55f1a | 38 | * |
mahphalke | 3:779bb1e55f1a | 39 | * @param dev The device structure. |
mahphalke | 3:779bb1e55f1a | 40 | * |
mahphalke | 3:779bb1e55f1a | 41 | * @param channel ADC channel to get Setup for. |
mahphalke | 3:779bb1e55f1a | 42 | * |
mahphalke | 3:779bb1e55f1a | 43 | * @return value of PGA field in the setup for an ADC channel. |
mahphalke | 3:779bb1e55f1a | 44 | */ |
mahphalke | 3:779bb1e55f1a | 45 | uint8_t ad7124_get_channel_pga(struct ad7124_dev *dev, uint8_t channel) |
mahphalke | 3:779bb1e55f1a | 46 | { |
mahphalke | 3:779bb1e55f1a | 47 | uint8_t setup = ad7124_get_channel_setup(dev, channel); |
mahphalke | 3:779bb1e55f1a | 48 | |
mahphalke | 3:779bb1e55f1a | 49 | return (dev->regs[AD7124_Config_0 + setup].value) & 0x07; |
mahphalke | 3:779bb1e55f1a | 50 | } |
mahphalke | 3:779bb1e55f1a | 51 | |
mahphalke | 3:779bb1e55f1a | 52 | |
mahphalke | 3:779bb1e55f1a | 53 | /* |
mahphalke | 3:779bb1e55f1a | 54 | * @brief helper function get the bipolar setting for an ADC channel |
mahphalke | 3:779bb1e55f1a | 55 | * |
mahphalke | 3:779bb1e55f1a | 56 | * @param dev The device structure. |
mahphalke | 3:779bb1e55f1a | 57 | * |
mahphalke | 3:779bb1e55f1a | 58 | * @param channel ADC channel to get bipolar mode for. |
mahphalke | 3:779bb1e55f1a | 59 | * |
mahphalke | 3:779bb1e55f1a | 60 | * @return value of bipolar field in the setup for an ADC channel. |
mahphalke | 3:779bb1e55f1a | 61 | */ |
mahphalke | 3:779bb1e55f1a | 62 | bool ad7124_get_channel_bipolar(struct ad7124_dev *dev, uint8_t channel) |
mahphalke | 3:779bb1e55f1a | 63 | { |
mahphalke | 3:779bb1e55f1a | 64 | uint8_t setup = ad7124_get_channel_setup(dev, channel); |
mahphalke | 3:779bb1e55f1a | 65 | |
mahphalke | 3:779bb1e55f1a | 66 | return ((dev->regs[AD7124_Config_0 + setup].value >> 11) & 0x1) ? true : false; |
mahphalke | 3:779bb1e55f1a | 67 | } |
mahphalke | 3:779bb1e55f1a | 68 | |
mahphalke | 3:779bb1e55f1a | 69 | |
mahphalke | 3:779bb1e55f1a | 70 | /* |
mahphalke | 3:779bb1e55f1a | 71 | * @brief converts ADC sample value to voltage based on gain setting |
mahphalke | 3:779bb1e55f1a | 72 | * |
mahphalke | 3:779bb1e55f1a | 73 | * @param dev The device structure. |
mahphalke | 3:779bb1e55f1a | 74 | * |
mahphalke | 3:779bb1e55f1a | 75 | * @param channel ADC channel to get Setup for. |
mahphalke | 3:779bb1e55f1a | 76 | * |
mahphalke | 3:779bb1e55f1a | 77 | * @param sample Raw ADC sample |
mahphalke | 3:779bb1e55f1a | 78 | * |
mahphalke | 3:779bb1e55f1a | 79 | * @return Sample ADC value converted to voltage. |
mahphalke | 3:779bb1e55f1a | 80 | * |
mahphalke | 3:779bb1e55f1a | 81 | * @note The conversion equation is implemented for simplicity, |
mahphalke | 3:779bb1e55f1a | 82 | * not for accuracy or performance |
mahphalke | 3:779bb1e55f1a | 83 | * |
mahphalke | 3:779bb1e55f1a | 84 | */ |
mahphalke | 3:779bb1e55f1a | 85 | float ad7124_convert_sample_to_voltage(struct ad7124_dev *dev, uint8_t channel, |
mahphalke | 3:779bb1e55f1a | 86 | uint32_t sample) |
mahphalke | 3:779bb1e55f1a | 87 | { |
mahphalke | 3:779bb1e55f1a | 88 | bool isBipolar = ad7124_get_channel_bipolar(dev, channel); |
mahphalke | 3:779bb1e55f1a | 89 | uint8_t channelPGA = ad7124_get_channel_pga(dev, channel); |
mahphalke | 3:779bb1e55f1a | 90 | |
mahphalke | 3:779bb1e55f1a | 91 | float convertedValue; |
mahphalke | 3:779bb1e55f1a | 92 | |
mahphalke | 3:779bb1e55f1a | 93 | if (isBipolar) { |
mahphalke | 3:779bb1e55f1a | 94 | convertedValue = ( ((float)sample / (1 << (AD7124_ADC_N_BITS -1))) -1 ) * \ |
mahphalke | 3:779bb1e55f1a | 95 | (AD7124_REF_VOLTAGE / AD7124_PGA_GAIN(channelPGA)); |
mahphalke | 3:779bb1e55f1a | 96 | } else { |
mahphalke | 3:779bb1e55f1a | 97 | convertedValue = ((float)sample * AD7124_REF_VOLTAGE)/(AD7124_PGA_GAIN( |
mahphalke | 3:779bb1e55f1a | 98 | channelPGA) * \ |
mahphalke | 3:779bb1e55f1a | 99 | (1 << AD7124_ADC_N_BITS)); |
mahphalke | 3:779bb1e55f1a | 100 | } |
mahphalke | 3:779bb1e55f1a | 101 | |
mahphalke | 3:779bb1e55f1a | 102 | return (convertedValue); |
mahphalke | 3:779bb1e55f1a | 103 | } |