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

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