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