| 1 | /**************************************************************************//** |
| 2 | * @file Commanad.c |
| 3 | * @brief Implementation of the commands given by user through UART for AD527x. |
| 4 | * @author Lucian Sin ([email protected]) |
| 5 | * |
| 6 | ******************************************************************************* |
| 7 | * Copyright 2013(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 "ad527x.h" |
| 48 | #include "TIME.h" |
| 49 | |
| 50 | /******************************************************************************/ |
| 51 | /************************ Constants Definitions *******************************/ |
| 52 | /******************************************************************************/ |
| 53 | |
| 54 | const struct cmd_info cmdList[] = { |
| 55 | [0] = { |
| 56 | .name = "help?", |
| 57 | .description = "Displays all available commands.", |
| 58 | .acceptedValue = "", |
| 59 | .example = "", |
| 60 | }, |
| 61 | [1] = { |
| 62 | .name = "reset!", |
| 63 | .description = "Makes a software reset of the device.", |
| 64 | .acceptedValue = "", |
| 65 | .example = "", |
| 66 | }, |
| 67 | [2] = { |
| 68 | .name = "rdac=", |
| 69 | .description = "Writes to the RDAC register.", |
| 70 | .acceptedValue = "Accepted values:\r\n\ |
| 71 | \t0 .. (2^DPotBitsNumber)-1 - the value written to RDAC.", |
| 72 | .example = "To set the rdac value to 150, type: rdac=150", |
| 73 | }, |
| 74 | [3] = { |
| 75 | .name = "rdac?", |
| 76 | .description = "Displays the last written value in RDAC register.", |
| 77 | .acceptedValue = "", |
| 78 | .example = "", |
| 79 | }, |
| 80 | [4] = { |
| 81 | .name = "store!", |
| 82 | .description = "Stores the RDAC setting to 50-TP.", |
| 83 | .acceptedValue = "", |
| 84 | .example = "", |
| 85 | }, |
| 86 | [5] = { |
| 87 | .name = "50TPValue?", |
| 88 | .description = "Displays the contents of the selected 50-TP register.", |
| 89 | .acceptedValue = "Accepted values:\r\n\ |
| 90 | \t0 .. 50 - selected 50-TP register", |
| 91 | .example = "To display the value of the 10th 50-TP register, type:\ |
| 92 | 50TPValue?10", |
| 93 | }, |
| 94 | [6] = { |
| 95 | .name = "50TPAddress?", |
| 96 | .description = "Displays the address of the last programmed 50-TP \ |
| 97 | register.", |
| 98 | .acceptedValue = "", |
| 99 | .example = "", |
| 100 | }, |
| 101 | [7] = { |
| 102 | .name = "power=", |
| 103 | .description = "Turns on/off the device.", |
| 104 | .acceptedValue = "Accepted values:\r\n\ |
| 105 | \t1 - normal mode.(default)\r\n\ |
| 106 | \t0 - shutdown mode.", |
| 107 | .example = "To turn off the device, type: power=0", |
| 108 | }, |
| 109 | [8] ={ |
| 110 | .name = "power?", |
| 111 | .description = "Displays the power status of the device.", |
| 112 | .acceptedValue = "", |
| 113 | .example = "", |
| 114 | }, |
| 115 | }; |
| 116 | |
| 117 | |
| 118 | const char cmdNo = (sizeof(cmdList) / sizeof(struct cmd_info)); |
| 119 | |
| 120 | /******************************************************************************/ |
| 121 | /************************ Variables Definitions *******************************/ |
| 122 | /******************************************************************************/ |
| 123 | cmdFunction cmdFunctions[9] = {GetHelp, DoReset, SetRdac, GetRdac, DoStore, |
| 124 | Get50TPValue, Get50TPAddress, SetPower, GetPower}; |
| 125 | /* Variables holding information about the device */ |
| 126 | unsigned short power = 1; // Power status of the device |
| 127 | unsigned short max_value = 0; // Maximum value for RDAC |
| 128 | AD527x_type deviceType; |
| 129 | |
| 130 | |
| 131 | /***************************************************************************//** |
| 132 | * @brief Displays all available commands. |
| 133 | * |
| 134 | * @return None. |
| 135 | *******************************************************************************/ |
| 136 | void GetHelp(double* param, char paramNo) // "help?" command |
| 137 | { |
| 138 | unsigned char displayCmd; |
| 139 | |
| 140 | CONSOLE_Print("Available commands:\r\n"); |
| 141 | for(displayCmd = 0; displayCmd < cmdNo; displayCmd++) |
| 142 | { |
| 143 | CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[displayCmd].name, |
| 144 | (char*)cmdList[displayCmd].description, |
| 145 | (char*)cmdList[displayCmd].acceptedValue); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /***************************************************************************//** |
| 150 | * @brief Internal function for displaying all the command with its description. |
| 151 | * |
| 152 | * @return None. |
| 153 | *******************************************************************************/ |
| 154 | void DisplayCmdList() |
| 155 | { |
| 156 | unsigned char displayCmd; |
| 157 | |
| 158 | for(displayCmd = 0; displayCmd < cmdNo; displayCmd++) |
| 159 | { |
| 160 | CONSOLE_Print("%s - %s\r\n", (char*)cmdList[displayCmd].name, \ |
| 161 | (char*)cmdList[displayCmd].description); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /***************************************************************************//** |
| 166 | * @brief Displays error message. |
| 167 | * |
| 168 | * @return None. |
| 169 | *******************************************************************************/ |
| 170 | void DisplayError(unsigned char funcNo) |
| 171 | { |
| 172 | /* Display error messages */ |
| 173 | CONSOLE_Print("Invalid parameter!\r\n"); |
| 174 | CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[funcNo].name, \ |
| 175 | (char*)cmdList[funcNo].description, \ |
| 176 | (char*)cmdList[funcNo].acceptedValue); |
| 177 | CONSOLE_Print("Example: %s\r\n", (char*)cmdList[funcNo].example); |
| 178 | } |
| 179 | |
| 180 | /***************************************************************************//** |
| 181 | * @brief Sets the current device type. |
| 182 | * |
| 183 | * @return None. |
| 184 | *******************************************************************************/ |
| 185 | void DoDeviceLock(void) |
| 186 | { |
| 187 | char device = 0; |
| 188 | char deviceLocked = -1; |
| 189 | |
| 190 | while(deviceLocked < 0) |
| 191 | { |
| 192 | CONSOLE_Print("Please specify your device.\r\n\ |
| 193 | For AD5270 type: 0\r\n\ |
| 194 | For AD5271 type: 1\r\n\ |
| 195 | For AD5272 type: 2\r\n\ |
| 196 | For AD5274 type: 3\r\n\ |
| 197 | "); |
| 198 | CONSOLE_GetCommand(&device); |
| 199 | if((device >= 0x30) && (device <= 0x33)) |
| 200 | { |
| 201 | deviceLocked = 1; |
| 202 | switch(device) |
| 203 | { |
| 204 | case 0x30 : |
| 205 | { |
| 206 | deviceType = ID_AD5270; |
| 207 | max_value = 0x3FF; |
| 208 | break; |
| 209 | } |
| 210 | case 0x31 : |
| 211 | { |
| 212 | deviceType = ID_AD5271; |
| 213 | max_value = 0xFF; |
| 214 | break; |
| 215 | } |
| 216 | case 0x32 : |
| 217 | { |
| 218 | deviceType = ID_AD5272; |
| 219 | max_value = 0x3FF; |
| 220 | break; |
| 221 | } |
| 222 | case 0x33 : |
| 223 | { |
| 224 | deviceType = ID_AD5274; |
| 225 | max_value = 0xFF; |
| 226 | break; |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | else |
| 231 | { |
| 232 | CONSOLE_Print("Please type the number corresponding for \ |
| 233 | your device!\r\n"); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | /***************************************************************************//** |
| 239 | * @brief Initializes the device. |
| 240 | * |
| 241 | * @return - The result of the initialization. |
| 242 | * Example: ERROR - the device was not initialized or the device |
| 243 | * is not present. |
| 244 | * SUCCES - the device was initialized and the device |
| 245 | * is present. |
| 246 | *******************************************************************************/ |
| 247 | char DoDeviceInit(void) |
| 248 | { |
| 249 | DoDeviceLock(); |
| 250 | |
| 251 | if(AD527x_Init(AD527x_I2C_ADDR_0, deviceType) == 0) |
| 252 | { |
| 253 | CONSOLE_Print("Device OK\r\n"); |
| 254 | DisplayCmdList(); |
| 255 | return SUCCESS; |
| 256 | } |
| 257 | else |
| 258 | { |
| 259 | CONSOLE_Print("Device Error\r\n"); |
| 260 | return ERROR; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | /***************************************************************************//** |
| 265 | * @brief Makes a software reset of the device. |
| 266 | * |
| 267 | * @return None. |
| 268 | *******************************************************************************/ |
| 269 | void DoReset(double* param, char paramNo) // "reset!" command |
| 270 | { |
| 271 | AD527x_SetRegister(AD527x_RESET, 0x0); |
| 272 | TIME_DelayMs(10); |
| 273 | /* setup AD527x */ |
| 274 | /* enable 50TP, allow update of RDAC register */ |
| 275 | AD527x_SetRegister(AD527x_WRITE_CONTROL,(AD527x_C1 | AD527x_C0)); |
| 276 | /* Send feedback to user */ |
| 277 | CONSOLE_Print("Device was reset.\r\n"); |
| 278 | } |
| 279 | |
| 280 | /***************************************************************************//** |
| 281 | * @brief Writes to the RDAC register. |
| 282 | * |
| 283 | * @param param[0] = value to be written to RDAC. |
| 284 | * |
| 285 | * @return None. |
| 286 | *******************************************************************************/ |
| 287 | void SetRdac(double* param, char paramNo) // "rdac=" command |
| 288 | { |
| 289 | unsigned short rdac; |
| 290 | |
| 291 | /* Check if the parameter is valid */ |
| 292 | if(paramNo >= 1) |
| 293 | { |
| 294 | if(param[0] < 0) |
| 295 | { |
| 296 | param[0] = 0; |
| 297 | } |
| 298 | if(param[0] > max_value) |
| 299 | { |
| 300 | param[0] = max_value; |
| 301 | } |
| 302 | |
| 303 | rdac = (unsigned short)param[0]; |
| 304 | /* Write to RDAC register */ |
| 305 | AD527x_SetRegister(AD527x_WRITE_RDAC, rdac); |
| 306 | /* Send feedback to user */ |
| 307 | CONSOLE_Print("%s%d\r\n",(char*)cmdList[2].name,rdac); |
| 308 | } |
| 309 | else |
| 310 | { |
| 311 | /* Display error messages */ |
| 312 | DisplayError(2); |
| 313 | |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | /***************************************************************************//** |
| 318 | * @brief Displays the last written value in RDAC register. |
| 319 | * |
| 320 | * @return None. |
| 321 | *******************************************************************************/ |
| 322 | void GetRdac(double* param, char paramNo) // "rdac?" command |
| 323 | { |
| 324 | unsigned short rdac; |
| 325 | |
| 326 | rdac = AD527x_GetRegister(AD527x_READ_RDAC, 0x0); |
| 327 | /* Send the requested value to user */ |
| 328 | CONSOLE_Print("%s%d\r\n",(char*)cmdList[2].name,rdac); |
| 329 | } |
| 330 | |
| 331 | /***************************************************************************//** |
| 332 | * @brief Stores the RDAC setting to 50-TP. |
| 333 | * |
| 334 | * @return None. |
| 335 | *******************************************************************************/ |
| 336 | void DoStore(double* param, char paramNo) // "store!" command |
| 337 | { |
| 338 | AD527x_SetRegister(AD527x_STORE_RDAC_50TP, 0x0); |
| 339 | TIME_DelayMs(400); |
| 340 | /* Send feedback to user */ |
| 341 | CONSOLE_Print("RDAC register was stored to 50-TP.\r\n"); |
| 342 | } |
| 343 | |
| 344 | /***************************************************************************//** |
| 345 | * @brief Displays the contents of the selected 50-TP register. |
| 346 | * |
| 347 | * @param param[0] - selected 50-TP register. |
| 348 | * |
| 349 | * @return None. |
| 350 | *******************************************************************************/ |
| 351 | void Get50TPValue(double* param, char paramNo) // "50TPValue?" command |
| 352 | { |
| 353 | unsigned short value50TP; |
| 354 | unsigned short wiper; |
| 355 | |
| 356 | /* Check if the parameter is valid */ |
| 357 | if(paramNo >= 1) |
| 358 | { |
| 359 | if(param[0] < 0) |
| 360 | { |
| 361 | param[0] = 0; |
| 362 | } |
| 363 | if(param[0] > 50) |
| 364 | { |
| 365 | param[0] = 50; |
| 366 | } |
| 367 | |
| 368 | value50TP = (unsigned short)param[0]; |
| 369 | /* Write to RDAC register */ |
| 370 | wiper = AD527x_GetRegister(AD527x_READ_CONTENT_50TP, value50TP); |
| 371 | /* Send the requested value to user */ |
| 372 | CONSOLE_Print("wiper=%d stored in 50-TPRegister=%d\r\n",wiper,value50TP); |
| 373 | } |
| 374 | else |
| 375 | { |
| 376 | /* Display error messages */ |
| 377 | DisplayError(5); |
| 378 | |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | /***************************************************************************//** |
| 383 | * @brief Displays the address of the last programmed 50-TP register. |
| 384 | * |
| 385 | * @return None. |
| 386 | *******************************************************************************/ |
| 387 | void Get50TPAddress(double* param, char paramNo) // "50TPAddress?" command |
| 388 | { |
| 389 | unsigned short address; |
| 390 | |
| 391 | address = AD527x_GetRegister(AD527x_READ_ADDRESS_50TP, 0x0); |
| 392 | /* Send the requested value to user */ |
| 393 | CONSOLE_Print("address of the last programmed 50-TPRegister=%d\r\n", |
| 394 | address); |
| 395 | } |
| 396 | |
| 397 | /***************************************************************************//** |
| 398 | * @brief Turns on/off the device. |
| 399 | * |
| 400 | * @param param[0] - power status of the device. |
| 401 | * Example: 0 - normal mode; |
| 402 | * 1 - shut down mode. |
| 403 | * |
| 404 | * @return None. |
| 405 | *******************************************************************************/ |
| 406 | void SetPower(double* param, char paramNo) // "power=" command |
| 407 | { |
| 408 | /* Check if the parameter is valid */ |
| 409 | if(paramNo >= 1) |
| 410 | { |
| 411 | if(param[0] < 0) |
| 412 | { |
| 413 | param[0] = 0; |
| 414 | } |
| 415 | if(param[0] > 1) |
| 416 | { |
| 417 | param[0] = 1; |
| 418 | } |
| 419 | power = (unsigned short)param[0]; |
| 420 | /* Turns on/off the device. */ |
| 421 | AD527x_SetRegister(AD527x_SHUTDOWN, !power); |
| 422 | /* Send feedback to user */ |
| 423 | CONSOLE_Print("%s%d\r\n",(char*)cmdList[7].name,power); |
| 424 | } |
| 425 | else |
| 426 | { |
| 427 | /* Display error messages */ |
| 428 | DisplayError(7); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | /***************************************************************************//** |
| 433 | * @brief Displays the power status of the device. |
| 434 | * |
| 435 | * @return None. |
| 436 | *******************************************************************************/ |
| 437 | void GetPower(double* param, char paramNo) // "power?" command |
| 438 | { |
| 439 | /* Send feedback to user */ |
| 440 | CONSOLE_Print("%s%d\r\n",(char*)cmdList[7].name,power); |
| 441 | } |