]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp-test-sw/commands/cmd_adc.c
Merge branch 'master' of rtime.felk.cvut.cz:rpp-test-sw
[pes-rpp/rpp-test-sw.git] / rpp-test-sw / commands / cmd_adc.c
1 /*
2  * Copyright (C) 2012-2013 Czech Technical University in Prague
3  *
4  * Created on: 28.2.2013
5  *
6  * Authors:
7  *     - Michal Horn
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * File : cmd_adc.c
23  *
24  * Abstract:
25  *      This file contains command for reading adc port.
26  *
27  */
28
29 #include "cmd_adc.h"
30
31 #ifndef DOCGEN
32
33 #include "hal/hal.h"
34 #include "rpp/rpp.h"
35 #include <stdio.h>
36
37 static double lsb2volts(unsigned lsb)
38 {
39         return ((double)lsb + 0.0)*2.5/4095*10;
40 }
41
42
43 /**
44  * @brief Read values from ADC port
45  *
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
50  */
51 int cmd_do_read_adc1_values(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
52 {
53         uint32_t i, min, max;   // Cycle control variable
54
55         rpp_adc_update();
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)
60                         return -CMDERR_NODEV;
61                 max = min;
62         }
63         else {              /* All pins */
64                 min = 1;
65                 max = PORT_ADC_CHANNEL_NUM;
66         }
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);
71         }
72         return 0;
73 }
74
75 int cmd_do_adc_watch(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
76 {
77         int i;
78
79         rpp_sci_printf("ADC Inputs Test [1-12]:\r\n");
80         rpp_sci_printf("=======================================================================\r\n");
81
82         for (i = 1; i < 13; i++)
83                 rpp_sci_printf("%5d ", i);
84         rpp_sci_printf("\n");
85
86         // Calculate wait time in OS ticks
87         static const portTickType freq_ticks = 100 /* ms */ / portTICK_RATE_MS;
88         portTickType last_wake_time = xTaskGetTickCount();
89
90         while (cmd_io->getc(cmd_io) < 0) {
91
92                 // Update inputs
93                 rpp_adc_update();
94
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 */
101
102
103                 vTaskDelayUntil(&last_wake_time, freq_ticks);
104         }
105         rpp_sci_printf("\n\n");
106         return 0;
107 }
108
109
110 #endif  /* DOCGEN */
111
112 /** Descriptor of command for adc port reading */
113 cmd_des_t const cmd_des_read_adc1 = {
114         0, 0,
115         "adcread","Read values from ADC inputs",
116         "### Command syntax ###\n"
117         "\n"
118         "     adcread\n"
119         "\n"
120         "### Description ###\n"
121         "\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"
124         "Volts.\n"
125         "\n"
126         "### Example ###\n"
127         "\n"
128         "     --> adcread\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
142 };
143
144 cmd_des_t const cmd_des_read_adc2 = {
145         0, 0,
146         "adcread*","Read a value from a single ADC input",
147         "### Command syntax ###\n"
148         "\n"
149         "    adcread<PIN>\n"
150         "\n"
151         "where `<PIN>` is a number in range 1 - 12.\n"
152         "\n"
153         "### Description ###\n"
154         "\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"
157         "\n"
158         "### Example ###\n"
159         "\n"
160         "    --> adcread1\n"
161         "    ADC1  2331 lsb ~ 11.66 V\n",
162         CMD_HANDLER(cmd_do_read_adc1_values), (void *)&cmd_list_adc
163 };
164
165 cmd_des_t const cmd_des_adcwatch = {
166         0, 0,
167         "adcwatch","Watch the values from ADC inputs",
168         "### Command syntax ###\n"
169         "\n"
170         "    adcwatch\n"
171         "\n"
172         "### Description ###\n"
173         "\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"
177         "key.\n"
178         "\n"
179         "### Example ###\n"
180         "\n"
181         "    --> adcwatch\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
188 };
189
190 /** List of commands for adc, defined as external */
191 cmd_des_t const *cmd_list_adc[] = {
192         &cmd_des_read_adc1,
193         &cmd_des_read_adc2,
194         &cmd_des_adcwatch,
195         NULL
196 };