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

Last change on this file since 11787 was 11667, checked in by neise, 14 years ago
File size: 8.9 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
38//-----------------------------------------------------------------------------
39// M A I N --- M A I N --- M A I N --- M A I N --- M A I N
40//-----------------------------------------------------------------------------
41int main(void)
42{
43 U08 highbyte;
44 U08 lowbyte;
45
46 U16 eth_red_bytes;
47
48 U08 rc;
49
50 app_init(); // Setup: Watchdog and I/Os
51 usart_init();
52 spi_init(); // Initialize SPI interface as master
53 timer_init(); // set up TIMER2: TIMER2_COMP interrupt occurs every 1ms
54 atmega_adc_init();
55
56 w5100_reset();
57 w5100_init();
58
59 ad7719_init();
60
61 // Enable interrupts
62 sei();
63
64
65for ( U08 i=0; i < (RESISTANCE_CHANNELS/8); ++i ) {
66 ad7719_enables[i]=0xFF;
67 ad7719_channels_ready[i]=0;
68 }
69for ( U08 i=0; i < (VOLTAGE_REGS); ++i ) {
70 if (i < (VOLTAGE_REGS)-1)
71 adc_enables[i]=0xFF;
72 else
73 adc_enables[i]=0x0F;
74 adc_channels_ready[i]=0;
75}
76
77 static U08 welcome[]="FSC 0.2 \n build: 05.06.2010\n";
78 usart_write_str(welcome);
79 /*
80 print_help();
81 print_adc_enable_status(true);
82 usart_write_char('\n');
83 print_ad7719_enable_status(true);
84 usart_write_char('\n');
85 usart_write_str((pU08)"time:");
86 usart_write_U32(sec , 7);
87 usart_write_str((pU08)" sec.\n");
88*/
89//MAIN LOOP
90while (1)
91{
92
93 // checks if socket is okay and resets in case of problem the W5100
94 rc=w5100_caretaker();
95 if ( rc != 0x17 && rc != 0x14)
96 {
97 usart_write_str((pU08)"caretaker! error code:");
98 usart_write_U08_hex(rc);
99 usart_write_crlf();
100 // something is not okay with the ethernet ...
101 // will be reset in the next revolution.. but maybe we want to do more...
102 }
103
104 // this if should check every 25ms if we have any data on the W5100
105 if ( (milisec % W5100_INPUT_CHECK_TIME == 0) && (w5100_ready) )
106 {
107 if ( (eth_red_bytes = get_S0_RX_RSR()) != 0) {
108 for (U08 rep=0; rep<1; ) {
109 if (eth_red_bytes == get_S0_RX_RSR() )
110 rep++;
111 else
112 eth_red_bytes =get_S0_RX_RSR();
113 }
114 usart_write_str((pU08)"ethgot:");
115 usart_write_U16(eth_red_bytes, 7);
116 usart_write_crlf();
117 parse_w5300_incoming( w5100_get_RX(ETH_READ_BUFFER_SIZE, true) );
118 }
119
120 }
121
122//----------------------------------------------------------------------------
123
124 if (check_if_adc_measurement_done()) {
125
126 if(adc_print_endless){
127 usart_write_str((pU08)"V|");
128 usart_write_U32(sec ,8);
129 print_adc_nicely(false,true);
130 write_status_via_eth();
131 //print_adc_stupid();
132 reset_voltage_done();
133 reset_voltage_values();
134 //telegram_start();
135 //w5100_set_TX((U08*) adc_values, ADC_VALUES_LEN_BYTE);
136
137 reset_voltage_done();
138 } else
139 if ( !adc_values_printed) {
140 adc_values_printed = true;
141 print_adc_nicely(true,true);
142 write_status_via_eth();
143 //print_adc_stupid();
144 //reset_voltage_done();
145 //telegram_start();
146 //w5100_set_TX((U08*) adc_values, ADC_VALUES_LEN_BYTE);
147
148 }
149 }
150
151 if (check_if_ad7719_measurement_done()) {
152
153 if(ad7719_print_endless){
154
155 print_ad7719_nicely();
156 write_status_via_eth();
157 reset_resistance_done();
158 reset_voltage_done();
159
160 //telegram_start();
161 //w5100_set_TX((U08*) ad7719_values, AD7719_VALUES_LEN_BYTE);
162
163 } else
164 if ( !ad7719_values_printed) {
165 ad7719_values_printed = true;
166 print_ad7719_nicely(true,true);
167 write_status_via_eth();
168 reset_resistance_done();
169 //telegram_start();
170 //w5100_set_TX((U08*) ad7719_values, AD7719_VALUES_LEN_BYTE);
171
172 }
173 }
174
175//----------------------------------------------------------------------------
176
177 // in order to check the user interface on a hardware near level.
178 // user can issue 'heartbeat command'.
179 // then this üpin is toggled with the 'main-while-loop-frequency'
180 // --> nice check for this frequency as well
181 if (heartbeat_enable) {
182 PORTA ^= (1<<HEARTBEATPIN);
183 }
184
185//----------------------------------------------------------------------------
186 //IF we need to send away one byte, and ready to send
187 // apparently depricated
188 if ( (usart_tx_buffer_index > 0) && (UCSRA & (1<<UDRE)) ) {
189 UDR = usart_tx_buffer[0];
190 // THis is shit
191 for (U08 i=0 ; i < USART_TX_BUFFER_SIZE; ++i) {
192 usart_tx_buffer[i] = usart_tx_buffer[i+1];
193 }
194 usart_tx_buffer_index--;
195 }
196
197//----------------------------------------------------------------------------
198
199 //IF USART DOR bit is set, PC is sending data to fast!!!
200 if ( UCSRA & (1<<DOR) ){
201 usart_write_str((pU08)"PC sending to fast!\n");
202 //usart_last_char = UDR;
203 UDR;
204 // flush TX_buffer and write warning message in
205 // maybe even switch off every measurement. ?
206 }
207//----------------------------------------------------------------------------
208
209 //IF TX_BUFFER was overrun.
210 if (usart_tx_buffer_overflow) {
211 // flash TX_buffer and write warning message in
212 // maybe even switch off every measurement. ?
213 //
214 // this should only happen, in verbose mode and with low baudrates.
215 }
216//----------------------------------------------------------------------------
217
218 //IF one command was received.
219 // -It is not allowed to send more than one command between two '\n'
220 if (usart_rx_ready){
221 //parse_ascii();
222 MSR_parser();
223 }
224//----------------------------------------------------------------------------
225
226 //IF ATmega internal ADC did finish a conversion --every 200us
227 if ( (ADCSRA & (1<<ADIF)) && !adc_measured_all) {
228 lowbyte = ADCL;
229 highbyte = ADCH;
230 ADCSRA |= 1<<ADIF;
231 *adc_current_reading = ((U16)highbyte<<8) | ((U16)lowbyte);
232 if (debug_mode) {
233 usart_write_U16_hex(*adc_current_reading);
234 usart_write_crlf();
235 }
236
237 if (*adc_readings_since_last_muxing == ADC_READINGS_UNTIL_SETTLED+adc_readings_until_mean-1) {
238 adc_values[*adc_current_channel] += *adc_current_reading;
239 adc_channels_ready[*adc_current_channel/8] |= (1<<(*adc_current_channel%8));
240 *adc_current_channel = increase_adc (*adc_current_channel);
241 adc_values[*adc_current_channel] = 0;
242 Set_V_Muxer(*adc_current_channel);
243 *adc_readings_since_last_muxing = 0;
244 _delay_ms(10); // this is a muxer delay
245 }
246 else if (adc_readings_since_last_muxing >= ADC_READINGS_UNTIL_SETTLED-1) {
247 adc_values[*adc_current_channel] += *adc_current_reading;
248 if (debug_mode) {
249 usart_write_str((pU08)"ch:");
250 usart_write_U08(*adc_current_channel,4);
251 usart_write_str((pU08)" v:");
252 usart_write_U16_hex(*adc_current_reading);
253 usart_write_str((pU08)" sum:");
254 usart_write_U16(adc_values[*adc_current_channel],8);
255 usart_write_crlf();
256 }
257
258 ++(*adc_readings_since_last_muxing);
259 }
260 else {
261 ++(*adc_readings_since_last_muxing);
262 }
263
264 }
265
266//----------------------------------------------------------------------------
267
268 //IF AD7719 ADC just finished a conversion -- every 60ms
269
270 if (AD7719_IS_READY() && !ad7719_measured_all) {
271 *ad7719_current_reading = read_adc(); // --takes at 4MHz SCLK speed about 6us
272 // AD7719 is only read out if settled. saves time.
273 if (*ad7719_readings_since_last_muxing == AD7719_READINGS_UNTIL_SETTLED-1) {
274 ad7719_values[*ad7719_current_channel] = *ad7719_current_reading;
275 *ad7719_readings_since_last_muxing=0;
276 if (debug_mode) {
277 usart_write_U32_hex(*ad7719_current_reading);
278 usart_write_char('\n');
279 usart_write_char('\n');
280 }
281 // now prepare the data to be send away via USART.
282
283 // note that this channel is ready, now and
284 // proceed to the next enabled channel.
285 ad7719_channels_ready[*ad7719_current_channel/8] |= (1<<(*ad7719_current_channel%8));
286 *ad7719_current_channel = increase_ad7719 (*ad7719_current_channel);
287 Set_T_Muxer(*ad7719_current_channel);
288 } else { // the AD7719 did not settle yet, we discard the reading
289 ++(*ad7719_readings_since_last_muxing);
290 if (debug_mode) {
291 usart_write_U32_hex(*ad7719_current_reading);
292 usart_write_char('\n');
293 }
294
295 // current reading is not used for anything else
296 }
297 }
298
299
300} // end of MAIN LOOP
301
302} // end of main function
303//-----------------------------------------------------------------------------
304// E N D E N D E N D E N D E N D E N D E N D
305//-----------------------------------------------------------------------------
306
Note: See TracBrowser for help on using the repository browser.