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

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