| 1 | /***************************************************************************//** |
| 2 | * @file Command.c |
| 3 | * @brief Implementation of the commands given by user through UART for PmodTMP2. |
| 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 "AD7091R.h" |
| 46 | #include "Console.h" |
| 47 | |
| 48 | /******************************************************************************/ |
| 49 | /************************ Constants Definitions *******************************/ |
| 50 | /******************************************************************************/ |
| 51 | /*!< List of available commands */ |
| 52 | const char* cmdList[] = {"help?", |
| 53 | "reset!", |
| 54 | "power?", |
| 55 | "power=", |
| 56 | "voltage?", |
| 57 | "register?"}; |
| 58 | const char* cmdDescription[] = { |
| 59 | " - Displays all available commands.", |
| 60 | " - Initiates a software reset.", |
| 61 | " - Displays the power status of the device.", |
| 62 | " - Powers on/off the device. Accepted values: \r\n\ |
| 63 | \t0 - Power off. \r\n\ |
| 64 | \t1 - Power on.", |
| 65 | " - Initiates a conversion and displays the captured voltage.", |
| 66 | " - Initiates a conversion and displays the value from the register."}; |
| 67 | const char* cmdExample[] = { |
| 68 | "", |
| 69 | "", |
| 70 | "", |
| 71 | "To power on the device, type: power=1", |
| 72 | "", |
| 73 | ""}; |
| 74 | const char cmdNo = (sizeof(cmdList) / sizeof(const char*)); |
| 75 | const float V_REF = 2.5; /*!< Vref = 2.5V */ |
| 76 | |
| 77 | /******************************************************************************/ |
| 78 | /************************ Variables Definitions *******************************/ |
| 79 | /******************************************************************************/ |
| 80 | cmdFunction cmdFunctions[6] = {GetHelp, DoReset, GetPower, SetPower, GetVoltage, |
| 81 | GetRegister}; |
| 82 | /*!< Variables holding information about the device */ |
| 83 | unsigned short registerValue = 0; /*!< Raw value of the conversion */ |
| 84 | unsigned char powerStatus = 1; /*!< 0/1 - Device is powered off/on */ |
| 85 | double voltage = 0; /*!< Value of the conversion in Volts */ |
| 86 | |
| 87 | /***************************************************************************//** |
| 88 | * @brief Displays all available commands. |
| 89 | * |
| 90 | * @return None. |
| 91 | *******************************************************************************/ |
| 92 | void GetHelp(double* param, char paramNo) /*!< "help?" command */ |
| 93 | { |
| 94 | char displayCmd; |
| 95 | |
| 96 | CONSOLE_Print("Available commands:\r\n"); |
| 97 | for(displayCmd = 0; displayCmd < cmdNo; displayCmd++) |
| 98 | { |
| 99 | CONSOLE_Print("%s%s\r\n", (char*)cmdList[displayCmd], |
| 100 | (char*)cmdDescription[displayCmd]); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /***************************************************************************//** |
| 105 | * @brief Initializes the device. |
| 106 | * |
| 107 | * @return - The result of the initialization. |
| 108 | * Example: ERROR - the device was not initialized or the device |
| 109 | * is not present. |
| 110 | * SUCCES - the device was initialized and the device |
| 111 | * is present. |
| 112 | *******************************************************************************/ |
| 113 | char DoDeviceInit(void) |
| 114 | { |
| 115 | if(AD7091R_Init() == 0) |
| 116 | { |
| 117 | CONSOLE_Print("AD7091R OK\r\n"); |
| 118 | GetHelp(NULL, 0); |
| 119 | return SUCCESS; |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | CONSOLE_Print("AD7091R Error\r\n"); |
| 124 | return ERROR; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /***************************************************************************//** |
| 129 | * @brief Resets the device. |
| 130 | * |
| 131 | * @return None. |
| 132 | *******************************************************************************/ |
| 133 | void DoReset(double* param, char paramNo) /*!< "reset!" command */ |
| 134 | { |
| 135 | AD7091R_SoftwareReset(); |
| 136 | powerStatus = 1; |
| 137 | /*!< Send feedback to user */ |
| 138 | CONSOLE_Print("Device was reset.\r\n"); |
| 139 | } |
| 140 | |
| 141 | /***************************************************************************//** |
| 142 | * @brief Displays the power status of the device. |
| 143 | * |
| 144 | * @return None. |
| 145 | *******************************************************************************/ |
| 146 | void GetPower(double* param, char paramNo) /*!< "power?" command */ |
| 147 | { |
| 148 | /*!< Send the requested value to user */ |
| 149 | CONSOLE_Print("%s%d\r\n",(char*)cmdList[3],powerStatus); |
| 150 | } |
| 151 | |
| 152 | /***************************************************************************//** |
| 153 | * @brief Powers on/off the device. |
| 154 | * |
| 155 | * @return None. |
| 156 | *******************************************************************************/ |
| 157 | void SetPower(double* param, char paramNo) /*!< "power=" command */ |
| 158 | { |
| 159 | /*!< Check if the parameter is valid */ |
| 160 | if(paramNo >= 1) |
| 161 | { |
| 162 | if(param[0] < 0) |
| 163 | { |
| 164 | param[0] = 0; |
| 165 | } |
| 166 | if(param[0] > 1) |
| 167 | { |
| 168 | param[0] = 1; |
| 169 | } |
| 170 | |
| 171 | powerStatus = (unsigned char)param[0]; |
| 172 | if(powerStatus == 1) |
| 173 | { |
| 174 | AD7091R_PowerUp(); |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | AD7091R_PowerDown(); |
| 179 | } |
| 180 | /* Send feedback to user */ |
| 181 | CONSOLE_Print("%s%d\r\n",(char*)cmdList[3],powerStatus); |
| 182 | } |
| 183 | else |
| 184 | { |
| 185 | /*!< Display error messages */ |
| 186 | CONSOLE_Print("Invalid parameter!\r\n"); |
| 187 | CONSOLE_Print("%s%s\r\n", (char*)cmdList[3], (char*)cmdDescription[3]); |
| 188 | CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[3]); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /***************************************************************************//** |
| 193 | * @brief Initiates a conversion and displays the captured voltage. |
| 194 | * |
| 195 | * @return None. |
| 196 | *******************************************************************************/ |
| 197 | void GetVoltage(double* param, char paramNo) /*!< "voltage?" command */ |
| 198 | { |
| 199 | if(powerStatus) |
| 200 | { |
| 201 | /*!< Start a conversion */ |
| 202 | registerValue = AD7091R_ReadSample(); |
| 203 | /*!< Get the input voltage from the straight binary raw data */ |
| 204 | voltage = ((double)registerValue / 4096) * V_REF; |
| 205 | /*!< Send the requested value to user */ |
| 206 | CONSOLE_Print("voltage=%.3f [V]\r\n",voltage); |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | CONSOLE_Print("Device is powered off. Please use 'power=1' command in \ |
| 211 | order to power on the device.\r\n"); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /***************************************************************************//** |
| 216 | * @brief Initiates a conversion and displays the value of the data register. |
| 217 | * |
| 218 | * @return None. |
| 219 | *******************************************************************************/ |
| 220 | void GetRegister(double* param, char paramNo) /*!< "register?" command */ |
| 221 | { |
| 222 | if(powerStatus) |
| 223 | { |
| 224 | /*!< Start a conversion */ |
| 225 | registerValue = AD7091R_ReadSample(); |
| 226 | /*!< Send the requested value to user */ |
| 227 | CONSOLE_Print("register=%d\r\n",registerValue); |
| 228 | } |
| 229 | else |
| 230 | { |
| 231 | CONSOLE_Print("Device is powered off. Please use 'power=1' command in \ |
| 232 | order to power on the device.\r\n"); |
| 233 | } |
| 234 | } |