Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/ad9361/sw/console_commands/console.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file console.c
3 * @brief Implementation of Console Driver.
4 * @author DBogdan ([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 /***************************** Include Files **********************************/
42 /******************************************************************************/
43 #include "stdarg.h"
44 #include "stdlib.h"
45 #include "stdio.h"
46 #include "console.h"
47
48 /***************************************************************************//**
49 * @brief Initializes the UART communication peripheral. If the value of the
50 * baud rate is not equal with the ipcore's baud rate the
51 * initialization going to FAIL.
52 *
53 * @param baudRate - Baud rate value.
54 * Example: 9600 - 9600 bps.
55 *
56 * @return status - Result of the initialization procedure.
57 * Example: 0 - if initialization was successful;
58 * -1 - if initialization was unsuccessful.
59 *
60 *******************************************************************************/
61 char uart_init(unsigned long baudRate)
62 {
63 return 0;
64 }
65
66 /***************************************************************************//**
67 * @brief Writes one character to UART.
68 *
69 * @param data - Character to write.
70 *
71 * @return None.
72 *******************************************************************************/
73 void uart_write_char(char data)
74 {
75 putchar(data);
76 }
77
78 /***************************************************************************//**
79 * @brief Reads one character from UART.
80 *
81 * @return received_char - Read character.
82 *
83 * Note: Blocking function - Waits until get a valid data.
84 *******************************************************************************/
85 void uart_read_char(char * data)
86 {
87 *data = getchar();
88 }
89
90 /***************************************************************************//**
91 * @brief Writes one character string to UART.
92 *
93 * @param data - Character to write.
94 *
95 * @return None.
96 *******************************************************************************/
97 void uart_write_string(const char* string)
98 {
99 while(*string)
100 {
101 uart_write_char(*string++);
102 }
103 }
104
105 /***************************************************************************//**
106 * @brief Converts an integer number to a string of ASCII characters string.
107 *
108 * @param number - Integer number.
109 * @param base - Numerical base used to represent the integer number as
110 * string.
111 *
112 * @return Pointer to the string of ASCII characters.
113 *******************************************************************************/
114 char *int_to_str(long number, char base)
115 {
116 unsigned long pos_number = 0;
117 char neg_sign = 0;
118 const char digits[17] = "0123456789ABCDEF";
119 static char buffer[17] = " ";
120 char* buffer_ptr = &buffer[16];
121
122 if((number < 0) && (base == 10))
123 {
124 neg_sign = 1;
125 pos_number = -1 * number;
126 }
127 else
128 {
129 pos_number = (unsigned long)number;
130 }
131 do
132 {
133 *buffer_ptr-- = digits[pos_number % base];
134 pos_number /= base;
135 }
136 while(pos_number != 0);
137 if(neg_sign)
138 {
139 *buffer_ptr-- = '-';
140 }
141 *buffer_ptr++;
142
143 return buffer_ptr;
144 }
145
146 /***************************************************************************//**
147 * @brief Prints formatted data to console.
148 *
149 * @param str - String to be printed.
150 *
151 * @return None.
152 *******************************************************************************/
153 void console_print(char* str, ...)
154 {
155 char* string_ptr;
156 char first_param = 0;
157 char second_param = 0;
158 unsigned long x_mask = 0;
159 unsigned long d_mask = 0;
160 char ch_number = 0;
161 unsigned long multiplier = 1;
162 char* str_arg;
163 long long_arg;
164 double double_arg;
165 va_list argp;
166
167 va_start(argp, str);
168 for(string_ptr = str; *string_ptr != '\0'; string_ptr++)
169 {
170 if(*string_ptr!='%')
171 {
172 uart_write_char(*string_ptr);
173 continue;
174 }
175 string_ptr++;
176 first_param = 0;
177 while((*string_ptr >= 0x30) & (*string_ptr <= 0x39))
178 {
179 first_param *= 10;
180 first_param += (*string_ptr - 0x30);
181 string_ptr++;
182 }
183 if(*string_ptr == '.')
184 {
185 string_ptr++;
186 second_param = 0;
187 while((*string_ptr >= 0x30) & (*string_ptr <= 0x39))
188 {
189 second_param *= 10;
190 second_param += (*string_ptr - 0x30);
191 string_ptr++;
192 }
193 }
194 switch(*string_ptr)
195 {
196 case 'c':
197 long_arg = va_arg(argp, long);
198 uart_write_char((char)long_arg);
199 break;
200 case 's':
201 str_arg = va_arg(argp, char*);
202 uart_write_string(str_arg);
203 break;
204 case 'd':
205 long_arg = va_arg(argp, long);
206 uart_write_string(int_to_str(long_arg, 10));
207 break;
208 case 'x':
209 long_arg = va_arg(argp, long);
210 x_mask = 268435456;
211 ch_number = 8;
212 while(x_mask > long_arg)
213 {
214 x_mask /= 16;
215 ch_number--;
216 }
217 while(ch_number < first_param)
218 {
219 uart_write_char('0');
220 ch_number++;
221 }
222 if(long_arg != 0)
223 {
224 uart_write_string(int_to_str(long_arg, 16));
225 }
226 break;
227 case 'f':
228 double_arg = va_arg(argp, double);
229 if(second_param == 0)
230 {
231 second_param = 3;
232 }
233 ch_number = second_param;
234 while(ch_number > 0)
235 {
236 multiplier *= 10;
237 ch_number--;
238 }
239 double_arg *= multiplier;
240 if(double_arg < 0)
241 {
242 double_arg *= -1;
243 uart_write_char('-');
244 }
245 long_arg = (long)double_arg;
246 uart_write_string(int_to_str((long_arg / multiplier), 10));
247 uart_write_char('.');
248 d_mask = 1000000000;
249 ch_number = 10;
250 while(d_mask > (long)(long_arg % multiplier))
251 {
252 d_mask /= 10;
253 ch_number--;
254 }
255 while(ch_number < second_param)
256 {
257 uart_write_char('0');
258 ch_number++;
259 }
260 if((long_arg % multiplier) != 0)
261 {
262 uart_write_string(int_to_str((long_arg % multiplier), 10));
263 }
264 break;
265 }
266 }
267 va_end(argp);
268 }
269
270 /***************************************************************************//**
271 * @brief Initializes the serial console.
272 *
273 * @param baud_rate - Baud rate value.
274 * Example: 9600 - 9600 bps.
275 *
276 * @return status - Result of the initialization procedure.
277 * Example: -1 - if initialization was unsuccessful;
278 * 0 - if initialization was successful.
279 *******************************************************************************/
280 char console_init(unsigned long baud_rate)
281 {
282 return uart_init(baud_rate);
283 }
284
285 /***************************************************************************//**
286 * @brief Reads one command from console.
287 *
288 * @param command - Read command.
289 *
290 * @return None.
291 *******************************************************************************/
292 void console_get_command(char* command)
293 {
294 char received_char = 0;
295 unsigned char char_number = 0;
296
297 while((received_char != '\n') && (received_char != '\r'))
298 {
299 uart_read_char(&received_char);
300 command[char_number++] = received_char;
301 }
302 }
303
304 /***************************************************************************//**
305 * @brief Compares two commands and returns the type of the command.
306 *
307 * @param received_cmd - Received command.
308 * @param expected_cmd - Expected command.
309 * @param param - Parameters' buffer.
310 * @param param_no - Nomber of parameters.
311 *
312 * @return cmd_type - Type of the command.
313 * Example: UNKNOWN_CMD - Commands don't match.
314 * DO_CMD - Do command (!).
315 * READ_CMD - Read command (?).
316 * WRITE_CMD - Write command (=).
317 *******************************************************************************/
318 int console_check_commands(char* received_cmd,
319 const char* expected_cmd,
320 double* param,
321 char* param_no)
322 {
323 int cmd_type = 1;
324 unsigned char char_index = 0;
325 char param_string[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
326 unsigned char param_index = 0;
327 unsigned char index = 0;
328 const char digits[17] = "0123456789ABCDEF";
329 unsigned char digit_index = 0;
330
331 while((expected_cmd[char_index] != '!') &&
332 (expected_cmd[char_index] != '?') &&
333 (expected_cmd[char_index] != '=') &&
334 (cmd_type != UNKNOWN_CMD))
335 {
336 if(expected_cmd[char_index] != received_cmd[char_index])
337 {
338 cmd_type = UNKNOWN_CMD;
339 }
340 char_index++;
341 }
342 if(cmd_type != UNKNOWN_CMD)
343 {
344 if(expected_cmd[char_index] == '!')
345 {
346 if(received_cmd[char_index] == '!')
347 {
348 cmd_type = DO_CMD;
349 }
350 else
351 {
352 cmd_type = UNKNOWN_CMD;
353 }
354 }
355 if(expected_cmd[char_index] == '?')
356 {
357 if(received_cmd[char_index] == '?')
358 {
359 cmd_type = READ_CMD;
360 }
361 else
362 {
363 cmd_type = UNKNOWN_CMD;
364 }
365 }
366 if(expected_cmd[char_index] == '=')
367 {
368 if(received_cmd[char_index] == '=')
369 {
370 cmd_type = WRITE_CMD;
371 }
372 else
373 {
374 cmd_type = UNKNOWN_CMD;
375 }
376 }
377 if((cmd_type == WRITE_CMD) || (cmd_type == READ_CMD))
378 {
379 char_index++;
380 while((received_cmd[char_index] != '\n') &&
381 (received_cmd[char_index] != '\r'))
382 {
383 if((received_cmd[char_index] == 0x20))
384 {
385 *param = 0;
386 if((param_string[0] == '0') && (param_string[1] == 'x'))
387 {
388 for(index = 2; index < param_index; index++)
389 {
390 for(digit_index = 0; digit_index < 16; digit_index++)
391 {
392 if(param_string[index] == digits[digit_index])
393 {
394 *param = *param * 16;
395 *param = *param + digit_index;
396 }
397 }
398 }
399 }
400 else
401 {
402 if(param_string[0] == '-')
403 {
404 *param = atof((const char*)(&param_string[1]));
405 *param *= (-1);
406 }
407 else
408 {
409 *param = atof((const char*)param_string);
410 }
411 }
412 param++;
413 *param_no += 1;
414 for(param_index = 0; param_index < 10; param_index++)
415 {
416 param_string[param_index] = 0;
417 }
418 param_index = 0;
419 char_index++;
420 }
421 else
422 {
423 param_string[param_index] = received_cmd[char_index];
424 char_index++;
425 param_index++;
426 }
427 }
428 if(param_index)
429 {
430 *param = 0;
431 if((param_string[0] == '0') && (param_string[1] == 'x'))
432 {
433 for(index = 2; index < param_index; index++)
434 {
435 for(digit_index = 0; digit_index < 16; digit_index++)
436 {
437 if(param_string[index] == digits[digit_index])
438 {
439 *param *= 16;
440 *param += digit_index;
441 }
442 }
443 }
444 }
445 else
446 {
447 if(param_string[0] == '-')
448 {
449 *param = atof((const char*)(&param_string[1]));
450 *param *= (-1);
451 }
452 else
453 {
454 *param = atof((const char*)param_string);
455 }
456 }
457 *param_no += 1;
458 }
459 }
460 }
461
462 return cmd_type;
463 }