| 1 | /**************************************************************************//** |
| 2 | * @file Commanad.c |
| 3 | * @brief Implementation of the commands given by user through UART for CN0188. |
| 4 | * @author Lucian Sin ([email protected]) |
| 5 | * |
| 6 | ******************************************************************************* |
| 7 | * Copyright 2014(c) Analog Devices, Inc. |
| 8 | * |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or without modification, |
| 12 | * are permitted provided that the following conditions are met: |
| 13 | * - Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * - Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in |
| 17 | * the documentation and/or other materials provided with the |
| 18 | * distribution. |
| 19 | * - Neither the name of Analog Devices, Inc. nor the names of its |
| 20 | * contributors may be used to endorse or promote products derived |
| 21 | * from this software without specific prior written permission. |
| 22 | * - The use of this software may or may not infringe the patent rights |
| 23 | * of one or more patent holders. This license does not release you |
| 24 | * from the requirement that you obtain separate licenses from these |
| 25 | * patent holders to use this software. |
| 26 | * - Use of the software either in source or binary form, must be run |
| 27 | * on or directly connected to an Analog Devices Inc. component. |
| 28 | * |
| 29 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED |
| 30 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY |
| 31 | * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 32 | * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 33 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 34 | * INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 35 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 36 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 37 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 38 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 39 | * |
| 40 | ******************************************************************************/ |
| 41 | |
| 42 | /******************************************************************************/ |
| 43 | /***************************** Include Files **********************************/ |
| 44 | /******************************************************************************/ |
| 45 | #include "Command.h" |
| 46 | #include "Console.h" |
| 47 | #include "ad7171.h" |
| 48 | |
| 49 | /******************************************************************************/ |
| 50 | /************************ Constants Definitions *******************************/ |
| 51 | /******************************************************************************/ |
| 52 | |
| 53 | const struct cmd_info cmdList[] = { |
| 54 | [0] = { |
| 55 | .name = "help?", |
| 56 | .description = "Displays all available commands.", |
| 57 | .acceptedValue = "", |
| 58 | .example = "", |
| 59 | }, |
| 60 | [1] = { |
| 61 | .name = "voltage?", |
| 62 | .description = "Displays the input voltage.", |
| 63 | .acceptedValue = "", |
| 64 | .example = "", |
| 65 | }, |
| 66 | [2] = { |
| 67 | .name = "adcCode?", |
| 68 | .description = "Displays the output ADC code.", |
| 69 | .acceptedValue = "", |
| 70 | .example = "", |
| 71 | }, |
| 72 | }; |
| 73 | |
| 74 | const char cmdNo = (sizeof(cmdList) / sizeof(struct cmd_info)); |
| 75 | |
| 76 | /******************************************************************************/ |
| 77 | /************************ Variables Definitions *******************************/ |
| 78 | /******************************************************************************/ |
| 79 | cmdFunction cmdFunctions[] = {GetHelp, GetVoltage, GetAdcCode}; |
| 80 | |
| 81 | /* Variables holding information about the device */ |
| 82 | float gain = 0; |
| 83 | float vRef = 0; |
| 84 | float IdPattern = 0; |
| 85 | AD7171_type deviceType; |
| 86 | |
| 87 | /***************************************************************************//** |
| 88 | * @brief Displays all available commands. |
| 89 | * |
| 90 | * @return None. |
| 91 | *******************************************************************************/ |
| 92 | void GetHelp(double* param, char paramNo) // "help?" command |
| 93 | { |
| 94 | unsigned char displayCmd; |
| 95 | |
| 96 | CONSOLE_Print("Available commands:\r\n"); |
| 97 | for(displayCmd = 0; displayCmd < cmdNo; displayCmd++) |
| 98 | { |
| 99 | CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[displayCmd].name, |
| 100 | (char*)cmdList[displayCmd].description, |
| 101 | (char*)cmdList[displayCmd].acceptedValue); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /***************************************************************************//** |
| 106 | * @brief Internal function for displaying all the command with its description. |
| 107 | * |
| 108 | * @return None. |
| 109 | *******************************************************************************/ |
| 110 | void DisplayCmdList() |
| 111 | { |
| 112 | unsigned char displayCmd; |
| 113 | |
| 114 | for(displayCmd = 0; displayCmd < cmdNo; displayCmd++) |
| 115 | { |
| 116 | CONSOLE_Print("%s - %s\r\n", (char*)cmdList[displayCmd].name, \ |
| 117 | (char*)cmdList[displayCmd].description); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /***************************************************************************//** |
| 122 | * @brief Displays error message. |
| 123 | * |
| 124 | * @return None. |
| 125 | *******************************************************************************/ |
| 126 | void DisplayError(unsigned char funcNo) |
| 127 | { |
| 128 | /* Display error messages */ |
| 129 | CONSOLE_Print("Invalid parameter!\r\n"); |
| 130 | CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[funcNo].name, \ |
| 131 | (char*)cmdList[funcNo].description, \ |
| 132 | (char*)cmdList[funcNo].acceptedValue); |
| 133 | CONSOLE_Print("Example: %s\r\n", (char*)cmdList[funcNo].example); |
| 134 | } |
| 135 | |
| 136 | /***************************************************************************//** |
| 137 | * @brief Sets the current device type. |
| 138 | * |
| 139 | * @return None. |
| 140 | *******************************************************************************/ |
| 141 | void DoDeviceLock(void) |
| 142 | { |
| 143 | char device = 0; |
| 144 | char deviceLocked = -1; |
| 145 | |
| 146 | while(deviceLocked < 0) |
| 147 | { |
| 148 | CONSOLE_Print("Please specify your device.\r\n\ |
| 149 | For CN0188 type: 0\r\n\ |
| 150 | For CN0218 type: 1\r\n\ |
| 151 | For CN0240 type: 2\r\n\ |
| 152 | "); |
| 153 | CONSOLE_GetCommand(&device); |
| 154 | if((device >= 0x30) && (device <= 0x32)) |
| 155 | { |
| 156 | deviceLocked = 1; |
| 157 | switch(device) |
| 158 | { |
| 159 | case 0x30 : |
| 160 | { |
| 161 | deviceType = ID_AD7171; |
| 162 | gain = 49.7; |
| 163 | vRef = 2.5; |
| 164 | IdPattern = 8 + 5; |
| 165 | break; |
| 166 | } |
| 167 | case 0x31 : |
| 168 | { |
| 169 | deviceType = ID_AD7171; |
| 170 | gain = 5; |
| 171 | vRef = 2.5; |
| 172 | IdPattern = 8 + 5; |
| 173 | break; |
| 174 | } |
| 175 | case 0x32 : |
| 176 | { |
| 177 | deviceType = ID_AD7170; |
| 178 | gain = 40; |
| 179 | vRef = 5; |
| 180 | IdPattern = 8 + 5; |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | CONSOLE_Print("Please type the number corresponding for \ |
| 188 | your device!\r\n"); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /***************************************************************************//** |
| 194 | * @brief Initializes the device. |
| 195 | * |
| 196 | * @return - The result of the initialization. |
| 197 | * Example: ERROR - the device was not initialized or the device |
| 198 | * is not present. |
| 199 | * SUCCES - the device was initialized and the device |
| 200 | * is present. |
| 201 | *******************************************************************************/ |
| 202 | char DoDeviceInit(void) |
| 203 | { |
| 204 | DoDeviceLock(); |
| 205 | |
| 206 | if(ad7171_Init(deviceType) == 0) |
| 207 | { |
| 208 | CONSOLE_Print("Device OK\r\n"); |
| 209 | DisplayCmdList(); |
| 210 | return SUCCESS; |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | CONSOLE_Print("Device Error\r\n"); |
| 215 | return ERROR; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /***************************************************************************//** |
| 220 | * @brief Displays the input voltage in [mV]. |
| 221 | * |
| 222 | * @return None. |
| 223 | *******************************************************************************/ |
| 224 | void GetVoltage(double* param, char paramNo) // "voltage?" command |
| 225 | { |
| 226 | float vin = 0; |
| 227 | unsigned char pattern = 0; |
| 228 | |
| 229 | vin = ad7171_GetVoltage(0x0, &pattern, vRef); |
| 230 | vin = (vin / gain) * 1000; |
| 231 | |
| 232 | /* Send feedback to user */ |
| 233 | if (pattern == IdPattern) |
| 234 | { |
| 235 | CONSOLE_Print("The serial transfer was correct.\r\n"); |
| 236 | CONSOLE_Print("Voltage=%.3f[mV].\r\n", vin); |
| 237 | } |
| 238 | else |
| 239 | { |
| 240 | CONSOLE_Print("The serial transfer was not correct.\r\n"); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /***************************************************************************//** |
| 245 | * @brief Displays the output ADC code. |
| 246 | * |
| 247 | * @return None. |
| 248 | *******************************************************************************/ |
| 249 | void GetAdcCode(double* param, char paramNo) // "adcCode?" command |
| 250 | { |
| 251 | unsigned short adcCode = 0; |
| 252 | unsigned char pattern = 0; |
| 253 | |
| 254 | adcCode = ad7171_GetAdcCode(0x0, &pattern); |
| 255 | /* Send feedback to user */ |
| 256 | if (pattern == IdPattern) |
| 257 | { |
| 258 | CONSOLE_Print("The serial transfer was correct.\r\n"); |
| 259 | CONSOLE_Print("ADC Code=%d.\r\n", adcCode); |
| 260 | } |
| 261 | else |
| 262 | { |
| 263 | CONSOLE_Print("The serial transfer was not correct.\r\n"); |
| 264 | } |
| 265 | } |