Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/device_commands/CN0271/Command.c

raw symbols SCIP
1 /**************************************************************************//**
2 * @file Commanad.c
3 * @brief Implementation of the commands given by user through UART for CN0271.
4 * @author Lucian Sin ([email protected])
5 *
6 *******************************************************************************
7 * Copyright 2014(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 "ad7790.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 = "adcCode?",
62 .description = "Displays the ADC Code.",
63 .acceptedValue = "",
64 .example = "",
65 },
66 [2] = {
67 .name = "temperature?",
68 .description = "Displays the temperature measured by the thermocouple.",
69 .acceptedValue = "",
70 .example = "",
71 },
72 [3] = {
73 .name = "reset!",
74 .description = "Resets the serial interface with AD7790.",
75 .acceptedValue = "",
76 .example = "",
77 },
78 };
79
80 const char cmdNo = (sizeof(cmdList) / sizeof(struct cmd_info));
81 const float VREF = 2.5;
82 const unsigned char GAIN = 1;
83
84 /******************************************************************************/
85 /************************ Variables Definitions *******************************/
86 /******************************************************************************/
87 cmdFunction cmdFunctions[] = {GetHelp, GetAdcCode, GetTemperature, DoReset};
88
89 /***************************************************************************//**
90 * @brief Displays all available commands.
91 *
92 * @return None.
93 *******************************************************************************/
94 void GetHelp(double* param, char paramNo) // "help?" command
95 {
96 unsigned char displayCmd;
97
98 CONSOLE_Print("Available commands:\r\n");
99 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
100 {
101 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[displayCmd].name,
102 (char*)cmdList[displayCmd].description,
103 (char*)cmdList[displayCmd].acceptedValue);
104 }
105 }
106
107 /***************************************************************************//**
108 * @brief Internal function for displaying all the command with its description.
109 *
110 * @return None.
111 *******************************************************************************/
112 void DisplayCmdList()
113 {
114 unsigned char displayCmd;
115
116 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
117 {
118 CONSOLE_Print("%s - %s\r\n", (char*)cmdList[displayCmd].name, \
119 (char*)cmdList[displayCmd].description);
120 }
121 }
122
123 /***************************************************************************//**
124 * @brief Displays error message.
125 *
126 * @return None.
127 *******************************************************************************/
128 void DisplayError(unsigned char funcNo)
129 {
130 /* Display error messages */
131 CONSOLE_Print("Invalid parameter!\r\n");
132 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[funcNo].name, \
133 (char*)cmdList[funcNo].description, \
134 (char*)cmdList[funcNo].acceptedValue);
135 CONSOLE_Print("Example: %s\r\n", (char*)cmdList[funcNo].example);
136 }
137
138 /***************************************************************************//**
139 * @brief Initializes the device.
140 *
141 * @return - The result of the initialization.
142 * Example: ERROR - the device was not initialized or the device
143 * is not present.
144 * SUCCES - the device was initialized and the device
145 * is present.
146 *******************************************************************************/
147 char DoDeviceInit(void)
148 {
149 if(AD7790_Init() == 0)
150 {
151 CONSOLE_Print("Device OK\r\n");
152 DisplayCmdList();
153 return SUCCESS;
154 }
155 else
156 {
157 CONSOLE_Print("Device Error\r\n");
158 return ERROR;
159 }
160 }
161
162 /***************************************************************************//**
163 * @brief Displays the ADC Code from AD7790.
164 *
165 * @return None.
166 *******************************************************************************/
167 void GetAdcCode(double* param, char paramNo) // "adcCode?" command
168 {
169 unsigned long dataDac = 0;
170 unsigned long data = 0;
171 unsigned char count = 0;
172
173 /* Calculate the average for 50 samples */
174 for (count = 0; count < 50; count++)
175 {
176 dataDac = AD7790_Read(AD7790_REG_DATA, 2);
177 data += dataDac;
178 }
179 data /= 50;
180 /* Send feedback to user */
181 CONSOLE_Print("ADC Code=0x%x.\r\n", data);
182 }
183
184 /***************************************************************************//**
185 * @brief Displays the temperature measured by the thermocouple.
186 *
187 * @return None.
188 *******************************************************************************/
189 void GetTemperature(double* param, char paramNo) // "temperature?" command
190 {
191 unsigned long dataDac = 0;
192 unsigned long data = 0;
193 unsigned char count = 0;
194 float temp = 0;
195 float voltage = 0;
196
197 /* Calculate the average for 50 samples */
198 for (count = 0; count < 50; count++)
199 {
200 dataDac = AD7790_Read(AD7790_REG_DATA, 2);
201 data += dataDac;
202 }
203 data /= 50;
204 //CONSOLE_Print("ADC Code=0x%x.\r\n", data);
205 voltage = AD7790_ConvertToVoltage(data, VREF, GAIN) + 2500; // in mV
206 //CONSOLE_Print("Voltage=%.3f[mV].\r\n", voltage);
207 temp = voltage / 5; // 5mV/degreeCelsius
208 /* Send feedback to user */
209 CONSOLE_Print("Temperature=%.3f[C].\r\n", temp);
210 }
211
212 /***************************************************************************//**
213 * @brief Resets the serial interface with AD7790.
214 *
215 * @return None.
216 *******************************************************************************/
217 void DoReset(double* param, char paramNo) // "reset!" command
218 {
219 AD7790_Reset();
220 /* Send feedback to user */
221 CONSOLE_Print("AD7790 was reset.\r\n");
222 }