source: firmware/FSC/src/FSC_test.c @ 10667

Last change on this file since 10667 was 10667, checked in by neise, 9 years ago
File size: 12.0 KB
Line 
1//-----------------------------------------------------------------------------
2#include "typedefs.h"
3#include "application.h"
4#include "spi_master.h"
5#include "ad7719_adc.h"
6#include "atmega_adc.h"   
7#include "usart.h"
8#include "macros.h"
9#include "muxer_fsc.h"
10#include "output.h"
11#include "parser.h"
12#include "interpol.h"
13#include "w5100_spi_interface.h"
14#include <avr/interrupt.h>
15#include <avr/wdt.h>
16#include <stdlib.h>
17//-----------------------------------------------------------------------------
18// definition of some functions:
19// these function are implemented in this file, this is not doog coding style.
20// sooner or later, they will be moved into more apropriate files.
21
22void parse(); //doesn't do anything at the moment
23
24// end of function definition:
25//-----------------------------------------------------------------------------
26
27
28// MAIN WORKFLOW GLOBAL VARIABLES
29        bool verbose;
30        bool heartbeat_enable;
31
32// USART global variables
33        U08 usart_rx_buffer[USART_RX_BUFFER_SIZE];
34        U08 usart_tx_buffer[USART_TX_BUFFER_SIZE];
35        U08 usart_rx_buffer_index = 0;
36        U08 usart_tx_buffer_index = 0;
37        U08 usart_last_char;                                    // last received char
38
39// USART FLAGS
40        bool usart_tx_buffer_overflow = false;  // true if usart_tx_buffer was full.
41       
42// TIMER global variable
43        volatile U32 local_ms = 0;
44
45// AD7719 global variables
46        U32 ad7719_values[TEMP_CHANNELS];
47        U08 ad7719_enables[CHANNEL_BITMAP];
48        U08 ad7719_channels_ready[CHANNEL_BITMAP];
49        U08 ad7719_readings_since_last_muxing = 0;
50        U08 ad7719_current_channel = 0;
51        U32 ad7719_current_reading = 0;
52        bool ad7719_measured_all = false;
53       
54// ATMEGA ADC global variables
55        U08 adc_values[V_CHANNELS + I_CHANNELS + H_CHANNELS]; // stores measured voltage in steps of 16mV
56        U08 adc_enables[V_BITMAP + I_BITMAP + H_BITMAP];
57        U08 adc_channels_ready[V_BITMAP + I_BITMAP + H_BITMAP];
58        U08 adc_readings_since_last_muxing = 0;
59        U08 adc_current_channel = 0;
60        U08 adc_current_reading = 0;
61        bool adc_measured_all = false;
62
63        bool once_told_you = true;
64        bool debug_mode = false;
65       
66#ifndef ___MAIN_WORKFLOW_GLOBAL_VARS
67#define ___MAIN_WORKFLOW_GLOBAL_VARS   
68        #define TEMP_CHANNELS 64
69        #define CHANNEL_BITMAP 8
70        #define AD7719_READINGS_UNTIL_SETTLED 1 // bei3:480ms
71        #define V_CHANNELS 40
72        #define I_CHANNELS 40
73        #define H_CHANNELS 4
74        #define V_BITMAP 5
75        #define I_BITMAP 5
76        #define H_BITMAP 1
77        #define ADC_READINGS_UNTIL_SETTLED 1
78       
79#endif // ___MAIN_WORKFLOW_GLOBAL_VARS
80
81
82//-----------------------------------------------------------------------------
83//   M A I N    ---   M A I N    ---   M A I N    ---   M A I N    ---  M A I N   
84//-----------------------------------------------------------------------------
85int main(void)
86{
87
88        app_init();               // Setup: Watchdog and I/Os
89        usart_init();
90        spi_init();             // Initialize SPI interface as master
91       
92// TIMER2 is used as local clock:
93// configure timer 2
94        TCCR2 = (1<<WGM21); // CTC Modus
95        TCCR2 |= (1<<CS21) | (1<<CS20); // Prescaler 64 --> counts up every 8us
96        OCR2 = 125-1;                                   // --> output compare interrupt occurs every 125 x 8us = 1ms
97        // Compare Interrupt erlauben
98        TIMSK |= (1<<OCIE2);
99
100  //  Enable interrupts
101  sei();             
102
103        usart_write_str((pU08)"Start of test firmware (06.05.11) now:\n");
104        if (local_ms % 10000 == 0) {
105                usart_write_str((pU08)"time: ");
106                usart_write_U32(local_ms/1000,6);
107                usart_write_str((pU08)"sec.\n");
108        }
109       
110        PORTB &= ~(1<<PB2); //#reset = LOW --> W5100 is in reset ...
111        _delay_ms(50); //reset
112       
113        PORTB |= 1<<PB2; //#reset = HIGH --> W5100 is active
114        _delay_ms(5);           // give it 5ms to accomodate.
115/*     
116        PORTB |= (1<<PB4);
117        usart_write_str((pU08)"PB4 switched HIGH. Please check & hit ENTER.\n");
118        while(!enter_received){
119                if ( UCSRA & (1<<RXC) ) {
120                        if (UDR == '\n')
121                                enter_received = true;
122                }
123        }
124        enter_received = false;
125*/
126
127        usart_write_str((pU08)"W5300 init begins at:");
128        usart_write_U32(local_ms,10);
129        usart_write_str((pU08)"ms.\n");
130       
131        w5100_init();
132 
133        usart_write_str((pU08)"W5300 init finished at:");
134        usart_write_U32(local_ms,10);
135        usart_write_str((pU08)"ms.\n");
136
137        usart_write_str((pU08)"Wait for PC to connect to FSC...");
138        while (!w5100_is_established()) {
139                usart_write_str((pU08)"Socket status is:");
140                usart_write_U08_hex(w5100_sock_status());
141                usart_write_char('\n');
142                _delay_ms(333);
143        }
144       
145        usart_write_str((pU08)"connection to PC established at:");
146        usart_write_U32(local_ms/1000,6);
147        usart_write_str((pU08)"sec.\n");
148       
149        usart_write_str((pU08)"Ready to investigate, what's happening next? - HIT ENTER - \n");
150        while(!usart_rx_ready){
151                _delay_ms(10);
152        }
153        usart_rx_ready = false;
154       
155       
156        usart_write_str((pU08)"S0_TX_FSR\t|S0_TX_RD\t|S0_TX_WR\t|S0_RX_RSR\t|S0_RX_RD\t| 0x0426 | 0x0427 |\n");
157        while(!usart_rx_ready){
158               
159                usart_write_U16_hex(get_S0_TX_FSR()); usart_write_char('\t'); usart_write_char('|');
160                usart_write_U16_hex(get_S0_TX_RD()); usart_write_char('\t'); usart_write_char('|');
161                usart_write_U16_hex(get_S0_TX_WR()); usart_write_char('\t'); usart_write_char('|');
162                usart_write_U16_hex(get_S0_RX_RSR()); usart_write_char('\t'); usart_write_char('|');
163                usart_write_U16_hex(get_S0_RX_RD()); usart_write_char('\t'); usart_write_char('|');
164                usart_write_U08_hex( w5100_read(0x0426) ); usart_write_char('\t'); usart_write_char('|');
165                usart_write_U08_hex( w5100_read(0x0427) ); usart_write_char('\t'); usart_write_char('|');
166               
167                usart_write_char('\n');
168
169                _delay_ms(400);
170                _delay_ms(400);
171        }
172        usart_rx_ready = false;
173
174        U16 received_bytes;
175        U08 really_downloaded_bytes;
176        while(!usart_rx_ready){
177                received_bytes = get_S0_RX_RSR();
178               
179                usart_write_U16_hex(get_S0_TX_FSR()); usart_write_char('\t'); usart_write_char('|');
180                usart_write_U16_hex(get_S0_TX_RD()); usart_write_char('\t'); usart_write_char('|');
181                usart_write_U16_hex(get_S0_TX_WR()); usart_write_char('\t'); usart_write_char('|');
182                usart_write_U16_hex(get_S0_RX_RSR()); usart_write_char('\t'); usart_write_char('|');
183                usart_write_U16_hex(get_S0_RX_RD()); usart_write_char('\t'); usart_write_char('|');
184               
185       
186                if (get_S0_RX_RSR() != 0) { // we have something to read
187                        usart_write_str((pU08)"\nReading ");
188                        usart_write_U16(get_S0_RX_RSR(), 6);
189                        usart_write_str((pU08)" b from W5100\n");
190               
191                        while (received_bytes != 0) {
192                                really_downloaded_bytes = w5100_get_RX(16, true);
193                                usart_write_char('!');
194                                usart_write_U08(really_downloaded_bytes,4);
195                                usart_write_char('\n');
196                                for (U08 i=0; i<really_downloaded_bytes; i++){
197                                        usart_write_U08_hex(eth_read_buffer[i]);
198                                        usart_write_char(' ');
199                                }
200                                received_bytes -= really_downloaded_bytes;
201                        }
202                }
203                usart_write_char('\n');
204               
205                _delay_ms(400);
206                _delay_ms(400);
207                _delay_ms(400);
208                _delay_ms(400);
209        }
210        usart_rx_ready = false;
211       
212        bool quit = false;
213        while (!quit) {
214        usart_write_str((pU08)"Enter the string to send to PC or \"XXX\" in order to quit\n");
215                while(!usart_rx_ready){
216                        _delay_ms(100);
217                }
218                usart_rx_ready = false;
219       
220                if ( (usart_rx_buffer[0]=='X') &&  (usart_rx_buffer[1]=='X') && (usart_rx_buffer[2]=='X') )
221                        quit = true;
222                else {
223                        // copy string to W5100 TX buffer and issue 'SEND' command.
224                        for (U08 i =0; i<usart_received_chars; i++){
225                                eth_write_buffer[i]=usart_rx_buffer[i];
226                        }
227                       
228                        w5100_set_TX(usart_received_chars);
229                       
230                }               
231                       
232               
233        }
234       
235} // end of main()     
236       
237/*
238//MAIN LOOP
239while (1)
240{
241        if (heartbeat_enable) PORTB ^= (1<<PB3); // toggle Out2_spare --> heartbeat
242//----------------------------------------------------------------------------
243        //IF we need to send away one byte, and ready to send
244
245        if ( (usart_tx_buffer_index > 0) && (UCSRA & (1<<UDRE)) ) {
246                UDR = usart_tx_buffer[0];
247                // THis is shit
248                for (U08 i=0 ; i < USART_TX_BUFFER_SIZE; ++i) {
249                        usart_tx_buffer[i] = usart_tx_buffer[i+1];
250                }
251                usart_tx_buffer_index--;
252        }
253//----------------------------------------------------------------------------
254
255        //IF we just received one byte, and there is enough space in the RX_buffer
256        if ( (UCSRA & (1<<RXC)) && (usart_rx_buffer_index < USART_RX_BUFFER_SIZE) ){
257                usart_last_char = UDR;
258                if (usart_last_char == '\n'){ // if EOL was received
259                        usart_rx_ready = true;
260                }else {
261                usart_rx_buffer[usart_rx_buffer_index] = usart_last_char;
262                usart_rx_buffer_index++;
263                }
264                // here is still something strange .... better send an enter automatically
265        } else if (UCSRA & (1<<RXC)) { // if there is no scace in the buffer; read anyway.
266                usart_last_char = UDR;
267                usart_rx_buffer_index =0;
268        }
269//----------------------------------------------------------------------------
270
271        //IF USART DOR bit is set, PC is sending data to fast!!!
272        if ( UCSRA & (1<<DOR) ){
273                // flush TX_buffer and write warning message in
274                // maybe even switch off every measurement. ?
275        }
276//----------------------------------------------------------------------------
277
278        //IF TX_BUFFER was overrun.     
279        if (usart_tx_buffer_overflow) {
280                // flash TX_buffer and write warning message in
281                // maybe even switch off every measurement. ?
282                //
283                // this should only happen, in verbose mode and with low baudrates.
284        }
285//----------------------------------------------------------------------------
286       
287        //IF one command was received.
288        //      -It is not allowed to send more than one command between two '\n'
289        if (usart_rx_ready){
290                parse();
291                usart_rx_buffer_index = 0;
292                usart_rx_ready = false;
293        }
294//----------------------------------------------------------------------------
295
296        //IF ATmega internal ADC did finish a conversion --every 200us
297        if ( (ADCSRA & (1<<ADIF)) && !adc_measured_all) {
298                adc_current_reading = ADCH;
299                if (adc_readings_since_last_muxing == ADC_READINGS_UNTIL_SETTLED) {
300                        adc_values[adc_current_channel] = adc_current_reading;
301                        adc_readings_since_last_muxing=0;
302                        // note that this channel is ready, now and
303                        adc_output(adc_current_channel, adc_current_reading);
304                        // proceed to the next enabled channel.
305                        adc_channels_ready[adc_current_channel/8] |= (1<<(adc_current_channel%8));
306                        adc_current_channel = increase_adc (adc_current_channel);
307                        Set_V_Muxer(adc_current_channel);
308                } else { // the ADC did not settle yet, we discard the reading
309                        ++adc_readings_since_last_muxing;
310                        // current reading is not used for anything else
311                }
312        }
313//----------------------------------------------------------------------------
314
315        //IF AD7719 ADC just finished a conversion -- every 60ms
316       
317        if (AD7719_IS_READY()) {
318                        ad7719_current_reading = read_adc(); // --takes at 4MHz SCLK speed about 6us
319                // AD7719 is only read out if settled. saves time.     
320                if (ad7719_readings_since_last_muxing == AD7719_READINGS_UNTIL_SETTLED) {
321                        ad7719_values[ad7719_current_channel] = ad7719_current_reading;
322                        ad7719_readings_since_last_muxing=0;
323                        // now prepare the data to be send away via USART.
324                        //ad7719_output(ad7719_current_channel, ad7719_current_reading);
325                        // note that this channel is ready, now and
326                        // proceed to the next enabled channel.
327                        ad7719_channels_ready[ad7719_current_channel/8] |= (1<<(ad7719_current_channel%8));
328                        ad7719_current_channel = increase_ad7719 (ad7719_current_channel);
329                        Set_T_Muxer(ad7719_current_channel);
330                } else { // the AD7719 did not settle yet, we discard the reading
331                        ++ad7719_readings_since_last_muxing;
332
333                        // current reading is not used for anything else
334                }
335        }
336//----------------------------------------------------------------------------
337        //IF one of the ADC measured all channels, we wanted to know.
338        check_if_measured_all();
339       
340        if (ad7719_measured_all && adc_measured_all)
341                adc_output_all();
342
343//----------------------------------------------------------------------------
344
345//      if (verbose == true)
346                // talk() was just defined so the
347                // code is not at this place ... look down.
348//              talk();
349
350
351} // end of MAIN LOOP
352//-----------------------------------------------------------------------------
353//    E N D     E N D     E N D     E N D     E N D     E N D     E N D     
354//-----------------------------------------------------------------------------
355
356*/
357
358
359
360ISR (TIMER2_COMP_vect)
361{
362 ++local_ms;
363}
364
365
366
367
Note: See TracBrowser for help on using the repository browser.