]> rtime.felk.cvut.cz Git - rpp-test-sw.git/blob - rpp-test-sw/commands/cmd_adc.c
Fixed warning detected by new ARM compiler 5.1.1
[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     } else {                    /* All pins */
63         min = 1;
64         max = PORT_ADC_CHANNEL_NUM;
65     }
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);
70     }
71     return 0;
72 }
73
74 int cmd_do_adc_watch(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
75 {
76     int i;
77     rpp_sci_printf("ADC Inputs Test [1-12]:\r\n");
78     rpp_sci_printf("=======================================================================\r\n");
79
80     for (i = 1; i < 13; i++)
81             rpp_sci_printf("%5d ", i);
82     rpp_sci_printf("\n");
83
84     // Calculate wait time in OS ticks
85     static const portTickType freq_ticks = 100 /* ms */ / portTICK_RATE_MS;
86     portTickType last_wake_time = xTaskGetTickCount();
87
88     while(cmd_io->getc(cmd_io) < 0) {
89
90         // Update inputs
91         rpp_adc_update();
92
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 */
99
100
101         vTaskDelayUntil(&last_wake_time, freq_ticks);
102     }
103     rpp_sci_printf("\n\n");
104     return 0;
105 }
106
107
108 #endif  /* DOCGEN */
109
110 /** Descriptor of command for adc port reading */
111 cmd_des_t const cmd_des_read_adc1={
112     0, 0,
113     "adcread","Read values from ADC inputs",
114     "### Command syntax ###\n"
115     "\n"
116     "     adcread\n"
117     "\n"
118     "### Description ###\n"
119     "\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"
122     "Volts.\n"
123     "\n"
124     "### Example ###\n"
125     "\n"
126     "     --> adcread\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
140 };
141
142 cmd_des_t const cmd_des_read_adc2={
143     0, 0,
144     "adcread*","Read a value from a single ADC input",
145     "### Command syntax ###\n"
146     "\n"
147     "    adcread<PIN>\n"
148     "\n"
149     "where `<PIN>` is a number in range 1 - 12.\n"
150     "\n"
151     "### Description ###\n"
152     "\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"
155     "\n"
156     "### Example ###\n"
157     "\n"
158     "    --> adcread1\n"
159     "    ADC1  2331 lsb ~ 11.66 V\n",
160     CMD_HANDLER(cmd_do_read_adc1_values), (void *)&cmd_list_adc
161 };
162
163 cmd_des_t const cmd_des_adcwatch={
164     0, 0,
165     "adcwatch","Watch the values from ADC inputs",
166     "### Command syntax ###\n"
167     "\n"
168     "    adcwatch\n"
169     "\n"
170     "### Description ###\n"
171     "\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"
175     "key.\n"
176     "\n"
177     "### Example ###\n"
178     "\n"
179     "    --> adcwatch\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
186 };
187
188 /** List of commands for adc, defined as external */
189 cmd_des_t const *cmd_list_adc[]={
190   &cmd_des_read_adc1,
191   &cmd_des_read_adc2,
192   &cmd_des_adcwatch,
193   NULL
194 };