Debian Codegraph @

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

raw symbols SCIP
1 /**************************************************************************//**
2 * @file Commanad.c
3 * @brief Implementation of the commands given by user through UART for CN0216.
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 "ad7791.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 = "adcCode?",
63 .description = "Displays the ADC Code.",
64 .acceptedValue = "",
65 .example = "",
66 },
67 [2] = {
68 .name = "ad7791Reset!",
69 .description = "Resets the serial interface with the AD7791.",
70 .acceptedValue = "",
71 .example = "",
72 },
73 };
74
75 const char cmdNo = (sizeof(cmdList) / sizeof(struct cmd_info));
76 float VREF = 5;
77
78 /******************************************************************************/
79 /************************ Variables Definitions *******************************/
80 /******************************************************************************/
81 cmdFunction cmdFunctions[] = {GetHelp, GetAdcCode, DoAd7791Reset};
82
83 /***************************************************************************//**
84 * @brief Displays all available commands.
85 *
86 * @return None.
87 *******************************************************************************/
88 void GetHelp(double* param, char paramNo) // "help?" command
89 {
90 unsigned char displayCmd;
91
92 CONSOLE_Print("Available commands:\r\n");
93 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
94 {
95 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[displayCmd].name,
96 (char*)cmdList[displayCmd].description,
97 (char*)cmdList[displayCmd].acceptedValue);
98 }
99 }
100
101 /***************************************************************************//**
102 * @brief Internal function for displaying all the command with its description.
103 *
104 * @return None.
105 *******************************************************************************/
106 void DisplayCmdList()
107 {
108 unsigned char displayCmd;
109
110 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
111 {
112 CONSOLE_Print("%s - %s\r\n", (char*)cmdList[displayCmd].name, \
113 (char*)cmdList[displayCmd].description);
114 }
115 }
116
117 /***************************************************************************//**
118 * @brief Displays error message.
119 *
120 * @return None.
121 *******************************************************************************/
122 void DisplayError(unsigned char funcNo)
123 {
124 /* Display error messages */
125 CONSOLE_Print("Invalid parameter!\r\n");
126 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[funcNo].name, \
127 (char*)cmdList[funcNo].description, \
128 (char*)cmdList[funcNo].acceptedValue);
129 CONSOLE_Print("Example: %s\r\n", (char*)cmdList[funcNo].example);
130 }
131
132 /***************************************************************************//**
133 * @brief Initializes the device.
134 *
135 * @return - The result of the initialization.
136 * Example: ERROR - the device was not initialized or the device
137 * is not present.
138 * SUCCES - the device was initialized and the device
139 * is present.
140 *******************************************************************************/
141 char DoDeviceInit(void)
142 {
143 if(AD7791_Init() == 0)
144 {
145 CONSOLE_Print("Device OK\r\n");
146 DisplayCmdList();
147 return SUCCESS;
148 }
149 else
150 {
151 CONSOLE_Print("Device Error\r\n");
152 return ERROR;
153 }
154 }
155
156 /***************************************************************************//**
157 * @brief Displays the ADC Code from AD7791.
158 *
159 * @return None.
160 *******************************************************************************/
161 void GetAdcCode(double* param, char paramNo) // "adcCode?" command
162 {
163 unsigned long dataDac = 0;
164 unsigned long data = 0;
165 unsigned char count = 0;
166
167 /* Calculate the average for 50 samples */
168 for (count = 0; count < 50; count++)
169 {
170 dataDac = AD7791_Read(AD7791_REG_DATA, 3);
171 data += dataDac;
172 }
173 data /= 50;
174 /* Send feedback to user */
175 CONSOLE_Print("ADC Code=0x%x.\r\n", data);
176 }
177
178 /***************************************************************************//**
179 * @brief Resets the serial interface with the AD7791.
180 *
181 * @return None.
182 *******************************************************************************/
183 void DoAd7791Reset(double* param, char paramNo) // ad7791Reset!
184 {
185 AD7791_Reset();
186 /* Send feedback to user */
187 CONSOLE_Print("AD7791 was reset.\r\n");
188 }