AD7124 Example Program
Dependencies: platform_drivers AD7124_no_OS
main.cpp@2:0897873979f3, 2019-09-05 (annotated)
- Committer:
- MitchAD
- Date:
- Thu Sep 05 20:50:43 2019 +0000
- Revision:
- 2:0897873979f3
- Parent:
- 0:83a0c7bbe493
Initial commit for the AD7124 Example Program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
malavikasaji | 0:83a0c7bbe493 | 1 | /* Copyright (c) 2019 Analog Devices, Inc. All rights reserved. |
malavikasaji | 0:83a0c7bbe493 | 2 | |
malavikasaji | 0:83a0c7bbe493 | 3 | Redistribution and use in source and binary forms, with or without modification, |
malavikasaji | 0:83a0c7bbe493 | 4 | are permitted provided that the following conditions are met: |
malavikasaji | 0:83a0c7bbe493 | 5 | - Redistributions of source code must retain the above copyright notice, |
malavikasaji | 0:83a0c7bbe493 | 6 | this list of conditions and the following disclaimer. |
malavikasaji | 0:83a0c7bbe493 | 7 | - Redistributions in binary form must reproduce the above copyright notice, |
malavikasaji | 0:83a0c7bbe493 | 8 | this list of conditions and the following disclaimer in the documentation |
malavikasaji | 0:83a0c7bbe493 | 9 | and/or other materials provided with the distribution. |
malavikasaji | 0:83a0c7bbe493 | 10 | - Modified versions of the software must be conspicuously marked as such. |
malavikasaji | 0:83a0c7bbe493 | 11 | - This software is licensed solely and exclusively for use with processors/products |
malavikasaji | 0:83a0c7bbe493 | 12 | manufactured by or for Analog Devices, Inc. |
malavikasaji | 0:83a0c7bbe493 | 13 | - This software may not be combined or merged with other code in any manner |
malavikasaji | 0:83a0c7bbe493 | 14 | that would cause the software to become subject to terms and conditions which |
malavikasaji | 0:83a0c7bbe493 | 15 | differ from those listed here. |
malavikasaji | 0:83a0c7bbe493 | 16 | - Neither the name of Analog Devices, Inc. nor the names of its contributors |
malavikasaji | 0:83a0c7bbe493 | 17 | may be used to endorse or promote products derived from this software without |
malavikasaji | 0:83a0c7bbe493 | 18 | specific prior written permission. |
malavikasaji | 0:83a0c7bbe493 | 19 | - The use of this software may or may not infringe the patent rights of one or |
malavikasaji | 0:83a0c7bbe493 | 20 | more patent holders. This license does not release you from the requirement |
malavikasaji | 0:83a0c7bbe493 | 21 | that you obtain separate licenses from these patent holders to use this software. |
malavikasaji | 0:83a0c7bbe493 | 22 | |
malavikasaji | 0:83a0c7bbe493 | 23 | THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. AND CONTRIBUTORS "AS IS" AND |
malavikasaji | 0:83a0c7bbe493 | 24 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, |
malavikasaji | 0:83a0c7bbe493 | 25 | TITLE, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN |
malavikasaji | 0:83a0c7bbe493 | 26 | NO EVENT SHALL ANALOG DEVICES, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
malavikasaji | 0:83a0c7bbe493 | 27 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES |
malavikasaji | 0:83a0c7bbe493 | 28 | (INCLUDING, BUT NOT LIMITED TO, DAMAGES ARISING OUT OF CLAIMS OF INTELLECTUAL |
malavikasaji | 0:83a0c7bbe493 | 29 | PROPERTY RIGHTS INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
malavikasaji | 0:83a0c7bbe493 | 30 | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
malavikasaji | 0:83a0c7bbe493 | 31 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
malavikasaji | 0:83a0c7bbe493 | 32 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
malavikasaji | 0:83a0c7bbe493 | 33 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
malavikasaji | 0:83a0c7bbe493 | 34 | |
malavikasaji | 0:83a0c7bbe493 | 35 | 2019-01-10-7CBSD SLA |
malavikasaji | 0:83a0c7bbe493 | 36 | |
MitchAD | 2:0897873979f3 | 37 | USING THE PROGRAM |
MitchAD | 2:0897873979f3 | 38 | * While using this program, the user has the option of selecting between two |
MitchAD | 2:0897873979f3 | 39 | * different configurations: Default and Custom. These configurations represent |
MitchAD | 2:0897873979f3 | 40 | * two unique register maps which can be altered by the user throughout using |
MitchAD | 2:0897873979f3 | 41 | * the program. There are sixteen unique channels and eight unique setups |
MitchAD | 2:0897873979f3 | 42 | * associated with the AD7124. Each of the channels is assigned a setup which |
MitchAD | 2:0897873979f3 | 43 | * includes fields such as gain and filter options associated with it. The |
MitchAD | 2:0897873979f3 | 44 | * following guide will detail how to get the most out of the program. |
MitchAD | 2:0897873979f3 | 45 | * |
MitchAD | 2:0897873979f3 | 46 | * After running the program. |
MitchAD | 2:0897873979f3 | 47 | * 1) Press menu item (11) "Print Device Register Map (User Friendly Mode)". |
MitchAD | 2:0897873979f3 | 48 | * This will show you each channel's information. There are only two channels |
MitchAD | 2:0897873979f3 | 49 | * enabled, one using setup "0" and the other using setup "1". |
MitchAD | 2:0897873979f3 | 50 | * |
MitchAD | 2:0897873979f3 | 51 | * 2) Press menu item (3) "Assign Channel Setup" |
MitchAD | 2:0897873979f3 | 52 | * You can now assign any channel any setup continuously until you elect to quit. |
MitchAD | 2:0897873979f3 | 53 | * Select channel (0) and assign it to setup (2), then quit (16). |
MitchAD | 2:0897873979f3 | 54 | * |
MitchAD | 2:0897873979f3 | 55 | * 3) Press menu item (11) "Print Device Register Map (User Friendly Mode)". |
MitchAD | 2:0897873979f3 | 56 | * You can now see that channel "0" uses setup "2". |
MitchAD | 2:0897873979f3 | 57 | * |
MitchAD | 2:0897873979f3 | 58 | * 4) Press menu item (7) "Select Filter/Filter Data Rate". |
MitchAD | 2:0897873979f3 | 59 | * Select setup (2) to change and then change the filter to Fast Settling (3) |
MitchAD | 2:0897873979f3 | 60 | * and select data rate (128). |
MitchAD | 2:0897873979f3 | 61 | * |
MitchAD | 2:0897873979f3 | 62 | * 5) Press menu item (11) "Print Device Register Map (User Friendly Mode)". |
MitchAD | 2:0897873979f3 | 63 | * As you can see, every channel with setup "2" has had their filters and filter |
MitchAD | 2:0897873979f3 | 64 | * data rate changed. |
MitchAD | 2:0897873979f3 | 65 | * ***NOTE*** |
MitchAD | 2:0897873979f3 | 66 | * When modifying a setup, it will affect every channel using that setup |
MitchAD | 2:0897873979f3 | 67 | * |
MitchAD | 2:0897873979f3 | 68 | * 6) Press menu item (12) "Print Device Register Map (Raw Data Mode)" |
MitchAD | 2:0897873979f3 | 69 | * Lets say you found a configuration that works for your application, you can |
MitchAD | 2:0897873979f3 | 70 | * now directly copy the current configuration's register map and paste it into |
MitchAD | 2:0897873979f3 | 71 | * the "ad7124_regs_custom.cpp" file to "save" that configuration for future use. |
MitchAD | 2:0897873979f3 | 72 | * This allows the user to further modify configurations when tweaking is needed |
MitchAD | 2:0897873979f3 | 73 | * so that the complete flexibilty of the AD7124 is at the hands of the particular |
MitchAD | 2:0897873979f3 | 74 | * application. |
MitchAD | 2:0897873979f3 | 75 | * |
MitchAD | 2:0897873979f3 | 76 | * Additional Information: |
MitchAD | 2:0897873979f3 | 77 | * There are many other features of this part that are not in this program. |
MitchAD | 2:0897873979f3 | 78 | * The way that the structs are set up, it is easy to add additional |
MitchAD | 2:0897873979f3 | 79 | * functionality for features such as "Analog positive/negative input" and |
MitchAD | 2:0897873979f3 | 80 | * "Clock source". Feel free to modify this program to suit your needs even |
MitchAD | 2:0897873979f3 | 81 | * further. |
malavikasaji | 0:83a0c7bbe493 | 82 | */ |
malavikasaji | 0:83a0c7bbe493 | 83 | |
MitchAD | 2:0897873979f3 | 84 | /***Libraries***/ |
MitchAD | 2:0897873979f3 | 85 | #include <stdio.h> |
MitchAD | 2:0897873979f3 | 86 | #include <string.h> |
MitchAD | 2:0897873979f3 | 87 | |
malavikasaji | 0:83a0c7bbe493 | 88 | #include "mbed.h" |
MitchAD | 2:0897873979f3 | 89 | #include "platform_drivers.h" |
malavikasaji | 0:83a0c7bbe493 | 90 | |
MitchAD | 2:0897873979f3 | 91 | #include "ad7124.h" |
MitchAD | 2:0897873979f3 | 92 | #include "ad7124_regs.h" |
MitchAD | 2:0897873979f3 | 93 | #include "ad7124_regs_configs.h" |
MitchAD | 2:0897873979f3 | 94 | |
MitchAD | 2:0897873979f3 | 95 | /***Defines for SPI Protocol***/ |
MitchAD | 2:0897873979f3 | 96 | #define SPI_PLATFORM MBED |
MitchAD | 2:0897873979f3 | 97 | #define SPI_TYPE GENERIC_SPI |
MitchAD | 2:0897873979f3 | 98 | #define SPI_ID 0 |
MitchAD | 2:0897873979f3 | 99 | #define SPI_MAX_FREQUENCY 1500000 |
MitchAD | 2:0897873979f3 | 100 | #define SPI_MODE SPI_MODE_3 |
MitchAD | 2:0897873979f3 | 101 | #define SPI_POLL_COUNT 10000 |
MitchAD | 2:0897873979f3 | 102 | |
MitchAD | 2:0897873979f3 | 103 | /***Defines for UART Protocol***/ |
MitchAD | 2:0897873979f3 | 104 | #define BAUD_RATE 115200 |
MitchAD | 2:0897873979f3 | 105 | |
MitchAD | 2:0897873979f3 | 106 | /***Defines for Code to Voltage***/ |
MitchAD | 2:0897873979f3 | 107 | #define VREF 2.5 |
MitchAD | 2:0897873979f3 | 108 | #define NUM_BITS 24 |
MitchAD | 2:0897873979f3 | 109 | |
MitchAD | 2:0897873979f3 | 110 | /***Defines for Active Channels Array***/ |
MitchAD | 2:0897873979f3 | 111 | #define NUM_OF_CHANNELS 16 |
MitchAD | 2:0897873979f3 | 112 | #define NUM_OF_SETUPS 8 |
MitchAD | 2:0897873979f3 | 113 | |
MitchAD | 2:0897873979f3 | 114 | /***Defines for Operating Modes***/ |
MitchAD | 2:0897873979f3 | 115 | #define CONTINOUS_CONV_MODE 0x0 |
MitchAD | 2:0897873979f3 | 116 | #define SINGLE_CONV_MODE 0x1 |
MitchAD | 2:0897873979f3 | 117 | #define STANDBY_MODE 0x2 |
MitchAD | 2:0897873979f3 | 118 | #define POWER_DOWN_MODE 0x3 |
MitchAD | 2:0897873979f3 | 119 | #define IDLE_MODE 0x4 |
MitchAD | 2:0897873979f3 | 120 | #define INTERNAL_ZERO_SCALE 0x5 |
MitchAD | 2:0897873979f3 | 121 | #define INTERNAL_FULL_SCALE 0x6 |
MitchAD | 2:0897873979f3 | 122 | #define SYSTEM_ZERO_SCALE 0x7 |
MitchAD | 2:0897873979f3 | 123 | #define SYSTEM_FULL_SCALE 0x8 |
MitchAD | 2:0897873979f3 | 124 | |
MitchAD | 2:0897873979f3 | 125 | /***Defines for Power Modes***/ |
MitchAD | 2:0897873979f3 | 126 | #define LOW_POWER_MODE 0x0 |
MitchAD | 2:0897873979f3 | 127 | #define MED_POWER_MODE 0x1 |
MitchAD | 2:0897873979f3 | 128 | #define HIGH_POWER_MODE 0x2 |
MitchAD | 2:0897873979f3 | 129 | |
MitchAD | 2:0897873979f3 | 130 | /***Defines for Filters***/ |
MitchAD | 2:0897873979f3 | 131 | #define SINC4 0x0 |
MitchAD | 2:0897873979f3 | 132 | #define SINC3 0x2 |
MitchAD | 2:0897873979f3 | 133 | #define FAST_SETTLING 0x4 |
MitchAD | 2:0897873979f3 | 134 | #define FAST_SETTLING_SINC3 0x5 |
MitchAD | 2:0897873979f3 | 135 | #define POST 0x7 |
MitchAD | 2:0897873979f3 | 136 | |
MitchAD | 2:0897873979f3 | 137 | /***Defines for Gains***/ |
MitchAD | 2:0897873979f3 | 138 | #define GAIN_1 0x0 |
MitchAD | 2:0897873979f3 | 139 | #define GAIN_2 0x1 |
MitchAD | 2:0897873979f3 | 140 | #define GAIN_4 0x2 |
MitchAD | 2:0897873979f3 | 141 | #define GAIN_8 0x3 |
MitchAD | 2:0897873979f3 | 142 | #define GAIN_16 0x4 |
MitchAD | 2:0897873979f3 | 143 | #define GAIN_32 0x5 |
MitchAD | 2:0897873979f3 | 144 | #define GAIN_64 0x6 |
MitchAD | 2:0897873979f3 | 145 | #define GAIN_128 0x7 |
MitchAD | 2:0897873979f3 | 146 | |
MitchAD | 2:0897873979f3 | 147 | /***Defines for Register Reads***/ |
MitchAD | 2:0897873979f3 | 148 | #define POWER_MODE_READ(x) (((x) >> 6) & 0x3) |
MitchAD | 2:0897873979f3 | 149 | #define GAIN_READ(x) (((x) >> 0) & 0x7) |
MitchAD | 2:0897873979f3 | 150 | #define FILTER_READ(x) (((x) >> 21) & 0x7) |
MitchAD | 2:0897873979f3 | 151 | #define DATA_RATE_READ(x) (((x) >> 0) & 0x7FF) |
MitchAD | 2:0897873979f3 | 152 | #define SETUP_READ(x) (((x) >> 12) & 0x7) |
MitchAD | 2:0897873979f3 | 153 | |
MitchAD | 2:0897873979f3 | 154 | /***Configuration Defines***/ |
MitchAD | 2:0897873979f3 | 155 | #define DEF_CONFIG 0 |
MitchAD | 2:0897873979f3 | 156 | #define CUSTOM_CONFIG 1 |
MitchAD | 2:0897873979f3 | 157 | |
MitchAD | 2:0897873979f3 | 158 | /*Connecting Hardware Pin Names |
MitchAD | 2:0897873979f3 | 159 | to Software Variables*/ |
MitchAD | 2:0897873979f3 | 160 | DigitalOut SS(SDP_SPI_CS_A); |
MitchAD | 2:0897873979f3 | 161 | mbed::SPI spi(SDP_SPI_MOSI, SDP_SPI_MISO, SDP_SPI_SCK); |
MitchAD | 2:0897873979f3 | 162 | mbed::I2C i2c(SDP_I2C_SDA, SDP_I2C_SCL); |
MitchAD | 2:0897873979f3 | 163 | |
MitchAD | 2:0897873979f3 | 164 | /*Configure and instantiate UART protocol |
MitchAD | 2:0897873979f3 | 165 | and baud rate*/ |
MitchAD | 2:0897873979f3 | 166 | Serial port(USBTX, USBRX, BAUD_RATE); |
MitchAD | 2:0897873979f3 | 167 | |
MitchAD | 2:0897873979f3 | 168 | /***Global Variables***/ |
MitchAD | 2:0897873979f3 | 169 | uint8_t adc_conf = DEF_CONFIG; |
malavikasaji | 0:83a0c7bbe493 | 170 | |
MitchAD | 2:0897873979f3 | 171 | const char* conf_strings[] = |
MitchAD | 2:0897873979f3 | 172 | {"Default Configuration","Custom Configuration"}; |
MitchAD | 2:0897873979f3 | 173 | const char* power_strings[] = |
MitchAD | 2:0897873979f3 | 174 | {"Low Power Mode","Medium Power Mode","High Power Mode"}; |
MitchAD | 2:0897873979f3 | 175 | const char* filter_strings[] = |
MitchAD | 2:0897873979f3 | 176 | {"Sinc4","","Sinc3","","Fast Settling","Fast Settling + Sinc3","","Post"}; |
MitchAD | 2:0897873979f3 | 177 | |
MitchAD | 2:0897873979f3 | 178 | /***Menu Function Declarations***/ |
MitchAD | 2:0897873979f3 | 179 | static int8_t menu_config_select(struct ad7124_dev ** dev, int8_t sel_conf); |
MitchAD | 2:0897873979f3 | 180 | |
MitchAD | 2:0897873979f3 | 181 | static int8_t menu_assign_setup(struct ad7124_dev * dev); |
MitchAD | 2:0897873979f3 | 182 | |
MitchAD | 2:0897873979f3 | 183 | static int8_t menu_enable_disable_channels(struct ad7124_dev * dev); |
MitchAD | 2:0897873979f3 | 184 | |
MitchAD | 2:0897873979f3 | 185 | static int8_t menu_sample_channels(struct ad7124_dev * dev); |
MitchAD | 2:0897873979f3 | 186 | |
MitchAD | 2:0897873979f3 | 187 | static int8_t menu_select_gain(struct ad7124_dev * dev); |
MitchAD | 2:0897873979f3 | 188 | |
MitchAD | 2:0897873979f3 | 189 | static int8_t menu_select_filter_options(struct ad7124_dev * dev); |
MitchAD | 2:0897873979f3 | 190 | |
MitchAD | 2:0897873979f3 | 191 | static int8_t menu_select_calibration(struct ad7124_dev * dev); |
MitchAD | 2:0897873979f3 | 192 | |
MitchAD | 2:0897873979f3 | 193 | static int8_t menu_select_power_mode(struct ad7124_dev * dev); |
MitchAD | 2:0897873979f3 | 194 | |
MitchAD | 2:0897873979f3 | 195 | static int8_t menu_print_ID(struct ad7124_dev * dev); |
MitchAD | 2:0897873979f3 | 196 | |
MitchAD | 2:0897873979f3 | 197 | static int8_t menu_print_channel_register_map_view_mode(struct ad7124_dev * dev); |
MitchAD | 2:0897873979f3 | 198 | |
MitchAD | 2:0897873979f3 | 199 | static int8_t menu_print_channel_register_map_raw_mode(struct ad7124_dev * dev); |
MitchAD | 2:0897873979f3 | 200 | |
MitchAD | 2:0897873979f3 | 201 | static int8_t menu_print_setups(struct ad7124_dev * dev); |
MitchAD | 2:0897873979f3 | 202 | |
MitchAD | 2:0897873979f3 | 203 | /***Support Functions***/ |
MitchAD | 2:0897873979f3 | 204 | static void print_title(void); |
MitchAD | 2:0897873979f3 | 205 | |
MitchAD | 2:0897873979f3 | 206 | static void print_prompt(void); |
MitchAD | 2:0897873979f3 | 207 | |
MitchAD | 2:0897873979f3 | 208 | static int8_t fill_structs(struct ad7124_dev * dev); |
MitchAD | 2:0897873979f3 | 209 | |
MitchAD | 2:0897873979f3 | 210 | static int8_t write_device(struct ad7124_dev * dev); |
MitchAD | 2:0897873979f3 | 211 | |
MitchAD | 2:0897873979f3 | 212 | static int8_t update_write_reg(struct ad7124_dev * dev, uint8_t reg, uint32_t mask, uint32_t val); |
MitchAD | 2:0897873979f3 | 213 | |
MitchAD | 2:0897873979f3 | 214 | static void code_to_voltage(uint32_t code, uint8_t channel); |
malavikasaji | 0:83a0c7bbe493 | 215 | |
MitchAD | 2:0897873979f3 | 216 | static int8_t err_detect(int8_t ret); |
MitchAD | 2:0897873979f3 | 217 | |
MitchAD | 2:0897873979f3 | 218 | static int8_t setup_select(uint8_t *setup); |
MitchAD | 2:0897873979f3 | 219 | |
MitchAD | 2:0897873979f3 | 220 | static int8_t channel_select(uint8_t *channel); |
MitchAD | 2:0897873979f3 | 221 | |
MitchAD | 2:0897873979f3 | 222 | /*SPI Initialization Parameters*/ |
MitchAD | 2:0897873979f3 | 223 | spi_init_param spi_params = { |
MitchAD | 2:0897873979f3 | 224 | SPI_PLATFORM, |
MitchAD | 2:0897873979f3 | 225 | SPI_TYPE, |
MitchAD | 2:0897873979f3 | 226 | SPI_ID, |
MitchAD | 2:0897873979f3 | 227 | SPI_MAX_FREQUENCY, |
MitchAD | 2:0897873979f3 | 228 | SPI_MODE, |
MitchAD | 2:0897873979f3 | 229 | SDP_SPI_CS_A |
MitchAD | 2:0897873979f3 | 230 | }; |
MitchAD | 2:0897873979f3 | 231 | |
MitchAD | 2:0897873979f3 | 232 | /*Struct with individual channel information*/ |
MitchAD | 2:0897873979f3 | 233 | struct channel_setup { |
MitchAD | 2:0897873979f3 | 234 | uint8_t setup_id; |
MitchAD | 2:0897873979f3 | 235 | bool bipolar; |
MitchAD | 2:0897873979f3 | 236 | uint8_t gain; |
MitchAD | 2:0897873979f3 | 237 | uint8_t filter; |
MitchAD | 2:0897873979f3 | 238 | uint16_t data_rate; |
MitchAD | 2:0897873979f3 | 239 | }; |
MitchAD | 2:0897873979f3 | 240 | |
MitchAD | 2:0897873979f3 | 241 | /*Struct with individual setup information*/ |
MitchAD | 2:0897873979f3 | 242 | struct channel_info { |
MitchAD | 2:0897873979f3 | 243 | uint8_t power_mode; |
MitchAD | 2:0897873979f3 | 244 | bool enable; |
MitchAD | 2:0897873979f3 | 245 | float sample; |
MitchAD | 2:0897873979f3 | 246 | struct channel_setup *setup; |
MitchAD | 2:0897873979f3 | 247 | }; |
MitchAD | 2:0897873979f3 | 248 | |
MitchAD | 2:0897873979f3 | 249 | /*Declaring array of 16 channels and 8 setups*/ |
MitchAD | 2:0897873979f3 | 250 | struct channel_info channels[NUM_OF_CHANNELS]; |
MitchAD | 2:0897873979f3 | 251 | struct channel_setup setups[NUM_OF_SETUPS]; |
MitchAD | 2:0897873979f3 | 252 | |
MitchAD | 2:0897873979f3 | 253 | struct ad7124_dev * dev = NULL; /*Device Handler (AD7124)*/ |
MitchAD | 2:0897873979f3 | 254 | |
MitchAD | 2:0897873979f3 | 255 | /* Main function |
MitchAD | 2:0897873979f3 | 256 | * |
MitchAD | 2:0897873979f3 | 257 | * Parameters: None |
MitchAD | 2:0897873979f3 | 258 | * Return Value: SUCCESS(0), FAILURE (Negative) |
MitchAD | 2:0897873979f3 | 259 | */ |
malavikasaji | 0:83a0c7bbe493 | 260 | int main() |
malavikasaji | 0:83a0c7bbe493 | 261 | { |
MitchAD | 2:0897873979f3 | 262 | uint8_t user_command; |
MitchAD | 2:0897873979f3 | 263 | |
MitchAD | 2:0897873979f3 | 264 | /*Setup device handler and write register map to device from |
MitchAD | 2:0897873979f3 | 265 | the defailt configuration*/ |
MitchAD | 2:0897873979f3 | 266 | int8_t connected = menu_config_select(&dev, DEF_CONFIG); |
MitchAD | 2:0897873979f3 | 267 | |
MitchAD | 2:0897873979f3 | 268 | print_title(); |
MitchAD | 2:0897873979f3 | 269 | |
MitchAD | 2:0897873979f3 | 270 | while(err_detect(connected) != FAILURE) { |
MitchAD | 2:0897873979f3 | 271 | |
MitchAD | 2:0897873979f3 | 272 | print_prompt(); |
MitchAD | 2:0897873979f3 | 273 | port.scanf("%d", (int *) &user_command); |
MitchAD | 2:0897873979f3 | 274 | |
MitchAD | 2:0897873979f3 | 275 | switch (user_command) { |
MitchAD | 2:0897873979f3 | 276 | case 1: |
MitchAD | 2:0897873979f3 | 277 | menu_config_select(&dev, DEF_CONFIG); |
MitchAD | 2:0897873979f3 | 278 | break; |
MitchAD | 2:0897873979f3 | 279 | |
MitchAD | 2:0897873979f3 | 280 | case 2: |
MitchAD | 2:0897873979f3 | 281 | menu_config_select(&dev, CUSTOM_CONFIG); |
MitchAD | 2:0897873979f3 | 282 | break; |
MitchAD | 2:0897873979f3 | 283 | |
MitchAD | 2:0897873979f3 | 284 | case 3: |
MitchAD | 2:0897873979f3 | 285 | menu_assign_setup(dev); |
MitchAD | 2:0897873979f3 | 286 | break; |
MitchAD | 2:0897873979f3 | 287 | |
MitchAD | 2:0897873979f3 | 288 | case 4: |
MitchAD | 2:0897873979f3 | 289 | menu_enable_disable_channels(dev); |
MitchAD | 2:0897873979f3 | 290 | break; |
MitchAD | 2:0897873979f3 | 291 | |
MitchAD | 2:0897873979f3 | 292 | case 5: |
MitchAD | 2:0897873979f3 | 293 | menu_sample_channels(dev); |
MitchAD | 2:0897873979f3 | 294 | break; |
MitchAD | 2:0897873979f3 | 295 | |
MitchAD | 2:0897873979f3 | 296 | case 6: |
MitchAD | 2:0897873979f3 | 297 | menu_select_gain(dev); |
MitchAD | 2:0897873979f3 | 298 | break; |
MitchAD | 2:0897873979f3 | 299 | |
MitchAD | 2:0897873979f3 | 300 | case 7: |
MitchAD | 2:0897873979f3 | 301 | menu_select_filter_options(dev); |
MitchAD | 2:0897873979f3 | 302 | break; |
MitchAD | 2:0897873979f3 | 303 | |
MitchAD | 2:0897873979f3 | 304 | case 8: |
MitchAD | 2:0897873979f3 | 305 | menu_select_calibration(dev); |
MitchAD | 2:0897873979f3 | 306 | break; |
MitchAD | 2:0897873979f3 | 307 | |
MitchAD | 2:0897873979f3 | 308 | case 9: |
MitchAD | 2:0897873979f3 | 309 | menu_select_power_mode(dev); |
MitchAD | 2:0897873979f3 | 310 | break; |
MitchAD | 2:0897873979f3 | 311 | |
MitchAD | 2:0897873979f3 | 312 | case 10: |
MitchAD | 2:0897873979f3 | 313 | menu_print_ID(dev); |
MitchAD | 2:0897873979f3 | 314 | break; |
MitchAD | 2:0897873979f3 | 315 | |
MitchAD | 2:0897873979f3 | 316 | case 11: |
MitchAD | 2:0897873979f3 | 317 | menu_print_channel_register_map_view_mode(dev); |
MitchAD | 2:0897873979f3 | 318 | break; |
MitchAD | 2:0897873979f3 | 319 | |
MitchAD | 2:0897873979f3 | 320 | case 12: |
MitchAD | 2:0897873979f3 | 321 | menu_print_channel_register_map_raw_mode(dev); |
MitchAD | 2:0897873979f3 | 322 | break; |
MitchAD | 2:0897873979f3 | 323 | |
MitchAD | 2:0897873979f3 | 324 | case 13: |
MitchAD | 2:0897873979f3 | 325 | menu_print_setups(dev); |
MitchAD | 2:0897873979f3 | 326 | break; |
MitchAD | 2:0897873979f3 | 327 | |
MitchAD | 2:0897873979f3 | 328 | default: |
MitchAD | 2:0897873979f3 | 329 | port.printf("\t***Illegal Entry***\n\n"); |
MitchAD | 2:0897873979f3 | 330 | break; |
MitchAD | 2:0897873979f3 | 331 | } |
MitchAD | 2:0897873979f3 | 332 | } |
MitchAD | 2:0897873979f3 | 333 | return FAILURE; |
MitchAD | 2:0897873979f3 | 334 | } |
MitchAD | 2:0897873979f3 | 335 | |
MitchAD | 2:0897873979f3 | 336 | /***Function Definitions***/ |
MitchAD | 2:0897873979f3 | 337 | |
MitchAD | 2:0897873979f3 | 338 | /* Print title of the program |
MitchAD | 2:0897873979f3 | 339 | * |
MitchAD | 2:0897873979f3 | 340 | * Parameters: None |
MitchAD | 2:0897873979f3 | 341 | * Return Value: None |
MitchAD | 2:0897873979f3 | 342 | */ |
MitchAD | 2:0897873979f3 | 343 | static void print_title() |
MitchAD | 2:0897873979f3 | 344 | { |
MitchAD | 2:0897873979f3 | 345 | port.printf("\n*****************************************************************\n"); |
MitchAD | 2:0897873979f3 | 346 | port.printf("* EVAL-AD7124 Demonstration Program *\n"); |
MitchAD | 2:0897873979f3 | 347 | port.printf("* *\n"); |
MitchAD | 2:0897873979f3 | 348 | port.printf("* This program demonstrates how to interface and configure the *\n"); |
MitchAD | 2:0897873979f3 | 349 | port.printf("* AD7124 High-Precision Sigma-Delta ADC *\n"); |
MitchAD | 2:0897873979f3 | 350 | port.printf("* *\n"); |
MitchAD | 2:0897873979f3 | 351 | port.printf("* *\n"); |
MitchAD | 2:0897873979f3 | 352 | port.printf("* Set the baud rate to 115200 and select the newline terminator.*\n"); |
MitchAD | 2:0897873979f3 | 353 | port.printf("* *\n"); |
MitchAD | 2:0897873979f3 | 354 | port.printf("*******************************************************************\n"); |
MitchAD | 2:0897873979f3 | 355 | } |
MitchAD | 2:0897873979f3 | 356 | |
MitchAD | 2:0897873979f3 | 357 | /*Print command summary |
MitchAD | 2:0897873979f3 | 358 | * |
MitchAD | 2:0897873979f3 | 359 | *Parameters: None |
MitchAD | 2:0897873979f3 | 360 | *Return Value: None |
MitchAD | 2:0897873979f3 | 361 | */ |
MitchAD | 2:0897873979f3 | 362 | static void print_prompt() |
MitchAD | 2:0897873979f3 | 363 | { |
MitchAD | 2:0897873979f3 | 364 | port.printf("\n\n\tMain Menu Summary\n"); |
MitchAD | 2:0897873979f3 | 365 | port.printf("\tConfiguration Selected: %s\n", conf_strings[adc_conf]); |
MitchAD | 2:0897873979f3 | 366 | port.printf("\t=======================================\n"); |
MitchAD | 2:0897873979f3 | 367 | port.printf("\t 1 - Select Configuration (A) - Default\n"); |
MitchAD | 2:0897873979f3 | 368 | port.printf("\t 2 - Select Configuration (C) - Custom\n\n"); |
MitchAD | 2:0897873979f3 | 369 | port.printf("\t 3 - Assign Channel Setup\n"); |
MitchAD | 2:0897873979f3 | 370 | port.printf("\t 4 - Enable/Disable Channel\n\n"); |
MitchAD | 2:0897873979f3 | 371 | port.printf("\t 5 - Sample Channel\n"); |
MitchAD | 2:0897873979f3 | 372 | port.printf("\t 6 - Select Gain\n"); |
MitchAD | 2:0897873979f3 | 373 | port.printf("\t 7 - Select Filter/Filter Data Rate\n"); |
MitchAD | 2:0897873979f3 | 374 | port.printf("\t 8 - Select Calibration\n"); |
MitchAD | 2:0897873979f3 | 375 | port.printf("\t 9 - Select Power Mode\n\n"); |
MitchAD | 2:0897873979f3 | 376 | port.printf("\t 10 - Print Device ID\n"); |
MitchAD | 2:0897873979f3 | 377 | port.printf("\t 11 - Print Device Register Map (User Friendly Mode)\n"); |
MitchAD | 2:0897873979f3 | 378 | port.printf("\t 12 - Print Device Register Map (Raw Data Mode)\n"); |
MitchAD | 2:0897873979f3 | 379 | port.printf("\t 13 - Print Setup Information\n\n"); |
MitchAD | 2:0897873979f3 | 380 | } |
MitchAD | 2:0897873979f3 | 381 | |
MitchAD | 2:0897873979f3 | 382 | /* Switch between default and custom configurations |
MitchAD | 2:0897873979f3 | 383 | * |
MitchAD | 2:0897873979f3 | 384 | * Parameters: Double pointer to device handler, configuration selection variable |
MitchAD | 2:0897873979f3 | 385 | * Return Value: SUCCESS (0) , FAILURE (Negative) |
MitchAD | 2:0897873979f3 | 386 | */ |
MitchAD | 2:0897873979f3 | 387 | static int8_t menu_config_select(struct ad7124_dev ** dev, int8_t sel_conf) |
MitchAD | 2:0897873979f3 | 388 | { |
MitchAD | 2:0897873979f3 | 389 | int8_t ret; |
MitchAD | 2:0897873979f3 | 390 | |
MitchAD | 2:0897873979f3 | 391 | /*Free device handler*/ |
MitchAD | 2:0897873979f3 | 392 | ret = ad7124_remove(*dev); |
MitchAD | 2:0897873979f3 | 393 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 394 | return FAILURE; |
MitchAD | 2:0897873979f3 | 395 | |
MitchAD | 2:0897873979f3 | 396 | /*Use user selected register map (default, custom)*/ |
MitchAD | 2:0897873979f3 | 397 | struct ad7124_init_param ad7124_params = { |
MitchAD | 2:0897873979f3 | 398 | spi_params, |
MitchAD | 2:0897873979f3 | 399 | (sel_conf == DEF_CONFIG) ? ad7124_regs_default : ad7124_regs_custom, |
MitchAD | 2:0897873979f3 | 400 | SPI_POLL_COUNT |
MitchAD | 2:0897873979f3 | 401 | }; |
MitchAD | 2:0897873979f3 | 402 | |
MitchAD | 2:0897873979f3 | 403 | /*Setup device handler with corresponding register map*/ |
MitchAD | 2:0897873979f3 | 404 | ret = ad7124_setup(dev, ad7124_params); |
MitchAD | 2:0897873979f3 | 405 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 406 | return FAILURE; |
MitchAD | 2:0897873979f3 | 407 | |
MitchAD | 2:0897873979f3 | 408 | /*Fill up channel and setup structs with register map information*/ |
MitchAD | 2:0897873979f3 | 409 | ret = fill_structs(*dev); |
MitchAD | 2:0897873979f3 | 410 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 411 | return FAILURE; |
MitchAD | 2:0897873979f3 | 412 | |
MitchAD | 2:0897873979f3 | 413 | /*Switch global configuration variable*/ |
MitchAD | 2:0897873979f3 | 414 | adc_conf = (sel_conf == DEF_CONFIG) ? DEF_CONFIG : CUSTOM_CONFIG; |
MitchAD | 2:0897873979f3 | 415 | |
MitchAD | 2:0897873979f3 | 416 | return SUCCESS; |
MitchAD | 2:0897873979f3 | 417 | } |
MitchAD | 2:0897873979f3 | 418 | |
MitchAD | 2:0897873979f3 | 419 | /* Assign any channel to a given setup. This channel will then use that setup's |
MitchAD | 2:0897873979f3 | 420 | * features when it is sampled. This function continues to probe the user for |
MitchAD | 2:0897873979f3 | 421 | * more channels to change the setup of until the proper "main menu" command |
MitchAD | 2:0897873979f3 | 422 | * is input. |
MitchAD | 2:0897873979f3 | 423 | * |
MitchAD | 2:0897873979f3 | 424 | * Paremeters: Device handler |
MitchAD | 2:0897873979f3 | 425 | * Return Value: SUCCESS (0), FAILURE (Negative) |
MitchAD | 2:0897873979f3 | 426 | */ |
MitchAD | 2:0897873979f3 | 427 | static int8_t menu_assign_setup(struct ad7124_dev * dev) |
MitchAD | 2:0897873979f3 | 428 | { |
MitchAD | 2:0897873979f3 | 429 | uint8_t channel, setup; |
MitchAD | 2:0897873979f3 | 430 | int8_t ret; |
MitchAD | 2:0897873979f3 | 431 | |
MitchAD | 2:0897873979f3 | 432 | /*Fill all structs with register map information from device*/ |
MitchAD | 2:0897873979f3 | 433 | ret = fill_structs(dev); |
MitchAD | 2:0897873979f3 | 434 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 435 | return FAILURE; |
MitchAD | 2:0897873979f3 | 436 | |
MitchAD | 2:0897873979f3 | 437 | /*Continue until user wants to quit*/ |
MitchAD | 2:0897873979f3 | 438 | while (1) { |
MitchAD | 2:0897873979f3 | 439 | /*Obtain Channel*/ |
MitchAD | 2:0897873979f3 | 440 | port.printf("\n\tSelect Channel to Assign Setup\n"); |
MitchAD | 2:0897873979f3 | 441 | ret = channel_select(&channel); |
MitchAD | 2:0897873979f3 | 442 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 443 | return FAILURE; |
MitchAD | 2:0897873979f3 | 444 | |
MitchAD | 2:0897873979f3 | 445 | /*Obtain Setup*/ |
MitchAD | 2:0897873979f3 | 446 | port.printf("\n\tSelect Setup to Assign to Channel %d\n", channel); |
MitchAD | 2:0897873979f3 | 447 | ret = setup_select(&setup); |
MitchAD | 2:0897873979f3 | 448 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 449 | return FAILURE; |
MitchAD | 2:0897873979f3 | 450 | |
MitchAD | 2:0897873979f3 | 451 | /*Write back to device*/ |
MitchAD | 2:0897873979f3 | 452 | channels[channel].setup = &setups[setup]; |
MitchAD | 2:0897873979f3 | 453 | channels[channel].setup->setup_id = setup; |
MitchAD | 2:0897873979f3 | 454 | ret = write_device(dev); |
MitchAD | 2:0897873979f3 | 455 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 456 | return FAILURE; |
MitchAD | 2:0897873979f3 | 457 | } |
MitchAD | 2:0897873979f3 | 458 | } |
MitchAD | 2:0897873979f3 | 459 | |
MitchAD | 2:0897873979f3 | 460 | /* Offers the feature of enabling and disabling any channels that the user |
MitchAD | 2:0897873979f3 | 461 | * selects. It also continuously probes the user for more channels to |
MitchAD | 2:0897873979f3 | 462 | * enable/disable until the "main menu" command is input. |
MitchAD | 2:0897873979f3 | 463 | * |
MitchAD | 2:0897873979f3 | 464 | * Parameters: Device handler |
MitchAD | 2:0897873979f3 | 465 | * Return Value: SUCCESS(0), FAILURE (Negative) |
MitchAD | 2:0897873979f3 | 466 | */ |
MitchAD | 2:0897873979f3 | 467 | static int8_t menu_enable_disable_channels(struct ad7124_dev * dev) |
MitchAD | 2:0897873979f3 | 468 | { |
MitchAD | 2:0897873979f3 | 469 | uint8_t channel, enable; |
MitchAD | 2:0897873979f3 | 470 | int8_t ret; |
MitchAD | 2:0897873979f3 | 471 | |
MitchAD | 2:0897873979f3 | 472 | /*Fill all structs with register map information from device*/ |
MitchAD | 2:0897873979f3 | 473 | ret = fill_structs(dev); |
MitchAD | 2:0897873979f3 | 474 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 475 | return FAILURE; |
MitchAD | 2:0897873979f3 | 476 | |
MitchAD | 2:0897873979f3 | 477 | while (1) { |
MitchAD | 2:0897873979f3 | 478 | /*Obtain channel*/ |
MitchAD | 2:0897873979f3 | 479 | port.printf("\n\tSelect Channel to Enable/Disable\n"); |
MitchAD | 2:0897873979f3 | 480 | ret = channel_select(&channel); |
MitchAD | 2:0897873979f3 | 481 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 482 | return FAILURE; |
MitchAD | 2:0897873979f3 | 483 | |
MitchAD | 2:0897873979f3 | 484 | /*Obtain enable/disable command*/ |
MitchAD | 2:0897873979f3 | 485 | port.printf("\n\tChoose to Enable or Disable Channel %d\n", channel); |
MitchAD | 2:0897873979f3 | 486 | port.printf("\t===============================\n"); |
MitchAD | 2:0897873979f3 | 487 | port.printf("\t 1 - Disable\n"); |
MitchAD | 2:0897873979f3 | 488 | port.printf("\t 2 - Enable\n\n"); |
MitchAD | 2:0897873979f3 | 489 | port.printf("\t 3 - Main Menu\n\n"); |
MitchAD | 2:0897873979f3 | 490 | |
MitchAD | 2:0897873979f3 | 491 | port.scanf("%d", (int *) &enable); |
MitchAD | 2:0897873979f3 | 492 | if (enable > 2) |
MitchAD | 2:0897873979f3 | 493 | return FAILURE; |
MitchAD | 2:0897873979f3 | 494 | |
MitchAD | 2:0897873979f3 | 495 | /*Write back to device*/ |
MitchAD | 2:0897873979f3 | 496 | channels[channel].enable = enable - 1; |
MitchAD | 2:0897873979f3 | 497 | ret = write_device(dev); |
MitchAD | 2:0897873979f3 | 498 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 499 | return FAILURE; |
MitchAD | 2:0897873979f3 | 500 | } |
MitchAD | 2:0897873979f3 | 501 | } |
MitchAD | 2:0897873979f3 | 502 | |
MitchAD | 2:0897873979f3 | 503 | /* Sample all active channels and print out the corresponding voltages. The first |
MitchAD | 2:0897873979f3 | 504 | * option is single conversion mode where all the channels are sampled one after |
MitchAD | 2:0897873979f3 | 505 | * the other. The device is put into standby mode after. The next option is |
MitchAD | 2:0897873979f3 | 506 | * continuous conversion mode where all channels are sampled continuously. The user |
MitchAD | 2:0897873979f3 | 507 | * can press any key to stop the continuous sampling. |
MitchAD | 2:0897873979f3 | 508 | * |
MitchAD | 2:0897873979f3 | 509 | * Parameters: Device handler |
MitchAD | 2:0897873979f3 | 510 | * Return Value: SUCCESS (0) , FAILURE (Negative) |
MitchAD | 2:0897873979f3 | 511 | */ |
MitchAD | 2:0897873979f3 | 512 | static int8_t menu_sample_channels(struct ad7124_dev * dev) |
MitchAD | 2:0897873979f3 | 513 | { |
MitchAD | 2:0897873979f3 | 514 | struct ad7124_st_reg *regs; |
MitchAD | 2:0897873979f3 | 515 | uint32_t bitfield_mask, temp_val, ones_mask = 0xFFFFFFFF; |
MitchAD | 2:0897873979f3 | 516 | uint8_t conversion_mode, channel, previous_channel; |
MitchAD | 2:0897873979f3 | 517 | int8_t ret; |
MitchAD | 2:0897873979f3 | 518 | |
MitchAD | 2:0897873979f3 | 519 | regs = dev->regs; |
MitchAD | 2:0897873979f3 | 520 | |
MitchAD | 2:0897873979f3 | 521 | /*Fill up channel and setup structs with register map information*/ |
MitchAD | 2:0897873979f3 | 522 | ret = fill_structs(dev); |
MitchAD | 2:0897873979f3 | 523 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 524 | return FAILURE; |
MitchAD | 2:0897873979f3 | 525 | |
MitchAD | 2:0897873979f3 | 526 | port.printf("\n\tSelect Conversion Mode\n"); |
MitchAD | 2:0897873979f3 | 527 | port.printf("\t=======================================\n"); |
MitchAD | 2:0897873979f3 | 528 | port.printf("\t 1 - Single Conversion Mode\n"); |
MitchAD | 2:0897873979f3 | 529 | port.printf("\t 2 - Continuous Conversion Mode\n"); |
MitchAD | 2:0897873979f3 | 530 | port.printf("\t 3 - Main Menu\n\n"); |
MitchAD | 2:0897873979f3 | 531 | |
MitchAD | 2:0897873979f3 | 532 | /*Obtain user input and check validity*/ |
MitchAD | 2:0897873979f3 | 533 | port.scanf("%d", (int *) &conversion_mode); |
MitchAD | 2:0897873979f3 | 534 | if ((conversion_mode < 1) || (conversion_mode > 2)) |
MitchAD | 2:0897873979f3 | 535 | return FAILURE; |
MitchAD | 2:0897873979f3 | 536 | |
MitchAD | 2:0897873979f3 | 537 | /*Update ADC operating mode to single/continous conversion mode*/ |
MitchAD | 2:0897873979f3 | 538 | bitfield_mask = AD7124_ADC_CTRL_REG_MODE(ones_mask); |
MitchAD | 2:0897873979f3 | 539 | temp_val = (conversion_mode == SINGLE_CONV_MODE) ? AD7124_ADC_CTRL_REG_MODE(SINGLE_CONV_MODE): |
MitchAD | 2:0897873979f3 | 540 | AD7124_ADC_CTRL_REG_MODE(CONTINOUS_CONV_MODE); |
MitchAD | 2:0897873979f3 | 541 | ret = update_write_reg(dev, AD7124_ADC_Control, bitfield_mask, temp_val); |
MitchAD | 2:0897873979f3 | 542 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 543 | return FAILURE; |
MitchAD | 2:0897873979f3 | 544 | |
MitchAD | 2:0897873979f3 | 545 | /*Continue to sample*/ |
MitchAD | 2:0897873979f3 | 546 | while (1) { |
MitchAD | 2:0897873979f3 | 547 | |
MitchAD | 2:0897873979f3 | 548 | /*Wait for conversion complete, then obtain sample*/ |
MitchAD | 2:0897873979f3 | 549 | ad7124_wait_for_conv_ready(dev, dev->spi_rdy_poll_cnt); |
MitchAD | 2:0897873979f3 | 550 | ret = ad7124_read_register(dev, ®s[AD7124_Data]); |
MitchAD | 2:0897873979f3 | 551 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 552 | return FAILURE; |
MitchAD | 2:0897873979f3 | 553 | |
MitchAD | 2:0897873979f3 | 554 | /*Save previous channel*/ |
MitchAD | 2:0897873979f3 | 555 | previous_channel = channel; |
MitchAD | 2:0897873979f3 | 556 | |
MitchAD | 2:0897873979f3 | 557 | /*Read status register to find out which channel was sampled*/ |
MitchAD | 2:0897873979f3 | 558 | ret = ad7124_read_register(dev, ®s[AD7124_Status]); |
MitchAD | 2:0897873979f3 | 559 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 560 | return FAILURE; |
MitchAD | 2:0897873979f3 | 561 | |
MitchAD | 2:0897873979f3 | 562 | channel = AD7124_STATUS_REG_CH_ACTIVE(regs[AD7124_Status].value); |
MitchAD | 2:0897873979f3 | 563 | |
MitchAD | 2:0897873979f3 | 564 | /*Print out samples if a new channel was sampled*/ |
MitchAD | 2:0897873979f3 | 565 | if (channel != previous_channel) { |
MitchAD | 2:0897873979f3 | 566 | code_to_voltage(regs[AD7124_Data].value, channel); |
MitchAD | 2:0897873979f3 | 567 | port.printf("Channel %d: %f\n", channel, channels[channel].sample); |
MitchAD | 2:0897873979f3 | 568 | } |
MitchAD | 2:0897873979f3 | 569 | |
MitchAD | 2:0897873979f3 | 570 | /*In all modes, print out conversions until user says stop*/ |
MitchAD | 2:0897873979f3 | 571 | if (!port.readable()) |
MitchAD | 2:0897873979f3 | 572 | return SUCCESS; |
malavikasaji | 0:83a0c7bbe493 | 573 | } |
malavikasaji | 0:83a0c7bbe493 | 574 | } |
malavikasaji | 0:83a0c7bbe493 | 575 | |
MitchAD | 2:0897873979f3 | 576 | /* Updates the gain of the setup selected. |
MitchAD | 2:0897873979f3 | 577 | * |
MitchAD | 2:0897873979f3 | 578 | * Parameters: Device handler |
MitchAD | 2:0897873979f3 | 579 | * Return Value: SUCCESS (0), FAILURE (Negative) |
MitchAD | 2:0897873979f3 | 580 | */ |
MitchAD | 2:0897873979f3 | 581 | static int8_t menu_select_gain(struct ad7124_dev * dev) |
MitchAD | 2:0897873979f3 | 582 | { |
MitchAD | 2:0897873979f3 | 583 | uint8_t setup, gain; |
MitchAD | 2:0897873979f3 | 584 | int8_t ret; |
MitchAD | 2:0897873979f3 | 585 | |
MitchAD | 2:0897873979f3 | 586 | /*Fill all structs with register map information from device*/ |
MitchAD | 2:0897873979f3 | 587 | ret = fill_structs(dev); |
MitchAD | 2:0897873979f3 | 588 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 589 | return FAILURE; |
MitchAD | 2:0897873979f3 | 590 | |
MitchAD | 2:0897873979f3 | 591 | port.printf("Select Setup to Change the Gain of\n"); |
MitchAD | 2:0897873979f3 | 592 | ret = setup_select(&setup); |
MitchAD | 2:0897873979f3 | 593 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 594 | return FAILURE; |
MitchAD | 2:0897873979f3 | 595 | |
MitchAD | 2:0897873979f3 | 596 | port.printf("Select the Gain to Assign to Setup %d\n", setup); |
MitchAD | 2:0897873979f3 | 597 | port.printf("\t=======================================\n"); |
MitchAD | 2:0897873979f3 | 598 | port.printf("\t 1 - Gain 1\n"); |
MitchAD | 2:0897873979f3 | 599 | port.printf("\t 2 - Gain 2\n"); |
MitchAD | 2:0897873979f3 | 600 | port.printf("\t 3 - Gain 4\n"); |
MitchAD | 2:0897873979f3 | 601 | port.printf("\t 4 - Gain 8\n"); |
MitchAD | 2:0897873979f3 | 602 | port.printf("\t 5 - Gain 16\n"); |
MitchAD | 2:0897873979f3 | 603 | port.printf("\t 6 - Gain 32\n"); |
MitchAD | 2:0897873979f3 | 604 | port.printf("\t 7 - Gain 64\n"); |
MitchAD | 2:0897873979f3 | 605 | port.printf("\t 8 - Gain 128\n"); |
MitchAD | 2:0897873979f3 | 606 | port.printf("\t 9 - Main Menu\n\n"); |
MitchAD | 2:0897873979f3 | 607 | |
MitchAD | 2:0897873979f3 | 608 | port.scanf("%d", (int *) &gain); |
MitchAD | 2:0897873979f3 | 609 | if (gain > 8) |
MitchAD | 2:0897873979f3 | 610 | return FAILURE; |
MitchAD | 2:0897873979f3 | 611 | |
MitchAD | 2:0897873979f3 | 612 | /*Write to device*/ |
MitchAD | 2:0897873979f3 | 613 | setups[setup].gain = gain-1; |
MitchAD | 2:0897873979f3 | 614 | ret = write_device(dev); |
MitchAD | 2:0897873979f3 | 615 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 616 | return FAILURE; |
MitchAD | 2:0897873979f3 | 617 | |
MitchAD | 2:0897873979f3 | 618 | return SUCCESS; |
MitchAD | 2:0897873979f3 | 619 | } |
MitchAD | 2:0897873979f3 | 620 | |
MitchAD | 2:0897873979f3 | 621 | /* Updates the filter options of the ADC for a given setup. The user selects |
MitchAD | 2:0897873979f3 | 622 | * the filter and the data rate of the filter. Also updates the data rate of the |
MitchAD | 2:0897873979f3 | 623 | * filter selected. |
MitchAD | 2:0897873979f3 | 624 | * |
MitchAD | 2:0897873979f3 | 625 | * Parameters: Device handler |
MitchAD | 2:0897873979f3 | 626 | * Return Value: SUCCESS (0), FAILURE (Negative) |
MitchAD | 2:0897873979f3 | 627 | */ |
MitchAD | 2:0897873979f3 | 628 | static int8_t menu_select_filter_options(struct ad7124_dev * dev) |
MitchAD | 2:0897873979f3 | 629 | { |
MitchAD | 2:0897873979f3 | 630 | uint8_t setup, filter; |
MitchAD | 2:0897873979f3 | 631 | uint16_t data_rate; |
MitchAD | 2:0897873979f3 | 632 | int8_t ret; |
MitchAD | 2:0897873979f3 | 633 | |
MitchAD | 2:0897873979f3 | 634 | /*Fill all structs with register map information from device*/ |
MitchAD | 2:0897873979f3 | 635 | ret = fill_structs(dev); |
MitchAD | 2:0897873979f3 | 636 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 637 | return FAILURE; |
MitchAD | 2:0897873979f3 | 638 | |
MitchAD | 2:0897873979f3 | 639 | port.printf("Select Setup to Change the Filter of\n"); |
MitchAD | 2:0897873979f3 | 640 | ret = setup_select(&setup); |
MitchAD | 2:0897873979f3 | 641 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 642 | return FAILURE; |
MitchAD | 2:0897873979f3 | 643 | |
MitchAD | 2:0897873979f3 | 644 | port.printf("\n\tSelect Filter\n"); |
MitchAD | 2:0897873979f3 | 645 | port.printf("\t=======================================\n"); |
MitchAD | 2:0897873979f3 | 646 | port.printf("\t 1 - Sinc4\n"); |
MitchAD | 2:0897873979f3 | 647 | port.printf("\t 2 - Sinc3\n"); |
MitchAD | 2:0897873979f3 | 648 | port.printf("\t 3 - Fast Settling\n"); |
MitchAD | 2:0897873979f3 | 649 | port.printf("\t 4 - Fast Settling + Sinc3\n"); |
MitchAD | 2:0897873979f3 | 650 | port.printf("\t 5 - Post\n"); |
MitchAD | 2:0897873979f3 | 651 | port.printf("\t 6 - Main Menu\n\n"); |
MitchAD | 2:0897873979f3 | 652 | |
MitchAD | 2:0897873979f3 | 653 | port.scanf("%d", (int *) &filter); |
MitchAD | 2:0897873979f3 | 654 | if ((filter < 1) || (filter > 5)) |
MitchAD | 2:0897873979f3 | 655 | return FAILURE; |
MitchAD | 2:0897873979f3 | 656 | |
MitchAD | 2:0897873979f3 | 657 | switch(filter) { |
MitchAD | 2:0897873979f3 | 658 | case 1: |
MitchAD | 2:0897873979f3 | 659 | setups[setup].filter = SINC4; |
MitchAD | 2:0897873979f3 | 660 | break; |
MitchAD | 2:0897873979f3 | 661 | |
MitchAD | 2:0897873979f3 | 662 | case 2: |
MitchAD | 2:0897873979f3 | 663 | setups[setup].filter = SINC3; |
MitchAD | 2:0897873979f3 | 664 | break; |
MitchAD | 2:0897873979f3 | 665 | |
MitchAD | 2:0897873979f3 | 666 | case 3: |
MitchAD | 2:0897873979f3 | 667 | setups[setup].filter = FAST_SETTLING; |
MitchAD | 2:0897873979f3 | 668 | break; |
MitchAD | 2:0897873979f3 | 669 | |
MitchAD | 2:0897873979f3 | 670 | case 4: |
MitchAD | 2:0897873979f3 | 671 | setups[setup].filter = FAST_SETTLING_SINC3; |
MitchAD | 2:0897873979f3 | 672 | break; |
MitchAD | 2:0897873979f3 | 673 | |
MitchAD | 2:0897873979f3 | 674 | case 5: |
MitchAD | 2:0897873979f3 | 675 | setups[setup].filter = POST; |
MitchAD | 2:0897873979f3 | 676 | break; |
MitchAD | 2:0897873979f3 | 677 | |
MitchAD | 2:0897873979f3 | 678 | default: |
MitchAD | 2:0897873979f3 | 679 | port.printf("\t***Illegal Entry***\n\n"); |
MitchAD | 2:0897873979f3 | 680 | return FAILURE; |
MitchAD | 2:0897873979f3 | 681 | } |
MitchAD | 2:0897873979f3 | 682 | |
MitchAD | 2:0897873979f3 | 683 | /*Write back to device*/ |
MitchAD | 2:0897873979f3 | 684 | ret = write_device(dev); |
MitchAD | 2:0897873979f3 | 685 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 686 | return FAILURE; |
MitchAD | 2:0897873979f3 | 687 | |
MitchAD | 2:0897873979f3 | 688 | port.printf("Select Data Rate for the Filter\n"); |
MitchAD | 2:0897873979f3 | 689 | port.printf("\t=======================================\n"); |
MitchAD | 2:0897873979f3 | 690 | port.printf("\t (1-2047)\n"); |
MitchAD | 2:0897873979f3 | 691 | port.printf("\t 2048 - Main Menu\n"); |
MitchAD | 2:0897873979f3 | 692 | |
MitchAD | 2:0897873979f3 | 693 | port.scanf("%d", (int *) &data_rate); |
MitchAD | 2:0897873979f3 | 694 | if ((data_rate < 1) || (data_rate > 2047)) |
MitchAD | 2:0897873979f3 | 695 | return FAILURE; |
MitchAD | 2:0897873979f3 | 696 | |
MitchAD | 2:0897873979f3 | 697 | /*Write back to device*/ |
MitchAD | 2:0897873979f3 | 698 | setups[setup].data_rate = data_rate; |
MitchAD | 2:0897873979f3 | 699 | ret = write_device(dev); |
MitchAD | 2:0897873979f3 | 700 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 701 | return FAILURE; |
MitchAD | 2:0897873979f3 | 702 | |
MitchAD | 2:0897873979f3 | 703 | return SUCCESS; |
MitchAD | 2:0897873979f3 | 704 | } |
MitchAD | 2:0897873979f3 | 705 | |
MitchAD | 2:0897873979f3 | 706 | /* Select one of the four claibration modes of the ADC. There are specific |
MitchAD | 2:0897873979f3 | 707 | * things that the user must do before selecting any of the calibrations. |
MitchAD | 2:0897873979f3 | 708 | * These are outlined on page 53 of the data sheet. |
MitchAD | 2:0897873979f3 | 709 | * |
MitchAD | 2:0897873979f3 | 710 | * Parameters: Device handler |
MitchAD | 2:0897873979f3 | 711 | * Return Value: SUCCESS (0), FAILURE (Negative) |
MitchAD | 2:0897873979f3 | 712 | */ |
MitchAD | 2:0897873979f3 | 713 | static int8_t menu_select_calibration(struct ad7124_dev * dev) |
MitchAD | 2:0897873979f3 | 714 | { |
MitchAD | 2:0897873979f3 | 715 | uint32_t bitfield_mask, temp_val, ones_mask = 0xFFFFFFFF; |
MitchAD | 2:0897873979f3 | 716 | uint8_t channel, calibration; |
MitchAD | 2:0897873979f3 | 717 | bool active_channels[NUM_OF_CHANNELS]; |
MitchAD | 2:0897873979f3 | 718 | int8_t ret; |
MitchAD | 2:0897873979f3 | 719 | |
MitchAD | 2:0897873979f3 | 720 | ret = channel_select(&channel); |
MitchAD | 2:0897873979f3 | 721 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 722 | return FAILURE; |
MitchAD | 2:0897873979f3 | 723 | |
MitchAD | 2:0897873979f3 | 724 | port.printf("\n\tSelect Calibration Mode\n"); |
MitchAD | 2:0897873979f3 | 725 | port.printf("\t=======================================\n"); |
MitchAD | 2:0897873979f3 | 726 | port.printf("\t 1 - Internal Zero Scale\n"); |
MitchAD | 2:0897873979f3 | 727 | port.printf("\t 2 - Internal Full Scale\n"); |
MitchAD | 2:0897873979f3 | 728 | port.printf("\t 3 - System Zero Scale\n"); |
MitchAD | 2:0897873979f3 | 729 | port.printf("\t 4 - System Full Scale\n"); |
MitchAD | 2:0897873979f3 | 730 | port.printf("\t 5 - Main Menu\n\n"); |
MitchAD | 2:0897873979f3 | 731 | |
MitchAD | 2:0897873979f3 | 732 | port.scanf("%d", (int *) &calibration); |
MitchAD | 2:0897873979f3 | 733 | if ((calibration < 1) || (calibration > 4)) |
MitchAD | 2:0897873979f3 | 734 | return FAILURE; |
MitchAD | 2:0897873979f3 | 735 | |
MitchAD | 2:0897873979f3 | 736 | /*Disable all channels except for one being calibrated*/ |
MitchAD | 2:0897873979f3 | 737 | for (int channel_index = 0; channel_index < NUM_OF_CHANNELS; channel_index++) { |
MitchAD | 2:0897873979f3 | 738 | if (channel != channel_index) |
MitchAD | 2:0897873979f3 | 739 | if (channels[channel_index].enable == true) |
MitchAD | 2:0897873979f3 | 740 | active_channels[channel_index] = true; |
MitchAD | 2:0897873979f3 | 741 | channels[channel_index].enable = false; |
MitchAD | 2:0897873979f3 | 742 | } |
MitchAD | 2:0897873979f3 | 743 | ret = write_device(dev); |
MitchAD | 2:0897873979f3 | 744 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 745 | return FAILURE; |
MitchAD | 2:0897873979f3 | 746 | |
MitchAD | 2:0897873979f3 | 747 | /*Put into low power mode*/ |
MitchAD | 2:0897873979f3 | 748 | bitfield_mask = AD7124_ADC_CTRL_REG_POWER_MODE(ones_mask); |
MitchAD | 2:0897873979f3 | 749 | temp_val = AD7124_ADC_CTRL_REG_POWER_MODE(LOW_POWER_MODE); |
MitchAD | 2:0897873979f3 | 750 | ret = update_write_reg(dev, AD7124_ADC_Control, bitfield_mask, temp_val); |
MitchAD | 2:0897873979f3 | 751 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 752 | return FAILURE; |
MitchAD | 2:0897873979f3 | 753 | |
MitchAD | 2:0897873979f3 | 754 | /*Put into standy mode*/ |
MitchAD | 2:0897873979f3 | 755 | bitfield_mask = AD7124_ADC_CTRL_REG_MODE(ones_mask); |
MitchAD | 2:0897873979f3 | 756 | temp_val = AD7124_ADC_CTRL_REG_MODE(LOW_POWER_MODE); |
MitchAD | 2:0897873979f3 | 757 | ret = update_write_reg(dev, AD7124_ADC_Control, bitfield_mask, temp_val); |
MitchAD | 2:0897873979f3 | 758 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 759 | return FAILURE; |
MitchAD | 2:0897873979f3 | 760 | |
MitchAD | 2:0897873979f3 | 761 | /*Switch based off calibration mode selected*/ |
MitchAD | 2:0897873979f3 | 762 | bitfield_mask = AD7124_ADC_CTRL_REG_MODE(ones_mask); |
MitchAD | 2:0897873979f3 | 763 | switch(calibration) { |
MitchAD | 2:0897873979f3 | 764 | case 1: |
MitchAD | 2:0897873979f3 | 765 | temp_val = AD7124_ADC_CTRL_REG_MODE(INTERNAL_ZERO_SCALE); |
MitchAD | 2:0897873979f3 | 766 | break; |
MitchAD | 2:0897873979f3 | 767 | |
MitchAD | 2:0897873979f3 | 768 | case 2: |
MitchAD | 2:0897873979f3 | 769 | update_write_reg(dev, channels[channel].setup->setup_id + AD7124_Gain_0, ones_mask, 0x800000); |
MitchAD | 2:0897873979f3 | 770 | temp_val = AD7124_ADC_CTRL_REG_MODE(INTERNAL_FULL_SCALE); |
MitchAD | 2:0897873979f3 | 771 | break; |
MitchAD | 2:0897873979f3 | 772 | |
MitchAD | 2:0897873979f3 | 773 | case 3: |
MitchAD | 2:0897873979f3 | 774 | temp_val = AD7124_ADC_CTRL_REG_MODE(SYSTEM_ZERO_SCALE); |
MitchAD | 2:0897873979f3 | 775 | break; |
MitchAD | 2:0897873979f3 | 776 | |
MitchAD | 2:0897873979f3 | 777 | case 4: |
MitchAD | 2:0897873979f3 | 778 | temp_val = AD7124_ADC_CTRL_REG_MODE(SYSTEM_FULL_SCALE); |
MitchAD | 2:0897873979f3 | 779 | break; |
MitchAD | 2:0897873979f3 | 780 | |
MitchAD | 2:0897873979f3 | 781 | default: |
MitchAD | 2:0897873979f3 | 782 | port.printf("\t***Illegal Entry***\n\n"); |
MitchAD | 2:0897873979f3 | 783 | return FAILURE; |
MitchAD | 2:0897873979f3 | 784 | } |
MitchAD | 2:0897873979f3 | 785 | ret = update_write_reg(dev, AD7124_ADC_Control, bitfield_mask, temp_val); |
MitchAD | 2:0897873979f3 | 786 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 787 | return FAILURE; |
MitchAD | 2:0897873979f3 | 788 | |
MitchAD | 2:0897873979f3 | 789 | ad7124_wait_for_conv_ready(dev, SPI_POLL_COUNT); |
MitchAD | 2:0897873979f3 | 790 | port.printf("\tSuccessfully Calibrated\n\n"); |
MitchAD | 2:0897873979f3 | 791 | wait(1); |
MitchAD | 2:0897873979f3 | 792 | |
MitchAD | 2:0897873979f3 | 793 | /*Re-activate disabled channels*/ |
MitchAD | 2:0897873979f3 | 794 | for (int channel_index = 0; channel_index < NUM_OF_CHANNELS; channel_index++) { |
MitchAD | 2:0897873979f3 | 795 | if (active_channels[channel_index] == true) |
MitchAD | 2:0897873979f3 | 796 | channels[channel_index].enable = true; |
MitchAD | 2:0897873979f3 | 797 | } |
MitchAD | 2:0897873979f3 | 798 | ret = write_device(dev); |
MitchAD | 2:0897873979f3 | 799 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 800 | return FAILURE; |
MitchAD | 2:0897873979f3 | 801 | |
MitchAD | 2:0897873979f3 | 802 | port.printf("\tSuccess\n"); |
MitchAD | 2:0897873979f3 | 803 | return SUCCESS; |
MitchAD | 2:0897873979f3 | 804 | } |
MitchAD | 2:0897873979f3 | 805 | |
MitchAD | 2:0897873979f3 | 806 | /* Choose the power mode of the device |
MitchAD | 2:0897873979f3 | 807 | * |
MitchAD | 2:0897873979f3 | 808 | * Parameters: Device handler |
MitchAD | 2:0897873979f3 | 809 | * Return Value: SUCCESS (0), FAILURE (Negative) |
MitchAD | 2:0897873979f3 | 810 | */ |
MitchAD | 2:0897873979f3 | 811 | static int8_t menu_select_power_mode(struct ad7124_dev * dev) |
MitchAD | 2:0897873979f3 | 812 | { |
MitchAD | 2:0897873979f3 | 813 | uint8_t power_mode; |
MitchAD | 2:0897873979f3 | 814 | int8_t ret; |
MitchAD | 2:0897873979f3 | 815 | |
MitchAD | 2:0897873979f3 | 816 | /*Fill all structs with register map information from device*/ |
MitchAD | 2:0897873979f3 | 817 | ret = fill_structs(dev); |
MitchAD | 2:0897873979f3 | 818 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 819 | return FAILURE; |
MitchAD | 2:0897873979f3 | 820 | |
MitchAD | 2:0897873979f3 | 821 | port.printf("\n\tSelect Filter\n"); |
MitchAD | 2:0897873979f3 | 822 | port.printf("\t=======================================\n"); |
MitchAD | 2:0897873979f3 | 823 | port.printf("\t 1 - Low Power Mode\n"); |
MitchAD | 2:0897873979f3 | 824 | port.printf("\t 2 - Medium Power Mode\n"); |
MitchAD | 2:0897873979f3 | 825 | port.printf("\t 3 - High Power Mode\n"); |
MitchAD | 2:0897873979f3 | 826 | port.printf("\t 4 - Main Menu\n"); |
MitchAD | 2:0897873979f3 | 827 | |
MitchAD | 2:0897873979f3 | 828 | port.scanf("%d", (int *) &power_mode); |
MitchAD | 2:0897873979f3 | 829 | if ((power_mode < 1) || (power_mode > 3)) |
MitchAD | 2:0897873979f3 | 830 | return FAILURE; |
MitchAD | 2:0897873979f3 | 831 | |
MitchAD | 2:0897873979f3 | 832 | /*Write to device*/ |
MitchAD | 2:0897873979f3 | 833 | channels[0].power_mode = power_mode - 1; |
MitchAD | 2:0897873979f3 | 834 | ret = write_device(dev); |
MitchAD | 2:0897873979f3 | 835 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 836 | return FAILURE; |
MitchAD | 2:0897873979f3 | 837 | |
MitchAD | 2:0897873979f3 | 838 | return SUCCESS; |
MitchAD | 2:0897873979f3 | 839 | } |
MitchAD | 2:0897873979f3 | 840 | |
MitchAD | 2:0897873979f3 | 841 | /* Print out the device ID. This is helpful for debugging SPI serial |
MitchAD | 2:0897873979f3 | 842 | * communication with the ADC |
MitchAD | 2:0897873979f3 | 843 | * |
MitchAD | 2:0897873979f3 | 844 | * Parameters: Device handler |
MitchAD | 2:0897873979f3 | 845 | * Return Value: SUCCESS (0), FAILURE (Negative) |
MitchAD | 2:0897873979f3 | 846 | */ |
MitchAD | 2:0897873979f3 | 847 | static int8_t menu_print_ID(struct ad7124_dev * dev) |
MitchAD | 2:0897873979f3 | 848 | { |
MitchAD | 2:0897873979f3 | 849 | struct ad7124_st_reg *regs; |
MitchAD | 2:0897873979f3 | 850 | int8_t ret; |
MitchAD | 2:0897873979f3 | 851 | |
MitchAD | 2:0897873979f3 | 852 | regs = dev->regs; |
MitchAD | 2:0897873979f3 | 853 | |
MitchAD | 2:0897873979f3 | 854 | ret = ad7124_read_register(dev, ®s[AD7124_ID]); |
MitchAD | 2:0897873979f3 | 855 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 856 | return FAILURE; |
MitchAD | 2:0897873979f3 | 857 | |
MitchAD | 2:0897873979f3 | 858 | port.printf("\tSuccessfully Read Device ID\n"); |
MitchAD | 2:0897873979f3 | 859 | port.printf("\tID: %#x\n", regs[AD7124_ID].value); |
MitchAD | 2:0897873979f3 | 860 | |
MitchAD | 2:0897873979f3 | 861 | return SUCCESS; |
MitchAD | 2:0897873979f3 | 862 | } |
MitchAD | 2:0897873979f3 | 863 | |
MitchAD | 2:0897873979f3 | 864 | /* Updates a specific bitfield for a particular register. This function allows |
MitchAD | 2:0897873979f3 | 865 | * for a register to only be altered in the area that is required |
MitchAD | 2:0897873979f3 | 866 | * |
MitchAD | 2:0897873979f3 | 867 | * Example to change power mode from low to medium power: |
MitchAD | 2:0897873979f3 | 868 | * |
MitchAD | 2:0897873979f3 | 869 | * (Mask of all ones) |
MitchAD | 2:0897873979f3 | 870 | * ones_mask = 0xFFFFFFFF; |
MitchAD | 2:0897873979f3 | 871 | * |
MitchAD | 2:0897873979f3 | 872 | * (Isolate power mode area) |
MitchAD | 2:0897873979f3 | 873 | * bitfield_mask = AD7124_ADC_CTRL_REG_POWER_MODE(ones_mask); |
MitchAD | 2:0897873979f3 | 874 | * |
MitchAD | 2:0897873979f3 | 875 | * (Provide new value) |
MitchAD | 2:0897873979f3 | 876 | * temp_val = AD7124_ADC_CTRL_REG_POWER_MODE(MED_POWER_MODE); |
MitchAD | 2:0897873979f3 | 877 | * |
MitchAD | 2:0897873979f3 | 878 | * (Update Value) |
MitchAD | 2:0897873979f3 | 879 | * update_write_reg(dev, AD7124_ADC_Control, bitfield_mask, temp_val; |
MitchAD | 2:0897873979f3 | 880 | * |
MitchAD | 2:0897873979f3 | 881 | * Parameters: Device handler, register, bitfield area mask (ones), value to be written |
MitchAD | 2:0897873979f3 | 882 | * Return Value: SUCCESS (0), FAILURE (Negative) |
MitchAD | 2:0897873979f3 | 883 | */ |
MitchAD | 2:0897873979f3 | 884 | static int8_t update_write_reg(struct ad7124_dev * dev, |
MitchAD | 2:0897873979f3 | 885 | uint8_t reg, uint32_t mask, uint32_t val) |
MitchAD | 2:0897873979f3 | 886 | { |
MitchAD | 2:0897873979f3 | 887 | struct ad7124_st_reg *regs; |
MitchAD | 2:0897873979f3 | 888 | int8_t ret; |
MitchAD | 2:0897873979f3 | 889 | |
MitchAD | 2:0897873979f3 | 890 | regs = dev->regs; |
MitchAD | 2:0897873979f3 | 891 | |
MitchAD | 2:0897873979f3 | 892 | ret = ad7124_read_register(dev, ®s[reg]); |
MitchAD | 2:0897873979f3 | 893 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 894 | return FAILURE; |
MitchAD | 2:0897873979f3 | 895 | |
MitchAD | 2:0897873979f3 | 896 | regs[reg].value = (regs[reg].value & ~mask) | (val & mask); |
MitchAD | 2:0897873979f3 | 897 | ret = ad7124_write_register(dev, regs[reg]); |
MitchAD | 2:0897873979f3 | 898 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 899 | return FAILURE; |
MitchAD | 2:0897873979f3 | 900 | |
MitchAD | 2:0897873979f3 | 901 | return SUCCESS; |
MitchAD | 2:0897873979f3 | 902 | } |
MitchAD | 2:0897873979f3 | 903 | |
MitchAD | 2:0897873979f3 | 904 | /* Converts a single channel's sample from the ADC into voltage based off |
MitchAD | 2:0897873979f3 | 905 | * neccessary information about the setup associated with that channel |
MitchAD | 2:0897873979f3 | 906 | * |
MitchAD | 2:0897873979f3 | 907 | * Parameters: code from ADC, channel being sampled |
MitchAD | 2:0897873979f3 | 908 | * Return Value: None |
MitchAD | 2:0897873979f3 | 909 | */ |
MitchAD | 2:0897873979f3 | 910 | static void code_to_voltage(uint32_t code, uint8_t channel) |
MitchAD | 2:0897873979f3 | 911 | { |
MitchAD | 2:0897873979f3 | 912 | uint8_t gain = 1 << (channels[channel].setup->gain); |
MitchAD | 2:0897873979f3 | 913 | uint8_t bipolar = channels[channel].setup->bipolar; |
MitchAD | 2:0897873979f3 | 914 | |
MitchAD | 2:0897873979f3 | 915 | if (bipolar) |
MitchAD | 2:0897873979f3 | 916 | channels[channel].sample = (((float)(code)/(1 << (NUM_BITS-1)))-1)*(2.5*gain); |
MitchAD | 2:0897873979f3 | 917 | else if (!bipolar) |
MitchAD | 2:0897873979f3 | 918 | channels[channel].sample = (float)code*VREF/((1 << NUM_BITS)*gain); |
MitchAD | 2:0897873979f3 | 919 | } |
MitchAD | 2:0897873979f3 | 920 | |
MitchAD | 2:0897873979f3 | 921 | /* Prints out all channel struct information in a user friendly format after |
MitchAD | 2:0897873979f3 | 922 | * reading the register map from the ADC. It only prints out channel member |
MitchAD | 2:0897873979f3 | 923 | * information, but can be modified to keep track of anything else. |
MitchAD | 2:0897873979f3 | 924 | * |
MitchAD | 2:0897873979f3 | 925 | * Parameters: Device handler |
MitchAD | 2:0897873979f3 | 926 | * Return Value: SUCCESS (0), FAILURE (Negative) |
MitchAD | 2:0897873979f3 | 927 | */ |
MitchAD | 2:0897873979f3 | 928 | static int8_t menu_print_channel_register_map_view_mode(struct ad7124_dev * dev) |
MitchAD | 2:0897873979f3 | 929 | { |
MitchAD | 2:0897873979f3 | 930 | uint8_t channel = 0; |
MitchAD | 2:0897873979f3 | 931 | int8_t ret; |
MitchAD | 2:0897873979f3 | 932 | |
MitchAD | 2:0897873979f3 | 933 | ret = fill_structs(dev); |
MitchAD | 2:0897873979f3 | 934 | if (err_detect(ret) == FAILURE ) |
MitchAD | 2:0897873979f3 | 935 | return FAILURE; |
MitchAD | 2:0897873979f3 | 936 | |
MitchAD | 2:0897873979f3 | 937 | port.printf("\tPower Mode: %s\n", power_strings[channels[channel].power_mode]); |
MitchAD | 2:0897873979f3 | 938 | port.printf("\t(Channel #, Enable, Setup, Gain, Filter, Filter Data Rate)\n"); |
MitchAD | 2:0897873979f3 | 939 | port.printf("\t=============================================================\n"); |
MitchAD | 2:0897873979f3 | 940 | for (channel = 0; channel < 16; channel++) { |
MitchAD | 2:0897873979f3 | 941 | port.printf("\tChannel %02d: %d, %d, %03d, %s, %d\n", |
MitchAD | 2:0897873979f3 | 942 | channel, channels[channel].enable, channels[channel].setup->setup_id, |
MitchAD | 2:0897873979f3 | 943 | 1 << channels[channel].setup->gain, filter_strings[channels[channel].setup->filter], |
MitchAD | 2:0897873979f3 | 944 | channels[channel].setup->data_rate); |
MitchAD | 2:0897873979f3 | 945 | } |
MitchAD | 2:0897873979f3 | 946 | return SUCCESS; |
MitchAD | 2:0897873979f3 | 947 | } |
MitchAD | 2:0897873979f3 | 948 | |
MitchAD | 2:0897873979f3 | 949 | /* Prints out the user friendly register map mode as well as the raw register map |
MitchAD | 2:0897873979f3 | 950 | * values. After selecting this function, the user can immediately copy over the |
MitchAD | 2:0897873979f3 | 951 | * current register map and paste it in the custom configuration register map file. |
MitchAD | 2:0897873979f3 | 952 | * This function waits for a key to be pressed to go back to the main menu to allow |
MitchAD | 2:0897873979f3 | 953 | * the user to easily copy the raw data from the serial monitor. |
MitchAD | 2:0897873979f3 | 954 | * |
MitchAD | 2:0897873979f3 | 955 | * Parameters: Device handler |
MitchAD | 2:0897873979f3 | 956 | * Return Value: SUCCESS (0), FAILURE (Negative) |
MitchAD | 2:0897873979f3 | 957 | */ |
MitchAD | 2:0897873979f3 | 958 | static int8_t menu_print_channel_register_map_raw_mode(struct ad7124_dev * dev) |
MitchAD | 2:0897873979f3 | 959 | { |
MitchAD | 2:0897873979f3 | 960 | struct ad7124_st_reg *regs; |
MitchAD | 2:0897873979f3 | 961 | uint8_t reg_nr; |
MitchAD | 2:0897873979f3 | 962 | int8_t ret; |
MitchAD | 2:0897873979f3 | 963 | |
MitchAD | 2:0897873979f3 | 964 | regs = dev->regs; |
MitchAD | 2:0897873979f3 | 965 | |
MitchAD | 2:0897873979f3 | 966 | port.printf("=====================================================\n"); |
MitchAD | 2:0897873979f3 | 967 | port.printf(" COPY BELOW HERE \n"); |
MitchAD | 2:0897873979f3 | 968 | port.printf("=====================================================\n\n"); |
MitchAD | 2:0897873979f3 | 969 | |
MitchAD | 2:0897873979f3 | 970 | port.printf("/*\n\n"); |
MitchAD | 2:0897873979f3 | 971 | menu_print_channel_register_map_view_mode(dev); |
MitchAD | 2:0897873979f3 | 972 | port.printf("\n*/\n"); |
MitchAD | 2:0897873979f3 | 973 | |
MitchAD | 2:0897873979f3 | 974 | port.printf("#include \"ad7124_regs_configs.h\"\n\n"); |
MitchAD | 2:0897873979f3 | 975 | port.printf("struct ad7124_st_reg ad7124_regs_custom[AD7124_REG_NO] = {\n"); |
MitchAD | 2:0897873979f3 | 976 | for (reg_nr = AD7124_Status; reg_nr < AD7124_REG_NO; reg_nr++) { |
MitchAD | 2:0897873979f3 | 977 | ret = ad7124_read_register(dev, ®s[reg_nr]); |
MitchAD | 2:0897873979f3 | 978 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 979 | return FAILURE; |
MitchAD | 2:0897873979f3 | 980 | |
MitchAD | 2:0897873979f3 | 981 | port.printf("\t\t{0x%02x, 0x%06x, %d, %d},\n", |
MitchAD | 2:0897873979f3 | 982 | regs[reg_nr].addr, regs[reg_nr].value, |
MitchAD | 2:0897873979f3 | 983 | regs[reg_nr].size, regs[reg_nr].rw); |
MitchAD | 2:0897873979f3 | 984 | } |
MitchAD | 2:0897873979f3 | 985 | port.printf("};\n"); |
MitchAD | 2:0897873979f3 | 986 | /*Wait until user enters a command to allow time to copy information*/ |
MitchAD | 2:0897873979f3 | 987 | while(1) { |
MitchAD | 2:0897873979f3 | 988 | if (!port.readable()) |
MitchAD | 2:0897873979f3 | 989 | return SUCCESS; |
MitchAD | 2:0897873979f3 | 990 | } |
MitchAD | 2:0897873979f3 | 991 | } |
MitchAD | 2:0897873979f3 | 992 | |
MitchAD | 2:0897873979f3 | 993 | /* Print all of the setup's information in a user friendly format. |
MitchAD | 2:0897873979f3 | 994 | * |
MitchAD | 2:0897873979f3 | 995 | * Parameters: Device handler |
MitchAD | 2:0897873979f3 | 996 | * Return Value: SUCCESS (0), FAILURE (Negative) |
MitchAD | 2:0897873979f3 | 997 | */ |
MitchAD | 2:0897873979f3 | 998 | static int8_t menu_print_setups(struct ad7124_dev * dev) |
MitchAD | 2:0897873979f3 | 999 | { |
MitchAD | 2:0897873979f3 | 1000 | uint8_t setup; |
MitchAD | 2:0897873979f3 | 1001 | int8_t ret; |
MitchAD | 2:0897873979f3 | 1002 | |
MitchAD | 2:0897873979f3 | 1003 | ret = fill_structs(dev); |
MitchAD | 2:0897873979f3 | 1004 | if (err_detect(ret) == FAILURE ) |
MitchAD | 2:0897873979f3 | 1005 | return FAILURE; |
MitchAD | 2:0897873979f3 | 1006 | |
MitchAD | 2:0897873979f3 | 1007 | port.printf("\t(Setup #: Gain, Filter, Filter Data Rate)\n"); |
MitchAD | 2:0897873979f3 | 1008 | port.printf("\t=============================================================\n"); |
MitchAD | 2:0897873979f3 | 1009 | for (setup = 0; setup < NUM_OF_SETUPS; setup++) { |
MitchAD | 2:0897873979f3 | 1010 | port.printf("\tSetup %d: %03d, %s, %d\n", |
MitchAD | 2:0897873979f3 | 1011 | setup, 1 << setups[setup].gain, filter_strings[setups[setup].filter], |
MitchAD | 2:0897873979f3 | 1012 | setups[setup].data_rate); |
MitchAD | 2:0897873979f3 | 1013 | } |
MitchAD | 2:0897873979f3 | 1014 | return SUCCESS; |
MitchAD | 2:0897873979f3 | 1015 | } |
MitchAD | 2:0897873979f3 | 1016 | |
MitchAD | 2:0897873979f3 | 1017 | /* Function to fill up structs based off what has been written to the ADC's |
MitchAD | 2:0897873979f3 | 1018 | * register map. Using this function along with "write_device", the user |
MitchAD | 2:0897873979f3 | 1019 | * can at all times have an up to date register map that can be written to thea |
MitchAD | 2:0897873979f3 | 1020 | * ADC's register map or updated with the most current values of the ADC. |
MitchAD | 2:0897873979f3 | 1021 | * |
MitchAD | 2:0897873979f3 | 1022 | * Parameters: Device handler |
MitchAD | 2:0897873979f3 | 1023 | * Return Value: SUCCESS (0) , FAILURE (Negative) |
MitchAD | 2:0897873979f3 | 1024 | */ |
MitchAD | 2:0897873979f3 | 1025 | static int8_t fill_structs(struct ad7124_dev * dev) |
MitchAD | 2:0897873979f3 | 1026 | { |
MitchAD | 2:0897873979f3 | 1027 | struct ad7124_st_reg *regs; |
MitchAD | 2:0897873979f3 | 1028 | uint8_t reg_nr, index = 0; |
MitchAD | 2:0897873979f3 | 1029 | int8_t ret; |
MitchAD | 2:0897873979f3 | 1030 | |
MitchAD | 2:0897873979f3 | 1031 | regs = dev->regs; |
MitchAD | 2:0897873979f3 | 1032 | |
MitchAD | 2:0897873979f3 | 1033 | for (reg_nr = AD7124_Status ; reg_nr < AD7124_Offset_0; reg_nr++) { |
MitchAD | 2:0897873979f3 | 1034 | /*Read register information from device*/ |
MitchAD | 2:0897873979f3 | 1035 | ret = ad7124_read_register(dev, ®s[reg_nr]); |
MitchAD | 2:0897873979f3 | 1036 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 1037 | return FAILURE; |
MitchAD | 2:0897873979f3 | 1038 | |
MitchAD | 2:0897873979f3 | 1039 | if (reg_nr == AD7124_ADC_Control) { |
MitchAD | 2:0897873979f3 | 1040 | /*Update power mode (0th channel only)*/ |
MitchAD | 2:0897873979f3 | 1041 | channels[index].power_mode = POWER_MODE_READ(regs[reg_nr].value); |
MitchAD | 2:0897873979f3 | 1042 | |
MitchAD | 2:0897873979f3 | 1043 | } else if ((reg_nr > AD7124_Mclk_Count) && (reg_nr < AD7124_Config_0)) { |
MitchAD | 2:0897873979f3 | 1044 | /*Updating enable bit*/ |
MitchAD | 2:0897873979f3 | 1045 | channels[index].enable = (regs[reg_nr].value & AD7124_CH_MAP_REG_CH_ENABLE); |
MitchAD | 2:0897873979f3 | 1046 | |
MitchAD | 2:0897873979f3 | 1047 | /*Updating setup*/ |
MitchAD | 2:0897873979f3 | 1048 | channels[index].setup = &setups[SETUP_READ(regs[reg_nr].value)]; |
MitchAD | 2:0897873979f3 | 1049 | |
MitchAD | 2:0897873979f3 | 1050 | /*Updating setup id*/ |
MitchAD | 2:0897873979f3 | 1051 | channels[index++].setup->setup_id = SETUP_READ(regs[reg_nr].value); |
MitchAD | 2:0897873979f3 | 1052 | |
MitchAD | 2:0897873979f3 | 1053 | /*Reset index*/ |
MitchAD | 2:0897873979f3 | 1054 | if (reg_nr == AD7124_Channel_15) |
MitchAD | 2:0897873979f3 | 1055 | index = 0; |
MitchAD | 2:0897873979f3 | 1056 | |
MitchAD | 2:0897873979f3 | 1057 | } else if ((reg_nr > AD7124_Channel_15) && (reg_nr < AD7124_Filter_0)) { |
MitchAD | 2:0897873979f3 | 1058 | /*Updating bipolar bit*/ |
MitchAD | 2:0897873979f3 | 1059 | setups[index].bipolar = (regs[reg_nr].value & AD7124_CFG_REG_BIPOLAR); |
MitchAD | 2:0897873979f3 | 1060 | |
MitchAD | 2:0897873979f3 | 1061 | /*Updating gain*/ |
MitchAD | 2:0897873979f3 | 1062 | setups[index++].gain = GAIN_READ(regs[reg_nr].value); |
MitchAD | 2:0897873979f3 | 1063 | |
MitchAD | 2:0897873979f3 | 1064 | /*Reset index*/ |
MitchAD | 2:0897873979f3 | 1065 | if (reg_nr == AD7124_Config_7) |
MitchAD | 2:0897873979f3 | 1066 | index = 0; |
MitchAD | 2:0897873979f3 | 1067 | |
MitchAD | 2:0897873979f3 | 1068 | } else if ((reg_nr > AD7124_Config_7) && (reg_nr < AD7124_Offset_0)) { |
MitchAD | 2:0897873979f3 | 1069 | /*Updating filter*/ |
MitchAD | 2:0897873979f3 | 1070 | setups[index].filter = FILTER_READ(regs[reg_nr].value); |
MitchAD | 2:0897873979f3 | 1071 | |
MitchAD | 2:0897873979f3 | 1072 | /*Updating filter data rate*/ |
MitchAD | 2:0897873979f3 | 1073 | setups[index++].data_rate = DATA_RATE_READ(regs[reg_nr].value); |
MitchAD | 2:0897873979f3 | 1074 | } |
MitchAD | 2:0897873979f3 | 1075 | } |
MitchAD | 2:0897873979f3 | 1076 | return SUCCESS; |
MitchAD | 2:0897873979f3 | 1077 | } |
MitchAD | 2:0897873979f3 | 1078 | |
MitchAD | 2:0897873979f3 | 1079 | /* Write back struct channel and setup struct values to the device. This |
MitchAD | 2:0897873979f3 | 1080 | * function only writes back the members of those structs. This function can |
MitchAD | 2:0897873979f3 | 1081 | * be modified in conjunction with the "fill_structs" function to add other |
MitchAD | 2:0897873979f3 | 1082 | * fields that you would like to modify and keep track of. Ex. AINP, AINM values |
MitchAD | 2:0897873979f3 | 1083 | * |
MitchAD | 2:0897873979f3 | 1084 | * Parameters: Device handler |
MitchAD | 2:0897873979f3 | 1085 | * Return Value: SUCCESS (0), FAILURE (Negative) |
MitchAD | 2:0897873979f3 | 1086 | */ |
MitchAD | 2:0897873979f3 | 1087 | static int8_t write_device(struct ad7124_dev * dev) |
MitchAD | 2:0897873979f3 | 1088 | { |
MitchAD | 2:0897873979f3 | 1089 | uint32_t ones_mask = 0xFFFFFFFF; |
MitchAD | 2:0897873979f3 | 1090 | uint32_t bitfield_mask, temp_val; |
MitchAD | 2:0897873979f3 | 1091 | uint8_t reg_nr, index = 0; |
MitchAD | 2:0897873979f3 | 1092 | int8_t ret; |
MitchAD | 2:0897873979f3 | 1093 | |
MitchAD | 2:0897873979f3 | 1094 | for (reg_nr = AD7124_Status; reg_nr < AD7124_Offset_0; reg_nr++) { |
MitchAD | 2:0897873979f3 | 1095 | |
MitchAD | 2:0897873979f3 | 1096 | if (reg_nr == AD7124_ADC_Control) { |
MitchAD | 2:0897873979f3 | 1097 | |
MitchAD | 2:0897873979f3 | 1098 | /*Write power mode*/ |
MitchAD | 2:0897873979f3 | 1099 | bitfield_mask = AD7124_ADC_CTRL_REG_POWER_MODE(ones_mask); |
MitchAD | 2:0897873979f3 | 1100 | temp_val = AD7124_ADC_CTRL_REG_POWER_MODE(channels[0].power_mode); |
MitchAD | 2:0897873979f3 | 1101 | ret = update_write_reg(dev, AD7124_ADC_Control, bitfield_mask, temp_val); |
MitchAD | 2:0897873979f3 | 1102 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 1103 | return FAILURE; |
MitchAD | 2:0897873979f3 | 1104 | |
MitchAD | 2:0897873979f3 | 1105 | } else if ((reg_nr > AD7124_Mclk_Count) && (reg_nr < AD7124_Config_0)) { |
MitchAD | 2:0897873979f3 | 1106 | /*Set enable bits*/ |
MitchAD | 2:0897873979f3 | 1107 | bitfield_mask = AD7124_CH_MAP_REG_CH_ENABLE; |
MitchAD | 2:0897873979f3 | 1108 | temp_val = (channels[index].enable == 1) ? AD7124_CH_MAP_REG_CH_ENABLE: |
MitchAD | 2:0897873979f3 | 1109 | !AD7124_CH_MAP_REG_CH_ENABLE; |
MitchAD | 2:0897873979f3 | 1110 | /*Write to device*/ |
MitchAD | 2:0897873979f3 | 1111 | ret = update_write_reg(dev, reg_nr, bitfield_mask, temp_val); |
MitchAD | 2:0897873979f3 | 1112 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 1113 | return FAILURE; |
MitchAD | 2:0897873979f3 | 1114 | |
MitchAD | 2:0897873979f3 | 1115 | /*Set setup bits*/ |
MitchAD | 2:0897873979f3 | 1116 | bitfield_mask = AD7124_CH_MAP_REG_SETUP(ones_mask); |
MitchAD | 2:0897873979f3 | 1117 | temp_val = AD7124_CH_MAP_REG_SETUP(channels[index].setup->setup_id); |
MitchAD | 2:0897873979f3 | 1118 | ret = update_write_reg(dev, reg_nr, bitfield_mask, temp_val); |
MitchAD | 2:0897873979f3 | 1119 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 1120 | return FAILURE; |
MitchAD | 2:0897873979f3 | 1121 | |
MitchAD | 2:0897873979f3 | 1122 | index++; |
MitchAD | 2:0897873979f3 | 1123 | /*Reset index*/ |
MitchAD | 2:0897873979f3 | 1124 | if (reg_nr == AD7124_Channel_15) |
MitchAD | 2:0897873979f3 | 1125 | index = 0; |
MitchAD | 2:0897873979f3 | 1126 | |
MitchAD | 2:0897873979f3 | 1127 | } else if ((reg_nr > AD7124_Channel_15) && (reg_nr < AD7124_Filter_0)) { |
MitchAD | 2:0897873979f3 | 1128 | /*Set gain bits*/ |
MitchAD | 2:0897873979f3 | 1129 | bitfield_mask = AD7124_CFG_REG_PGA(ones_mask); |
MitchAD | 2:0897873979f3 | 1130 | temp_val = AD7124_CFG_REG_PGA(setups[index++].gain); |
MitchAD | 2:0897873979f3 | 1131 | |
MitchAD | 2:0897873979f3 | 1132 | /*Write to device*/ |
MitchAD | 2:0897873979f3 | 1133 | ret = update_write_reg(dev, reg_nr, bitfield_mask, temp_val); |
MitchAD | 2:0897873979f3 | 1134 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 1135 | return FAILURE; |
MitchAD | 2:0897873979f3 | 1136 | |
MitchAD | 2:0897873979f3 | 1137 | /*Reset index*/ |
MitchAD | 2:0897873979f3 | 1138 | if (reg_nr == AD7124_Config_7) |
MitchAD | 2:0897873979f3 | 1139 | index = 0; |
MitchAD | 2:0897873979f3 | 1140 | |
MitchAD | 2:0897873979f3 | 1141 | } else if ((reg_nr > AD7124_Config_7) && (reg_nr < AD7124_Offset_0)) { |
MitchAD | 2:0897873979f3 | 1142 | /*Set filter bits*/ |
MitchAD | 2:0897873979f3 | 1143 | bitfield_mask = AD7124_FILT_REG_FILTER(ones_mask); |
MitchAD | 2:0897873979f3 | 1144 | temp_val = AD7124_FILT_REG_FILTER(setups[index].filter); |
MitchAD | 2:0897873979f3 | 1145 | ret = update_write_reg(dev, reg_nr, bitfield_mask, temp_val); |
MitchAD | 2:0897873979f3 | 1146 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 1147 | return FAILURE; |
MitchAD | 2:0897873979f3 | 1148 | |
MitchAD | 2:0897873979f3 | 1149 | /*Set data rate bits*/ |
MitchAD | 2:0897873979f3 | 1150 | bitfield_mask = AD7124_FILT_REG_FS(ones_mask); |
MitchAD | 2:0897873979f3 | 1151 | temp_val = AD7124_FILT_REG_FS(setups[index++].data_rate); |
MitchAD | 2:0897873979f3 | 1152 | ret = update_write_reg(dev, reg_nr, bitfield_mask, temp_val); |
MitchAD | 2:0897873979f3 | 1153 | if (err_detect(ret) == FAILURE) |
MitchAD | 2:0897873979f3 | 1154 | return FAILURE; |
MitchAD | 2:0897873979f3 | 1155 | } |
MitchAD | 2:0897873979f3 | 1156 | } |
MitchAD | 2:0897873979f3 | 1157 | return SUCCESS; |
MitchAD | 2:0897873979f3 | 1158 | } |
MitchAD | 2:0897873979f3 | 1159 | |
MitchAD | 2:0897873979f3 | 1160 | /* Prints out the error message corresponding to the No-OS drivers functions |
MitchAD | 2:0897873979f3 | 1161 | * |
MitchAD | 2:0897873979f3 | 1162 | * Parameters: Return value from some function |
MitchAD | 2:0897873979f3 | 1163 | * Return Value: SUCCESS (0), FAILURE (Negative) |
MitchAD | 2:0897873979f3 | 1164 | */ |
MitchAD | 2:0897873979f3 | 1165 | static int8_t err_detect(int8_t ret) |
MitchAD | 2:0897873979f3 | 1166 | { |
MitchAD | 2:0897873979f3 | 1167 | switch(ret) { |
MitchAD | 2:0897873979f3 | 1168 | case -1: |
MitchAD | 2:0897873979f3 | 1169 | port.printf("\tInvalid Argument...\n"); |
MitchAD | 2:0897873979f3 | 1170 | wait(1); |
MitchAD | 2:0897873979f3 | 1171 | return FAILURE; |
MitchAD | 2:0897873979f3 | 1172 | |
MitchAD | 2:0897873979f3 | 1173 | case -2: |
MitchAD | 2:0897873979f3 | 1174 | port.printf("\tCommunication Error on Receive...\n"); |
MitchAD | 2:0897873979f3 | 1175 | wait(1); |
MitchAD | 2:0897873979f3 | 1176 | return FAILURE; |
MitchAD | 2:0897873979f3 | 1177 | |
MitchAD | 2:0897873979f3 | 1178 | case -3: |
MitchAD | 2:0897873979f3 | 1179 | port.printf("\tA Timeout has Occured...\n"); |
MitchAD | 2:0897873979f3 | 1180 | wait(1); |
MitchAD | 2:0897873979f3 | 1181 | return FAILURE; |
MitchAD | 2:0897873979f3 | 1182 | |
MitchAD | 2:0897873979f3 | 1183 | default: |
MitchAD | 2:0897873979f3 | 1184 | return SUCCESS; |
MitchAD | 2:0897873979f3 | 1185 | } |
MitchAD | 2:0897873979f3 | 1186 | } |
MitchAD | 2:0897873979f3 | 1187 | |
MitchAD | 2:0897873979f3 | 1188 | /* Obtains the setup that the user would like to select |
MitchAD | 2:0897873979f3 | 1189 | * |
MitchAD | 2:0897873979f3 | 1190 | * Parameters: None |
MitchAD | 2:0897873979f3 | 1191 | * Return Value: SUCCESS (0), FAILURE (Negative) |
MitchAD | 2:0897873979f3 | 1192 | */ |
MitchAD | 2:0897873979f3 | 1193 | static int8_t setup_select(uint8_t *setup) |
MitchAD | 2:0897873979f3 | 1194 | { |
MitchAD | 2:0897873979f3 | 1195 | port.printf("\t===============================\n"); |
MitchAD | 2:0897873979f3 | 1196 | port.printf("\t Setup (0 - 7)\n"); |
MitchAD | 2:0897873979f3 | 1197 | port.printf("\t 8 - Main Menu\n\n"); |
MitchAD | 2:0897873979f3 | 1198 | |
MitchAD | 2:0897873979f3 | 1199 | port.scanf("%d", (int *) setup); |
MitchAD | 2:0897873979f3 | 1200 | if (*setup > 7) |
MitchAD | 2:0897873979f3 | 1201 | return FAILURE; |
MitchAD | 2:0897873979f3 | 1202 | |
MitchAD | 2:0897873979f3 | 1203 | return SUCCESS; |
MitchAD | 2:0897873979f3 | 1204 | } |
MitchAD | 2:0897873979f3 | 1205 | |
MitchAD | 2:0897873979f3 | 1206 | /* Obtains the channel that the user would like to select |
MitchAD | 2:0897873979f3 | 1207 | * |
MitchAD | 2:0897873979f3 | 1208 | * Parameters: None |
MitchAD | 2:0897873979f3 | 1209 | * Return Value: SUCCESS (0), FAILURE (Negative) |
MitchAD | 2:0897873979f3 | 1210 | */ |
MitchAD | 2:0897873979f3 | 1211 | static int8_t channel_select(uint8_t *channel) |
MitchAD | 2:0897873979f3 | 1212 | { |
MitchAD | 2:0897873979f3 | 1213 | port.printf("\t===============================\n"); |
MitchAD | 2:0897873979f3 | 1214 | port.printf("\t Channel (0 - 15)\n"); |
MitchAD | 2:0897873979f3 | 1215 | port.printf("\t 16 - Main Menu\n\n"); |
MitchAD | 2:0897873979f3 | 1216 | |
MitchAD | 2:0897873979f3 | 1217 | port.scanf("%d", (int *) channel); |
MitchAD | 2:0897873979f3 | 1218 | if (*channel > 15) |
MitchAD | 2:0897873979f3 | 1219 | return FAILURE; |
MitchAD | 2:0897873979f3 | 1220 | |
MitchAD | 2:0897873979f3 | 1221 | return SUCCESS; |
MitchAD | 2:0897873979f3 | 1222 | } |