source: firmware/FSC/src/FSC_neu.c@ 15707

Last change on this file since 15707 was 10667, checked in by neise, 13 years ago
File size: 10.8 KB
Line 
1//-----------------------------------------------------------------------------
2#include "typedefs.h"
3#include "muxer_fsc.h"
4#include "parser.h"
5#include "application.h"
6#include "output.h"
7#include "spi_master.h"
8#include "ad7719_adc.h"
9#include "atmega_adc.h"
10#include "usart.h"
11#include "macros.h"
12#include "interpol.h"
13#include "w5100_spi_interface.h"
14#include <avr/interrupt.h>
15#include <avr/wdt.h>
16#include <stdlib.h>
17//-----------------------------------------------------------------------------
18
19// MAIN WORKFLOW GLOBAL VARIABLES
20 bool heartbeat_enable = true;
21 bool human_readable_interface_active = true;
22
23
24// USART global variables
25 U08 usart_rx_buffer[USART_RX_BUFFER_SIZE];
26 U08 usart_tx_buffer[USART_TX_BUFFER_SIZE];
27 U08 usart_received_chars;
28 U08 usart_rx_buffer_index = 0;
29 U08 usart_tx_buffer_index = 0;
30 U08 usart_last_char; // last received char
31
32// USART FLAGS
33 bool usart_tx_buffer_overflow = false; // true if usart_tx_buffer was full.
34 bool usart_rx_ready = false; // EOL was received, parser needs to be called
35
36// TIMER global variable
37 volatile U32 local_ms = 0;
38
39// AD7719 global variables
40 U32 ad7719_values[TEMP_CHANNELS];
41 U08 ad7719_enables[CHANNEL_BITMAP];
42 U08 ad7719_channels_ready[CHANNEL_BITMAP];
43 U08 ad7719_readings_since_last_muxing = 0;
44 U08 ad7719_current_channel = 0;
45 U32 ad7719_current_reading = 0;
46 bool ad7719_measured_all = false;
47
48// ATMEGA ADC global variables
49 U08 adc_values[V_CHANNELS + I_CHANNELS + H_CHANNELS]; // stores measured voltage in steps of 16mV
50 U08 adc_enables[V_BITMAP + I_BITMAP + H_BITMAP];
51 U08 adc_channels_ready[V_BITMAP + I_BITMAP + H_BITMAP];
52 U08 adc_readings_since_last_muxing = 0;
53 U08 adc_current_channel = 0;
54 U08 adc_current_reading = 0;
55 bool adc_measured_all = false;
56
57 bool once_told_you = true;
58 bool debug_mode = false;
59
60//-----------------------------------------------------------------------------
61// M A I N --- M A I N --- M A I N --- M A I N --- M A I N
62//-----------------------------------------------------------------------------
63int main(void)
64{
65 app_init(); // Setup: Watchdog and I/Os
66 usart_init(); // Initialize serial interface
67 spi_init(); // Initialize SPI interface as master
68 ad7719_init(); // Initialize AD7719 ADC as SPI slave
69 atmega_adc_init();
70
71// TIMER2 is used as local clock:
72// configure timer 2
73 TCCR2 = (1<<WGM21); // CTC Modus
74 TCCR2 |= (1<<CS21) | (1<<CS20); // Prescaler 64 --> counts up every 8us
75 OCR2 = 125-1; // --> output compare interrupt occurs every 125 x 8us = 1ms
76 // Compare Interrupt erlauben
77 TIMSK |= (1<<OCIE2);
78
79 // Enable interrupts
80 sei();
81
82 PORTB &= ~(1<<PB2); // PULL PB2 low --> this is #RST of WIZ812MJ --> WIZ812MJ is inactive.
83
84 static U08 welcome[]="\n\nwelcome to FACT FSC commandline interface v0.5\nready?";
85 usart_write_str(welcome);
86
87
88
89for ( U08 i=0; i<CHANNEL_BITMAP; ++i ) {
90 ad7719_enables[i]=0x00;
91 ad7719_channels_ready[i]=0;
92 }
93 ad7719_enables[0]=0x08;
94for ( U08 i=0; i<V_BITMAP + I_BITMAP; ++i ) {
95 adc_enables[i]=0xFF;
96 adc_channels_ready[i]=0;
97}
98 adc_enables[V_BITMAP + I_BITMAP + H_BITMAP -1] = 0xF0;
99 adc_channels_ready[V_BITMAP + I_BITMAP + H_BITMAP -1]=0;
100
101
102
103//MAIN LOOP
104while (1)
105{
106 if (heartbeat_enable) PORTB ^= (1<<PB3); // toggle Out2_spare --> heartbeat
107//----------------------------------------------------------------------------
108 //IF we need to send away one byte, and ready to send
109 // THIS IS NOT USED AT THE MOMENT
110 if ( (usart_tx_buffer_index > 0) && (UCSRA & (1<<UDRE)) ) {
111 UDR = usart_tx_buffer[0];
112 // THis is shit
113 for (U08 i=0 ; i < USART_TX_BUFFER_SIZE; ++i) {
114 usart_tx_buffer[i] = usart_tx_buffer[i+1];
115 }
116 usart_tx_buffer_index--;
117 }
118//----------------------------------------------------------------------------
119
120 //IF we just received one byte, and there is enough space in the RX_buffer
121 if ( (UCSRA & (1<<RXC)) && (usart_rx_buffer_index < USART_RX_BUFFER_SIZE) ){
122 usart_last_char = UDR;
123 if (usart_last_char == '\n'){ // if EOL was received
124 usart_rx_ready = true;
125 }else {
126 usart_rx_buffer[usart_rx_buffer_index] = usart_last_char;
127 usart_rx_buffer_index++;
128 }
129 // here is still something strange .... better send an enter automatically
130 } else if (UCSRA & (1<<RXC)) { // if there is no space in the buffer; read anyway.
131 usart_last_char = UDR;
132 usart_rx_buffer_index =0;
133 }
134//----------------------------------------------------------------------------
135
136 //IF USART DOR bit is set, PC is sending data to fast!!!
137 if ( UCSRA & (1<<DOR) ){
138 // flush TX_buffer and write warning message in
139 // maybe even switch off every measurement. ?
140 }
141//----------------------------------------------------------------------------
142
143 //IF TX_BUFFER was overrun.
144 if (usart_tx_buffer_overflow) {
145
146 // TX BUFFER is not used in this firmware version.
147
148 // flash TX_buffer and write warning message in
149 // maybe even switch off every measurement. ?
150 //
151 // this should only happen, in verbose mode and with low baudrates.
152 }
153//----------------------------------------------------------------------------
154
155 //IF one command was received.
156 // -It is not allowed to send more than one command between two '\n'
157 if (usart_rx_ready){
158 parse_ascii();
159 usart_rx_buffer_index = 0;
160 usart_rx_ready = false;
161 }
162//----------------------------------------------------------------------------
163
164 //IF ATmega internal ADC did finish a conversion --every 200us
165 if ( (ADCSRA & (1<<ADIF)) && !adc_measured_all) {
166 adc_current_reading = ADCH;
167 if (adc_readings_since_last_muxing == ADC_READINGS_UNTIL_SETTLED) {
168 adc_values[adc_current_channel] = adc_current_reading;
169 adc_readings_since_last_muxing=0;
170 // note that this channel is ready, now and
171 //adc_output(adc_current_channel, adc_current_reading);
172 // proceed to the next enabled channel.
173 adc_channels_ready[adc_current_channel/8] |= (1<<(adc_current_channel%8));
174 adc_current_channel = increase_adc (adc_current_channel);
175 Set_V_Muxer(adc_current_channel);
176 _delay_ms(10);
177 } else { // the ADC did not settle yet, we discard the reading
178 ++adc_readings_since_last_muxing;
179 // current reading is not used for anything else
180 }
181 }
182//----------------------------------------------------------------------------
183
184 //IF AD7719 ADC just finished a conversion -- every 60ms
185
186 if (AD7719_IS_READY() && !ad7719_measured_all) {
187 ad7719_current_reading = read_adc(); // --takes at 4MHz SCLK speed about 6us
188 // AD7719 is only read out if settled. saves time.
189 if (ad7719_readings_since_last_muxing == AD7719_READINGS_UNTIL_SETTLED) {
190 ad7719_values[ad7719_current_channel] = ad7719_current_reading;
191 ad7719_readings_since_last_muxing=0;
192 if (debug_mode) {
193 usart_write_U32_hex(ad7719_current_reading);
194 usart_write_char('\n');
195 usart_write_char('\n');
196 }
197 // now prepare the data to be send away via USART.
198 //ad7719_output(ad7719_current_channel, ad7719_current_reading);
199 // note that this channel is ready, now and
200 // proceed to the next enabled channel.
201 ad7719_channels_ready[ad7719_current_channel/8] |= (1<<(ad7719_current_channel%8));
202 ad7719_current_channel = increase_ad7719 (ad7719_current_channel);
203 Set_T_Muxer(ad7719_current_channel);
204 } else { // the AD7719 did not settle yet, we discard the reading
205 ++ad7719_readings_since_last_muxing;
206 if (debug_mode) {
207 usart_write_U32_hex(ad7719_current_reading);
208 usart_write_char('\n');
209 }
210
211 // current reading is not used for anything else
212 }
213 }
214//----------------------------------------------------------------------------
215 //IF one of the ADC measured all channels, we wanted to know.
216 check_what_measurement_was_finished();
217
218 if (ad7719_measured_all && adc_measured_all && !once_told_you)
219 {
220 adc_output_all();
221 once_told_you = true;
222 }
223//----------------------------------------------------------------------------
224
225} // end of MAIN LOOP
226//-----------------------------------------------------------------------------
227// E N D E N D E N D E N D E N D E N D E N D
228//-----------------------------------------------------------------------------
229} // end of main() function
230
231
232ISR (TIMER2_COMP_vect)
233{
234 ++local_ms;
235}
236
237
238U08 increase_adc (U08 channel){
239U08 effective_channel;
240 for ( U08 increase = 1 ; increase <= V_CHANNELS + I_CHANNELS + H_CHANNELS; increase++)
241 {
242 effective_channel = (channel + increase) % (V_CHANNELS + I_CHANNELS + H_CHANNELS);
243 if (adc_enables[effective_channel/8] & (1<<effective_channel%8))
244 return effective_channel;
245 }
246 return channel;
247} // end if increase_adc;
248
249U08 increase_ad7719 (U08 channel){
250U08 effective_channel;
251 for ( U08 increase = 1 ; increase <= TEMP_CHANNELS; increase++)
252 {
253 effective_channel = (channel + increase) % (TEMP_CHANNELS);
254 if (ad7719_enables[effective_channel/8] & (1<<effective_channel%8))
255 return effective_channel;
256 }
257 return channel;
258} // end if increase_adc;
259
260void check_what_measurement_was_finished() {
261 adc_measured_all = true;
262 for ( U08 i=0; i<V_BITMAP + I_BITMAP + H_BITMAP; ++i ) {
263 if ((adc_enables[i] ^ adc_channels_ready[i]) != 0x00) {
264 adc_measured_all = false;
265 break;
266 }
267 }
268 ad7719_measured_all = true;
269 for ( U08 i=0; i<CHANNEL_BITMAP; ++i ) {
270 if ((ad7719_enables[i] ^ ad7719_channels_ready[i]) != 0x00) {
271 ad7719_measured_all = false;
272 break;
273 }
274 }
275
276
277}
278
279
280
281void set_ad7719_enable_register() {
282
283 usart_write_str((pU08)"\n set enable bits of AD7719 Port ");
284 if ((usart_rx_buffer_index>=5) &&
285 (usart_rx_buffer[2] >= 'A' && usart_rx_buffer[2] <= 'H'))
286 {
287 usart_write_char(usart_rx_buffer[2]);
288 usart_write_str((pU08)" to ");
289 usart_write_U08_hex(usart_rx_buffer[4]);
290 usart_write_char('\n');
291 ad7719_enables[usart_rx_buffer[2]-'A']=usart_rx_buffer[4];
292 ad7719_channels_ready[usart_rx_buffer[2]-'A']=0x00;
293 }
294 else if ((usart_rx_buffer_index=3) &&
295 (usart_rx_buffer[1] >= 'A' && usart_rx_buffer[1] <= 'H'))
296 {
297 usart_write_char(usart_rx_buffer[1]);
298 if (usart_rx_buffer[2]!='0') {
299 usart_write_str((pU08)" to 0xFF\n");
300 ad7719_enables[usart_rx_buffer[1]-'A']=0xFF;
301 } else
302 {
303 usart_write_str((pU08)" to 0x00\n");
304 ad7719_enables[usart_rx_buffer[1]-'A']=0x00;
305 }
306 ad7719_channels_ready[usart_rx_buffer[1]-'A']=0x00;
307 }
308 else
309 {
310 usart_write_str((pU08)"\n something wrong\n");
311 usart_write_str((pU08)"usart_rx_buffer_index: ");
312 usart_write_U08(usart_rx_buffer_index, 3);
313 usart_write_str((pU08)"\n usart_rx_buffer[2]: ");
314 usart_write_char(usart_rx_buffer[2]);
315 usart_write_str((pU08)"\n usart_rx_buffer[4]: ");
316 usart_write_U08_hex(usart_rx_buffer[4]);
317 usart_write_char('\n');
318 }
319}
320
321void set_adc_enable_register() {
322 // TODO
323 usart_write_str((pU08)"setting of ATmega internal ADC enable registers is not supported. yet.\n");
324}
Note: See TracBrowser for help on using the repository browser.