Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/Microchip/PIC32MX320F128H/PmodDPOT/Command.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file Command.c
3 * @brief Implementation of the commands given by user through UART for PmodTMP2.
4 * @author DNechita ([email protected])
5 ********************************************************************************
6 * Copyright 2013(c) Analog Devices, Inc.
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * - Neither the name of Analog Devices, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 * - The use of this software may or may not infringe the patent rights
22 * of one or more patent holders. This license does not release you
23 * from the requirement that you obtain separate licenses from these
24 * patent holders to use this software.
25 * - Use of the software either in source or binary form, must be run
26 * on or directly connected to an Analog Devices Inc. component.
27 *
28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *
39 *******************************************************************************/
40
41 /******************************************************************************/
42 /***************************** Include Files **********************************/
43 /******************************************************************************/
44 #include "Command.h"
45 #include "AD5160.h"
46 #include "Console.h"
47
48 /******************************************************************************/
49 /************************ Constants Definitions *******************************/
50 /******************************************************************************/
51 /*!< List of available commands */
52 const char* cmdList[] = {"help?",
53 "resistance=",
54 "resistance?",
55 "register=",
56 "register?"};
57 const char* cmdDescription[] = {
58 " - Displays all available commands.",
59 " - Sets the output resistance between W and B. Accepted values: \r\n\
60 \t60 .. 10020 - resistance value to be set in ohms.",
61 " - Displays the last written resistance value in ohms.",
62 " - Writes to the DAC register. Accepted values:\r\n\
63 \t0 .. 255 - the value written to the DAC.",
64 " - Displays the last written value in register."};
65 const char* cmdExample[] = {
66 "",
67 "To set the resistance value to 1000 [ohms], type: resistance=1000",
68 "",
69 "To set the register value to 150, type: register=150",
70 ""};
71 const char cmdNo = (sizeof(cmdList) / sizeof(const char*));
72 const short R_AB = 10000; /*!< 10 KOhm. R_AB is the nominal resistance. */
73 const char R_W = 60; /*!< 60 Ohm is the wiper contact resistance. */
74 /******************************************************************************/
75 /************************ Variables Definitions *******************************/
76 /******************************************************************************/
77 cmdFunction cmdFunctions[9] = {GetHelp, SetResistance, GetResistance,
78 SetRegister, GetRegister};
79 /*!< Variables holding information about the device */
80 unsigned char registerValue = 0; /*!< Last register value written to the device */
81 double outResistance = 0; /*!< Last resistance value written to the device */
82
83 /***************************************************************************//**
84 * @brief Displays all available commands.
85 *
86 * @return None.
87 *******************************************************************************/
88 void GetHelp(double* param, char paramNo) /*!< "help?" command */
89 {
90 char displayCmd;
91
92 CONSOLE_Print("Available commands:\r\n");
93 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
94 {
95 CONSOLE_Print("%s%s\r\n", (char*)cmdList[displayCmd],
96 (char*)cmdDescription[displayCmd]);
97 }
98 }
99
100 /***************************************************************************//**
101 * @brief Initializes the device.
102 *
103 * @return - The result of the initialization.
104 * Example: ERROR - the device was not initialized or the device
105 * is not present.
106 * SUCCES - the device was initialized and the device
107 * is present.
108 *******************************************************************************/
109 char DoDeviceInit(void)
110 {
111 if(AD5160_Init() == 0)
112 {
113 CONSOLE_Print("AD5160 OK\r\n");
114 GetHelp(NULL, 0);
115
116 return SUCCESS;
117 }
118 else
119 {
120 CONSOLE_Print("AD5160 Error\r\n");
121
122 return ERROR;
123 }
124 }
125
126 /***************************************************************************//**
127 * @brief Sets the output resistance between W and B, in ohms.
128 *
129 * @return None.
130 *******************************************************************************/
131 void SetResistance(double* param, char paramNo) /*!< "resistance=" command */
132 {
133 double tempFloat = 0;
134
135 /*!< Check if the parameter is valid */
136 if(paramNo >= 1)
137 {
138 if(param[0] < 60)
139 {
140 param[0] = 60;
141 }
142 if(param[0] > 10020)
143 {
144 param[0] = 10020;
145 }
146 outResistance = param[0];
147 /*!< Find the binary value corresponding to the output voltage*/
148 tempFloat = ((outResistance - R_W) / (double)R_AB) * 256;
149 registerValue = (unsigned char)tempFloat;
150 /*!< Round the value */
151 if(tempFloat - registerValue >= 0.5)
152 {
153 registerValue ++;
154 }
155 /*!< Write to DAC register */
156 AD5160_Set(registerValue);
157 /*!< Send feedback to user */
158 outResistance = R_AB * ((double)registerValue / 256) + R_W;
159 /*!< Convert float to string and send it to user */
160 CONSOLE_Print("%s%d [ohms]\r\n",(char*)cmdList[1],\
161 (unsigned short)outResistance);
162 }
163 else
164 {
165 /*!< Display error messages */
166 CONSOLE_Print("Invalid parameter!\r\n");
167 CONSOLE_Print("%s%s\r\n", (char*)cmdList[1], (char*)cmdDescription[1]);
168 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[1]);
169 }
170 }
171
172 /***************************************************************************//**
173 * @brief Displays the last written resistance value in ohms.
174 *
175 * @return None.
176 *******************************************************************************/
177 void GetResistance(double* param, char paramNo) /*!< "resistance?" command */
178 {
179 /*!< Get the output resistance using data from registerValue */
180 outResistance = R_AB * ((double)registerValue / 256) + R_W;
181 /*!< Send the requested value to user */
182 CONSOLE_Print("%s%d [ohms]\r\n",(char*)cmdList[1], \
183 (unsigned short)outResistance);
184 }
185
186 /***************************************************************************//**
187 * @brief Writes to the DAC register.
188 *
189 * @return None.
190 *******************************************************************************/
191 void SetRegister(double* param, char paramNo) /*!< "register=" command */
192 {
193 /*!< Check if the parameter is valid */
194 if(paramNo >= 1)
195 {
196 if(param[0] < 0)
197 {
198 param[0] = 0;
199 }
200 if(param[0] > 255)
201 {
202 param[0] = 255;
203 }
204
205 registerValue = (unsigned char)param[0];
206 /*!< Write to DAC register */
207 AD5160_Set(registerValue);
208 /*!< Send feedback to user */
209 CONSOLE_Print("%s%d\r\n",(char*)cmdList[3],registerValue);
210 }
211 else
212 {
213 /*!< Display error messages */
214 CONSOLE_Print("Invalid parameter!\r\n");
215 CONSOLE_Print("%s%s\r\n", (char*)cmdList[3], (char*)cmdDescription[3]);
216 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[3]);
217 }
218 }
219
220 /***************************************************************************//**
221 * @brief Displays the last written value in register.
222 *
223 * @return None.
224 *******************************************************************************/
225 void GetRegister(double* param, char paramNo) /*!< "register?" command */
226 {
227 /*!< Send the requested value to user */
228 CONSOLE_Print("%s%d\r\n",(char*)cmdList[3],registerValue);
229 }