source: multiio/commands/main_multiio_dac.c @ 0ecf946

Last change on this file since 0ecf946 was e38d987, checked in by Joel Sherrill <joel.sherrill@…>, on 03/18/11 at 13:19:33

2011-03-18 Joel Sherrill <joel.sherrill@…>

  • ChangeLog?, Makefile, commands/Makefile, commands/main_multiio_adc.c, commands/main_multiio_benchmark.c, commands/main_multiio_dac.c, commands/main_multiio_din.c, commands/main_multiio_dout.c, commands/main_multiio_irq.c, commands/multiio_commands.h, include/multiio.h, stub/multio_stub.c: New files.
  • Property mode set to 100644
File size: 5.3 KB
Line 
1/*
2 *  dac command
3 *
4 *  COPYRIGHT (c) 1989-2009.
5 *  On-Line Applications Research Corporation (OAR).
6 *
7 *  The license and distribution terms for this file may be
8 *  found in the file LICENSE in this distribution or at
9 *  http://www.rtems.com/license/LICENSE.
10 *
11 *  $Id$
12 */
13
14#include <stdio.h>
15#include <stdlib.h>
16#include <rtems.h>
17#include <rtems/shell.h>
18#include <rtems/stringto.h>
19#include "multiio.h"
20
21#define VALIDATE_VOLTAGE(_v) \
22  if ( (_v) < rtems_dac_get_minimum_voltage() || \
23       (_v) > rtems_dac_get_maximum_voltage() ) { \
24    printf( "Voltage must be between %fV and %f\n", \
25              rtems_dac_get_minimum_voltage(), \
26              rtems_dac_get_maximum_voltage()); \
27    fail = true; \
28  }
29
30static char dac_usage[] =
31  "Usage: %s dac voltage\n"
32  "       %s dac low high step time_per_step maximum_time\n"
33  "\n"
34  "Where: dac must be 0-%d\n"
35  "       voltages and step must be %fV to %fV\n"
36  "       times are in milliseconds\n"
37  "  First form is a single write.\n"
38  "  Second form writes a pattern.\n";
39
40#define PRINT_USAGE() \
41   printf( dac_usage, argv[0], argv[0], \
42           rtems_dac_get_maximum(), \
43           rtems_dac_get_minimum_voltage(), \
44           rtems_dac_get_maximum_voltage()); \
45
46int main_multiio_dac(int argc, char **argv)
47{
48  int       dac;
49  float     low_voltage;
50  float     high_voltage;
51  float     step_voltage;
52  float     current_voltage;
53  float     current_step;
54  int       step_time;
55  int       maximum_time;
56  uint32_t  step_ticks;
57  bool      fail = false;
58  int       elapsed;
59
60  /*
61   *  Verify that we have the right number of arguments.
62   */
63  if ( (argc != 3) && (argc != 7) ) {
64    printf( "Incorrect number of arguments\n" );
65    PRINT_USAGE();
66    return -1;
67  }
68
69  /*
70   *  Convert the string arguments into number values
71   */
72  if ( rtems_string_to_int( argv[1], &dac, NULL, 0 ) ) {
73    printf( "DAC (%s) is not a number\n", argv[1] );
74    fail = true;
75  }
76
77  if ( rtems_string_to_float( argv[2], &low_voltage, NULL ) ) {
78    printf( "Voltage (%s) is not a number\n", argv[2] );
79    fail = true;
80  }
81
82  /*
83   *  Validate the output dac and voltage.
84   */
85  if ( dac < 0 || dac > rtems_dac_get_maximum() ) {
86    printf( "DAC number must be 0-%d\n", rtems_dac_get_maximum() );
87    fail = true;
88  }
89
90  VALIDATE_VOLTAGE( low_voltage );
91
92  /*
93   *  Now do a single write to the DAC
94   */
95  if ( argc == 3 ) {
96    if ( fail ) {
97      PRINT_USAGE();
98      return -1;
99    }
100    printf( "Write %6.4f to to dac %d\n", low_voltage, dac );
101    rtems_set_dac_voltage(dac, low_voltage);
102    return 0;
103  }
104
105  /*
106   *  Finish parsing the arguments to do a pattern
107   */
108  fail = false;
109
110  if ( rtems_string_to_float( argv[3], &high_voltage, NULL ) ) {
111    printf( "Voltage (%s) is not a number\n", argv[3] );
112    fail = true;
113  }
114
115  VALIDATE_VOLTAGE( high_voltage );
116
117  if ( rtems_string_to_float( argv[4], &step_voltage, NULL ) ) {
118    printf( "Step voltage (%s) is not a number\n", argv[4] );
119    fail = true;
120  }
121
122  VALIDATE_VOLTAGE( step_voltage );
123
124  if ( step_voltage < 0.0 ) {
125    printf( "Step voltage must be greater than 0\n" );
126    fail = true;
127  }
128
129  if ( rtems_string_to_int( argv[5], &step_time, NULL, 0 ) ) {
130    printf( "Step time (%s) is not a number\n", argv[5] );
131    fail = true;
132  }
133
134  if ( rtems_string_to_int( argv[6], &maximum_time, NULL, 0 ) ) {
135    printf( "Maximum time (%s) is not a number\n", argv[6] );
136    fail = true;
137  }
138
139  if ( step_time >= maximum_time ) {
140    printf(
141      "Step time (%d) must be less than maximum time (%d)\n",
142      step_time,
143      maximum_time
144    );
145    fail = true;
146  }
147
148  if ( step_time < 0 ) {
149    printf( "Step time must be greater than 0\n" );
150    fail = true;
151  }
152
153  if ( maximum_time < 0 ) {
154    printf( "Maximum time must be greater than 0\n" );
155    fail = true;
156  }
157
158  /*
159   *  Now write the pattern to the DAC
160   */
161
162  if ( fail ) {
163    PRINT_USAGE();
164    return -1;
165  }
166
167  printf(
168    "Write %6.4f-%6.4f step=%6.4f stepTime=%d msecs dac=%d max=%d msecs\n",
169    low_voltage,
170    high_voltage,
171    step_voltage,
172    step_time,
173    dac,
174    maximum_time
175  );
176
177  elapsed         = 0;
178  step_ticks      = RTEMS_MILLISECONDS_TO_TICKS(step_time);
179  current_voltage = low_voltage;
180  current_step    = step_voltage;
181
182  if ( low_voltage > high_voltage )
183    current_step    *= -1.0;
184
185  while (1) {
186 
187    #if defined(TESTING)
188      printf( "%d: Write %6.4f to to dac %d\n", elapsed, current_voltage, dac );
189    #endif
190    rtems_set_dac_voltage(dac, current_voltage);
191
192    current_voltage += current_step;
193    if ( current_voltage < low_voltage ) {
194      current_step    = step_voltage;
195      current_voltage = low_voltage;
196    } else if ( current_voltage > high_voltage ) {
197      current_step    = -1.0 * step_voltage;
198      current_voltage = high_voltage;
199    }
200
201    elapsed += step_time;
202    if ( elapsed > maximum_time )
203      break;
204
205    rtems_task_wake_after( step_ticks );
206  }
207   
208  return 0;
209}
210
211rtems_shell_cmd_t Shell_MULTIIO_DAC_Command = {
212  "multiio_dac",                            /* name */
213  "Write Analog Outputs",                   /* usage */
214  "multiio",                                /* topic */
215  main_multiio_dac,                         /* command */
216  NULL,                                     /* alias */
217  NULL                                      /* next */
218};
219
220
221rtems_shell_alias_t Shell_MULTIIO_DAC_Alias = {
222  "multiio_dac",         /* command */
223  "dac"                  /* alias */
224};
Note: See TracBrowser for help on using the repository browser.