| 1 | /**************************************************************************//** |
| 2 | * @file Commanad.c |
| 3 | * @brief Implementation of the commands given by user through UART for CN0150. |
| 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 "ad7887.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 = "calibration=", |
| 63 | .description = "Makes a two points calibration.", |
| 64 | .acceptedValue = "Accepted values:\r\n\ |
| 65 | \tpower input1: \r\n\ |
| 66 | \t-50 .. -5 - first point input power in [dBm].\r\n\ |
| 67 | \tpower input2: \r\n\ |
| 68 | \t-50 .. -5 - second point input power in [dBm].", |
| 69 | .example = "To choose -30[dBm] and -20[dBm] as the two calibration \ |
| 70 | points, type: calibration=-30 -20", |
| 71 | }, |
| 72 | [2] = { |
| 73 | .name = "pinCalc?", |
| 74 | .description = "Displays the calculated input power.", |
| 75 | .acceptedValue = "", |
| 76 | .example = "", |
| 77 | }, |
| 78 | [3] = { |
| 79 | .name = "error?", |
| 80 | .description = "Displays the error associated with the last input \ |
| 81 | power calculation.", |
| 82 | .acceptedValue = "Accepted values: \r\n\ |
| 83 | \t-50 .. -5 - true input power in [dBm].", |
| 84 | .example = "To display the error between the last calculated input \ |
| 85 | power and the last true \r\n input power=-20[dBm], type: error?-20", |
| 86 | }, |
| 87 | }; |
| 88 | |
| 89 | const char cmdNo = (sizeof(cmdList) / sizeof(struct cmd_info)); |
| 90 | |
| 91 | /******************************************************************************/ |
| 92 | /************************ Variables Definitions *******************************/ |
| 93 | /******************************************************************************/ |
| 94 | cmdFunction cmdFunctions[] = {GetHelp, DoCalibration, GetPinCalc, GetError}; |
| 95 | |
| 96 | /* Variables holding information about the device */ |
| 97 | float slope = -24; // nominal value |
| 98 | float intercept = 20; // nominal value |
| 99 | float pinCalc = 0; |
| 100 | unsigned char data[2] = {0x21, 0x00}; /* control bytes for ADC |
| 101 | 0x21 - the on-chip reference is disabled; mode 2; single channel. */ |
| 102 | |
| 103 | /***************************************************************************//** |
| 104 | * @brief Displays all available commands. |
| 105 | * |
| 106 | * @return None. |
| 107 | *******************************************************************************/ |
| 108 | void GetHelp(double* param, char paramNo) // "help?" command |
| 109 | { |
| 110 | unsigned char displayCmd; |
| 111 | |
| 112 | CONSOLE_Print("Available commands:\r\n"); |
| 113 | for(displayCmd = 0; displayCmd < cmdNo; displayCmd++) |
| 114 | { |
| 115 | CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[displayCmd].name, |
| 116 | (char*)cmdList[displayCmd].description, |
| 117 | (char*)cmdList[displayCmd].acceptedValue); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /***************************************************************************//** |
| 122 | * @brief Internal function for displaying all the command with its description. |
| 123 | * |
| 124 | * @return None. |
| 125 | *******************************************************************************/ |
| 126 | void DisplayCmdList() |
| 127 | { |
| 128 | unsigned char displayCmd; |
| 129 | |
| 130 | for(displayCmd = 0; displayCmd < cmdNo; displayCmd++) |
| 131 | { |
| 132 | CONSOLE_Print("%s - %s\r\n", (char*)cmdList[displayCmd].name, \ |
| 133 | (char*)cmdList[displayCmd].description); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /***************************************************************************//** |
| 138 | * @brief Displays error message. |
| 139 | * |
| 140 | * @return None. |
| 141 | *******************************************************************************/ |
| 142 | void DisplayError(unsigned char funcNo) |
| 143 | { |
| 144 | /* Display error messages */ |
| 145 | CONSOLE_Print("Invalid parameter!\r\n"); |
| 146 | CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[funcNo].name, \ |
| 147 | (char*)cmdList[funcNo].description, \ |
| 148 | (char*)cmdList[funcNo].acceptedValue); |
| 149 | CONSOLE_Print("Example: %s\r\n", (char*)cmdList[funcNo].example); |
| 150 | } |
| 151 | |
| 152 | /***************************************************************************//** |
| 153 | * @brief Initializes the device. |
| 154 | * |
| 155 | * @return - The result of the initialization. |
| 156 | * Example: ERROR - the device was not initialized or the device |
| 157 | * is not present. |
| 158 | * SUCCES - the device was initialized and the device |
| 159 | * is present. |
| 160 | *******************************************************************************/ |
| 161 | char DoDeviceInit(void) |
| 162 | { |
| 163 | if(AD7887_Init() == 0) |
| 164 | { |
| 165 | CONSOLE_Print("Device OK\r\n"); |
| 166 | DisplayCmdList(); |
| 167 | return SUCCESS; |
| 168 | } |
| 169 | else |
| 170 | { |
| 171 | CONSOLE_Print("Device Error\r\n"); |
| 172 | return ERROR; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /***************************************************************************//** |
| 177 | * @brief Makes a two points calibration. |
| 178 | * |
| 179 | * @param param[0] - first point input power in [dBm]. |
| 180 | * param[1] - second point input power in [dBm]. |
| 181 | * |
| 182 | * @return None. |
| 183 | *******************************************************************************/ |
| 184 | void DoCalibration(double* param, char paramNo) // "calibration=" command |
| 185 | { |
| 186 | char pin1 = 0; |
| 187 | char pin2 = 0; |
| 188 | unsigned short code1 = 0; |
| 189 | unsigned short code2 = 0; |
| 190 | unsigned char count = 0; |
| 191 | unsigned long average1 = 0; |
| 192 | unsigned long average2 = 0; |
| 193 | |
| 194 | /* Check if the parameters are valid. */ |
| 195 | if(paramNo >= 2) |
| 196 | { |
| 197 | if (param[0] < -50) |
| 198 | { |
| 199 | param[0] = -50; |
| 200 | } |
| 201 | if (param[0] > -5) |
| 202 | { |
| 203 | param[0] = -5; |
| 204 | } |
| 205 | pin1 = (char)param[0]; |
| 206 | |
| 207 | if (param[1] < -50) |
| 208 | { |
| 209 | param[1] = -50; |
| 210 | } |
| 211 | if (param[1] > -5) |
| 212 | { |
| 213 | param[1] = -5; |
| 214 | } |
| 215 | pin2 = (char)param[1]; |
| 216 | |
| 217 | CONSOLE_Print("First set the input power to %d[dBm].\r\n", pin1); |
| 218 | CONSOLE_Print("You have 10 seconds for that.\r\n"); |
| 219 | /* Make a 10 seconds delay. */ |
| 220 | for (count = 0; count < 10; count++) |
| 221 | { |
| 222 | TIME_DelayMs(1000); |
| 223 | } |
| 224 | /* Calculate the average ADC code from 50 samples. */ |
| 225 | for (count = 0; count < 50; count++) |
| 226 | { |
| 227 | code1 = (AD7887_Read16Bits(data)) & 0x0FFF; |
| 228 | average1 += code1; |
| 229 | } |
| 230 | average1 /= 50; |
| 231 | code1 = (unsigned short)average1; |
| 232 | |
| 233 | CONSOLE_Print("Now set the input power to %d[dBm].\r\n", pin2); |
| 234 | CONSOLE_Print("You have 10 seconds for that.\r\n"); |
| 235 | /* Make a 10 seconds delay. */ |
| 236 | for (count = 0; count < 10; count++) |
| 237 | { |
| 238 | TIME_DelayMs(1000); |
| 239 | } |
| 240 | /* Calculate the average ADC code from 50 samples. */ |
| 241 | for (count = 0; count < 50; count++) |
| 242 | { |
| 243 | code2 = (AD7887_Read16Bits(data)) & 0x0FFF; |
| 244 | average2 += code2; |
| 245 | } |
| 246 | average2 /= 50; |
| 247 | code2 = (unsigned short)average2; |
| 248 | /* Calculate the slope and intercept according to datasheet formulas. */ |
| 249 | slope = (float)(code2 - code1) / (pin2 - pin1); |
| 250 | intercept = (float)pin2 - (float)(code2 / slope); |
| 251 | /* Send feedback to user */ |
| 252 | CONSOLE_Print("The slope is %.3f.\r\n", slope); |
| 253 | CONSOLE_Print("The intercept is %.3f.\r\n", intercept); |
| 254 | } |
| 255 | else |
| 256 | { |
| 257 | /* Display error messages */ |
| 258 | DisplayError(1); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /***************************************************************************//** |
| 263 | * @brief Displays the calculated input power. |
| 264 | * |
| 265 | * @return None. |
| 266 | *******************************************************************************/ |
| 267 | void GetPinCalc(double* param, char paramNo) // "pinCalc?" command |
| 268 | { |
| 269 | unsigned char count = 0; |
| 270 | unsigned short code = 0; |
| 271 | unsigned long average = 0; |
| 272 | |
| 273 | /* Calculate the average ADC code from 50 samples. */ |
| 274 | for (count = 0; count < 50; count++) |
| 275 | { |
| 276 | code = (AD7887_Read16Bits(data)) & 0x0FFF; |
| 277 | average += code; |
| 278 | } |
| 279 | average /= 50; |
| 280 | code = (unsigned short)average; |
| 281 | /* Calculate the input power according to datasheet formula. */ |
| 282 | pinCalc = (float)(code / slope) + intercept; |
| 283 | /* Send feedback to user */ |
| 284 | CONSOLE_Print("ADC Code=%d. \r\n", code); |
| 285 | CONSOLE_Print("The calculated input power is %.3f[dBm]. \r\n", pinCalc); |
| 286 | } |
| 287 | |
| 288 | /***************************************************************************//** |
| 289 | * @brief Displays the error associated with the last input power calculation. |
| 290 | * |
| 291 | * @param param[0] - last true input power. |
| 292 | * |
| 293 | * @return None. |
| 294 | *******************************************************************************/ |
| 295 | void GetError(double* param, char paramNo) // "error?" command |
| 296 | { |
| 297 | float error = 0; |
| 298 | |
| 299 | if(paramNo >= 1) |
| 300 | { |
| 301 | if (param[0] < -50) |
| 302 | { |
| 303 | param[0] = -50; |
| 304 | } |
| 305 | if (param[0] > -5) |
| 306 | { |
| 307 | param[0] = -5; |
| 308 | } |
| 309 | /* Calculate the error according to datasheet formula. */ |
| 310 | error = pinCalc - param[0]; |
| 311 | /* Send feedback to user */ |
| 312 | CONSOLE_Print("The error between the last calculated input power and \ |
| 313 | the last true input power is %.3f. \r\n", error); |
| 314 | } |
| 315 | else |
| 316 | { |
| 317 | /* Display error messages */ |
| 318 | DisplayError(3); |
| 319 | } |
| 320 | } |