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

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