]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp-test-sw/commands/cmd_adc.c
Change license to MIT
[pes-rpp/rpp-test-sw.git] / rpp-test-sw / commands / cmd_adc.c
1 /*
2  * Copyright (C) 2012-2016 Czech Technical University in Prague
3  *
4  * Created on: 28.2.2013
5  *
6  * Authors:
7  *     - Michal Horn
8  *
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
16  * conditions:
17  *
18  * The above copyright notice and this permission notice shall be
19  * included in all copies or substantial portions of the Software.
20  *
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.
29  *
30  * File : cmd_adc.c
31  *
32  * Abstract:
33  *      This file contains command for reading adc port.
34  *
35  */
36
37 #include "cmd_adc.h"
38
39 #ifndef DOCGEN
40
41 #include "rpp/rpp.h"
42 #include <stdio.h>
43 #include "drv/port.h"
44
45 /**
46  * @brief Read values from ADC port
47  *
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
52  */
53 int cmd_do_read_adc1_values(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
54 {
55         uint32_t i, min, max;   // Cycle control variable
56
57         rpp_adc_update();
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)
62                         return -CMDERR_NODEV;
63                 max = min;
64         }
65         else {              /* All pins */
66                 min = 0;
67                 max = PORT_ADC_CHANNELS - 1;
68         }
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);
73         }
74         return 0;
75 }
76
77 int cmd_do_adc_watch(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
78 {
79         int i;
80
81         rpp_sci_printf("ADC Inputs Test [0-%d]:\r\n", PORT_ADC_CHANNELS - 1);
82
83         for (i = 0; i < PORT_ADC_CHANNELS; i++)
84                 rpp_sci_printf("======");
85         rpp_sci_printf("\n");
86
87         for (i = 0; i < PORT_ADC_CHANNELS; i++)
88                 rpp_sci_printf("%5d ", i);
89         rpp_sci_printf("\n");
90
91         // Calculate wait time in OS ticks
92         static const portTickType freq_ticks = 100 /* ms */ / portTICK_RATE_MS;
93         portTickType last_wake_time = xTaskGetTickCount();
94
95         while (cmd_io->getc(cmd_io) < 0) {
96         // Update inputs
97         rpp_adc_update();
98
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 */
105
106
107                 vTaskDelayUntil(&last_wake_time, freq_ticks);
108         }
109         rpp_sci_printf("\n\n");
110         return 0;
111 }
112
113
114 #endif  /* DOCGEN */
115
116 /** Descriptor of command for adc port reading */
117 cmd_des_t const cmd_des_read_adc1 = {
118         0, 0,
119         "adcread","Read values from ADC inputs",
120         "### Command syntax ###\n"
121         "\n"
122         "     adcread\n"
123         "\n"
124         "### Description ###\n"
125         "\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"
128         "Volts.\n"
129         "\n"
130         "### Example ###\n"
131         "\n"
132         "     --> adcread\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
150 };
151
152 cmd_des_t const cmd_des_read_adc2 = {
153         0, 0,
154         "adcread*","Read a value from a single ADC input",
155         "### Command syntax ###\n"
156         "\n"
157         "    adcread<PIN>\n"
158         "\n"
159         "where `<PIN>` is a number in range 0 - 15.\n"
160         "\n"
161         "### Description ###\n"
162         "\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"
165         "\n"
166         "### Example ###\n"
167         "\n"
168         "    --> adcread1\n"
169         "    ADC1  2331 lsb ~ 11.66 V\n",
170         CMD_HANDLER(cmd_do_read_adc1_values), (void *)&cmd_list_adc
171 };
172
173 cmd_des_t const cmd_des_adcwatch = {
174         0, 0,
175         "adcwatch","Watch the values from ADC inputs",
176         "### Command syntax ###\n"
177         "\n"
178         "    adcwatch\n"
179         "\n"
180         "### Description ###\n"
181         "\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"
185         "key.\n"
186         "\n"
187         "### Example ###\n"
188         "\n"
189         "    --> adcwatch\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
196 };
197
198 /** List of commands for adc, defined as external */
199 cmd_des_t const *cmd_list_adc[] = {
200         &cmd_des_read_adc1,
201         &cmd_des_read_adc2,
202         &cmd_des_adcwatch,
203         NULL
204 };