source: firmware/FSC/src/FSC_eth_with_user_interface.c@ 10902

Last change on this file since 10902 was 10705, checked in by neise, 14 years ago
File size: 7.8 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 "timer.h"
15#include <avr/interrupt.h>
16#include <avr/wdt.h>
17#include <stdlib.h>
18
19// MAIN WORKFLOW GLOBAL VARIABLES
20 bool verbose; // should go to w5100.c or so
21 bool heartbeat_enable;
22
23
24 bool ad7719_measured_all = false;
25 bool ad7719_values_printed = true;
26 bool ad7719_print_endless = false;
27
28// ATMEGA ADC global variables
29
30 bool adc_measured_all = false;
31 bool adc_values_printed = true;
32 bool adc_print_endless = false;
33
34 bool once_told_you = true;
35 bool debug_mode = false;
36
37// ETHERNET VARS
38U08 downloaded_bytes;
39
40//-----------------------------------------------------------------------------
41// M A I N --- M A I N --- M A I N --- M A I N --- M A I N
42//-----------------------------------------------------------------------------
43int main(void)
44{
45 app_init(); // Setup: Watchdog and I/Os
46 usart_init();
47 spi_init(); // Initialize SPI interface as master
48 timer_init(); // set up TIMER2: TIMER2_COMP interrupt occurs every 1ms
49 atmega_adc_init();
50
51 w5100_reset();
52 w5100_init();
53
54 ad7719_init();
55
56 // Enable interrupts
57 sei();
58
59
60for ( U08 i=0; i < (RESISTANCE_CHANNELS/8); ++i ) {
61 ad7719_enables[i]=0xFF;
62 ad7719_channels_ready[i]=0;
63 }
64for ( U08 i=0; i < (VOLTAGE_CHANNELS/8); ++i ) {
65 adc_enables[i]=0x00;
66 adc_channels_ready[i]=0;
67}
68
69 static U08 welcome[]="welcome to:\n FSC command interface 0.1 \n build: ??.05.2010\n";
70 usart_write_str(welcome);
71 /*
72 print_help();
73 print_adc_enable_status(true);
74 usart_write_char('\n');
75 print_ad7719_enable_status(true);
76 usart_write_char('\n');
77 usart_write_str((pU08)"time:");
78 usart_write_U32(sec , 7);
79 usart_write_str((pU08)" sec.\n");
80*/
81//MAIN LOOP
82while (1)
83{
84
85 // checks if socket is okay and resets in case of problem the W5100
86 if (w5100_caretaker())
87 {
88 // something is not okay with the ethernet ...
89 // will be reset in the next revolution.. but maybe we want to do more...
90 }
91
92 // this if should check every 25ms if we have any data on the W5100
93 if ( (milisec % W5100_INPUT_CHECK_TIME == 0) && (w5100_ready) )
94 {
95 if (get_S0_RX_RSR() != 0) { // we have something to read
96 downloaded_bytes = w5100_get_RX(ETH_READ_BUFFER_SIZE, true);
97 parse_w5300_incoming( downloaded_bytes );
98 }
99 }
100
101//----------------------------------------------------------------------------
102
103 if (check_if_adc_measurement_done()) {
104
105 if(adc_print_endless){
106 usart_write_str((pU08)"V|");
107 usart_write_U32(sec ,8);
108 print_adc_nicely(false,true);
109 telegram_start();
110 w5100_set_TX((U08*) adc_values, ADC_VALUES_LEN_BYTE);
111
112 reset_voltage_done();
113 } else
114 if ( !adc_values_printed) {
115 adc_values_printed = true;
116 print_adc_nicely(true,true);
117 telegram_start();
118 w5100_set_TX((U08*) adc_values, ADC_VALUES_LEN_BYTE);
119
120 }
121 }
122
123 if (check_if_ad7719_measurement_done()) {
124
125 if(ad7719_print_endless){
126 usart_write_str((pU08)"R|");
127 usart_write_U32(sec ,8);
128 print_ad7719_nicely(false,true);
129 reset_resistance_done();
130 telegram_start();
131 w5100_set_TX((U08*) ad7719_values, AD7719_VALUES_LEN_BYTE);
132
133 } else
134 if ( !ad7719_values_printed) {
135 ad7719_values_printed = true;
136 print_ad7719_nicely(true,true);
137 telegram_start();
138 w5100_set_TX((U08*) ad7719_values, AD7719_VALUES_LEN_BYTE);
139
140 }
141 }
142
143//----------------------------------------------------------------------------
144
145 // in order to check the user interface on a hardware near level.
146 // user can issue 'heartbeat command'.
147 // then this üpin is toggled with the 'main-while-loop-frequency'
148 // --> nice check for this frequency as well
149 if (heartbeat_enable) {
150 PORTA ^= (1<<HEARTBEATPIN);
151 }
152
153//----------------------------------------------------------------------------
154 //IF we need to send away one byte, and ready to send
155 // apparently depricated
156 if ( (usart_tx_buffer_index > 0) && (UCSRA & (1<<UDRE)) ) {
157 UDR = usart_tx_buffer[0];
158 // THis is shit
159 for (U08 i=0 ; i < USART_TX_BUFFER_SIZE; ++i) {
160 usart_tx_buffer[i] = usart_tx_buffer[i+1];
161 }
162 usart_tx_buffer_index--;
163 }
164
165//----------------------------------------------------------------------------
166
167 //IF USART DOR bit is set, PC is sending data to fast!!!
168 if ( UCSRA & (1<<DOR) ){
169 usart_write_str((pU08)"PC sending to fast!\n");
170 //usart_last_char = UDR;
171 UDR;
172 // flush TX_buffer and write warning message in
173 // maybe even switch off every measurement. ?
174 }
175//----------------------------------------------------------------------------
176
177 //IF TX_BUFFER was overrun.
178 if (usart_tx_buffer_overflow) {
179 // flash TX_buffer and write warning message in
180 // maybe even switch off every measurement. ?
181 //
182 // this should only happen, in verbose mode and with low baudrates.
183 }
184//----------------------------------------------------------------------------
185
186 //IF one command was received.
187 // -It is not allowed to send more than one command between two '\n'
188 if (usart_rx_ready){
189 void MSR_parser();
190 }
191//----------------------------------------------------------------------------
192
193 //IF ATmega internal ADC did finish a conversion --every 200us
194 if ( (ADCSRA & (1<<ADIF)) && !adc_measured_all) {
195 *adc_current_reading = (U16)ADCL;
196 *adc_current_reading += (U16)ADCH<<8;
197 if (*adc_readings_since_last_muxing == ADC_READINGS_UNTIL_SETTLED+adc_readings_until_mean-1) {
198 adc_channels_ready[*adc_current_channel/8] |= (1<<(*adc_current_channel%8));
199 *adc_current_channel = increase_adc (*adc_current_channel);
200 adc_values[*adc_current_channel] = 0;
201 Set_V_Muxer(*adc_current_channel);
202 *adc_readings_since_last_muxing = 0;
203 _delay_ms(10);
204 }
205 else if (adc_readings_since_last_muxing >= ADC_READINGS_UNTIL_SETTLED-1) {
206 adc_values[*adc_current_channel] += *adc_current_reading;
207 ++(*adc_readings_since_last_muxing);
208 }
209 else {
210 ++(*adc_readings_since_last_muxing);
211 }
212
213 }
214//----------------------------------------------------------------------------
215
216 //IF AD7719 ADC just finished a conversion -- every 60ms
217
218 if (AD7719_IS_READY() && !ad7719_measured_all) {
219 *ad7719_current_reading = read_adc(); // --takes at 4MHz SCLK speed about 6us
220 // AD7719 is only read out if settled. saves time.
221 if (*ad7719_readings_since_last_muxing == AD7719_READINGS_UNTIL_SETTLED-1) {
222 ad7719_values[*ad7719_current_channel] = *ad7719_current_reading;
223 *ad7719_readings_since_last_muxing=0;
224 if (debug_mode) {
225 usart_write_U32_hex(*ad7719_current_reading);
226 usart_write_char('\n');
227 usart_write_char('\n');
228 }
229 // now prepare the data to be send away via USART.
230
231 // note that this channel is ready, now and
232 // proceed to the next enabled channel.
233 ad7719_channels_ready[*ad7719_current_channel/8] |= (1<<(*ad7719_current_channel%8));
234 *ad7719_current_channel = increase_ad7719 (*ad7719_current_channel);
235 Set_T_Muxer(*ad7719_current_channel);
236 } else { // the AD7719 did not settle yet, we discard the reading
237 ++(*ad7719_readings_since_last_muxing);
238 if (debug_mode) {
239 usart_write_U32_hex(*ad7719_current_reading);
240 usart_write_char('\n');
241 }
242
243 // current reading is not used for anything else
244 }
245 }
246
247
248} // end of MAIN LOOP
249
250} // end of main function
251//-----------------------------------------------------------------------------
252// E N D E N D E N D E N D E N D E N D E N D
253//-----------------------------------------------------------------------------
254
Note: See TracBrowser for help on using the repository browser.