source: multiio/pcmmio/original/main_pcmmio_dac.c @ 6955473

Last change on this file since 6955473 was 6955473, checked in by Joel Sherrill <joel.sherrill@…>, on 07/23/09 at 14:39:20

2009-07-23 Joel Sherrill <joel.sherrill@…>

  • main_pcmmio_adc.c, main_pcmmio_dac.c, main_pcmmio_din.c, main_pcmmio_dout.c, main_pcmmio_irq.c: Update checks of return status from rtems_string_to_XXX to reflect change to rtems_status_code.
  • Property mode set to 100644
File size: 5.1 KB
Line 
1/*
2 *  pcmmio_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 "pcmmio_commands.h"
15#include <rtems/string2.h>
16
17#include <stdlib.h>
18
19#if defined(TESTING)
20  #define set_dac_voltage(_dac, _voltage) \
21    /* printf( "Testing: Write %6.4f to to dac %d\n", _voltage, _dac ); */
22#endif
23
24#define VALIDATE_VOLTAGE(_v) \
25  if ( (_v) < -10.0 || (_v) > 10.0 ) { \
26    printf( "Voltage must be between -10.0V and +10.0V\n" ); \
27    fail = true; \
28  }
29
30char pcmmio_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-7\n"
35  "       voltages and step must be -10V to +10V\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( pcmmio_dac_usage, argv[0], argv[0] );
42
43int main_pcmmio_dac(int argc, char **argv)
44{
45  int       dac;
46  float     low_voltage;
47  float     high_voltage;
48  float     step_voltage;
49  float     current_voltage;
50  float     current_step;
51  int       step_time;
52  int       maximum_time;
53  uint32_t  step_ticks;
54  bool      fail = false;
55  int       elapsed;
56
57  /*
58   *  Verify that we have the right number of arguments.
59   */
60  if ( (argc != 3) && (argc != 7) ) {
61    printf( "Incorrect number of arguments\n" );
62    PRINT_USAGE();
63    return -1;
64  }
65
66  /*
67   *  Convert the string arguments into number values
68   */
69  if ( rtems_string_to_int( argv[1], &dac, NULL, 0 ) ) {
70    printf( "DAC (%s) is not a number\n", argv[1] );
71    fail = true;
72  }
73
74  if ( rtems_string_to_float( argv[2], &low_voltage, NULL ) ) {
75    printf( "Voltage (%s) is not a number\n", argv[2] );
76    fail = true;
77  }
78
79  /*
80   *  Validate the output dac and voltage.
81   */
82  if ( dac < 0 || dac > 7 ) {
83    puts( "DAC number must be 0-7" );
84    fail = true;
85  }
86
87  VALIDATE_VOLTAGE( low_voltage );
88
89  /*
90   *  Now do a single write to the DAC
91   */
92  if ( argc == 3 ) {
93    if ( fail ) {
94      PRINT_USAGE();
95      return -1;
96    }
97    printf( "Write %6.4f to to dac %d\n", low_voltage, dac );
98    set_dac_voltage(dac, low_voltage);
99    return 0;
100  }
101
102  /*
103   *  Finish parsing the arguments to do a pattern
104   */
105  fail = false;
106
107  if ( rtems_string_to_float( argv[3], &high_voltage, NULL ) ) {
108    printf( "Voltage (%s) is not a number\n", argv[3] );
109    fail = true;
110  }
111
112  VALIDATE_VOLTAGE( high_voltage );
113
114  if ( rtems_string_to_float( argv[4], &step_voltage, NULL ) ) {
115    printf( "Step voltage (%s) is not a number\n", argv[4] );
116    fail = true;
117  }
118
119  VALIDATE_VOLTAGE( step_voltage );
120
121  if ( step_voltage < 0.0 ) {
122    printf( "Step voltage must be greater than 0\n" );
123    fail = true;
124  }
125
126  if ( rtems_string_to_int( argv[5], &step_time, NULL, 0 ) ) {
127    printf( "Step time (%s) is not a number\n", argv[5] );
128    fail = true;
129  }
130
131  if ( rtems_string_to_int( argv[6], &maximum_time, NULL, 0 ) ) {
132    printf( "Maximum time (%s) is not a number\n", argv[6] );
133    fail = true;
134  }
135
136  if ( step_time >= maximum_time ) {
137    printf(
138      "Step time (%d) must be less than maximum time (%d)\n",
139      step_time,
140      maximum_time
141    );
142    fail = true;
143  }
144
145  if ( step_time < 0 ) {
146    printf( "Step time must be greater than 0\n" );
147    fail = true;
148  }
149
150  if ( maximum_time < 0 ) {
151    printf( "Maximum time must be greater than 0\n" );
152    fail = true;
153  }
154
155  /*
156   *  Now write the pattern to the DAC
157   */
158
159  if ( fail ) {
160    PRINT_USAGE();
161    return -1;
162  }
163
164  printf(
165    "Write %6.4f-%6.4f step=%6.4f stepTime=%d msecs dac=%d max=%d msecs\n",
166    low_voltage,
167    high_voltage,
168    step_voltage,
169    step_time,
170    dac,
171    maximum_time
172  );
173
174  elapsed         = 0;
175  step_ticks      = RTEMS_MILLISECONDS_TO_TICKS(step_time);
176  current_voltage = low_voltage;
177  current_step    = step_voltage;
178
179  if ( low_voltage > high_voltage )
180    current_step    *= -1.0;
181
182  while (1) {
183 
184    #if defined(TESTING)
185      printf( "%d: Write %6.4f to to dac %d\n", elapsed, current_voltage, dac );
186    #endif
187    set_dac_voltage(dac, current_voltage);
188
189    current_voltage += current_step;
190    if ( current_voltage < low_voltage ) {
191      current_step    = step_voltage;
192      current_voltage = low_voltage;
193    } else if ( current_voltage > high_voltage ) {
194      current_step    = -1.0 * step_voltage;
195      current_voltage = high_voltage;
196    }
197
198    elapsed += step_time;
199    if ( elapsed > maximum_time )
200      break;
201
202    rtems_task_wake_after( step_ticks );
203  }
204   
205  return 0;
206}
207
208rtems_shell_cmd_t Shell_PCMMIO_DAC_Command = {
209  "pcmmio_dac",                                    /* name */
210  "Read PCMMIO Analog Inputs",                     /* usage */
211  "pcmmio",                                        /* topic */
212  main_pcmmio_dac,                                 /* command */
213  NULL,                                            /* alias */
214  NULL                                             /* next */
215};
216
217rtems_shell_alias_t Shell_PCMMIO_DAC_Alias = {
218  "pcmmio_dac",          /* command */
219  "dac"                  /* alias */
220};
221
Note: See TracBrowser for help on using the repository browser.