]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp-test-sw/commands/cmd_adc.c
Compile with Makefile instead of CSS
[pes-rpp/rpp-test-sw.git] / rpp-test-sw / commands / cmd_adc.c
1 /*
2  * Copyright (C) 2012-2015 Czech Technical University in Prague
3  *
4  * Created on: 28.2.2013
5  *
6  * Authors:
7  *     - Michal Horn
8  *
9  * This document contains proprietary information belonging to Czech
10  * Technical University in Prague. Passing on and copying of this
11  * document, and communication of its contents is not permitted
12  * without prior written authorization.
13  *
14  * File : cmd_adc.c
15  *
16  * Abstract:
17  *      This file contains command for reading adc port.
18  *
19  */
20
21 #include "cmd_adc.h"
22
23 #ifndef DOCGEN
24
25 #include "hal/hal.h"
26 #include "rpp/rpp.h"
27 #include <stdio.h>
28
29 static double lsb2volts(unsigned lsb)
30 {
31         return ((double)lsb + 0.0)*2.5/4095*10;
32 }
33
34
35 /**
36  * @brief Read values from ADC port
37  *
38  * @param[in]   cmd_io  Pointer to IO stack
39  * @param[in]   des             Pointer to command descriptor
40  * @param[in]   param   Parameters of command
41  * @return 0 when OK or error code lower than 0
42  */
43 int cmd_do_read_adc1_values(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
44 {
45         uint32_t i, min, max;   // Cycle control variable
46
47         rpp_adc_update();
48         if (param[1] == param[0] + 7) { /* Single pin variant */
49                 if (sscanf(param[1], "%d", &min) != 1)
50                         return -CMDERR_BADPAR;
51                 if (min > 15)
52                         return -CMDERR_NODEV;
53                 max = min;
54         }
55         else {              /* All pins */
56                 min = 0;
57                 max = PORT_ADC_CHANNEL_NUM-1;
58         }
59         for (i = min; i <= max; i++) {
60                 unsigned d = rpp_adc_get(i+1);
61                 double v = lsb2volts(d);
62                 rpp_sci_printf("ADC%-2d %4d lsb ~ %5.2f V\n", i, d, v);
63         }
64         return 0;
65 }
66
67 int cmd_do_adc_watch(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
68 {
69         int i;
70
71         rpp_sci_printf("ADC Inputs Test [0-15]:\r\n");
72         rpp_sci_printf("=======================================================================\r\n");
73
74         for (i = 0; i < 16; i++)
75                 rpp_sci_printf("%5d ", i);
76         rpp_sci_printf("\n");
77
78         for (i = 1; i < 13; i++)
79                 rpp_sci_printf("%5d ", i);
80         rpp_sci_printf("\n");
81
82         // Calculate wait time in OS ticks
83         static const portTickType freq_ticks = 100 /* ms */ / portTICK_RATE_MS;
84         portTickType last_wake_time = xTaskGetTickCount();
85
86         while (cmd_io->getc(cmd_io) < 0) {
87         // Update inputs
88         rpp_adc_update();
89
90                 for (i = 0; i < 16; i++)
91                         rpp_sci_printf("%5d ", rpp_adc_get(i+1));
92                 rpp_sci_printf("lsb\n");
93                 for (i = 0; i < 16; i++)
94                         rpp_sci_printf("%5.2f ", lsb2volts(rpp_adc_get(i+1)));
95                 rpp_sci_printf("V\r\033[A"); /* Cursor up */
96
97
98                 vTaskDelayUntil(&last_wake_time, freq_ticks);
99         }
100         rpp_sci_printf("\n\n");
101         return 0;
102 }
103
104
105 #endif  /* DOCGEN */
106
107 /** Descriptor of command for adc port reading */
108 cmd_des_t const cmd_des_read_adc1 = {
109         0, 0,
110         "adcread","Read values from ADC inputs",
111         "### Command syntax ###\n"
112         "\n"
113         "     adcread\n"
114         "\n"
115         "### Description ###\n"
116         "\n"
117         "This command reads values corresponding to analog voltages on ADC\n"
118         "inputs 0-15 and prints them as decimal numbers as well as converted to\n"
119         "Volts.\n"
120         "\n"
121         "### Example ###\n"
122         "\n"
123         "     --> adcread\n"
124         "     ADC0  2332 lsb ~ 11.66 V\n"
125         "     ADC1  2332 lsb ~ 11.66 V\n"
126         "     ADC2   107 lsb ~  0.54 V\n"
127         "     ADC3   108 lsb ~  0.54 V\n"
128         "     ADC4   107 lsb ~  0.54 V\n"
129         "     ADC5   108 lsb ~  0.54 V\n"
130         "     ADC6   111 lsb ~  0.56 V\n"
131         "     ADC7   110 lsb ~  0.55 V\n"
132         "     ADC8   109 lsb ~  0.55 V\n"
133         "     ADC9   107 lsb ~  0.54 V\n"
134         "     ADC10  107 lsb ~  0.54 V\n"
135         "     ADC11  110 lsb ~  0.55 V\n"
136         "     ADC12  108 lsb ~  0.54 V\n"
137         "     ADC13  108 lsb ~  0.54 V\n"
138         "     ADC14  108 lsb ~  0.54 V\n"
139         "     ADC15  108 lsb ~  0.54 V\n",
140         CMD_HANDLER(cmd_do_read_adc1_values), (void *)&cmd_list_adc
141 };
142
143 cmd_des_t const cmd_des_read_adc2 = {
144         0, 0,
145         "adcread*","Read a value from a single ADC input",
146         "### Command syntax ###\n"
147         "\n"
148         "    adcread<PIN>\n"
149         "\n"
150         "where `<PIN>` is a number in range 0 - 15.\n"
151         "\n"
152         "### Description ###\n"
153         "\n"
154         "This command reads the value corresponding to analog voltage on an ADC\n"
155         "input and prints it as decimal numbers as well as converted to Volts.\n"
156         "\n"
157         "### Example ###\n"
158         "\n"
159         "    --> adcread1\n"
160         "    ADC1  2331 lsb ~ 11.66 V\n",
161         CMD_HANDLER(cmd_do_read_adc1_values), (void *)&cmd_list_adc
162 };
163
164 cmd_des_t const cmd_des_adcwatch = {
165         0, 0,
166         "adcwatch","Watch the values from ADC inputs",
167         "### Command syntax ###\n"
168         "\n"
169         "    adcwatch\n"
170         "\n"
171         "### Description ###\n"
172         "\n"
173         "This command reads values corresponding to analog voltages on ADC\n"
174         "inputs 0-15 10 times per second and prints them as decimal numbers (in\n"
175         "lsb units) as well as converted to Volts. The command is ended by any\n"
176         "key.\n"
177         "\n"
178         "### Example ###\n"
179         "\n"
180         "    --> adcwatch\n"
181         "    ADC Inputs Test [0-15]:\n"
182         "    =========================================================================================\n"
183         "        0     2     3     4     5     6     7     8     9    10    11    12    13    14    15\n"
184         "     2331   107   108   106   107   110   110   109   107   107   110   109   109   109   109 lsb\n"
185         "    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",
186         CMD_HANDLER(cmd_do_adc_watch), (void *)&cmd_list_adc
187 };
188
189 /** List of commands for adc, defined as external */
190 cmd_des_t const *cmd_list_adc[] = {
191         &cmd_des_read_adc1,
192         &cmd_des_read_adc2,
193         &cmd_des_adcwatch,
194         NULL
195 };