2 * Copyright (C) 2012-2016 Czech Technical University in Prague
4 * Created on: 28.2.2013
9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without
12 * restriction, including without limitation the rights to use,
13 * copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
33 * This file contains command for reading adc port.
46 * @brief Read values from ADC port
48 * @param[in] cmd_io Pointer to IO stack
49 * @param[in] des Pointer to command descriptor
50 * @param[in] param Parameters of command
51 * @return 0 when OK or error code lower than 0
53 int cmd_do_read_adc1_values(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
55 uint32_t i, min, max; // Cycle control variable
58 if (param[1] == param[0] + 7) { /* Single pin variant */
59 if (sscanf(param[1], "%d", &min) != 1)
60 return -CMDERR_BADPAR;
61 if (min >= PORT_ADC_CHANNELS)
67 max = PORT_ADC_CHANNELS - 1;
69 for (i = min; i <= max; i++) {
70 unsigned d = rpp_adc_get(i+1);
71 double v = port_adc_lsb2volts(d);
72 rpp_sci_printf("ADC%-2d %4d lsb ~ %5.2f V\n", i, d, v);
77 int cmd_do_adc_watch(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
81 rpp_sci_printf("ADC Inputs Test [0-%d]:\r\n", PORT_ADC_CHANNELS - 1);
83 for (i = 0; i < PORT_ADC_CHANNELS; i++)
84 rpp_sci_printf("======");
87 for (i = 0; i < PORT_ADC_CHANNELS; i++)
88 rpp_sci_printf("%5d ", i);
91 // Calculate wait time in OS ticks
92 static const portTickType freq_ticks = 100 /* ms */ / portTICK_RATE_MS;
93 portTickType last_wake_time = xTaskGetTickCount();
95 while (cmd_io->getc(cmd_io) < 0) {
99 for (i = 0; i < PORT_ADC_CHANNELS; i++)
100 rpp_sci_printf("%5d ", rpp_adc_get(i+1));
101 rpp_sci_printf("lsb\n");
102 for (i = 0; i < PORT_ADC_CHANNELS; i++)
103 rpp_sci_printf("%5.2f ", port_adc_lsb2volts(rpp_adc_get(i+1)));
104 rpp_sci_printf("V\r\033[A"); /* Cursor up */
107 vTaskDelayUntil(&last_wake_time, freq_ticks);
109 rpp_sci_printf("\n\n");
116 /** Descriptor of command for adc port reading */
117 cmd_des_t const cmd_des_read_adc1 = {
119 "adcread","Read values from ADC inputs",
120 "### Command syntax ###\n"
124 "### Description ###\n"
126 "This command reads values corresponding to analog voltages on ADC\n"
127 "inputs 0-15 and prints them as decimal numbers as well as converted to\n"
133 " ADC0 2332 lsb ~ 11.66 V\n"
134 " ADC1 2332 lsb ~ 11.66 V\n"
135 " ADC2 107 lsb ~ 0.54 V\n"
136 " ADC3 108 lsb ~ 0.54 V\n"
137 " ADC4 107 lsb ~ 0.54 V\n"
138 " ADC5 108 lsb ~ 0.54 V\n"
139 " ADC6 111 lsb ~ 0.56 V\n"
140 " ADC7 110 lsb ~ 0.55 V\n"
141 " ADC8 109 lsb ~ 0.55 V\n"
142 " ADC9 107 lsb ~ 0.54 V\n"
143 " ADC10 107 lsb ~ 0.54 V\n"
144 " ADC11 110 lsb ~ 0.55 V\n"
145 " ADC12 108 lsb ~ 0.54 V\n"
146 " ADC13 108 lsb ~ 0.54 V\n"
147 " ADC14 108 lsb ~ 0.54 V\n"
148 " ADC15 108 lsb ~ 0.54 V\n",
149 CMD_HANDLER(cmd_do_read_adc1_values), (void *)&cmd_list_adc
152 cmd_des_t const cmd_des_read_adc2 = {
154 "adcread*","Read a value from a single ADC input",
155 "### Command syntax ###\n"
159 "where `<PIN>` is a number in range 0 - 15.\n"
161 "### Description ###\n"
163 "This command reads the value corresponding to analog voltage on an ADC\n"
164 "input and prints it as decimal numbers as well as converted to Volts.\n"
169 " ADC1 2331 lsb ~ 11.66 V\n",
170 CMD_HANDLER(cmd_do_read_adc1_values), (void *)&cmd_list_adc
173 cmd_des_t const cmd_des_adcwatch = {
175 "adcwatch","Watch the values from ADC inputs",
176 "### Command syntax ###\n"
180 "### Description ###\n"
182 "This command reads values corresponding to analog voltages on ADC\n"
183 "inputs 0-15 10 times per second and prints them as decimal numbers (in\n"
184 "lsb units) as well as converted to Volts. The command is ended by any\n"
190 " ADC Inputs Test [0-15]:\n"
191 " =========================================================================================\n"
192 " 0 2 3 4 5 6 7 8 9 10 11 12 13 14 15\n"
193 " 2331 107 108 106 107 110 110 109 107 107 110 109 109 109 109 lsb\n"
194 " 11.66 0.54 0.54 0.53 0.54 0.55 0.55 0.55 0.54 0.54 0.55 0.55 0.55 0.55 0.55 V\n",
195 CMD_HANDLER(cmd_do_adc_watch), (void *)&cmd_list_adc
198 /** List of commands for adc, defined as external */
199 cmd_des_t const *cmd_list_adc[] = {