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

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