2 * Copyright (C) 2012-2013 Czech Technical University in Prague
4 * Created on: 28.2.2013
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * This file contains command for reading adc port.
37 static double lsb2volts(unsigned lsb)
39 return ((double)lsb + 0.0)*2.5/4095*10;
44 * @brief Read values from ADC port
46 * @param[in] cmd_io Pointer to IO stack
47 * @param[in] des Pointer to command descriptor
48 * @param[in] param Parameters of command
49 * @return 0 when OK or error code lower than 0
51 int cmd_do_read_adc1_values(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
53 uint32_t i, min, max; // Cycle control variable
56 if (param[1] == param[0] + 7) { /* Single pin variant */
57 if (sscanf(param[1], "%d", &min) != 1)
58 return -CMDERR_BADPAR;
59 if (min < 1 || min > 12)
62 } else { /* All pins */
64 max = PORT_ADC_CHANNEL_NUM;
66 for (i = min-1; i < max; i++) {
67 unsigned d = rpp_adc_get(i+1);
68 double v = lsb2volts(d);
69 rpp_sci_printf("ADC%-2d %4d lsb ~ %5.2f V\n", i+1, d, v);
74 int cmd_do_adc_watch(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
77 rpp_sci_printf("ADC Inputs Test [1-12]:\r\n");
78 rpp_sci_printf("=======================================================================\r\n");
80 for (i = 1; i < 13; i++)
81 rpp_sci_printf("%5d ", i);
84 // Calculate wait time in OS ticks
85 static const portTickType freq_ticks = 100 /* ms */ / portTICK_RATE_MS;
86 portTickType last_wake_time = xTaskGetTickCount();
88 while(cmd_io->getc(cmd_io) < 0) {
93 for (i = 1; i < 13; i++)
94 rpp_sci_printf("%5d ", rpp_adc_get(i));
95 rpp_sci_printf("lsb\n");
96 for (i = 1; i < 13; i++)
97 rpp_sci_printf("%5.2f ", lsb2volts(rpp_adc_get(i)));
98 rpp_sci_printf("V\r\033[A"); /* Cursor up */
101 vTaskDelayUntil(&last_wake_time, freq_ticks);
103 rpp_sci_printf("\n\n");
110 /** Descriptor of command for adc port reading */
111 cmd_des_t const cmd_des_read_adc1={
113 "adcread","Read values from ADC inputs",
114 "### Command syntax ###\n"
118 "### Description ###\n"
120 "This command reads values corresponding to analog voltages on ADC\n"
121 "inputs 1-12 and prints them as decimal numbers as well as converted to\n"
127 " ADC1 2332 lsb ~ 11.66 V\n"
128 " ADC2 107 lsb ~ 0.54 V\n"
129 " ADC3 108 lsb ~ 0.54 V\n"
130 " ADC4 107 lsb ~ 0.54 V\n"
131 " ADC5 108 lsb ~ 0.54 V\n"
132 " ADC6 111 lsb ~ 0.56 V\n"
133 " ADC7 110 lsb ~ 0.55 V\n"
134 " ADC8 109 lsb ~ 0.55 V\n"
135 " ADC9 107 lsb ~ 0.54 V\n"
136 " ADC10 107 lsb ~ 0.54 V\n"
137 " ADC11 110 lsb ~ 0.55 V\n"
138 " ADC12 108 lsb ~ 0.54 V\n",
139 CMD_HANDLER(cmd_do_read_adc1_values), (void *)&cmd_list_adc
142 cmd_des_t const cmd_des_read_adc2={
144 "adcread*","Read a value from a single ADC input",
145 "### Command syntax ###\n"
149 "where `<PIN>` is a number in range 1 - 12.\n"
151 "### Description ###\n"
153 "This command reads the value corresponding to analog voltage on an ADC\n"
154 "input and prints it as decimal numbers as well as converted to Volts.\n"
159 " ADC1 2331 lsb ~ 11.66 V\n",
160 CMD_HANDLER(cmd_do_read_adc1_values), (void *)&cmd_list_adc
163 cmd_des_t const cmd_des_adcwatch={
165 "adcwatch","Watch the values from ADC inputs",
166 "### Command syntax ###\n"
170 "### Description ###\n"
172 "This command reads values corresponding to analog voltages on ADC\n"
173 "inputs 1-12 10 times per second and prints them as decimal numbers (in\n"
174 "lsb units) as well as converted to Volts. The command is ended by any\n"
180 " ADC Inputs Test [1-12]:\n"
181 " =======================================================================\n"
182 " 1 2 3 4 5 6 7 8 9 10 11 12\n"
183 " 2331 107 108 106 107 110 110 109 107 107 110 109 lsb\n"
184 " 11.66 0.54 0.54 0.53 0.54 0.55 0.55 0.55 0.54 0.54 0.55 0.55 V\n",
185 CMD_HANDLER(cmd_do_adc_watch), (void *)&cmd_list_adc
188 /** List of commands for adc, defined as external */
189 cmd_des_t const *cmd_list_adc[]={