| 1 | /**************************************************************************//** |
| 2 | * @file Commanad.c |
| 3 | * @brief Implementation of the commands given by user through UART for CN0194. |
| 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 "ad7685.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 = "adcCode?", |
| 62 | .description = "Displays the ADC Code for selected channel.", |
| 63 | .acceptedValue = "Accepted values: \r\n\ |
| 64 | \t1 - channel 1. \r\n\ |
| 65 | \t2 - channel 2.", |
| 66 | .example = "To display the ADC code for channel 2, type: adcCode?2", |
| 67 | }, |
| 68 | [2] = { |
| 69 | .name = "voltage?", |
| 70 | .description = "Displays the input voltage for selected channel.", |
| 71 | .acceptedValue = "Accepted values: \r\n\ |
| 72 | \t1 - channel 1. \r\n\ |
| 73 | \t2 - channel 2.", |
| 74 | .example = "To display the input voltage for channel 1, type: voltage?1", |
| 75 | }, |
| 76 | }; |
| 77 | |
| 78 | const char cmdNo = (sizeof(cmdList) / sizeof(struct cmd_info)); |
| 79 | |
| 80 | /******************************************************************************/ |
| 81 | /************************ Variables Definitions *******************************/ |
| 82 | /******************************************************************************/ |
| 83 | cmdFunction cmdFunctions[] = {GetHelp, GetAdcCode, GetVoltage}; |
| 84 | |
| 85 | /***************************************************************************//** |
| 86 | * @brief Displays all available commands. |
| 87 | * |
| 88 | * @return None. |
| 89 | *******************************************************************************/ |
| 90 | void GetHelp(double* param, char paramNo) // "help?" command |
| 91 | { |
| 92 | unsigned char displayCmd; |
| 93 | |
| 94 | CONSOLE_Print("Available commands:\r\n"); |
| 95 | for(displayCmd = 0; displayCmd < cmdNo; displayCmd++) |
| 96 | { |
| 97 | CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[displayCmd].name, |
| 98 | (char*)cmdList[displayCmd].description, |
| 99 | (char*)cmdList[displayCmd].acceptedValue); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /***************************************************************************//** |
| 104 | * @brief Internal function for displaying all the command with its description. |
| 105 | * |
| 106 | * @return None. |
| 107 | *******************************************************************************/ |
| 108 | void DisplayCmdList() |
| 109 | { |
| 110 | unsigned char displayCmd; |
| 111 | |
| 112 | for(displayCmd = 0; displayCmd < cmdNo; displayCmd++) |
| 113 | { |
| 114 | CONSOLE_Print("%s - %s\r\n", (char*)cmdList[displayCmd].name, \ |
| 115 | (char*)cmdList[displayCmd].description); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /***************************************************************************//** |
| 120 | * @brief Displays error message. |
| 121 | * |
| 122 | * @return None. |
| 123 | *******************************************************************************/ |
| 124 | void DisplayError(unsigned char funcNo) |
| 125 | { |
| 126 | /* Display error messages */ |
| 127 | CONSOLE_Print("Invalid parameter!\r\n"); |
| 128 | CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[funcNo].name, \ |
| 129 | (char*)cmdList[funcNo].description, \ |
| 130 | (char*)cmdList[funcNo].acceptedValue); |
| 131 | CONSOLE_Print("Example: %s\r\n", (char*)cmdList[funcNo].example); |
| 132 | } |
| 133 | |
| 134 | /***************************************************************************//** |
| 135 | * @brief Initializes the device. |
| 136 | * |
| 137 | * @return - The result of the initialization. |
| 138 | * Example: ERROR - the device was not initialized or the device |
| 139 | * is not present. |
| 140 | * SUCCES - the device was initialized and the device |
| 141 | * is present. |
| 142 | *******************************************************************************/ |
| 143 | char DoDeviceInit(void) |
| 144 | { |
| 145 | if(ad7685_Init() == 0) |
| 146 | { |
| 147 | CONSOLE_Print("Device OK\r\n"); |
| 148 | DisplayCmdList(); |
| 149 | return SUCCESS; |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | CONSOLE_Print("Device Error\r\n"); |
| 154 | return ERROR; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /***************************************************************************//** |
| 159 | * @brief Displays the ADC Code for selected channel. |
| 160 | * |
| 161 | * @param param[0] - selected channel. |
| 162 | * |
| 163 | * @return None. |
| 164 | *******************************************************************************/ |
| 165 | void GetAdcCode(double* param, char paramNo) // "adcCode?" command |
| 166 | { |
| 167 | unsigned char adcNo; |
| 168 | unsigned short *dataDac = 0; |
| 169 | |
| 170 | /* Check if the parameter is valid */ |
| 171 | if(paramNo >= 1) |
| 172 | { |
| 173 | if(param[0] < 1) |
| 174 | { |
| 175 | param[0] = 1; |
| 176 | } |
| 177 | if(param[0] > 2) |
| 178 | { |
| 179 | param[0] = 2; |
| 180 | } |
| 181 | |
| 182 | adcNo = (unsigned char)param[0]; |
| 183 | ad7685_GetConversion(2, dataDac); |
| 184 | /* Send feedback to user */ |
| 185 | CONSOLE_Print("ADC Code=%d for channel %d.\r\n", dataDac[adcNo - 1], adcNo); |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | /* Display error messages */ |
| 190 | DisplayError(1); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /***************************************************************************//** |
| 195 | * @brief Displays the input voltage for selected channel. |
| 196 | * |
| 197 | * @param param[0] - selected channel. |
| 198 | * |
| 199 | * @return None. |
| 200 | *******************************************************************************/ |
| 201 | void GetVoltage(double* param, char paramNo) // "voltage?" command |
| 202 | { |
| 203 | float voltage[2] = {0.0, 0.0}; |
| 204 | unsigned char adcNo; |
| 205 | unsigned short *dataDac = 0; |
| 206 | |
| 207 | /* Check if the parameter is valid */ |
| 208 | if(paramNo >= 1) |
| 209 | { |
| 210 | if(param[0] < 1) |
| 211 | { |
| 212 | param[0] = 1; |
| 213 | } |
| 214 | if(param[0] > 2) |
| 215 | { |
| 216 | param[0] = 2; |
| 217 | } |
| 218 | |
| 219 | adcNo = (unsigned char)param[0]; |
| 220 | ad7685_GetConversion(2, dataDac); |
| 221 | /* Voltage formula for input inverter configuration. */ |
| 222 | voltage[adcNo - 1] = (10.0 - (float)(dataDac[adcNo - 1] * 20) / 65535); |
| 223 | /* Send feedback to user */ |
| 224 | CONSOLE_Print("Voltage=%.3f for channel %d.\r\n", voltage[adcNo - 1], adcNo); |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | /* Display error messages */ |
| 229 | DisplayError(2); |
| 230 | } |
| 231 | } |