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

Last change on this file since 10698 was 10698, 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 // checks if socket is okay and resets in case of problem the W5100
85 if (w5100_caretaker())
86 {
87 // something is not okay with the ethernet ...
88 // will be reset in the next revolution.. but maybe we want to do more...
89 }
90
91 // this if should check every 25ms if we have any data on the W5100
92 if ( (milisec % W5100_INPUT_CHECK_TIME == 0) && (w5100_ready) )
93 {
94 if (get_S0_RX_RSR() != 0) { // we have something to read
95 downloaded_bytes = w5100_get_RX(ETH_READ_BUFFER_SIZE, true);
96 parse_w5300_incoming( downloaded_bytes );
97 }
98 }
99
100//----------------------------------------------------------------------------
101
102 if (check_if_adc_measurement_done()) {
103
104 if(adc_print_endless){
105 usart_write_str((pU08)"V|");
106 usart_write_U32(sec ,8);
107 print_adc_nicely(false,true);
108 telegram_start();
109 w5100_set_TX((U08*) adc_values, ADC_VALUES_LEN_BYTE);
110
111 reset_voltage_done();
112 } else
113 if ( !adc_values_printed) {
114 adc_values_printed = true;
115 print_adc_nicely(true,true);
116 telegram_start();
117 w5100_set_TX((U08*) adc_values, ADC_VALUES_LEN_BYTE);
118
119 }
120 }
121
122 if (check_if_ad7719_measurement_done()) {
123
124 if(ad7719_print_endless){
125 usart_write_str((pU08)"R|");
126 usart_write_U32(sec ,8);
127 print_ad7719_nicely(false,true);
128 reset_resistance_done();
129 telegram_start();
130 w5100_set_TX((U08*) ad7719_values, AD7719_VALUES_LEN_BYTE);
131
132 } else
133 if ( !ad7719_values_printed) {
134 ad7719_values_printed = true;
135 print_ad7719_nicely(true,true);
136 telegram_start();
137 w5100_set_TX((U08*) ad7719_values, AD7719_VALUES_LEN_BYTE);
138
139 }
140 }
141
142//----------------------------------------------------------------------------
143
144 // in order to check the user interface on a hardware near level.
145 // user can issue 'heartbeat command'.
146 // then this üpin is toggled with the 'main-while-loop-frequency'
147 // --> nice check for this frequency as well
148 if (heartbeat_enable) {
149 PORTA ^= (1<<HEARTBEATPIN);
150 }
151
152//----------------------------------------------------------------------------
153 //IF we need to send away one byte, and ready to send
154 // apparently depricated
155 if ( (usart_tx_buffer_index > 0) && (UCSRA & (1<<UDRE)) ) {
156 UDR = usart_tx_buffer[0];
157 // THis is shit
158 for (U08 i=0 ; i < USART_TX_BUFFER_SIZE; ++i) {
159 usart_tx_buffer[i] = usart_tx_buffer[i+1];
160 }
161 usart_tx_buffer_index--;
162 }
163
164//----------------------------------------------------------------------------
165
166 //IF USART DOR bit is set, PC is sending data to fast!!!
167 if ( UCSRA & (1<<DOR) ){
168 usart_write_str((pU08)"PC sending to fast!\n");
169 //usart_last_char = UDR;
170 UDR;
171 // flush TX_buffer and write warning message in
172 // maybe even switch off every measurement. ?
173 }
174//----------------------------------------------------------------------------
175
176 //IF TX_BUFFER was overrun.
177 if (usart_tx_buffer_overflow) {
178 // flash TX_buffer and write warning message in
179 // maybe even switch off every measurement. ?
180 //
181 // this should only happen, in verbose mode and with low baudrates.
182 }
183//----------------------------------------------------------------------------
184
185 //IF one command was received.
186 // -It is not allowed to send more than one command between two '\n'
187 if (usart_rx_ready){
188 void MSR_parser();
189 }
190//----------------------------------------------------------------------------
191
192 //IF ATmega internal ADC did finish a conversion --every 200us
193 if ( (ADCSRA & (1<<ADIF)) && !adc_measured_all) {
194 *adc_current_reading = (U16)ADCL;
195 *adc_current_reading += (U16)ADCH<<8;
196 if (*adc_readings_since_last_muxing == ADC_READINGS_UNTIL_SETTLED+adc_readings_until_mean-1) {
197 adc_channels_ready[*adc_current_channel/8] |= (1<<(*adc_current_channel%8));
198 *adc_current_channel = increase_adc (*adc_current_channel);
199 adc_values[*adc_current_channel] = 0;
200 Set_V_Muxer(*adc_current_channel);
201 *adc_readings_since_last_muxing = 0;
202 _delay_ms(10);
203 }
204 else if (adc_readings_since_last_muxing >= ADC_READINGS_UNTIL_SETTLED-1) {
205 adc_values[*adc_current_channel] += *adc_current_reading;
206 ++(*adc_readings_since_last_muxing);
207 }
208 else {
209 ++(*adc_readings_since_last_muxing);
210 }
211
212 }
213//----------------------------------------------------------------------------
214
215 //IF AD7719 ADC just finished a conversion -- every 60ms
216
217 if (AD7719_IS_READY() && !ad7719_measured_all) {
218 *ad7719_current_reading = read_adc(); // --takes at 4MHz SCLK speed about 6us
219 // AD7719 is only read out if settled. saves time.
220 if (*ad7719_readings_since_last_muxing == AD7719_READINGS_UNTIL_SETTLED-1) {
221 ad7719_values[*ad7719_current_channel] = *ad7719_current_reading;
222 *ad7719_readings_since_last_muxing=0;
223 if (debug_mode) {
224 usart_write_U32_hex(*ad7719_current_reading);
225 usart_write_char('\n');
226 usart_write_char('\n');
227 }
228 // now prepare the data to be send away via USART.
229
230 // note that this channel is ready, now and
231 // proceed to the next enabled channel.
232 ad7719_channels_ready[*ad7719_current_channel/8] |= (1<<(*ad7719_current_channel%8));
233 *ad7719_current_channel = increase_ad7719 (*ad7719_current_channel);
234 Set_T_Muxer(*ad7719_current_channel);
235 } else { // the AD7719 did not settle yet, we discard the reading
236 ++(*ad7719_readings_since_last_muxing);
237 if (debug_mode) {
238 usart_write_U32_hex(*ad7719_current_reading);
239 usart_write_char('\n');
240 }
241
242 // current reading is not used for anything else
243 }
244 }
245
246
247} // end of MAIN LOOP
248
249} // end of main function
250//-----------------------------------------------------------------------------
251// E N D E N D E N D E N D E N D E N D E N D
252//-----------------------------------------------------------------------------
253
Note: See TracBrowser for help on using the repository browser.