source: firmware/FSC/src/parser.c@ 11696

Last change on this file since 11696 was 11667, checked in by neise, 14 years ago
File size: 13.9 KB
Line 
1#include "parser.h"
2#include "output.h"
3#include "application.h"
4#include "usart.h"
5#include "w5100_spi_interface.h"
6#include "timer.h"
7// this method parses the data,
8// which came in via USART
9// later it might as well parse the data from ethernet.
10void parse_ascii() {
11 usart_rx_buffer[USART_RX_BUFFER_SIZE-1] = 0;
12 usart_write_str((pU08)"got:");
13 usart_write_str(usart_rx_buffer);
14
15// look at first byte
16// I hope, I can manage to use one byte commands
17 switch (usart_rx_buffer[0]) {
18 case 'E': // AD7719 enable bitmaps may be set
19 set_ad7719_enable_register();
20 break;
21 case 'e': // ATmega internal ADC enable bitmaps may be set
22 // not supported yet.
23 set_adc_enable_register();
24 break;
25 case 'h':
26 usart_write_str((pU08)"\nheartbeat ");
27 heartbeat_enable = true;
28 if (usart_rx_buffer[1] == '0'){
29 heartbeat_enable = false;
30 usart_write_str((pU08)"off\n");
31 } else {
32 usart_write_str((pU08)"on\n");
33 }
34 break;
35 case 'G': // GET the Temperature channels, which are enabled
36 once_told_you = false;
37 for ( U08 i=0; i<RESISTANCE_CHANNELS/8; ++i ) {
38 ad7719_channels_ready[i]=0;
39 }
40 break;
41
42 case 'g': // GET the voltage/current/humidity channels, which are enabled
43 once_told_you = false;
44 for ( U08 i=0; i<VOLTAGE_REGS; ++i ) {
45 adc_channels_ready[i]=0;
46 }
47 break;
48
49 case 'P':
50 print_ad7719_nicely();
51 break;
52
53 case 'p':
54 print_adc_nicely();
55 break;
56
57 case 's':
58 print_status();
59 break;
60
61 case 'd':
62 usart_write_str((pU08)"\ndebug mode ");
63 debug_mode = true;
64 if (usart_rx_buffer[1] == '0'){
65 debug_mode = false;
66 usart_write_str((pU08)"off\n");
67 } else {
68 usart_write_str((pU08)"on\n");
69 }
70 break;
71 }
72
73
74 usart_write_str((pU08)"\nready?");
75 for (U08 i=0; i<USART_RX_BUFFER_SIZE; ++i)
76 usart_rx_buffer[i] = 0;
77}
78
79void parse_non_human_readable_input() {
80
81
82}
83
84// this guy checks, if an incoming byte is:
85// to be stored, because it belongs to a command
86// to be discarded, because something went wrong, and the bytemade no sense...
87// the end of a commmand, and so the 'real parser' may do his work.
88/*
89void incoming_byte_parser() {
90 static bool receiving_command = false;
91 U08 current_byte = UDR;
92
93 if (!receiving_command) {
94 switch (current_byte) {
95 case 0x01:
96 receiving_command = true;
97 break;
98 }
99 }
100
101 if (receiving_command)
102 {
103 usart_rx_buffer[usart_received_chars] = usart_last_char;
104 usart_received_chars++;
105 }
106
107
108}
109*/
110
111
112// this is the recent MSR parser
113/////////////////////////////////////////////////////////////////////////////////////////////////////
114/////////////// P A R S E R /////////////////////////////////////////////////////////////////////////
115/////////////////////////////////////////////////////////////////////////////////////////////////////
116// this method parses the data,
117// which came in via USART
118// later it might as well parse the data from ethernet.
119void MSR_parser() {
120
121U08 input_number = 0;
122bool enable = false;
123// look at first byte
124// I hope, I can manage to use one byte commands
125 usart_rx_buffer[USART_RX_BUFFER_SIZE-1] = 0;
126 usart_write_char(' ');
127 usart_write_str(usart_rx_buffer);
128 usart_write_char('\n');
129
130
131 if (usart_received_chars >0) {
132 switch (usart_rx_buffer[0]) {
133 case 'E': // user wants to enable/disable something
134 case 'e':
135 enable = true;
136 case 'D':
137 case 'd':
138
139 if (usart_received_chars>=1) { // let's see what.
140 if ( usart_rx_buffer[1] != 'r' &&
141 usart_rx_buffer[1] != 'v' &&
142 usart_rx_buffer[1] != 'h' &&
143 usart_rx_buffer[1] != 'p' )
144 {
145 //print_invalid_enable_statement();
146 break;
147 }
148 }
149
150 input_number = 0;
151 if (usart_received_chars >2) { // lets check the first digit
152 if ( usart_rx_buffer[2] >= '0' && usart_rx_buffer[2] <= '9' ) {
153 input_number = usart_rx_buffer[2] - '0';
154 } else {
155 //print_invalid_enable_statement();
156 break;
157 }
158 }
159 if (usart_received_chars >3) { // lets check the 2nd digit
160 if ( usart_rx_buffer[3] >= '0' && usart_rx_buffer[3] <= '9' ) {
161 input_number = 10*input_number + usart_rx_buffer[3] - '0';
162 } else {
163 // okay as well ... if the second digit is missing ..
164 // we might have trailing spaces
165 // that's okay .. we just accept the first number.
166 }
167 }
168 if (usart_received_chars>2) {
169 usart_write_str((pU08)"\n I will switch ");
170 if (enable)
171 usart_write_str((pU08)"on ");
172 else
173 usart_write_str((pU08)"off ");
174
175 // now we know, what the user wanted ... and we need to do it.
176 switch (usart_rx_buffer[1]) {
177 case 'r':
178 if (enable)
179 ad7719_enables[input_number/8] |= (1<<(input_number%8));
180 else {
181 ad7719_enables[input_number/8] &= ~(1<<(input_number%8));
182 ad7719_channels_ready[input_number/8] &= ~(1<<(input_number%8));
183 }
184 usart_write_str((pU08)" resistance channel ");
185 usart_write_U08(input_number,2);
186 usart_write_char('\n');
187 break;
188 case 'v':
189 if (enable)
190 adc_enables[input_number/8] |= (1<<(input_number%8));
191 else {
192 adc_enables[input_number/8] &= ~(1<<(input_number%8));
193 adc_channels_ready[input_number/8] &= ~(1<<(input_number%8));
194 }
195 usart_write_str((pU08)" voltage channel ");
196 usart_write_U08(input_number,2);
197 usart_write_char('\n');
198 break;
199
200 case 'h':
201 if (enable)
202 adc_enables[1] |= (1<<(input_number%4));
203 else {
204 adc_enables[1] &= ~(1<<(input_number%4));
205 adc_channels_ready[1] &= ~(1<<(input_number%4));
206 }
207 usart_write_str((pU08)" humidity channel ");
208 usart_write_U08(input_number,2);
209 usart_write_char('\n');
210 break;
211 case 'p':
212 if (enable)
213 adc_enables[1] |= (1<<((input_number%4) + 4));
214 else {
215 adc_enables[1] &= ~(1<<((input_number%4) + 4));
216 adc_channels_ready[1] &= ~(1<<((input_number%4) + 4));
217 }
218 usart_write_str((pU08)" pressure channel ");
219 usart_write_U08(input_number,2);
220 usart_write_char('\n');
221 break;
222 default:
223 usart_write_str((pU08)"\n DAMN! this should never happen"
224 "- enable/disable switch-case!\n");
225 break;
226 }
227
228
229
230 }// end of if usart_received_chars>2 --> this should not be necessary at all
231 break;
232
233 case 'b':
234 usart_write_str((pU08)"\nheartbeat ");
235 heartbeat_enable = true;
236 if (usart_rx_buffer[1] == '0'){
237 heartbeat_enable = false;
238 usart_write_str((pU08)"off\n");
239 } else {
240 usart_write_str((pU08)"on\n");
241 }
242 break;
243 case 'G': // GET the Temperature channels, which are enabled
244 ad7719_values_printed = false;
245 reset_resistance_done();
246 reset_resistance_values();
247 break;
248
249 case 'g': // GET the voltage/current/humidity channels, which are enabled
250 adc_values_printed = false;
251 reset_voltage_done();
252 reset_voltage_values();
253 break;
254
255 case 'p':
256 adc_print_endless = true;
257 if (usart_rx_buffer[1] == '0')
258 adc_print_endless = false;
259 break;
260
261 case 'P':
262 ad7719_print_endless = true;
263 if (usart_rx_buffer[1] == '0')
264 ad7719_print_endless = false;
265 break;
266
267 case 's':
268 print_status();
269 //print_adc_enable_status(true);
270 //usart_write_char('\n');
271
272 //print_ad7719_enable_status(true);
273 //usart_write_char('\n');
274
275 //usart_write_str((pU08)"time:");
276 //usart_write_float((float)local_ms/1000 , 1,7);
277 //usart_write_str((pU08)" sec.\n");
278 break;
279
280 case 'S':
281 write_status_via_eth();
282 break;
283
284 case '!':
285 usart_write_str((pU08)"\ndebug mode ");
286 debug_mode = true;
287 if (usart_rx_buffer[1] == '0'){
288 debug_mode = false;
289 usart_write_str((pU08)"off\n");
290 } else {
291 usart_write_str((pU08)"on\n");
292 }
293 break;
294
295 case 'h':
296 case 'H':
297 print_help();
298 break;
299 }
300 } else
301 {
302 // zero bytes received
303 }
304
305
306 for (U08 i=0; i<USART_RX_BUFFER_SIZE; ++i)
307 usart_rx_buffer[i] = 0;
308 usart_received_chars = 0;
309 usart_rx_ready = false;
310} // END of MSR_parser();
311
312
313
314///////////////////////////////////////////////////////////////////////////////////////
315// W5300 incoming commands parser
316void parse_w5300_incoming( U08 bytes_in_w5100_rx_buffer ) {
317
318 usart_writeln_str((pU08)"parsget:");
319 for (U08 i =0; i< ETH_READ_BUFFER_SIZE ; i++) {
320 usart_write_U08_hex(eth_read_buffer[i]);
321 usart_write_char((U08)eth_read_buffer[i]);
322
323 }
324 usart_write_crlf();
325
326 if (bytes_in_w5100_rx_buffer == 0) // it was stupid to call this parser without having any bytes downloaded
327 return;
328
329 switch (eth_read_buffer[0]) {
330 case 'w': // *w*rite to register command
331 write_FSC_register();
332 read_FSC_register();
333 break;
334
335 case 'r': // *r*ead from register command
336 read_FSC_register();
337 break;
338
339 case 't': // measure *t*emperature (resistance) channels only
340 ad7719_values_printed = false;
341 reset_resistance_done();
342 simple_acknowledge();
343 break;
344
345 case 'v': // measure *v*oltage channels only
346 adc_values_printed = false;
347 reset_voltage_done();
348 simple_acknowledge();
349 break;
350
351 case 'm': // *m*easure active channels commands
352 reset_done();
353 ad7719_print_endless = true;
354
355 simple_acknowledge();
356 break;
357
358 case 's': // return *s*tatus information
359 // this might take a lot of time... about... 25us per byte .. --> less than 25ms
360 write_status_via_eth();
361 //w5100_set_TX(FSCregister, FSC_REGISTER_LENGTH);
362 break;
363
364 case 'k':
365 eth_write_stuff();
366 break;
367
368 case 'z': // re*z*et ATmega32 and peripherals
369 eth_write_buffer[0]='!';
370 eth_write_buffer[1]='!';
371 w5100_set_TX(eth_write_buffer, 2);
372 // let watchdog occur!
373 // gotta read, how this works...
374 break;
375 }
376}
377
378void write_FSC_register() {
379 FSCregister[eth_read_buffer[2]]=eth_read_buffer[3];
380}
381
382void read_FSC_register() {
383 eth_write_buffer[0]=eth_read_buffer[0];
384 eth_write_buffer[1]=eth_read_buffer[1];
385 eth_write_buffer[2]=eth_read_buffer[2];
386 eth_write_buffer[3]=FSCregister[eth_read_buffer[2]];
387 w5100_set_TX(eth_write_buffer, 4);
388}
389
390void reset_resistance_done(){
391 for (U08 i=0; i < (RESISTANCE_CHANNELS/8); i++){
392 ad7719_channels_ready[i] = 0;
393 }
394}
395void reset_resistance_values(){
396 for (U08 i=0; i < RESISTANCE_CHANNELS; i++){
397 ad7719_values[i] = 0;
398 }
399}
400
401
402void reset_voltage_done(){
403 for (U08 i=0; i < (VOLTAGE_REGS); i++){
404 adc_channels_ready[i] = 0;
405 }
406}
407void reset_voltage_values(){
408 for (U08 i=0; i < VOLTAGE_CHANNELS; i++){
409 adc_values[i] = 0;
410 }
411}
412
413void reset_done(){
414 reset_resistance_done();
415 reset_voltage_done();
416}
417
418void simple_acknowledge(){
419 eth_write_buffer[0]=eth_read_buffer[0];
420 eth_write_buffer[1]=eth_read_buffer[1];
421 w5100_set_TX(eth_write_buffer, 2);
422}
423
424void write_status_via_eth() {
425float resistance;
426
427 // take care: ethernet write buffer is just 32 bytes long.
428 eth_write_str("status: "); eth_writeln_str(nc_U32_to_hex(status));
429 // better switch off interrupts here:
430 // otherwise the numbers in time_sec and time_ms might get corrupt during output.
431 cli();
432 eth_write_str("time_s: "); eth_write_str(nc_U32_to_str(sec, 1));
433 eth_write_str(".");eth_writeln_str(nc_U16_to_str(milisec, 1));
434 //eth_write_str("mili_s: "); eth_writeln_str(nc_U16_to_str(milisec, 1));
435 sei();
436
437 //eth_write_str("R_ref : "); eth_writeln_str(nc_U16_to_str(ref_resistor, 6));
438
439 eth_writeln_str("VOLTAGES\n");
440 eth_write_str("enable:");
441 for (U08 i=0; i<VOLTAGE_REGS ; i++){
442 eth_write_str(nc_U08_to_bin(adc_enables[i]));
443 // need to tweak here in order to get some nice spaces...
444 eth_write_buffer[eth_write_index] = ' ';
445 eth_write_index++;
446 }
447 eth_write_buffer[eth_write_index++] = '\n';
448
449 eth_write_str(" done:");
450 for (U08 i=0; i<VOLTAGE_REGS ; i++){
451 eth_write_str(nc_U08_to_bin(adc_channels_ready[i]));
452 // need to tweak here in order to get some nice spaces...
453 eth_write_buffer[eth_write_index] = ' ';
454 eth_write_index++;
455 }
456 eth_write_buffer[eth_write_index++] = '\n';
457
458 eth_write_str("values:");
459 for (U08 i=0; i<VOLTAGE_CHANNELS ; i++){
460 eth_write_str(nc_U16_to_str(adc_values[i]/10, 1) );
461 // need to tweak here in order to get some nice spaces...
462 eth_write_buffer[eth_write_index] = ' ';
463 eth_write_index++;
464 }
465 eth_write_buffer[eth_write_index++] = '\n';
466
467 eth_writeln_str("RESISTANCES\n");
468 eth_write_str("enable:");
469 for (U08 i=0; i<(RESISTANCE_CHANNELS/8) ; i++){
470 eth_write_str(nc_U08_to_bin(ad7719_enables[i]));
471 // need to tweak here in order to get some nice spaces...
472 eth_write_buffer[eth_write_index] = ' ';
473 eth_write_index++;
474 }
475 eth_write_buffer[eth_write_index++] = '\n';
476
477 eth_write_str(" done:");
478 for (U08 i=0; i<(RESISTANCE_CHANNELS/8) ; i++){
479 eth_write_str(nc_U08_to_bin(ad7719_channels_ready[i]));
480 // need to tweak here in order to get some nice spaces...
481 eth_write_buffer[eth_write_index] = ' ';
482 eth_write_index++;
483 }
484 eth_write_buffer[eth_write_index++] = '\n';
485
486 eth_write_str("values:\n");
487 for (U08 i=0; i<RESISTANCE_CHANNELS ; i++){
488 //eth_write_str(nc_U32_to_str(ad7719_values[i], 10) );
489
490 resistance = (6.25 * 1024 * ad7719_values[i]) / ((U32)1 << 25);
491 eth_write_str(nc_float_to_str(resistance , 2, 2) );
492 // need to tweak here in order to get some nice spaces...
493 if (i%8 == 7)
494 eth_write_buffer[eth_write_index] = '\n';
495 else
496 eth_write_buffer[eth_write_index] = ' ';
497
498 eth_write_index++;
499 }
500 eth_writeln_str("\nend.\n");
501
502
503}
504
505void eth_write_stuff(void) {
506 S08 buf []="Test Ah A!!\n";
507 for (U08 i =0 ; i<255 ; i++) {
508 eth_write_str(nc_U08_to_str(i,4));
509 eth_write_str(buf);
510 }
511}
Note: See TracBrowser for help on using the repository browser.