Sean Smith
/
WIP
Initial Commit
main.cpp@1:384a1737b0df, 2019-05-28 (annotated)
- Committer:
- ssmith73
- Date:
- Tue May 28 19:37:12 2019 +0000
- Revision:
- 1:384a1737b0df
Initial commit of EVAL-AD5686x-AD5696x
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ssmith73 | 1:384a1737b0df | 1 | /*************************************************************************//** |
ssmith73 | 1:384a1737b0df | 2 | * @file main.cpp |
ssmith73 | 1:384a1737b0df | 3 | * @brief Main application code for AD5686 firmware example program |
ssmith73 | 1:384a1737b0df | 4 | * @author ssmith (sean.smith@analog.com) |
ssmith73 | 1:384a1737b0df | 5 | ****************************************************************************** |
ssmith73 | 1:384a1737b0df | 6 | * Copyright (c) 2019 Analog Devices, Inc. |
ssmith73 | 1:384a1737b0df | 7 | * |
ssmith73 | 1:384a1737b0df | 8 | * All rights reserved. |
ssmith73 | 1:384a1737b0df | 9 | * |
ssmith73 | 1:384a1737b0df | 10 | * Redistribution and use in source and binary forms, with or without |
ssmith73 | 1:384a1737b0df | 11 | * modification, are permitted provided that the following conditions are met: |
ssmith73 | 1:384a1737b0df | 12 | * - Redistributions of source code must retain the above copyright notice, |
ssmith73 | 1:384a1737b0df | 13 | * this list of conditions and the following disclaimer. |
ssmith73 | 1:384a1737b0df | 14 | * - Redistributions in binary form must reproduce the above copyright notice, |
ssmith73 | 1:384a1737b0df | 15 | * this list of conditions and the following disclaimer in the documentation |
ssmith73 | 1:384a1737b0df | 16 | * and/or other materials provided with the distribution. |
ssmith73 | 1:384a1737b0df | 17 | * - Modified versions of the software must be conspicuously marked as such. |
ssmith73 | 1:384a1737b0df | 18 | * - This software is licensed solely and exclusively for use with |
ssmith73 | 1:384a1737b0df | 19 | * processors/products manufactured by or for Analog Devices, Inc. |
ssmith73 | 1:384a1737b0df | 20 | * - This software may not be combined or merged with other code in any manner |
ssmith73 | 1:384a1737b0df | 21 | * that would cause the software to become subject to terms and |
ssmith73 | 1:384a1737b0df | 22 | * conditions which differ from those listed here. |
ssmith73 | 1:384a1737b0df | 23 | * - Neither the name of Analog Devices, Inc. nor the names of its |
ssmith73 | 1:384a1737b0df | 24 | * contributors may be used to endorse or promote products derived |
ssmith73 | 1:384a1737b0df | 25 | * from this software without specific prior written permission. |
ssmith73 | 1:384a1737b0df | 26 | * - The use of this software may or may not infringe the patent rights |
ssmith73 | 1:384a1737b0df | 27 | * of one or more patent holders. This license does not release you from |
ssmith73 | 1:384a1737b0df | 28 | * the requirement that you obtain separate licenses from these patent |
ssmith73 | 1:384a1737b0df | 29 | * holders to use this software. |
ssmith73 | 1:384a1737b0df | 30 | * |
ssmith73 | 1:384a1737b0df | 31 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. AND CONTRIBUTORS "AS IS" |
ssmith73 | 1:384a1737b0df | 32 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
ssmith73 | 1:384a1737b0df | 33 | * NON-INFRINGEMENT, TITLE, MERCHANTABILITY AND FITNESS FOR A |
ssmith73 | 1:384a1737b0df | 34 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANALOG DEVICES, |
ssmith73 | 1:384a1737b0df | 35 | * INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
ssmith73 | 1:384a1737b0df | 36 | * SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES |
ssmith73 | 1:384a1737b0df | 37 | * (INCLUDING, BUT NOT LIMITED TO, DAMAGES ARISING OUT OF CLAIMS OF |
ssmith73 | 1:384a1737b0df | 38 | * INTELLECTUAL PROPERTY RIGHTS INFRINGEMENT; PROCUREMENT OF SUBSTITUTE |
ssmith73 | 1:384a1737b0df | 39 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
ssmith73 | 1:384a1737b0df | 40 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
ssmith73 | 1:384a1737b0df | 41 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
ssmith73 | 1:384a1737b0df | 42 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
ssmith73 | 1:384a1737b0df | 43 | * POSSIBILITY OF SUCH DAMAGE. |
ssmith73 | 1:384a1737b0df | 44 | * |
ssmith73 | 1:384a1737b0df | 45 | * 20180927-7CBSD SLA |
ssmith73 | 1:384a1737b0df | 46 | *****************************************************************************/ |
ssmith73 | 1:384a1737b0df | 47 | |
ssmith73 | 1:384a1737b0df | 48 | #include <stdio.h> |
ssmith73 | 1:384a1737b0df | 49 | #include <mbed.h> |
ssmith73 | 1:384a1737b0df | 50 | #include "app_config.h" |
ssmith73 | 1:384a1737b0df | 51 | |
ssmith73 | 1:384a1737b0df | 52 | #define NOT_USED 0 |
ssmith73 | 1:384a1737b0df | 53 | #define WAIT_MENU_TIME 1 |
ssmith73 | 1:384a1737b0df | 54 | |
ssmith73 | 1:384a1737b0df | 55 | static void print_title(void); |
ssmith73 | 1:384a1737b0df | 56 | static float get_voltage_float(void); |
ssmith73 | 1:384a1737b0df | 57 | static int getMenuSelect(uint8_t *menuSelect); |
ssmith73 | 1:384a1737b0df | 58 | static void print_prompt(int16_t selected_dac, float ref_voltage); |
ssmith73 | 1:384a1737b0df | 59 | static uint16_t voltage_to_code(float voltage, float vRef); |
ssmith73 | 1:384a1737b0df | 60 | static uint8_t menu_1_select_dac(uint16_t *selected_dac); |
ssmith73 | 1:384a1737b0df | 61 | static void menu_2_write_to_input_register(int16_t selected_dac, float vref); |
ssmith73 | 1:384a1737b0df | 62 | static void menu_3_update_dac(int16_t selected_dac); |
ssmith73 | 1:384a1737b0df | 63 | static void menu_4_write_and_update_dac(int16_t selected_dac, float vref); |
ssmith73 | 1:384a1737b0df | 64 | static int8_t menu_5_power_down_mode(int16_t selected_dac); |
ssmith73 | 1:384a1737b0df | 65 | static int8_t menu_6_select_ref_voltage(float *vref); |
ssmith73 | 1:384a1737b0df | 66 | static void menu_7_read_back_registers(); |
ssmith73 | 1:384a1737b0df | 67 | static int8_t menu_8_set_ldac_mask(); |
ssmith73 | 1:384a1737b0df | 68 | static void menu_9_assert_ldac(); |
ssmith73 | 1:384a1737b0df | 69 | static int8_t menu_10_set_gain(); |
ssmith73 | 1:384a1737b0df | 70 | static void menu_11_assert_soft_reset(); |
ssmith73 | 1:384a1737b0df | 71 | static void menu_12_assert_hard_reset(); |
ssmith73 | 1:384a1737b0df | 72 | static uint16_t get_voltage_code(float vRef); |
ssmith73 | 1:384a1737b0df | 73 | static uint8_t selectChannel(uint16_t selectedDac); |
ssmith73 | 1:384a1737b0df | 74 | static void set_default_pins(void); |
ssmith73 | 1:384a1737b0df | 75 | |
ssmith73 | 1:384a1737b0df | 76 | /******************************************************************************/ |
ssmith73 | 1:384a1737b0df | 77 | /************************** Variables Declarations ****************************/ |
ssmith73 | 1:384a1737b0df | 78 | /******************************************************************************/ |
ssmith73 | 1:384a1737b0df | 79 | extern ad5686_chip_info chip_info[]; |
ssmith73 | 1:384a1737b0df | 80 | |
ssmith73 | 1:384a1737b0df | 81 | /** |
ssmith73 | 1:384a1737b0df | 82 | The following definitions are a requirement for the platform_driver |
ssmith73 | 1:384a1737b0df | 83 | Pin are changed if required in the app_config.h file |
ssmith73 | 1:384a1737b0df | 84 | ***/ |
ssmith73 | 1:384a1737b0df | 85 | DigitalOut SS(SPI_CS); |
ssmith73 | 1:384a1737b0df | 86 | mbed::SPI spi(SPI_MOSI, SPI_MISO, SPI_SCK); |
ssmith73 | 1:384a1737b0df | 87 | mbed::I2C i2c(I2C_SDA, I2C_SCL); |
ssmith73 | 1:384a1737b0df | 88 | |
ssmith73 | 1:384a1737b0df | 89 | i2c_init_param i2c_params = |
ssmith73 | 1:384a1737b0df | 90 | { |
ssmith73 | 1:384a1737b0df | 91 | // i2c type |
ssmith73 | 1:384a1737b0df | 92 | GENERIC_I2C, |
ssmith73 | 1:384a1737b0df | 93 | // i2c id |
ssmith73 | 1:384a1737b0df | 94 | NOT_USED, |
ssmith73 | 1:384a1737b0df | 95 | // i2c max speed (hz) |
ssmith73 | 1:384a1737b0df | 96 | 100000, |
ssmith73 | 1:384a1737b0df | 97 | // i2c slave address (AD5693) |
ssmith73 | 1:384a1737b0df | 98 | //0x98 |
ssmith73 | 1:384a1737b0df | 99 | // i2c slave address (AD5696) |
ssmith73 | 1:384a1737b0df | 100 | 0x18 |
ssmith73 | 1:384a1737b0df | 101 | }; |
ssmith73 | 1:384a1737b0df | 102 | |
ssmith73 | 1:384a1737b0df | 103 | |
ssmith73 | 1:384a1737b0df | 104 | spi_init_param spi_params = { |
ssmith73 | 1:384a1737b0df | 105 | MBED, |
ssmith73 | 1:384a1737b0df | 106 | //spi_type |
ssmith73 | 1:384a1737b0df | 107 | GENERIC_SPI, |
ssmith73 | 1:384a1737b0df | 108 | //id |
ssmith73 | 1:384a1737b0df | 109 | NOT_USED, |
ssmith73 | 1:384a1737b0df | 110 | //frequency |
ssmith73 | 1:384a1737b0df | 111 | 2000000, |
ssmith73 | 1:384a1737b0df | 112 | // SPI mode |
ssmith73 | 1:384a1737b0df | 113 | SPI_MODE_2, |
ssmith73 | 1:384a1737b0df | 114 | //CS mapping |
ssmith73 | 1:384a1737b0df | 115 | SPI_CS, |
ssmith73 | 1:384a1737b0df | 116 | }; |
ssmith73 | 1:384a1737b0df | 117 | |
ssmith73 | 1:384a1737b0df | 118 | gpio_desc gpio_gain = |
ssmith73 | 1:384a1737b0df | 119 | { |
ssmith73 | 1:384a1737b0df | 120 | GENERIC_GPIO, |
ssmith73 | 1:384a1737b0df | 121 | NOT_USED, |
ssmith73 | 1:384a1737b0df | 122 | NOT_USED, |
ssmith73 | 1:384a1737b0df | 123 | GAIN_PIN |
ssmith73 | 1:384a1737b0df | 124 | }; |
ssmith73 | 1:384a1737b0df | 125 | |
ssmith73 | 1:384a1737b0df | 126 | gpio_desc gpio_ldac = |
ssmith73 | 1:384a1737b0df | 127 | { |
ssmith73 | 1:384a1737b0df | 128 | GENERIC_GPIO, |
ssmith73 | 1:384a1737b0df | 129 | NOT_USED, |
ssmith73 | 1:384a1737b0df | 130 | NOT_USED, |
ssmith73 | 1:384a1737b0df | 131 | LDAC_PIN |
ssmith73 | 1:384a1737b0df | 132 | }; |
ssmith73 | 1:384a1737b0df | 133 | |
ssmith73 | 1:384a1737b0df | 134 | gpio_desc gpio_reset = |
ssmith73 | 1:384a1737b0df | 135 | { |
ssmith73 | 1:384a1737b0df | 136 | GENERIC_GPIO, |
ssmith73 | 1:384a1737b0df | 137 | //TYPE |
ssmith73 | 1:384a1737b0df | 138 | NOT_USED, |
ssmith73 | 1:384a1737b0df | 139 | //ID |
ssmith73 | 1:384a1737b0df | 140 | NOT_USED, |
ssmith73 | 1:384a1737b0df | 141 | //NUMBER |
ssmith73 | 1:384a1737b0df | 142 | RESET_PIN |
ssmith73 | 1:384a1737b0df | 143 | }; |
ssmith73 | 1:384a1737b0df | 144 | |
ssmith73 | 1:384a1737b0df | 145 | ad5686_init_param init_params = { |
ssmith73 | 1:384a1737b0df | 146 | |
ssmith73 | 1:384a1737b0df | 147 | // I2C parameters |
ssmith73 | 1:384a1737b0df | 148 | i2c_params, |
ssmith73 | 1:384a1737b0df | 149 | // SPI parameters |
ssmith73 | 1:384a1737b0df | 150 | spi_params, |
ssmith73 | 1:384a1737b0df | 151 | //GPIO's |
ssmith73 | 1:384a1737b0df | 152 | gpio_reset, |
ssmith73 | 1:384a1737b0df | 153 | gpio_ldac, |
ssmith73 | 1:384a1737b0df | 154 | gpio_gain, |
ssmith73 | 1:384a1737b0df | 155 | //Set this in app_config.h |
ssmith73 | 1:384a1737b0df | 156 | ACTIVE_DEVICE, |
ssmith73 | 1:384a1737b0df | 157 | }; |
ssmith73 | 1:384a1737b0df | 158 | |
ssmith73 | 1:384a1737b0df | 159 | |
ssmith73 | 1:384a1737b0df | 160 | ad5686_dev *device; |
ssmith73 | 1:384a1737b0df | 161 | int32_t connected = -1; |
ssmith73 | 1:384a1737b0df | 162 | uint8_t gpioGainVal = 0; |
ssmith73 | 1:384a1737b0df | 163 | |
ssmith73 | 1:384a1737b0df | 164 | //! Configure and instantiate the UART |
ssmith73 | 1:384a1737b0df | 165 | Serial pc(USBTX, USBRX, 115200); |
ssmith73 | 1:384a1737b0df | 166 | |
ssmith73 | 1:384a1737b0df | 167 | int main() |
ssmith73 | 1:384a1737b0df | 168 | { |
ssmith73 | 1:384a1737b0df | 169 | |
ssmith73 | 1:384a1737b0df | 170 | uint8_t menuSelect = 0; |
ssmith73 | 1:384a1737b0df | 171 | uint16_t selected_dac = 1; |
ssmith73 | 1:384a1737b0df | 172 | float ref_voltage = 2.5; |
ssmith73 | 1:384a1737b0df | 173 | |
ssmith73 | 1:384a1737b0df | 174 | print_title(); |
ssmith73 | 1:384a1737b0df | 175 | connected = ad5686_init(&device, init_params); |
ssmith73 | 1:384a1737b0df | 176 | set_default_pins(); |
ssmith73 | 1:384a1737b0df | 177 | while (connected == SUCCESS) |
ssmith73 | 1:384a1737b0df | 178 | { |
ssmith73 | 1:384a1737b0df | 179 | // Switch menu based on user's input |
ssmith73 | 1:384a1737b0df | 180 | print_prompt(selected_dac, ref_voltage); |
ssmith73 | 1:384a1737b0df | 181 | if (getMenuSelect(&menuSelect) == FAILURE) |
ssmith73 | 1:384a1737b0df | 182 | print_prompt(selected_dac, ref_voltage); |
ssmith73 | 1:384a1737b0df | 183 | else |
ssmith73 | 1:384a1737b0df | 184 | switch (menuSelect) |
ssmith73 | 1:384a1737b0df | 185 | { |
ssmith73 | 1:384a1737b0df | 186 | case 1: |
ssmith73 | 1:384a1737b0df | 187 | menu_1_select_dac(&selected_dac); |
ssmith73 | 1:384a1737b0df | 188 | break; |
ssmith73 | 1:384a1737b0df | 189 | |
ssmith73 | 1:384a1737b0df | 190 | case 2: |
ssmith73 | 1:384a1737b0df | 191 | menu_2_write_to_input_register(selected_dac, ref_voltage); |
ssmith73 | 1:384a1737b0df | 192 | break; |
ssmith73 | 1:384a1737b0df | 193 | |
ssmith73 | 1:384a1737b0df | 194 | case 3: |
ssmith73 | 1:384a1737b0df | 195 | menu_3_update_dac(selected_dac); |
ssmith73 | 1:384a1737b0df | 196 | break; |
ssmith73 | 1:384a1737b0df | 197 | |
ssmith73 | 1:384a1737b0df | 198 | case 4: |
ssmith73 | 1:384a1737b0df | 199 | menu_4_write_and_update_dac(selected_dac, ref_voltage); |
ssmith73 | 1:384a1737b0df | 200 | break; |
ssmith73 | 1:384a1737b0df | 201 | |
ssmith73 | 1:384a1737b0df | 202 | case 5: |
ssmith73 | 1:384a1737b0df | 203 | menu_5_power_down_mode(selected_dac); |
ssmith73 | 1:384a1737b0df | 204 | break; |
ssmith73 | 1:384a1737b0df | 205 | |
ssmith73 | 1:384a1737b0df | 206 | case 6: |
ssmith73 | 1:384a1737b0df | 207 | menu_6_select_ref_voltage(&ref_voltage); |
ssmith73 | 1:384a1737b0df | 208 | break; |
ssmith73 | 1:384a1737b0df | 209 | |
ssmith73 | 1:384a1737b0df | 210 | case 7: |
ssmith73 | 1:384a1737b0df | 211 | menu_7_read_back_registers(); |
ssmith73 | 1:384a1737b0df | 212 | break; |
ssmith73 | 1:384a1737b0df | 213 | |
ssmith73 | 1:384a1737b0df | 214 | case 8: |
ssmith73 | 1:384a1737b0df | 215 | menu_8_set_ldac_mask(); |
ssmith73 | 1:384a1737b0df | 216 | break; |
ssmith73 | 1:384a1737b0df | 217 | |
ssmith73 | 1:384a1737b0df | 218 | case 9: |
ssmith73 | 1:384a1737b0df | 219 | menu_9_assert_ldac(); |
ssmith73 | 1:384a1737b0df | 220 | break; |
ssmith73 | 1:384a1737b0df | 221 | |
ssmith73 | 1:384a1737b0df | 222 | case 10: |
ssmith73 | 1:384a1737b0df | 223 | menu_10_set_gain(); |
ssmith73 | 1:384a1737b0df | 224 | break; |
ssmith73 | 1:384a1737b0df | 225 | |
ssmith73 | 1:384a1737b0df | 226 | case 11: |
ssmith73 | 1:384a1737b0df | 227 | menu_11_assert_soft_reset(); |
ssmith73 | 1:384a1737b0df | 228 | break; |
ssmith73 | 1:384a1737b0df | 229 | |
ssmith73 | 1:384a1737b0df | 230 | case 12: |
ssmith73 | 1:384a1737b0df | 231 | menu_12_assert_hard_reset(); |
ssmith73 | 1:384a1737b0df | 232 | break; |
ssmith73 | 1:384a1737b0df | 233 | |
ssmith73 | 1:384a1737b0df | 234 | default : |
ssmith73 | 1:384a1737b0df | 235 | pc.printf("Incorrect Option\n"); |
ssmith73 | 1:384a1737b0df | 236 | break; |
ssmith73 | 1:384a1737b0df | 237 | } |
ssmith73 | 1:384a1737b0df | 238 | //Allow some time before menu switches |
ssmith73 | 1:384a1737b0df | 239 | wait(1); |
ssmith73 | 1:384a1737b0df | 240 | } |
ssmith73 | 1:384a1737b0df | 241 | //Should never be reached |
ssmith73 | 1:384a1737b0df | 242 | return 0; |
ssmith73 | 1:384a1737b0df | 243 | } |
ssmith73 | 1:384a1737b0df | 244 | |
ssmith73 | 1:384a1737b0df | 245 | static void print_title() |
ssmith73 | 1:384a1737b0df | 246 | { |
ssmith73 | 1:384a1737b0df | 247 | pc.printf("*****************************************************************\n"); |
ssmith73 | 1:384a1737b0df | 248 | pc.printf("* EVAL-nanoDAC+ Demonstration Program for mbed *\n"); |
ssmith73 | 1:384a1737b0df | 249 | pc.printf("* *\n"); |
ssmith73 | 1:384a1737b0df | 250 | pc.printf("* This program demonstrates communication with the nanoDAC+ *\n"); |
ssmith73 | 1:384a1737b0df | 251 | pc.printf("* family of Rail-to-Rail DACs with SPI/I2C Interface. *\n"); |
ssmith73 | 1:384a1737b0df | 252 | pc.printf("* *\n"); |
ssmith73 | 1:384a1737b0df | 253 | pc.printf("* Set the baud rate to 115200 *\n"); |
ssmith73 | 1:384a1737b0df | 254 | pc.printf("* For Compile requirements see the user-guide *\n"); |
ssmith73 | 1:384a1737b0df | 255 | pc.printf("*****************************************************************\n"); |
ssmith73 | 1:384a1737b0df | 256 | } |
ssmith73 | 1:384a1737b0df | 257 | |
ssmith73 | 1:384a1737b0df | 258 | |
ssmith73 | 1:384a1737b0df | 259 | /*************************************************************************//** |
ssmith73 | 1:384a1737b0df | 260 | * @brief - Prints the "main menu" prompt to the console |
ssmith73 | 1:384a1737b0df | 261 | * |
ssmith73 | 1:384a1737b0df | 262 | * @param selected_dac - only 1,2,4,8 (DAC A/B/C/D) are valid |
ssmith73 | 1:384a1737b0df | 263 | * @param ref_voltage - For displaying in the UI |
ssmith73 | 1:384a1737b0df | 264 | * |
ssmith73 | 1:384a1737b0df | 265 | * @return None. |
ssmith73 | 1:384a1737b0df | 266 | *****************************************************************************/ |
ssmith73 | 1:384a1737b0df | 267 | static void print_prompt(int16_t selected_dac, float ref_voltage) |
ssmith73 | 1:384a1737b0df | 268 | { |
ssmith73 | 1:384a1737b0df | 269 | pc.printf("\n\nCommand Summary:\n\n"); |
ssmith73 | 1:384a1737b0df | 270 | |
ssmith73 | 1:384a1737b0df | 271 | if (chip_info[device->act_device].register_map == AD5686_REG_MAP) |
ssmith73 | 1:384a1737b0df | 272 | { |
ssmith73 | 1:384a1737b0df | 273 | pc.printf(" 1 -Select DAC\n"); |
ssmith73 | 1:384a1737b0df | 274 | pc.printf(" 2 -Write to input register (no update)\n"); |
ssmith73 | 1:384a1737b0df | 275 | pc.printf(" 3 -Update DAC from input\n"); |
ssmith73 | 1:384a1737b0df | 276 | pc.printf(" 4 -Write and update DAC\n"); |
ssmith73 | 1:384a1737b0df | 277 | pc.printf(" 5 -Power down mode\n"); |
ssmith73 | 1:384a1737b0df | 278 | pc.printf(" 6 -Select reference voltage\n"); |
ssmith73 | 1:384a1737b0df | 279 | pc.printf(" 7 -Read back all registers\n"); |
ssmith73 | 1:384a1737b0df | 280 | pc.printf(" 8 -Set LDAC# mask\n"); |
ssmith73 | 1:384a1737b0df | 281 | pc.printf(" 9 -Assert LDAC#\n"); |
ssmith73 | 1:384a1737b0df | 282 | pc.printf(" 10-Set gain\n"); |
ssmith73 | 1:384a1737b0df | 283 | pc.printf(" 11-Assert Software Reset\n"); |
ssmith73 | 1:384a1737b0df | 284 | pc.printf(" 12-Assert Hardware Reset\n"); |
ssmith73 | 1:384a1737b0df | 285 | pc.printf("\n"); |
ssmith73 | 1:384a1737b0df | 286 | pc.printf("\n Reference Voltage: %f\n", ref_voltage); |
ssmith73 | 1:384a1737b0df | 287 | |
ssmith73 | 1:384a1737b0df | 288 | switch (selected_dac) |
ssmith73 | 1:384a1737b0df | 289 | { |
ssmith73 | 1:384a1737b0df | 290 | case 1:{ pc.printf(" DAC A is currently selected\n"); break; } |
ssmith73 | 1:384a1737b0df | 291 | case 2:{ pc.printf(" DAC B is currently selected\n"); break; } |
ssmith73 | 1:384a1737b0df | 292 | case 4:{ pc.printf(" DAC C is currently selected\n"); break; } |
ssmith73 | 1:384a1737b0df | 293 | case 8:{ pc.printf(" DAC D is currently selected\n"); break; } |
ssmith73 | 1:384a1737b0df | 294 | default: { |
ssmith73 | 1:384a1737b0df | 295 | pc.printf(" Please select a DAC (Option 1):\n"); |
ssmith73 | 1:384a1737b0df | 296 | selected_dac = 0; |
ssmith73 | 1:384a1737b0df | 297 | } |
ssmith73 | 1:384a1737b0df | 298 | } |
ssmith73 | 1:384a1737b0df | 299 | } |
ssmith73 | 1:384a1737b0df | 300 | else |
ssmith73 | 1:384a1737b0df | 301 | { |
ssmith73 | 1:384a1737b0df | 302 | /* |
ssmith73 | 1:384a1737b0df | 303 | * This code runs for products in the nanoDAC+ family that have the |
ssmith73 | 1:384a1737b0df | 304 | * reduced register map |
ssmith73 | 1:384a1737b0df | 305 | */ |
ssmith73 | 1:384a1737b0df | 306 | |
ssmith73 | 1:384a1737b0df | 307 | pc.printf(" 1 -NOP: Do nothing\n"); |
ssmith73 | 1:384a1737b0df | 308 | pc.printf(" 2 -Write Input register\n"); |
ssmith73 | 1:384a1737b0df | 309 | pc.printf(" 3 -Software LDAC\n"); |
ssmith73 | 1:384a1737b0df | 310 | pc.printf(" 4 -Write to DAC and Input registers\n"); |
ssmith73 | 1:384a1737b0df | 311 | pc.printf(" 5 -Write to control registers\n"); |
ssmith73 | 1:384a1737b0df | 312 | pc.printf("\n"); |
ssmith73 | 1:384a1737b0df | 313 | pc.printf("\n Reference Voltage: %f\n", ref_voltage); |
ssmith73 | 1:384a1737b0df | 314 | |
ssmith73 | 1:384a1737b0df | 315 | } |
ssmith73 | 1:384a1737b0df | 316 | } |
ssmith73 | 1:384a1737b0df | 317 | |
ssmith73 | 1:384a1737b0df | 318 | static int getMenuSelect(uint8_t *menuSelect) { |
ssmith73 | 1:384a1737b0df | 319 | int inp = pc.scanf("%2d", (int *) menuSelect); |
ssmith73 | 1:384a1737b0df | 320 | |
ssmith73 | 1:384a1737b0df | 321 | if (inp == 0 || *menuSelect > 12) |
ssmith73 | 1:384a1737b0df | 322 | { |
ssmith73 | 1:384a1737b0df | 323 | pc.printf("\n\n***** Invalid entry: ignoring *****\n"); |
ssmith73 | 1:384a1737b0df | 324 | wait(WAIT_MENU_TIME); |
ssmith73 | 1:384a1737b0df | 325 | return FAILURE; |
ssmith73 | 1:384a1737b0df | 326 | } |
ssmith73 | 1:384a1737b0df | 327 | else |
ssmith73 | 1:384a1737b0df | 328 | return SUCCESS; |
ssmith73 | 1:384a1737b0df | 329 | } |
ssmith73 | 1:384a1737b0df | 330 | |
ssmith73 | 1:384a1737b0df | 331 | /*************************************************************************//** |
ssmith73 | 1:384a1737b0df | 332 | * @brief - Select active DAC - only one channel can be active |
ssmith73 | 1:384a1737b0df | 333 | * |
ssmith73 | 1:384a1737b0df | 334 | * @param selected_dac - only 1,2,4,8 (DAC A/B/C/D) are valid |
ssmith73 | 1:384a1737b0df | 335 | * |
ssmith73 | 1:384a1737b0df | 336 | * @return None. |
ssmith73 | 1:384a1737b0df | 337 | *****************************************************************************/ |
ssmith73 | 1:384a1737b0df | 338 | static uint8_t menu_1_select_dac(uint16_t *selected_dac) |
ssmith73 | 1:384a1737b0df | 339 | { |
ssmith73 | 1:384a1737b0df | 340 | char string[5] = "0001"; |
ssmith73 | 1:384a1737b0df | 341 | char *str = string; |
ssmith73 | 1:384a1737b0df | 342 | int8_t cnt = 3; |
ssmith73 | 1:384a1737b0df | 343 | uint16_t dac = 0; |
ssmith73 | 1:384a1737b0df | 344 | pc.printf(" \nDAC selections are represented in binary with\n"); |
ssmith73 | 1:384a1737b0df | 345 | pc.printf(" bits corresponding to: DCBA\n"); |
ssmith73 | 1:384a1737b0df | 346 | pc.printf(" (See datasheet for details and explanation)\n"); |
ssmith73 | 1:384a1737b0df | 347 | pc.printf(" For example:\n\n"); |
ssmith73 | 1:384a1737b0df | 348 | pc.printf(" 0001 - DAC A\n"); |
ssmith73 | 1:384a1737b0df | 349 | pc.printf(" 0010 - DAC B\n"); |
ssmith73 | 1:384a1737b0df | 350 | pc.printf(" 0100 - DAC C\n"); |
ssmith73 | 1:384a1737b0df | 351 | pc.printf(" 1000 - DAC D\n"); |
ssmith73 | 1:384a1737b0df | 352 | pc.printf(" i.e. Enter 4 binary digits. All other values will be ignored\n"); |
ssmith73 | 1:384a1737b0df | 353 | pc.printf(" Enter DAC to write to: "); |
ssmith73 | 1:384a1737b0df | 354 | pc.scanf("%4s", string); |
ssmith73 | 1:384a1737b0df | 355 | while (cnt >= 0) |
ssmith73 | 1:384a1737b0df | 356 | { |
ssmith73 | 1:384a1737b0df | 357 | if ((*str != '1' && *str != '0') || *str == '\0') |
ssmith73 | 1:384a1737b0df | 358 | { |
ssmith73 | 1:384a1737b0df | 359 | pc.printf("\nInvalid entry - exactly 4 binary characters are required\n"); |
ssmith73 | 1:384a1737b0df | 360 | dac = 0; |
ssmith73 | 1:384a1737b0df | 361 | break; |
ssmith73 | 1:384a1737b0df | 362 | } |
ssmith73 | 1:384a1737b0df | 363 | else if (*str++ == '1') |
ssmith73 | 1:384a1737b0df | 364 | dac |= (1 << cnt); |
ssmith73 | 1:384a1737b0df | 365 | cnt--; |
ssmith73 | 1:384a1737b0df | 366 | } |
ssmith73 | 1:384a1737b0df | 367 | |
ssmith73 | 1:384a1737b0df | 368 | switch (dac) |
ssmith73 | 1:384a1737b0df | 369 | { |
ssmith73 | 1:384a1737b0df | 370 | case 1:{ pc.printf("You selected DAC A\n"); *selected_dac = dac; break; } |
ssmith73 | 1:384a1737b0df | 371 | case 2:{ pc.printf("You selected DAC B\n"); *selected_dac = dac; break; } |
ssmith73 | 1:384a1737b0df | 372 | case 4:{ pc.printf("You selected DAC C\n"); *selected_dac = dac; break; } |
ssmith73 | 1:384a1737b0df | 373 | case 8:{ pc.printf("You selected DAC D\n"); *selected_dac = dac; break; } |
ssmith73 | 1:384a1737b0df | 374 | default: { |
ssmith73 | 1:384a1737b0df | 375 | pc.printf("\n Please select DAC in the format described above:\n"); |
ssmith73 | 1:384a1737b0df | 376 | pc.printf("\n No changes made:\n"); |
ssmith73 | 1:384a1737b0df | 377 | wait(2); |
ssmith73 | 1:384a1737b0df | 378 | } |
ssmith73 | 1:384a1737b0df | 379 | } |
ssmith73 | 1:384a1737b0df | 380 | |
ssmith73 | 1:384a1737b0df | 381 | return SUCCESS; // Always returns success, consider adding a fail code later. |
ssmith73 | 1:384a1737b0df | 382 | } |
ssmith73 | 1:384a1737b0df | 383 | |
ssmith73 | 1:384a1737b0df | 384 | /*************************************************************************//** |
ssmith73 | 1:384a1737b0df | 385 | * @brief Write to input-register - do not update DAC |
ssmith73 | 1:384a1737b0df | 386 | * |
ssmith73 | 1:384a1737b0df | 387 | * @param selected_dac - only 1,2,4,8 (DAC A/B/C/D) are valid |
ssmith73 | 1:384a1737b0df | 388 | * |
ssmith73 | 1:384a1737b0df | 389 | * @return None. |
ssmith73 | 1:384a1737b0df | 390 | *****************************************************************************/ |
ssmith73 | 1:384a1737b0df | 391 | static void menu_2_write_to_input_register(int16_t selected_dac, float vref) |
ssmith73 | 1:384a1737b0df | 392 | { |
ssmith73 | 1:384a1737b0df | 393 | unsigned short vdata = get_voltage_code(vref); |
ssmith73 | 1:384a1737b0df | 394 | enum ad5686_dac_channels |
ssmith73 | 1:384a1737b0df | 395 | chan = (ad5686_dac_channels)selectChannel(selected_dac); |
ssmith73 | 1:384a1737b0df | 396 | ad5686_write_register(device, chan, vdata); |
ssmith73 | 1:384a1737b0df | 397 | } |
ssmith73 | 1:384a1737b0df | 398 | |
ssmith73 | 1:384a1737b0df | 399 | /*************************************************************************//** |
ssmith73 | 1:384a1737b0df | 400 | * @brief - Updates DAC outputs from its input registers |
ssmith73 | 1:384a1737b0df | 401 | * |
ssmith73 | 1:384a1737b0df | 402 | * @param selected_dac - Selected DAC (1,2,4,8 -> DAC A,B,C,D) |
ssmith73 | 1:384a1737b0df | 403 | *****************************************************************************/ |
ssmith73 | 1:384a1737b0df | 404 | static void menu_3_update_dac(int16_t selected_dac) |
ssmith73 | 1:384a1737b0df | 405 | { |
ssmith73 | 1:384a1737b0df | 406 | enum ad5686_dac_channels |
ssmith73 | 1:384a1737b0df | 407 | chan = (ad5686_dac_channels)selectChannel(selected_dac); |
ssmith73 | 1:384a1737b0df | 408 | ad5686_update_register(device, chan); |
ssmith73 | 1:384a1737b0df | 409 | pc.printf(" Updated DAC(s)\n"); |
ssmith73 | 1:384a1737b0df | 410 | wait(3); |
ssmith73 | 1:384a1737b0df | 411 | } |
ssmith73 | 1:384a1737b0df | 412 | |
ssmith73 | 1:384a1737b0df | 413 | /*************************************************************************//** |
ssmith73 | 1:384a1737b0df | 414 | * @brief - Write a value to the select_dac |
ssmith73 | 1:384a1737b0df | 415 | * |
ssmith73 | 1:384a1737b0df | 416 | * @param selected_dac - Selected DAC (1,2,4,8 -> DAC A,B,C,D) |
ssmith73 | 1:384a1737b0df | 417 | * @param vRef - Reference voltage |
ssmith73 | 1:384a1737b0df | 418 | *****************************************************************************/ |
ssmith73 | 1:384a1737b0df | 419 | static void menu_4_write_and_update_dac(int16_t selected_dac, float vref) |
ssmith73 | 1:384a1737b0df | 420 | { |
ssmith73 | 1:384a1737b0df | 421 | unsigned short vdata = get_voltage_code(vref); |
ssmith73 | 1:384a1737b0df | 422 | |
ssmith73 | 1:384a1737b0df | 423 | pc.printf("Entered code: %x ", vdata); |
ssmith73 | 1:384a1737b0df | 424 | |
ssmith73 | 1:384a1737b0df | 425 | //!Convert from user-selected 2**n dac-select to the enum |
ssmith73 | 1:384a1737b0df | 426 | //!used in the NoOS driver |
ssmith73 | 1:384a1737b0df | 427 | enum ad5686_dac_channels |
ssmith73 | 1:384a1737b0df | 428 | chan = (ad5686_dac_channels)selectChannel(selected_dac); |
ssmith73 | 1:384a1737b0df | 429 | |
ssmith73 | 1:384a1737b0df | 430 | ad5686_write_update_register(device, chan, vdata); |
ssmith73 | 1:384a1737b0df | 431 | } |
ssmith73 | 1:384a1737b0df | 432 | |
ssmith73 | 1:384a1737b0df | 433 | /*************************************************************************//** |
ssmith73 | 1:384a1737b0df | 434 | * @brief - Write a value to the select_dac |
ssmith73 | 1:384a1737b0df | 435 | * |
ssmith73 | 1:384a1737b0df | 436 | * @param selected_dac - Selected DAC (1,2,4,8 -> DAC A,B,C,D) |
ssmith73 | 1:384a1737b0df | 437 | * @param vRef - Reference voltage |
ssmith73 | 1:384a1737b0df | 438 | * |
ssmith73 | 1:384a1737b0df | 439 | * @return - currently active channel |
ssmith73 | 1:384a1737b0df | 440 | *****************************************************************************/ |
ssmith73 | 1:384a1737b0df | 441 | static uint8_t selectChannel(uint16_t selectedDac) |
ssmith73 | 1:384a1737b0df | 442 | { |
ssmith73 | 1:384a1737b0df | 443 | enum ad5686_dac_channels chan; |
ssmith73 | 1:384a1737b0df | 444 | switch (selectedDac) |
ssmith73 | 1:384a1737b0df | 445 | { |
ssmith73 | 1:384a1737b0df | 446 | case 1 : {chan = AD5686_CH_0; break; } |
ssmith73 | 1:384a1737b0df | 447 | case 2 : {chan = AD5686_CH_1; break; } |
ssmith73 | 1:384a1737b0df | 448 | case 4 : {chan = AD5686_CH_2; break; } |
ssmith73 | 1:384a1737b0df | 449 | case 8 : {chan = AD5686_CH_3; break; } |
ssmith73 | 1:384a1737b0df | 450 | case 16 : {chan = AD5686_CH_4; break; } |
ssmith73 | 1:384a1737b0df | 451 | case 32 : {chan = AD5686_CH_5; break; } |
ssmith73 | 1:384a1737b0df | 452 | case 64 : {chan = AD5686_CH_6; break; } |
ssmith73 | 1:384a1737b0df | 453 | case 128: {chan = AD5686_CH_7; break; } |
ssmith73 | 1:384a1737b0df | 454 | default: { |
ssmith73 | 1:384a1737b0df | 455 | break; |
ssmith73 | 1:384a1737b0df | 456 | } |
ssmith73 | 1:384a1737b0df | 457 | } |
ssmith73 | 1:384a1737b0df | 458 | return (uint8_t)chan; |
ssmith73 | 1:384a1737b0df | 459 | |
ssmith73 | 1:384a1737b0df | 460 | } |
ssmith73 | 1:384a1737b0df | 461 | |
ssmith73 | 1:384a1737b0df | 462 | /*************************************************************************//** |
ssmith73 | 1:384a1737b0df | 463 | * @brief - Configure the power-down mode |
ssmith73 | 1:384a1737b0df | 464 | * |
ssmith73 | 1:384a1737b0df | 465 | * @param selected_dac - Selected DAC (1,2,4,8 -> DAC A,B,C,D) |
ssmith73 | 1:384a1737b0df | 466 | * @param vRef - Reference voltage |
ssmith73 | 1:384a1737b0df | 467 | * |
ssmith73 | 1:384a1737b0df | 468 | * @return - PASS/FAIL flag - currently unused |
ssmith73 | 1:384a1737b0df | 469 | *****************************************************************************/ |
ssmith73 | 1:384a1737b0df | 470 | static int8_t menu_5_power_down_mode(int16_t selected_dac) |
ssmith73 | 1:384a1737b0df | 471 | { |
ssmith73 | 1:384a1737b0df | 472 | // Cancel if no DAC selected |
ssmith73 | 1:384a1737b0df | 473 | if(selected_dac == 0) |
ssmith73 | 1:384a1737b0df | 474 | { |
ssmith73 | 1:384a1737b0df | 475 | pc.printf(" No DAC selected, no changes made"); |
ssmith73 | 1:384a1737b0df | 476 | return FAILURE; |
ssmith73 | 1:384a1737b0df | 477 | } |
ssmith73 | 1:384a1737b0df | 478 | |
ssmith73 | 1:384a1737b0df | 479 | // Prompt for power mode |
ssmith73 | 1:384a1737b0df | 480 | printf("\n Power Modes:\n"); |
ssmith73 | 1:384a1737b0df | 481 | printf(" 1-Normal Operation\n"); |
ssmith73 | 1:384a1737b0df | 482 | printf(" 2-1kOhm to GND Power-Down\n"); |
ssmith73 | 1:384a1737b0df | 483 | printf(" 3-100kOhm to GND Power-Down\n"); |
ssmith73 | 1:384a1737b0df | 484 | printf(" 4-Three-State Power-Down\n"); |
ssmith73 | 1:384a1737b0df | 485 | printf(" Select a power mode: "); |
ssmith73 | 1:384a1737b0df | 486 | |
ssmith73 | 1:384a1737b0df | 487 | // Get input |
ssmith73 | 1:384a1737b0df | 488 | int mode_input; |
ssmith73 | 1:384a1737b0df | 489 | int inp = pc.scanf("%1d", &mode_input); |
ssmith73 | 1:384a1737b0df | 490 | if (inp == 0) |
ssmith73 | 1:384a1737b0df | 491 | { |
ssmith73 | 1:384a1737b0df | 492 | pc .printf("***** Invalid entry: ignoring *****\n"); |
ssmith73 | 1:384a1737b0df | 493 | wait(3); |
ssmith73 | 1:384a1737b0df | 494 | } |
ssmith73 | 1:384a1737b0df | 495 | |
ssmith73 | 1:384a1737b0df | 496 | |
ssmith73 | 1:384a1737b0df | 497 | uint8_t selected_mode = 0; |
ssmith73 | 1:384a1737b0df | 498 | |
ssmith73 | 1:384a1737b0df | 499 | // Basic input validation |
ssmith73 | 1:384a1737b0df | 500 | if(mode_input > 4 || mode_input < 1) |
ssmith73 | 1:384a1737b0df | 501 | { |
ssmith73 | 1:384a1737b0df | 502 | printf("Invalid selection - no changes made\n"); |
ssmith73 | 1:384a1737b0df | 503 | wait(2); |
ssmith73 | 1:384a1737b0df | 504 | return SUCCESS; |
ssmith73 | 1:384a1737b0df | 505 | } |
ssmith73 | 1:384a1737b0df | 506 | else |
ssmith73 | 1:384a1737b0df | 507 | // Show validated input on console |
ssmith73 | 1:384a1737b0df | 508 | pc.printf("\nSelected DAC: %d", mode_input); |
ssmith73 | 1:384a1737b0df | 509 | |
ssmith73 | 1:384a1737b0df | 510 | // Select proper power mode |
ssmith73 | 1:384a1737b0df | 511 | switch(mode_input) |
ssmith73 | 1:384a1737b0df | 512 | { |
ssmith73 | 1:384a1737b0df | 513 | case 1: |
ssmith73 | 1:384a1737b0df | 514 | selected_mode = AD5686_PWRM_NORMAL; |
ssmith73 | 1:384a1737b0df | 515 | break; |
ssmith73 | 1:384a1737b0df | 516 | |
ssmith73 | 1:384a1737b0df | 517 | case 2: |
ssmith73 | 1:384a1737b0df | 518 | selected_mode = AD5686_PWRM_1K; |
ssmith73 | 1:384a1737b0df | 519 | break; |
ssmith73 | 1:384a1737b0df | 520 | |
ssmith73 | 1:384a1737b0df | 521 | case 3: |
ssmith73 | 1:384a1737b0df | 522 | selected_mode = AD5686_PWRM_100K; |
ssmith73 | 1:384a1737b0df | 523 | break; |
ssmith73 | 1:384a1737b0df | 524 | |
ssmith73 | 1:384a1737b0df | 525 | case 4: |
ssmith73 | 1:384a1737b0df | 526 | selected_mode = AD5686_PWRM_THREESTATE; |
ssmith73 | 1:384a1737b0df | 527 | break; |
ssmith73 | 1:384a1737b0df | 528 | |
ssmith73 | 1:384a1737b0df | 529 | default: |
ssmith73 | 1:384a1737b0df | 530 | break; |
ssmith73 | 1:384a1737b0df | 531 | } |
ssmith73 | 1:384a1737b0df | 532 | |
ssmith73 | 1:384a1737b0df | 533 | // Check bit-wise which DACs are selected |
ssmith73 | 1:384a1737b0df | 534 | uint8_t dac1 = (selected_dac >> 0) & 1; |
ssmith73 | 1:384a1737b0df | 535 | uint8_t dac2 = (selected_dac >> 1) & 1; |
ssmith73 | 1:384a1737b0df | 536 | uint8_t dac3 = (selected_dac >> 2) & 1; |
ssmith73 | 1:384a1737b0df | 537 | uint8_t dac4 = (selected_dac >> 3) & 1; |
ssmith73 | 1:384a1737b0df | 538 | |
ssmith73 | 1:384a1737b0df | 539 | // Apply power to selected DACS |
ssmith73 | 1:384a1737b0df | 540 | if(dac1) |
ssmith73 | 1:384a1737b0df | 541 | { |
ssmith73 | 1:384a1737b0df | 542 | pc.printf(" Applying power mode to DAC A..."); |
ssmith73 | 1:384a1737b0df | 543 | ad5686_power_mode(device, AD5686_CH_0, selected_mode); |
ssmith73 | 1:384a1737b0df | 544 | } |
ssmith73 | 1:384a1737b0df | 545 | if (dac2) |
ssmith73 | 1:384a1737b0df | 546 | { |
ssmith73 | 1:384a1737b0df | 547 | pc.printf(" Applying power mode to DAC B..."); |
ssmith73 | 1:384a1737b0df | 548 | ad5686_power_mode(device, AD5686_CH_1, selected_mode); |
ssmith73 | 1:384a1737b0df | 549 | } |
ssmith73 | 1:384a1737b0df | 550 | if (dac3) |
ssmith73 | 1:384a1737b0df | 551 | { |
ssmith73 | 1:384a1737b0df | 552 | pc.printf(" Applying power mode to DAC C..."); |
ssmith73 | 1:384a1737b0df | 553 | ad5686_power_mode(device, AD5686_CH_2, selected_mode); |
ssmith73 | 1:384a1737b0df | 554 | } |
ssmith73 | 1:384a1737b0df | 555 | if (dac4) |
ssmith73 | 1:384a1737b0df | 556 | { |
ssmith73 | 1:384a1737b0df | 557 | pc.printf(" Applying power mode to DAC D..."); |
ssmith73 | 1:384a1737b0df | 558 | ad5686_power_mode(device, AD5686_CH_3, selected_mode); |
ssmith73 | 1:384a1737b0df | 559 | } |
ssmith73 | 1:384a1737b0df | 560 | |
ssmith73 | 1:384a1737b0df | 561 | pc.printf(" Done!"); |
ssmith73 | 1:384a1737b0df | 562 | |
ssmith73 | 1:384a1737b0df | 563 | return SUCCESS; |
ssmith73 | 1:384a1737b0df | 564 | } |
ssmith73 | 1:384a1737b0df | 565 | |
ssmith73 | 1:384a1737b0df | 566 | /*************************************************************************//** |
ssmith73 | 1:384a1737b0df | 567 | * @brief - Select internal or external reference |
ssmith73 | 1:384a1737b0df | 568 | * |
ssmith73 | 1:384a1737b0df | 569 | * @param vRef - Reference voltage |
ssmith73 | 1:384a1737b0df | 570 | * |
ssmith73 | 1:384a1737b0df | 571 | * @return - PASS/FAIL flag - currently unused |
ssmith73 | 1:384a1737b0df | 572 | *****************************************************************************/ |
ssmith73 | 1:384a1737b0df | 573 | static int8_t menu_6_select_ref_voltage(float *vref) |
ssmith73 | 1:384a1737b0df | 574 | { |
ssmith73 | 1:384a1737b0df | 575 | // Prompt for internal or external |
ssmith73 | 1:384a1737b0df | 576 | pc.printf(" 1-Internal (2.5v)\n"); |
ssmith73 | 1:384a1737b0df | 577 | pc.printf(" 2-External\n"); |
ssmith73 | 1:384a1737b0df | 578 | pc.printf(" Select a reference voltage source:"); |
ssmith73 | 1:384a1737b0df | 579 | |
ssmith73 | 1:384a1737b0df | 580 | int vref_source; |
ssmith73 | 1:384a1737b0df | 581 | int inp = pc.scanf("%1d", &vref_source); |
ssmith73 | 1:384a1737b0df | 582 | if (inp == 0) |
ssmith73 | 1:384a1737b0df | 583 | { |
ssmith73 | 1:384a1737b0df | 584 | pc .printf("***** Invalid entry: ignoring *****\n"); |
ssmith73 | 1:384a1737b0df | 585 | wait(3); |
ssmith73 | 1:384a1737b0df | 586 | } |
ssmith73 | 1:384a1737b0df | 587 | |
ssmith73 | 1:384a1737b0df | 588 | if (vref_source == INTERNAL) |
ssmith73 | 1:384a1737b0df | 589 | pc.printf("\nInternal selected\n"); |
ssmith73 | 1:384a1737b0df | 590 | else if (vref_source == EXTERNAL) |
ssmith73 | 1:384a1737b0df | 591 | pc.printf(" External selected\n"); |
ssmith73 | 1:384a1737b0df | 592 | else |
ssmith73 | 1:384a1737b0df | 593 | { |
ssmith73 | 1:384a1737b0df | 594 | pc.printf("\nInvalid Entry: No changes made\n"); |
ssmith73 | 1:384a1737b0df | 595 | wait(2); |
ssmith73 | 1:384a1737b0df | 596 | return FAILURE; |
ssmith73 | 1:384a1737b0df | 597 | } |
ssmith73 | 1:384a1737b0df | 598 | |
ssmith73 | 1:384a1737b0df | 599 | |
ssmith73 | 1:384a1737b0df | 600 | float fvref = 0; // Custom vref |
ssmith73 | 1:384a1737b0df | 601 | |
ssmith73 | 1:384a1737b0df | 602 | switch(vref_source) |
ssmith73 | 1:384a1737b0df | 603 | { |
ssmith73 | 1:384a1737b0df | 604 | case 1: |
ssmith73 | 1:384a1737b0df | 605 | *vref = 2.5; |
ssmith73 | 1:384a1737b0df | 606 | break; |
ssmith73 | 1:384a1737b0df | 607 | |
ssmith73 | 1:384a1737b0df | 608 | case 2: |
ssmith73 | 1:384a1737b0df | 609 | // If external, prompt for exact vref |
ssmith73 | 1:384a1737b0df | 610 | pc.printf(" Enter selected external reference voltage:"); |
ssmith73 | 1:384a1737b0df | 611 | inp = pc.scanf("%f", &fvref); |
ssmith73 | 1:384a1737b0df | 612 | if (inp == 0) |
ssmith73 | 1:384a1737b0df | 613 | { |
ssmith73 | 1:384a1737b0df | 614 | pc .printf("***** Invalid entry: ignoring *****\n"); |
ssmith73 | 1:384a1737b0df | 615 | wait(3); |
ssmith73 | 1:384a1737b0df | 616 | } |
ssmith73 | 1:384a1737b0df | 617 | pc.printf("%fV selected\n", fvref); |
ssmith73 | 1:384a1737b0df | 618 | *vref = fvref; |
ssmith73 | 1:384a1737b0df | 619 | wait(2); |
ssmith73 | 1:384a1737b0df | 620 | break; |
ssmith73 | 1:384a1737b0df | 621 | |
ssmith73 | 1:384a1737b0df | 622 | default: |
ssmith73 | 1:384a1737b0df | 623 | pc.printf(" Incorrect entry\n"); |
ssmith73 | 1:384a1737b0df | 624 | wait(2); |
ssmith73 | 1:384a1737b0df | 625 | break; |
ssmith73 | 1:384a1737b0df | 626 | } |
ssmith73 | 1:384a1737b0df | 627 | |
ssmith73 | 1:384a1737b0df | 628 | return SUCCESS; |
ssmith73 | 1:384a1737b0df | 629 | |
ssmith73 | 1:384a1737b0df | 630 | } |
ssmith73 | 1:384a1737b0df | 631 | |
ssmith73 | 1:384a1737b0df | 632 | |
ssmith73 | 1:384a1737b0df | 633 | /*************************************************************************//** |
ssmith73 | 1:384a1737b0df | 634 | * @brief - Reads back all DAC registers |
ssmith73 | 1:384a1737b0df | 635 | *****************************************************************************/ |
ssmith73 | 1:384a1737b0df | 636 | static void menu_7_read_back_registers() |
ssmith73 | 1:384a1737b0df | 637 | { |
ssmith73 | 1:384a1737b0df | 638 | uint32_t reg1 = ad5686_read_back_register(device, AD5686_CH_0); |
ssmith73 | 1:384a1737b0df | 639 | uint32_t reg2 = ad5686_read_back_register(device, AD5686_CH_1); |
ssmith73 | 1:384a1737b0df | 640 | uint32_t reg3 = ad5686_read_back_register(device, AD5686_CH_2); |
ssmith73 | 1:384a1737b0df | 641 | uint32_t reg4 = ad5686_read_back_register(device, AD5686_CH_3); |
ssmith73 | 1:384a1737b0df | 642 | |
ssmith73 | 1:384a1737b0df | 643 | pc.printf("\n All DAC register values:\n"); |
ssmith73 | 1:384a1737b0df | 644 | pc.printf(" DAC A - %x\n", (unsigned int) reg1); |
ssmith73 | 1:384a1737b0df | 645 | pc.printf(" DAC B - %x\n", (unsigned int) reg2); |
ssmith73 | 1:384a1737b0df | 646 | pc.printf(" DAC C - %x\n", (unsigned int) reg3); |
ssmith73 | 1:384a1737b0df | 647 | pc.printf(" DAC D - %x\n", (unsigned int) reg4); |
ssmith73 | 1:384a1737b0df | 648 | } |
ssmith73 | 1:384a1737b0df | 649 | |
ssmith73 | 1:384a1737b0df | 650 | /*************************************************************************//** |
ssmith73 | 1:384a1737b0df | 651 | * @brief - Set the LDAC mask bits |
ssmith73 | 1:384a1737b0df | 652 | * |
ssmith73 | 1:384a1737b0df | 653 | * @return - PASS/FAIL flag - currently unused |
ssmith73 | 1:384a1737b0df | 654 | *****************************************************************************/ |
ssmith73 | 1:384a1737b0df | 655 | static int8_t menu_8_set_ldac_mask() |
ssmith73 | 1:384a1737b0df | 656 | { |
ssmith73 | 1:384a1737b0df | 657 | int enable; |
ssmith73 | 1:384a1737b0df | 658 | int inp; |
ssmith73 | 1:384a1737b0df | 659 | enum ad5686_dac_channels channel; |
ssmith73 | 1:384a1737b0df | 660 | |
ssmith73 | 1:384a1737b0df | 661 | pc.printf("\n Enter channel to update as a decimal number (0-n)\n"); |
ssmith73 | 1:384a1737b0df | 662 | pc.printf(" If masked, selected channel will not be updated when LDAC is asserted\n"); |
ssmith73 | 1:384a1737b0df | 663 | pc.printf(" Channel: "); |
ssmith73 | 1:384a1737b0df | 664 | inp = pc.scanf("%2d", (int *)&channel); |
ssmith73 | 1:384a1737b0df | 665 | if (inp == 0) |
ssmith73 | 1:384a1737b0df | 666 | { |
ssmith73 | 1:384a1737b0df | 667 | pc .printf("***** Invalid entry: ignoring *****\n"); |
ssmith73 | 1:384a1737b0df | 668 | wait(3); |
ssmith73 | 1:384a1737b0df | 669 | } |
ssmith73 | 1:384a1737b0df | 670 | |
ssmith73 | 1:384a1737b0df | 671 | pc.printf("\n Enter LDAC mask value (1 set, 0 clear) as a decimal number\n"); |
ssmith73 | 1:384a1737b0df | 672 | pc.printf(" Mask: "); |
ssmith73 | 1:384a1737b0df | 673 | |
ssmith73 | 1:384a1737b0df | 674 | inp = pc.scanf("%1d", &enable); |
ssmith73 | 1:384a1737b0df | 675 | if (inp == 0) |
ssmith73 | 1:384a1737b0df | 676 | { |
ssmith73 | 1:384a1737b0df | 677 | pc .printf("***** Invalid entry: ignoring *****\n"); |
ssmith73 | 1:384a1737b0df | 678 | wait(3); |
ssmith73 | 1:384a1737b0df | 679 | } |
ssmith73 | 1:384a1737b0df | 680 | |
ssmith73 | 1:384a1737b0df | 681 | switch (enable) |
ssmith73 | 1:384a1737b0df | 682 | { |
ssmith73 | 1:384a1737b0df | 683 | case 0: pc.printf("\n\nClearing LDAC mask\n"); break; |
ssmith73 | 1:384a1737b0df | 684 | case 1: pc.printf("\n\nSettting LDAC mask\n"); break; |
ssmith73 | 1:384a1737b0df | 685 | default: |
ssmith73 | 1:384a1737b0df | 686 | pc.printf("\n\nInvalid entry: No changes made\n"); |
ssmith73 | 1:384a1737b0df | 687 | return FAILURE; |
ssmith73 | 1:384a1737b0df | 688 | } |
ssmith73 | 1:384a1737b0df | 689 | |
ssmith73 | 1:384a1737b0df | 690 | if (enable) pc.printf("\n\nSet LDAC mask for channel %d\n", channel); |
ssmith73 | 1:384a1737b0df | 691 | else pc.printf("Cleared LDAC mask for channel %d\n", channel); |
ssmith73 | 1:384a1737b0df | 692 | |
ssmith73 | 1:384a1737b0df | 693 | ad5686_ldac_mask(device, channel, enable); |
ssmith73 | 1:384a1737b0df | 694 | |
ssmith73 | 1:384a1737b0df | 695 | return SUCCESS; |
ssmith73 | 1:384a1737b0df | 696 | } |
ssmith73 | 1:384a1737b0df | 697 | |
ssmith73 | 1:384a1737b0df | 698 | /*************************************************************************//** |
ssmith73 | 1:384a1737b0df | 699 | * @brief - Toggle the LDAC pin and update the DAC(s) |
ssmith73 | 1:384a1737b0df | 700 | *****************************************************************************/ |
ssmith73 | 1:384a1737b0df | 701 | static void menu_9_assert_ldac() |
ssmith73 | 1:384a1737b0df | 702 | { |
ssmith73 | 1:384a1737b0df | 703 | gpio_direction_output(device->gpio_ldac, GPIO_LOW); |
ssmith73 | 1:384a1737b0df | 704 | // Wait to allow for controller latency |
ssmith73 | 1:384a1737b0df | 705 | wait(0.1); |
ssmith73 | 1:384a1737b0df | 706 | gpio_direction_output(device->gpio_ldac, GPIO_HIGH); |
ssmith73 | 1:384a1737b0df | 707 | pc.printf(" Asserted LDAC\n"); |
ssmith73 | 1:384a1737b0df | 708 | } |
ssmith73 | 1:384a1737b0df | 709 | |
ssmith73 | 1:384a1737b0df | 710 | /*************************************************************************//** |
ssmith73 | 1:384a1737b0df | 711 | * @brief - Set the GAIN pin |
ssmith73 | 1:384a1737b0df | 712 | * |
ssmith73 | 1:384a1737b0df | 713 | * @return - PASS/FAIL flag - currently unused |
ssmith73 | 1:384a1737b0df | 714 | *****************************************************************************/ |
ssmith73 | 1:384a1737b0df | 715 | static int8_t menu_10_set_gain() |
ssmith73 | 1:384a1737b0df | 716 | { |
ssmith73 | 1:384a1737b0df | 717 | pc.printf(" GAIN options: \n"); |
ssmith73 | 1:384a1737b0df | 718 | pc.printf(" 1-Low gain (1x)\n"); |
ssmith73 | 1:384a1737b0df | 719 | pc.printf(" 2-High gain (2x)\n"); |
ssmith73 | 1:384a1737b0df | 720 | pc.printf(" Make selection: \n"); |
ssmith73 | 1:384a1737b0df | 721 | |
ssmith73 | 1:384a1737b0df | 722 | int selected_gain; |
ssmith73 | 1:384a1737b0df | 723 | int inp = pc.scanf("%d", &selected_gain); |
ssmith73 | 1:384a1737b0df | 724 | if (inp == 0) |
ssmith73 | 1:384a1737b0df | 725 | { |
ssmith73 | 1:384a1737b0df | 726 | pc .printf("***** Invalid entry: ignoring *****\n"); |
ssmith73 | 1:384a1737b0df | 727 | wait(3); |
ssmith73 | 1:384a1737b0df | 728 | } |
ssmith73 | 1:384a1737b0df | 729 | if (selected_gain > 2 || selected_gain < 1) |
ssmith73 | 1:384a1737b0df | 730 | { |
ssmith73 | 1:384a1737b0df | 731 | pc.printf("Invalid selection - no changes made\n"); |
ssmith73 | 1:384a1737b0df | 732 | wait(2); |
ssmith73 | 1:384a1737b0df | 733 | return FAILURE; |
ssmith73 | 1:384a1737b0df | 734 | } |
ssmith73 | 1:384a1737b0df | 735 | else |
ssmith73 | 1:384a1737b0df | 736 | pc.printf("Selected gain: %d\n", selected_gain); |
ssmith73 | 1:384a1737b0df | 737 | |
ssmith73 | 1:384a1737b0df | 738 | |
ssmith73 | 1:384a1737b0df | 739 | switch (selected_gain) |
ssmith73 | 1:384a1737b0df | 740 | { |
ssmith73 | 1:384a1737b0df | 741 | case 1: |
ssmith73 | 1:384a1737b0df | 742 | //AD5686_GAIN_LOW; |
ssmith73 | 1:384a1737b0df | 743 | gpio_gain.id = NOT_USED; |
ssmith73 | 1:384a1737b0df | 744 | gpio_set_value(&gpio_gain, GPIO_LOW); |
ssmith73 | 1:384a1737b0df | 745 | pc.printf(" Setting gain low"); |
ssmith73 | 1:384a1737b0df | 746 | gpioGainVal = 1; |
ssmith73 | 1:384a1737b0df | 747 | |
ssmith73 | 1:384a1737b0df | 748 | break; |
ssmith73 | 1:384a1737b0df | 749 | |
ssmith73 | 1:384a1737b0df | 750 | case 2: |
ssmith73 | 1:384a1737b0df | 751 | gpio_set_value(device->gpio_gain, GPIO_HIGH); |
ssmith73 | 1:384a1737b0df | 752 | pc.printf(" Setting gain high\n"); |
ssmith73 | 1:384a1737b0df | 753 | gpioGainVal = 0; |
ssmith73 | 1:384a1737b0df | 754 | break; |
ssmith73 | 1:384a1737b0df | 755 | |
ssmith73 | 1:384a1737b0df | 756 | default: |
ssmith73 | 1:384a1737b0df | 757 | break; |
ssmith73 | 1:384a1737b0df | 758 | } |
ssmith73 | 1:384a1737b0df | 759 | |
ssmith73 | 1:384a1737b0df | 760 | return SUCCESS; |
ssmith73 | 1:384a1737b0df | 761 | } |
ssmith73 | 1:384a1737b0df | 762 | |
ssmith73 | 1:384a1737b0df | 763 | /*************************************************************************//** |
ssmith73 | 1:384a1737b0df | 764 | * @brief - Do a soft (software) reset |
ssmith73 | 1:384a1737b0df | 765 | *****************************************************************************/ |
ssmith73 | 1:384a1737b0df | 766 | static void menu_11_assert_soft_reset() |
ssmith73 | 1:384a1737b0df | 767 | { |
ssmith73 | 1:384a1737b0df | 768 | pc.printf(" Performing software reset\n"); |
ssmith73 | 1:384a1737b0df | 769 | ad5686_software_reset(device); |
ssmith73 | 1:384a1737b0df | 770 | } |
ssmith73 | 1:384a1737b0df | 771 | |
ssmith73 | 1:384a1737b0df | 772 | /*************************************************************************//** |
ssmith73 | 1:384a1737b0df | 773 | * @brief - Do a hardware (gpio) reset |
ssmith73 | 1:384a1737b0df | 774 | *****************************************************************************/ |
ssmith73 | 1:384a1737b0df | 775 | static void menu_12_assert_hard_reset() |
ssmith73 | 1:384a1737b0df | 776 | { |
ssmith73 | 1:384a1737b0df | 777 | // Pull reset low then high |
ssmith73 | 1:384a1737b0df | 778 | pc.printf(" Performing hardware reset\n"); |
ssmith73 | 1:384a1737b0df | 779 | gpio_direction_output(device->gpio_reset, GPIO_LOW); |
ssmith73 | 1:384a1737b0df | 780 | // Wait to allow for controller latency |
ssmith73 | 1:384a1737b0df | 781 | wait(0.1); |
ssmith73 | 1:384a1737b0df | 782 | gpio_set_value(device->gpio_reset, GPIO_HIGH); |
ssmith73 | 1:384a1737b0df | 783 | |
ssmith73 | 1:384a1737b0df | 784 | } |
ssmith73 | 1:384a1737b0df | 785 | |
ssmith73 | 1:384a1737b0df | 786 | /*************************************************************************//** |
ssmith73 | 1:384a1737b0df | 787 | * @brief - Configure the GPIO pins to default values |
ssmith73 | 1:384a1737b0df | 788 | *****************************************************************************/ |
ssmith73 | 1:384a1737b0df | 789 | static void set_default_pins() |
ssmith73 | 1:384a1737b0df | 790 | { |
ssmith73 | 1:384a1737b0df | 791 | gpio_set_value(device->gpio_reset, GPIO_HIGH); |
ssmith73 | 1:384a1737b0df | 792 | gpio_set_value(device->gpio_gain, GPIO_LOW); |
ssmith73 | 1:384a1737b0df | 793 | gpio_set_value(device->gpio_ldac, GPIO_LOW); |
ssmith73 | 1:384a1737b0df | 794 | } |
ssmith73 | 1:384a1737b0df | 795 | |
ssmith73 | 1:384a1737b0df | 796 | /*************************************************************************//** |
ssmith73 | 1:384a1737b0df | 797 | * @brief Gets a voltage from the user and converts |
ssmith73 | 1:384a1737b0df | 798 | * it to the code the DAC understands |
ssmith73 | 1:384a1737b0df | 799 | * |
ssmith73 | 1:384a1737b0df | 800 | * @param vRef - voltage value to convert - 0 to VREF |
ssmith73 | 1:384a1737b0df | 801 | * |
ssmith73 | 1:384a1737b0df | 802 | * @return - corrosponding code value |
ssmith73 | 1:384a1737b0df | 803 | *****************************************************************************/ |
ssmith73 | 1:384a1737b0df | 804 | static uint16_t get_voltage_code(float vRef) |
ssmith73 | 1:384a1737b0df | 805 | { |
ssmith73 | 1:384a1737b0df | 806 | return voltage_to_code(get_voltage_float(), vRef); |
ssmith73 | 1:384a1737b0df | 807 | } |
ssmith73 | 1:384a1737b0df | 808 | |
ssmith73 | 1:384a1737b0df | 809 | /*************************************************************************//** |
ssmith73 | 1:384a1737b0df | 810 | * @brief - Convert voltage float to code the DAC understands |
ssmith73 | 1:384a1737b0df | 811 | * |
ssmith73 | 1:384a1737b0df | 812 | * @param voltage - voltage value to convert - 0 to VREF |
ssmith73 | 1:384a1737b0df | 813 | * @param vRef - Reference voltage |
ssmith73 | 1:384a1737b0df | 814 | * |
ssmith73 | 1:384a1737b0df | 815 | * @return - corrosponding code value |
ssmith73 | 1:384a1737b0df | 816 | *****************************************************************************/ |
ssmith73 | 1:384a1737b0df | 817 | static uint16_t voltage_to_code(float voltage, float vRef) |
ssmith73 | 1:384a1737b0df | 818 | { |
ssmith73 | 1:384a1737b0df | 819 | |
ssmith73 | 1:384a1737b0df | 820 | pc.printf("GAIN PIN STATE: %d\n", gpioGainVal); |
ssmith73 | 1:384a1737b0df | 821 | if (gpioGainVal == GPIO_HIGH) |
ssmith73 | 1:384a1737b0df | 822 | vRef *= 2; |
ssmith73 | 1:384a1737b0df | 823 | |
ssmith73 | 1:384a1737b0df | 824 | uint32_t max_code = ((uint32_t)1 << 16) - 1; |
ssmith73 | 1:384a1737b0df | 825 | return (unsigned short)(voltage * (float)max_code / vRef); |
ssmith73 | 1:384a1737b0df | 826 | } |
ssmith73 | 1:384a1737b0df | 827 | |
ssmith73 | 1:384a1737b0df | 828 | /*************************************************************************//** |
ssmith73 | 1:384a1737b0df | 829 | * @brief - Gets a voltage from the user |
ssmith73 | 1:384a1737b0df | 830 | * |
ssmith73 | 1:384a1737b0df | 831 | * @return - voltage as a float |
ssmith73 | 1:384a1737b0df | 832 | *****************************************************************************/ |
ssmith73 | 1:384a1737b0df | 833 | static float get_voltage_float() |
ssmith73 | 1:384a1737b0df | 834 | { |
ssmith73 | 1:384a1737b0df | 835 | float dac_voltage; |
ssmith73 | 1:384a1737b0df | 836 | pc.printf(" Enter Desired DAC output voltage: "); |
ssmith73 | 1:384a1737b0df | 837 | wait_ms(1); |
ssmith73 | 1:384a1737b0df | 838 | int inp = pc.scanf("%f", &dac_voltage); |
ssmith73 | 1:384a1737b0df | 839 | if (inp == 0) |
ssmith73 | 1:384a1737b0df | 840 | { |
ssmith73 | 1:384a1737b0df | 841 | pc .printf("***** Invalid entry: ignoring *****\n"); |
ssmith73 | 1:384a1737b0df | 842 | wait(3); |
ssmith73 | 1:384a1737b0df | 843 | } |
ssmith73 | 1:384a1737b0df | 844 | pc.printf("%f V\n", dac_voltage); |
ssmith73 | 1:384a1737b0df | 845 | |
ssmith73 | 1:384a1737b0df | 846 | return dac_voltage; |
ssmith73 | 1:384a1737b0df | 847 | } |
ssmith73 | 1:384a1737b0df | 848 |