| 1 | /***************************************************************************//** |
| 2 | * @file Command.c |
| 3 | * @brief Implementation of the commands given by user through UART for AD5421. |
| 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 |
| 12 | * modification, 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 |
| 30 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, |
| 31 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 32 | * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 33 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 34 | * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 35 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 36 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 37 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 38 | * OF THIS 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 "AD5421.h" |
| 48 | #include "TIME.h" |
| 49 | |
| 50 | /******************************************************************************/ |
| 51 | /************************ Constants Definitions *******************************/ |
| 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 = "reset!", |
| 62 | .description = "Resets the AD5421 device.", |
| 63 | .acceptedValue = "", |
| 64 | .example = "", |
| 65 | }, |
| 66 | [2] = { |
| 67 | .name = "current=", |
| 68 | .description = "Sets the output current.", |
| 69 | .acceptedValue = "Accepted values: \r\n\ |
| 70 | \t4 .. 20 - the desired output current in milliamps.", |
| 71 | .example = "To set the output current to 7.5mA, type: current=7.5", |
| 72 | }, |
| 73 | [3] = { |
| 74 | .name = "current?", |
| 75 | .description = "Displays the output current.", |
| 76 | .acceptedValue = "", |
| 77 | .example = "", |
| 78 | }, |
| 79 | [4] = { |
| 80 | .name = "register=", |
| 81 | .description = "Writes to the DAC register.", |
| 82 | .acceptedValue = "Accepted values: \r\n\ |
| 83 | \t0 .. 65535 - the value written to the DAC.", |
| 84 | .example = "To set the DAC register value to 20000, \ |
| 85 | type: register=20000", |
| 86 | }, |
| 87 | [5] = { |
| 88 | .name = "register?", |
| 89 | .description = "Displays the last written value to the DAC register.", |
| 90 | .acceptedValue = "", |
| 91 | .example = "", |
| 92 | }, |
| 93 | [6] = { |
| 94 | .name = "offset=", |
| 95 | .description = "Sets the offset.", |
| 96 | .acceptedValue = "Accepted values: \r\n\ |
| 97 | \t -32768 .. +32767 - digital offset adjustment(LSBs)", |
| 98 | .example = "To set the Offset register value to -10000, \ |
| 99 | type: offset=-10000", |
| 100 | }, |
| 101 | [7] = { |
| 102 | .name = "offset?", |
| 103 | .description = "Displays the offset.", |
| 104 | .acceptedValue = "", |
| 105 | .example = "", |
| 106 | }, |
| 107 | [8] = { |
| 108 | .name = "gain=", |
| 109 | .description = "Sets the gain.", |
| 110 | .acceptedValue = "Accepted values: \r\n\ |
| 111 | \t-65535 .. 0 - digital gain adjustment(LSBs)", |
| 112 | .example = "To set the Gain register value to -30000, \ |
| 113 | type: gain=-30000", |
| 114 | }, |
| 115 | [9] = { |
| 116 | .name = "gain?", |
| 117 | .description = "Displays the gain.", |
| 118 | .acceptedValue = "", |
| 119 | .example = "", |
| 120 | }, |
| 121 | [10] = { |
| 122 | .name = "temp?", |
| 123 | .description = "Displays the die temperature.", |
| 124 | .acceptedValue = "", |
| 125 | .example = "", |
| 126 | }, |
| 127 | [11] = { |
| 128 | .name = "vloop?", |
| 129 | .description = "Displays the Vloop - COM voltage.", |
| 130 | .acceptedValue = "", |
| 131 | .example = "", |
| 132 | }, |
| 133 | [12] = { |
| 134 | .name = "faultReg?", |
| 135 | .description = "Displays the Fault register.", |
| 136 | .acceptedValue = "", |
| 137 | .example = "", |
| 138 | } |
| 139 | }; |
| 140 | |
| 141 | const char cmdNo = (sizeof(cmdList) / sizeof(struct cmd_info)); |
| 142 | |
| 143 | /******************************************************************************/ |
| 144 | /************************ Variables Definitions *******************************/ |
| 145 | /******************************************************************************/ |
| 146 | cmdFunction cmdFunctions[13] = {GetHelp, DoReset, SetCurrent, GetCurrent, |
| 147 | SetRegister, GetRegister, SetOffset, GetOffset, |
| 148 | SetGain, GetGain, GetTemp, GetVloop, GetFaultReg}; |
| 149 | |
| 150 | /* Variables holding information about the device */ |
| 151 | int dacReg = 0; // Content of DAC register |
| 152 | int offsetReg = 0; // Content of Offset Adjust register |
| 153 | int gainReg = 0; // Content of Gain Adjust register |
| 154 | |
| 155 | /***************************************************************************//** |
| 156 | * @brief Displays error message. |
| 157 | * |
| 158 | * @return None. |
| 159 | *******************************************************************************/ |
| 160 | void DisplayError(unsigned char funcNo) |
| 161 | { |
| 162 | /* Display error messages */ |
| 163 | CONSOLE_Print("Invalid parameter!\r\n"); |
| 164 | CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[funcNo].name, \ |
| 165 | (char*)cmdList[funcNo].description, \ |
| 166 | (char*)cmdList[funcNo].acceptedValue); |
| 167 | CONSOLE_Print("Example: %s\r\n", (char*)cmdList[funcNo].example); |
| 168 | } |
| 169 | |
| 170 | /***************************************************************************//** |
| 171 | * @brief Internal function for displaying all the command with its description. |
| 172 | * |
| 173 | * @return None. |
| 174 | *******************************************************************************/ |
| 175 | void DisplayCmdList() |
| 176 | { |
| 177 | unsigned char displayCmd; |
| 178 | |
| 179 | for(displayCmd = 0; displayCmd < cmdNo; displayCmd++) |
| 180 | { |
| 181 | CONSOLE_Print("%s - %s\r\n", (char*)cmdList[displayCmd].name, \ |
| 182 | (char*)cmdList[displayCmd].description); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /***************************************************************************//** |
| 187 | * @brief Displays all available commands. |
| 188 | * |
| 189 | * @return None. |
| 190 | *******************************************************************************/ |
| 191 | void GetHelp(double* param, char paramNo) // "help?" command |
| 192 | { |
| 193 | unsigned char displayCmd; |
| 194 | |
| 195 | CONSOLE_Print("Available commands:\r\n"); |
| 196 | for(displayCmd = 0; displayCmd < cmdNo; displayCmd++) |
| 197 | { |
| 198 | CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[displayCmd].name, |
| 199 | (char*)cmdList[displayCmd].description, |
| 200 | (char*)cmdList[displayCmd].acceptedValue); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /***************************************************************************//** |
| 205 | * @brief Initializes the device. |
| 206 | * |
| 207 | * @return - The result of the initialization. |
| 208 | * Example: ERROR - the device was not initialized or the device |
| 209 | * is not present. |
| 210 | * SUCCES - the device was initialized and the device |
| 211 | * is present. |
| 212 | *******************************************************************************/ |
| 213 | char DoDeviceInit(void) |
| 214 | { |
| 215 | if(AD5421_Init() == 0) |
| 216 | { |
| 217 | CONSOLE_Print("AD5421 OK\r\n"); |
| 218 | dacReg = AD5421_GetDac(); |
| 219 | offsetReg = AD5421_GetOffset(); |
| 220 | gainReg = AD5421_GetGain(); |
| 221 | DisplayCmdList(); |
| 222 | return SUCCESS; |
| 223 | } |
| 224 | else |
| 225 | { |
| 226 | CONSOLE_Print("AD5421 Error\r\n"); |
| 227 | return ERROR; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /***************************************************************************//** |
| 232 | * @brief Resets the device. |
| 233 | * |
| 234 | * @return None. |
| 235 | *******************************************************************************/ |
| 236 | void DoReset(double* param, char paramNo) // "reset!" command |
| 237 | { |
| 238 | int spiData = 0; |
| 239 | |
| 240 | AD5421_Reset(); |
| 241 | dacReg = 0; |
| 242 | offsetReg = 32768; |
| 243 | gainReg = 65535; |
| 244 | /* Setup AD5421 control register. */ |
| 245 | /* Write to the control register. */ |
| 246 | spiData = AD5421_CMD(AD5421_CMDWRCTRL); |
| 247 | /* Set certain bits. */ |
| 248 | spiData |= (CTRL_AUTO_FAULT_RDBK | |
| 249 | CTRL_SEL_ADC_INPUT | |
| 250 | CTRL_ONCHIP_ADC | |
| 251 | CTRL_SPI_WATCHDOG); |
| 252 | /* Send data via SPI. */ |
| 253 | AD5421_Set(&spiData); |
| 254 | TIME_DelayUs(100); |
| 255 | /* Send feedback to user */ |
| 256 | CONSOLE_Print("Device was reset.\r\n"); |
| 257 | } |
| 258 | |
| 259 | /***************************************************************************//** |
| 260 | * @brief Writes to the DAC register. |
| 261 | * |
| 262 | * @param param[0] - desired value to be written in register. |
| 263 | * |
| 264 | * @return None. |
| 265 | *******************************************************************************/ |
| 266 | void SetRegister(double* param, char paramNo) // "register=" command |
| 267 | { |
| 268 | /* Check if the parameter is valid */ |
| 269 | if(paramNo >= 1) |
| 270 | { |
| 271 | if(param[0] < 0) |
| 272 | { |
| 273 | param[0] = 0; |
| 274 | } |
| 275 | if(param[0] > 65535) |
| 276 | { |
| 277 | param[0] = 65535; |
| 278 | } |
| 279 | dacReg = (int)param[0]; |
| 280 | /* Write to DAC register */ |
| 281 | AD5421_SetDac(dacReg); |
| 282 | /* Send feedback to user */ |
| 283 | CONSOLE_Print("%s%d\r\n",cmdList[4].name, dacReg); |
| 284 | } |
| 285 | else |
| 286 | { |
| 287 | /* Display error messages */ |
| 288 | DisplayError(4); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /***************************************************************************//** |
| 293 | * @brief Displays last written value to the DAC register. |
| 294 | * |
| 295 | * @return None. |
| 296 | *******************************************************************************/ |
| 297 | void GetRegister(double* param, char paramNo) // "register?" command |
| 298 | { |
| 299 | dacReg = AD5421_GetDac(); |
| 300 | /* Send the requested value to user */ |
| 301 | CONSOLE_Print("%s%d\r\n",cmdList[4].name, dacReg); |
| 302 | } |
| 303 | |
| 304 | /***************************************************************************//** |
| 305 | * @brief Writes to the Offset Adjust register. |
| 306 | * |
| 307 | * @param param[0] - desired value to be written in register. |
| 308 | * |
| 309 | * @return None. |
| 310 | *******************************************************************************/ |
| 311 | void SetOffset(double* param, char paramNo) // "offset=" command |
| 312 | { |
| 313 | /* Check if the parameter is valid */ |
| 314 | if(paramNo >= 1) |
| 315 | { |
| 316 | if(param[0] < -32768) |
| 317 | { |
| 318 | param[0] = -32768; |
| 319 | } |
| 320 | if(param[0] > 32767) |
| 321 | { |
| 322 | param[0] = 32767; |
| 323 | } |
| 324 | offsetReg = (int)param[0] + 32768; |
| 325 | /* Write to Offset Adjust register */ |
| 326 | AD5421_SetOffset(offsetReg); |
| 327 | /* Write to DAC register in order to validate the Offset register value change */ |
| 328 | AD5421_SetDac(dacReg); |
| 329 | /* Send feedback to user */ |
| 330 | CONSOLE_Print("%s%d\r\n", cmdList[6].name, offsetReg - 32768); |
| 331 | } |
| 332 | else |
| 333 | { |
| 334 | /* Display error messages */ |
| 335 | DisplayError(6); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | /***************************************************************************//** |
| 340 | * @brief Displays last written value to the Offset Adjust register. |
| 341 | * |
| 342 | * @return None. |
| 343 | *******************************************************************************/ |
| 344 | void GetOffset(double* param, char paramNo) // "offset?" command |
| 345 | { |
| 346 | int value = 0; |
| 347 | |
| 348 | offsetReg = AD5421_GetOffset(); |
| 349 | /* Calculate offset according to the datasheet formula. */ |
| 350 | value = offsetReg - 32768; |
| 351 | /* Send the requested value to user */ |
| 352 | CONSOLE_Print("%s%d [LSBs]\r\n", cmdList[6].name, value); |
| 353 | } |
| 354 | |
| 355 | |
| 356 | /***************************************************************************//** |
| 357 | * @brief Writes to the Gain Adjust register. |
| 358 | * |
| 359 | * @param param[0] - desired value to be written in register. |
| 360 | * |
| 361 | * @return None. |
| 362 | *******************************************************************************/ |
| 363 | void SetGain(double* param, char paramNo) // "gain=" command |
| 364 | { |
| 365 | /* Check if the parameter is valid */ |
| 366 | if(paramNo >= 1) |
| 367 | { |
| 368 | if(param[0] < -65535) |
| 369 | { |
| 370 | param[0] = -65535; |
| 371 | } |
| 372 | if(param[0] > 0) |
| 373 | { |
| 374 | param[0] = 0; |
| 375 | } |
| 376 | gainReg = (int)param[0] + 65535; |
| 377 | /* Write to Gain Adjust register */ |
| 378 | AD5421_SetGain(gainReg); |
| 379 | /* Write to DAC register in order to validate the Gain register value change */ |
| 380 | AD5421_SetDac(dacReg); |
| 381 | /* Send feedback to user */ |
| 382 | CONSOLE_Print("%s%d\r\n", cmdList[8].name, gainReg - 65535); |
| 383 | } |
| 384 | else |
| 385 | { |
| 386 | /* Display error messages */ |
| 387 | DisplayError(8); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | /***************************************************************************//** |
| 392 | * @brief Displays last gain value from the Gain Adjust register. |
| 393 | * |
| 394 | * @return None. |
| 395 | *******************************************************************************/ |
| 396 | void GetGain(double* param, char paramNo) // "gain?" command |
| 397 | { |
| 398 | int value = 0; |
| 399 | |
| 400 | gainReg = AD5421_GetGain(); |
| 401 | /* Calculate according to the datasheet formula. */ |
| 402 | value = gainReg - 65535; |
| 403 | /* Send the requested value to user */ |
| 404 | CONSOLE_Print("%s%d [LSBs]\r\n", cmdList[8].name, value); |
| 405 | } |
| 406 | |
| 407 | /***************************************************************************//** |
| 408 | * @brief Displays the die temperature. |
| 409 | * |
| 410 | * @return None. |
| 411 | *******************************************************************************/ |
| 412 | void GetTemp(double* param, char paramNo) // "temp?" command |
| 413 | { |
| 414 | int value = 0; |
| 415 | |
| 416 | value = AD5421_GetTemp(); |
| 417 | /* Send the requested value to user */ |
| 418 | CONSOLE_Print("temperature=%d [C]\r\n", value); |
| 419 | } |
| 420 | |
| 421 | /***************************************************************************//** |
| 422 | * @brief Displays the Vloop - COM voltage. |
| 423 | * |
| 424 | * @return None. |
| 425 | *******************************************************************************/ |
| 426 | void GetVloop(double* param, char paramNo) // "vloop?" command |
| 427 | { |
| 428 | float value = 0; |
| 429 | |
| 430 | value = AD5421_GetVloop(); |
| 431 | /* Send the requested value to user */ |
| 432 | CONSOLE_Print("Vloop - COM = %.3f [V]\r\n", value); |
| 433 | } |
| 434 | |
| 435 | /***************************************************************************//** |
| 436 | * @brief Displays the Fault register. |
| 437 | * |
| 438 | * @return None. |
| 439 | *******************************************************************************/ |
| 440 | void GetFaultReg(double* param, char paramNo) // "faultReg?" command |
| 441 | { |
| 442 | int value = 0; |
| 443 | |
| 444 | value = AD5421_GetFault(); |
| 445 | /* Send the requested value to user */ |
| 446 | CONSOLE_Print("Fault register = 0x%x\r\n", value); |
| 447 | } |
| 448 | |
| 449 | /***************************************************************************//** |
| 450 | * @brief Displays the output current in milliamps. |
| 451 | * |
| 452 | * @return None. |
| 453 | *******************************************************************************/ |
| 454 | void GetCurrent(double* param, char paramNo) // "current?" command |
| 455 | { |
| 456 | float value = 0; |
| 457 | |
| 458 | /* Datasheet formula */ |
| 459 | value = (16 * ((float)gainReg / 65536) * ((float)dacReg / 65536)) + |
| 460 | 4 + (16 * ((float)offsetReg - 32768) / 65536); |
| 461 | /* Establish the current limits in software, in hardware is already done */ |
| 462 | if (value < 4) |
| 463 | { |
| 464 | value = 4; |
| 465 | } |
| 466 | else if (value > 20) |
| 467 | { |
| 468 | value = 20; |
| 469 | } |
| 470 | /* Send the requested value to user */ |
| 471 | CONSOLE_Print("%s%.3f [mA]\r\n", cmdList[2].name, value); |
| 472 | } |
| 473 | |
| 474 | /***************************************************************************//** |
| 475 | * @brief Sets the output current. |
| 476 | * |
| 477 | * @param param[0] - desired value to be set in milliamps. |
| 478 | * |
| 479 | * @return None. |
| 480 | *******************************************************************************/ |
| 481 | void SetCurrent(double* param, char paramNo) // "current=" command |
| 482 | { |
| 483 | float floatValue = 0; |
| 484 | |
| 485 | /* Check if the parameter is valid */ |
| 486 | if(paramNo >= 1) |
| 487 | { |
| 488 | if(param[0] < 4) |
| 489 | { |
| 490 | param[0] = 4; |
| 491 | } |
| 492 | if(param[0] > 20) |
| 493 | { |
| 494 | param[0] = 20; |
| 495 | } |
| 496 | /* Datasheet formula */ |
| 497 | floatValue = ((float)param[0] - (4 + (16 * ((float)offsetReg - 32768)) / |
| 498 | 65536)) * 65536 * 65536 / (16 * (float)gainReg); |
| 499 | /* Verify the limits of the value to be written in DAC register */ |
| 500 | if (floatValue > 65535) |
| 501 | { |
| 502 | floatValue = 65535; |
| 503 | } |
| 504 | else if (floatValue < 0) |
| 505 | { |
| 506 | floatValue = 0; |
| 507 | } |
| 508 | |
| 509 | dacReg = (int)floatValue; |
| 510 | /* Write to DAC register */ |
| 511 | AD5421_SetDac(dacReg); |
| 512 | /* Datasheet formula */ |
| 513 | floatValue = (16 * ((float)gainReg / 65536) * ((float)dacReg / 65536)) |
| 514 | + 4 + (16 * ((float)offsetReg - 32768) / 65536); |
| 515 | /* Establish the current limits in software, in hardware is already done */ |
| 516 | if (floatValue < 4) |
| 517 | { |
| 518 | floatValue = 4; |
| 519 | } |
| 520 | else if (floatValue > 20) |
| 521 | { |
| 522 | floatValue = 20; |
| 523 | } |
| 524 | /* Send the requested value to user */ |
| 525 | CONSOLE_Print("%s%.3f [mA]\r\n", cmdList[2].name, floatValue); |
| 526 | } |
| 527 | else |
| 528 | { |
| 529 | /* Display error messages */ |
| 530 | DisplayError(2); |
| 531 | } |
| 532 | } |