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)
65 max = PORT_ADC_CHANNEL_NUM;
67 for (i = min-1; i < max; i++) {
68 unsigned d = rpp_adc_get(i+1);
69 double v = lsb2volts(d);
70 rpp_sci_printf("ADC%-2d %4d lsb ~ %5.2f V\n", i+1, d, v);
75 int cmd_do_adc_watch(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
79 rpp_sci_printf("ADC Inputs Test [1-12]:\r\n");
80 rpp_sci_printf("=======================================================================\r\n");
82 for (i = 1; i < 13; i++)
83 rpp_sci_printf("%5d ", i);
86 // Calculate wait time in OS ticks
87 static const portTickType freq_ticks = 100 /* ms */ / portTICK_RATE_MS;
88 portTickType last_wake_time = xTaskGetTickCount();
90 while (cmd_io->getc(cmd_io) < 0) {
95 for (i = 1; i < 13; i++)
96 rpp_sci_printf("%5d ", rpp_adc_get(i));
97 rpp_sci_printf("lsb\n");
98 for (i = 1; i < 13; i++)
99 rpp_sci_printf("%5.2f ", lsb2volts(rpp_adc_get(i)));
100 rpp_sci_printf("V\r\033[A"); /* Cursor up */
103 vTaskDelayUntil(&last_wake_time, freq_ticks);
105 rpp_sci_printf("\n\n");
112 /** Descriptor of command for adc port reading */
113 cmd_des_t const cmd_des_read_adc1 = {
115 "adcread","Read values from ADC inputs",
116 "### Command syntax ###\n"
120 "### Description ###\n"
122 "This command reads values corresponding to analog voltages on ADC\n"
123 "inputs 1-12 and prints them as decimal numbers as well as converted to\n"
129 " ADC1 2332 lsb ~ 11.66 V\n"
130 " ADC2 107 lsb ~ 0.54 V\n"
131 " ADC3 108 lsb ~ 0.54 V\n"
132 " ADC4 107 lsb ~ 0.54 V\n"
133 " ADC5 108 lsb ~ 0.54 V\n"
134 " ADC6 111 lsb ~ 0.56 V\n"
135 " ADC7 110 lsb ~ 0.55 V\n"
136 " ADC8 109 lsb ~ 0.55 V\n"
137 " ADC9 107 lsb ~ 0.54 V\n"
138 " ADC10 107 lsb ~ 0.54 V\n"
139 " ADC11 110 lsb ~ 0.55 V\n"
140 " ADC12 108 lsb ~ 0.54 V\n",
141 CMD_HANDLER(cmd_do_read_adc1_values), (void *)&cmd_list_adc
144 cmd_des_t const cmd_des_read_adc2 = {
146 "adcread*","Read a value from a single ADC input",
147 "### Command syntax ###\n"
151 "where `<PIN>` is a number in range 1 - 12.\n"
153 "### Description ###\n"
155 "This command reads the value corresponding to analog voltage on an ADC\n"
156 "input and prints it as decimal numbers as well as converted to Volts.\n"
161 " ADC1 2331 lsb ~ 11.66 V\n",
162 CMD_HANDLER(cmd_do_read_adc1_values), (void *)&cmd_list_adc
165 cmd_des_t const cmd_des_adcwatch = {
167 "adcwatch","Watch the values from ADC inputs",
168 "### Command syntax ###\n"
172 "### Description ###\n"
174 "This command reads values corresponding to analog voltages on ADC\n"
175 "inputs 1-12 10 times per second and prints them as decimal numbers (in\n"
176 "lsb units) as well as converted to Volts. The command is ended by any\n"
182 " ADC Inputs Test [1-12]:\n"
183 " =======================================================================\n"
184 " 1 2 3 4 5 6 7 8 9 10 11 12\n"
185 " 2331 107 108 106 107 110 110 109 107 107 110 109 lsb\n"
186 " 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",
187 CMD_HANDLER(cmd_do_adc_watch), (void *)&cmd_list_adc
190 /** List of commands for adc, defined as external */
191 cmd_des_t const *cmd_list_adc[] = {