| 1 | /***************************************************************************//** |
| 2 | * @file Command.c |
| 3 | * @brief Implementation of the commands given by user through UART for PmodAD2. |
| 4 | * @author DNechita ([email protected]) |
| 5 | ******************************************************************************** |
| 6 | * Copyright 2013(c) Analog Devices, Inc. |
| 7 | * |
| 8 | * All rights reserved. |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions are met: |
| 12 | * - Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * - Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in |
| 16 | * the documentation and/or other materials provided with the |
| 17 | * distribution. |
| 18 | * - Neither the name of Analog Devices, Inc. nor the names of its |
| 19 | * contributors may be used to endorse or promote products derived |
| 20 | * from this software without specific prior written permission. |
| 21 | * - The use of this software may or may not infringe the patent rights |
| 22 | * of one or more patent holders. This license does not release you |
| 23 | * from the requirement that you obtain separate licenses from these |
| 24 | * patent holders to use this software. |
| 25 | * - Use of the software either in source or binary form, must be run |
| 26 | * on or directly connected to an Analog Devices Inc. component. |
| 27 | * |
| 28 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR |
| 29 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, |
| 30 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 31 | * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 32 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 33 | * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 34 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 36 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 37 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 38 | * |
| 39 | *******************************************************************************/ |
| 40 | |
| 41 | /******************************************************************************/ |
| 42 | /***************************** Include Files **********************************/ |
| 43 | /******************************************************************************/ |
| 44 | #include "Command.h" |
| 45 | #include "AD799x.h" |
| 46 | #include "Console.h" |
| 47 | |
| 48 | /******************************************************************************/ |
| 49 | /************************ Constants Definitions *******************************/ |
| 50 | /******************************************************************************/ |
| 51 | |
| 52 | /*!< List of available commands */ |
| 53 | const char* cmdList[] = {"help?", |
| 54 | "voltage?", |
| 55 | "register?"}; |
| 56 | const char* cmdDescription[] = { |
| 57 | " - Diplays all available commands.", |
| 58 | " - Initiates a conversion and displays the captured voltage for selected \ |
| 59 | channel. Accepted values: \r\n\ |
| 60 | \t0..3 - selected channel", |
| 61 | " - Initiates a conversion and displays the data register in decimal format \ |
| 62 | for selected channel. Accepted values:\r\n\ |
| 63 | \t0..3 - selected channel"}; |
| 64 | const char* cmdExample[] = { |
| 65 | "", |
| 66 | "To display the voltage captured on channel 1, type: voltage?1", |
| 67 | "To display the register for channel 0, type: register?0"}; |
| 68 | const char cmdNo = (sizeof(cmdList) / sizeof(const char*)); |
| 69 | const float VREF = 3.3; /*!< Vref = 3.3V */ |
| 70 | |
| 71 | /******************************************************************************/ |
| 72 | /************************ Variables Definitions *******************************/ |
| 73 | /******************************************************************************/ |
| 74 | cmdFunction cmdFunctions[3] = {GetHelp, GetVoltage, GetRegister}; |
| 75 | |
| 76 | /*!< Variables holding information about the device */ |
| 77 | unsigned short channelsValues[4] = {0, 0, 0, 0}; |
| 78 | |
| 79 | /***************************************************************************//** |
| 80 | * @brief Displays all available commands. |
| 81 | * |
| 82 | * @return None. |
| 83 | *******************************************************************************/ |
| 84 | void GetHelp(double* param, char paramNo) /*!< "help?" command */ |
| 85 | { |
| 86 | char displayCmd; |
| 87 | |
| 88 | CONSOLE_Print("Available commands:\r\n"); |
| 89 | for(displayCmd = 0; displayCmd < cmdNo; displayCmd++) |
| 90 | { |
| 91 | CONSOLE_Print("%s%s\r\n", (char*)cmdList[displayCmd], |
| 92 | (char*)cmdDescription[displayCmd]); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /***************************************************************************//** |
| 97 | * @brief Initializes the device. |
| 98 | * |
| 99 | * @return - The result of the initialization. |
| 100 | * Example: ERROR - the device was not initialized or the device |
| 101 | * is not present. |
| 102 | * SUCCES - the device was initialized and the device |
| 103 | * is present. |
| 104 | *******************************************************************************/ |
| 105 | char DoDeviceInit(void) |
| 106 | { |
| 107 | if(AD799x_Init(AD7991, 0) == 0) |
| 108 | { |
| 109 | CONSOLE_Print("AD7991 OK\r\n"); |
| 110 | GetHelp(NULL, 0); |
| 111 | return SUCCESS; |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | CONSOLE_Print("AD7991 Error\r\n"); |
| 116 | return ERROR; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /***************************************************************************//** |
| 121 | * @brief Initiates a conversion and displays the captured voltage. |
| 122 | * |
| 123 | * @return None. |
| 124 | *******************************************************************************/ |
| 125 | void GetVoltage(double* param, char paramNo) /*!< "voltage?" command */ |
| 126 | { |
| 127 | double voltage = 0; |
| 128 | unsigned char currentChannel = 0; |
| 129 | unsigned short registerValue = 0; |
| 130 | |
| 131 | /*!< Check if the parameter is valid */ |
| 132 | if(paramNo >= 1) |
| 133 | { |
| 134 | if(param[0] < 0) |
| 135 | { |
| 136 | param[0] = 0; |
| 137 | } |
| 138 | if(param[0] > 3) |
| 139 | { |
| 140 | param[0] = 3; |
| 141 | } |
| 142 | |
| 143 | currentChannel = (unsigned char)param[0]; |
| 144 | /*!< Power on the selected channel and shutdown the others */ |
| 145 | AD799x_SetConfigurationReg(AD799X_CHANNEL(currentChannel)); |
| 146 | /*!< Start a conversion */ |
| 147 | AD799x_GetConversionResult(&channelsValues[currentChannel], |
| 148 | ¤tChannel); |
| 149 | registerValue = channelsValues[currentChannel]; |
| 150 | /*!< Get the input voltage from the straight binary raw data */ |
| 151 | voltage = AD799x_ConvertToVolts(registerValue, VREF); |
| 152 | /*!< Send the requested value to user */ |
| 153 | CONSOLE_Print("Voltage=%.3f [V] for channel %d.\r\n",voltage, |
| 154 | currentChannel); |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | /*!< Display error messages */ |
| 159 | CONSOLE_Print("Invalid parameter!\r\n"); |
| 160 | CONSOLE_Print("%s%s\r\n", (char*)cmdList[1], (char*)cmdDescription[1]); |
| 161 | CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[1]); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /***************************************************************************//** |
| 166 | * @brief Initiates a conversion and displays the data register in decimal format. |
| 167 | * |
| 168 | * @return None. |
| 169 | *******************************************************************************/ |
| 170 | void GetRegister(double* param, char paramNo) /*!< "register?" command */ |
| 171 | { |
| 172 | unsigned char currentChannel = 0; |
| 173 | unsigned short registerValue = 0; |
| 174 | |
| 175 | /*!< Check if the parameter is valid */ |
| 176 | if(paramNo >= 1) |
| 177 | { |
| 178 | if(param[0] < 0) |
| 179 | { |
| 180 | param[0] = 0; |
| 181 | } |
| 182 | if(param[0] > 3) |
| 183 | { |
| 184 | param[0] = 3; |
| 185 | } |
| 186 | |
| 187 | currentChannel = (unsigned char)param[0]; |
| 188 | /*!< Power on the selected channel and shutdown the others */ |
| 189 | AD799x_SetConfigurationReg(AD799X_CHANNEL(currentChannel)); |
| 190 | /*!< Start a conversion */ |
| 191 | AD799x_GetConversionResult(&channelsValues[currentChannel], |
| 192 | ¤tChannel); |
| 193 | registerValue = channelsValues[currentChannel]; |
| 194 | /*!< Send the requested value to user */ |
| 195 | CONSOLE_Print("Register=%d for channel %d.\r\n",registerValue, |
| 196 | currentChannel); |
| 197 | } |
| 198 | else |
| 199 | { |
| 200 | /*!< Display error messages */ |
| 201 | CONSOLE_Print("Invalid parameter!\r\n"); |
| 202 | CONSOLE_Print("%s%s\r\n", (char*)cmdList[2], (char*)cmdDescription[2]); |
| 203 | CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[2]); |
| 204 | } |
| 205 | } |