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