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 <avr/interrupt.h>
|
---|
15 | #include <avr/wdt.h>
|
---|
16 | #include <stdlib.h>
|
---|
17 | //-----------------------------------------------------------------------------
|
---|
18 | // definition of some functions:
|
---|
19 | // these function are implemented in this file, this is not doog coding style.
|
---|
20 | // sooner or later, they will be moved into more apropriate files.
|
---|
21 |
|
---|
22 | void parse(); //doesn't do anything at the moment
|
---|
23 |
|
---|
24 | // end of function definition:
|
---|
25 | //-----------------------------------------------------------------------------
|
---|
26 |
|
---|
27 |
|
---|
28 | // MAIN WORKFLOW GLOBAL VARIABLES
|
---|
29 | bool verbose;
|
---|
30 | bool heartbeat_enable;
|
---|
31 |
|
---|
32 | // USART global variables
|
---|
33 | U08 usart_rx_buffer[USART_RX_BUFFER_SIZE];
|
---|
34 | U08 usart_tx_buffer[USART_TX_BUFFER_SIZE];
|
---|
35 | U08 usart_rx_buffer_index = 0;
|
---|
36 | U08 usart_tx_buffer_index = 0;
|
---|
37 | U08 usart_last_char; // last received char
|
---|
38 |
|
---|
39 | // USART FLAGS
|
---|
40 | bool usart_tx_buffer_overflow = false; // true if usart_tx_buffer was full.
|
---|
41 | //bool usart_rx_ready = false; // EOL was received, parser needs to be called
|
---|
42 |
|
---|
43 | // TIMER global variable
|
---|
44 | volatile U32 local_ms = 0;
|
---|
45 |
|
---|
46 | // AD7719 global variables
|
---|
47 | U32 ad7719_values[TEMP_CHANNELS];
|
---|
48 | U08 ad7719_enables[CHANNEL_BITMAP];
|
---|
49 | U08 ad7719_channels_ready[CHANNEL_BITMAP];
|
---|
50 | U08 ad7719_readings_since_last_muxing = 0;
|
---|
51 | U08 ad7719_current_channel = 0;
|
---|
52 | U32 ad7719_current_reading = 0;
|
---|
53 | bool ad7719_measured_all = false;
|
---|
54 |
|
---|
55 | // ATMEGA ADC global variables
|
---|
56 | U08 adc_values[V_CHANNELS + I_CHANNELS + H_CHANNELS]; // stores measured voltage in steps of 16mV
|
---|
57 | U08 adc_enables[V_BITMAP + I_BITMAP + H_BITMAP];
|
---|
58 | U08 adc_channels_ready[V_BITMAP + I_BITMAP + H_BITMAP];
|
---|
59 | U08 adc_readings_since_last_muxing = 0;
|
---|
60 | U08 adc_current_channel = 0;
|
---|
61 | U08 adc_current_reading = 0;
|
---|
62 | bool adc_measured_all = false;
|
---|
63 |
|
---|
64 | bool once_told_you = true;
|
---|
65 | bool debug_mode = false;
|
---|
66 |
|
---|
67 | #ifndef ___MAIN_WORKFLOW_GLOBAL_VARS
|
---|
68 | #define ___MAIN_WORKFLOW_GLOBAL_VARS
|
---|
69 | #define TEMP_CHANNELS 64
|
---|
70 | #define CHANNEL_BITMAP 8
|
---|
71 | #define AD7719_READINGS_UNTIL_SETTLED 1 // bei3:480ms
|
---|
72 | #define V_CHANNELS 40
|
---|
73 | #define I_CHANNELS 40
|
---|
74 | #define H_CHANNELS 4
|
---|
75 | #define V_BITMAP 5
|
---|
76 | #define I_BITMAP 5
|
---|
77 | #define H_BITMAP 1
|
---|
78 | #define ADC_READINGS_UNTIL_SETTLED 1
|
---|
79 |
|
---|
80 | #endif // ___MAIN_WORKFLOW_GLOBAL_VARS
|
---|
81 |
|
---|
82 |
|
---|
83 | //-----------------------------------------------------------------------------
|
---|
84 | // M A I N --- M A I N --- M A I N --- M A I N --- M A I N
|
---|
85 | //-----------------------------------------------------------------------------
|
---|
86 | int main(void)
|
---|
87 | {
|
---|
88 | U08 register_content;
|
---|
89 |
|
---|
90 | app_init(); // Setup: Watchdog and I/Os
|
---|
91 | usart_init();
|
---|
92 |
|
---|
93 | spi_init(); // Initialize SPI interface as master
|
---|
94 |
|
---|
95 | // TIMER2 is used as local clock:
|
---|
96 | // configure timer 2
|
---|
97 | TCCR2 = (1<<WGM21); // CTC Modus
|
---|
98 | TCCR2 |= (1<<CS21) | (1<<CS20); // Prescaler 64 --> counts up every 8us
|
---|
99 | OCR2 = 125-1; // --> output compare interrupt occurs every 125 x 8us = 1ms
|
---|
100 | // Compare Interrupt erlauben
|
---|
101 | TIMSK |= (1<<OCIE2);
|
---|
102 |
|
---|
103 | // Enable interrupts
|
---|
104 | sei();
|
---|
105 |
|
---|
106 | usart_write_str((pU08)"Start of test firmware (06.05.11) now:\n");
|
---|
107 | if (local_ms % 10000 == 0) {
|
---|
108 | usart_write_str((pU08)"time: ");
|
---|
109 | usart_write_U32(local_ms/1000,6);
|
---|
110 | usart_write_str((pU08)"sec.\n");
|
---|
111 | }
|
---|
112 |
|
---|
113 | PORTB &= ~(1<<PB2); //#reset = LOW --> W5100 is in reset ...
|
---|
114 | _delay_ms(50); //reset
|
---|
115 |
|
---|
116 | PORTB |= 1<<PB2; //#reset = HIGH --> W5100 is active
|
---|
117 | _delay_ms(5); // give it 5ms to accomodate.
|
---|
118 |
|
---|
119 |
|
---|
120 | // DDRB |= (1 << PB2); // setze 5tes bit im data direction register von port B
|
---|
121 | // DDRB |= (1 << PB3); // setze 5tes bit im data direction register von port B
|
---|
122 | // DDRB |= (1 << PB4); // setze 5tes bit im data direction register von port B
|
---|
123 |
|
---|
124 | while (1)
|
---|
125 | {
|
---|
126 | PORTB ^= (1<<PB4); // toggle zustand vom 5ten bit in Port B
|
---|
127 | PORTB ^= (1<<PB3); // toggle zustand vom 5ten bit in Port B
|
---|
128 | PORTB ^= (1<<PB2); // toggle zustand vom 5ten bit in Port B
|
---|
129 |
|
---|
130 | _delay_ms(1); //reset
|
---|
131 |
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | while (1) {
|
---|
136 | usart_write_str((pU08)"Start to read from W5300 CM_RTR0 register at:");
|
---|
137 | usart_write_U32(local_ms,10);
|
---|
138 | usart_write_str((pU08)"ms.\n");
|
---|
139 |
|
---|
140 | register_content = w5100_read(CM_RTR0); // read something
|
---|
141 |
|
---|
142 | usart_write_str((pU08)"register content of CM_RTR0 retrieved at:");
|
---|
143 | usart_write_U32(local_ms,10);
|
---|
144 | usart_write_str((pU08)"ms.\t");
|
---|
145 | usart_write_str((pU08)"register content is: 0x");
|
---|
146 | usart_write_U08_hex(register_content);
|
---|
147 | usart_write_str((pU08)"\n");
|
---|
148 |
|
---|
149 |
|
---|
150 | _delay_ms(400); //reset
|
---|
151 | _delay_ms(400); //reset
|
---|
152 | _delay_ms(400); //reset
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | usart_write_str((pU08)"W5300 init begins at:");
|
---|
157 | usart_write_U32(local_ms,10);
|
---|
158 | usart_write_str((pU08)"ms.\n");
|
---|
159 |
|
---|
160 | w5100_init();
|
---|
161 |
|
---|
162 |
|
---|
163 |
|
---|
164 |
|
---|
165 |
|
---|
166 |
|
---|
167 | } // end of main()
|
---|
168 |
|
---|
169 | /*
|
---|
170 | //MAIN LOOP
|
---|
171 | while (1)
|
---|
172 | {
|
---|
173 | if (heartbeat_enable) PORTB ^= (1<<PB3); // toggle Out2_spare --> heartbeat
|
---|
174 | //----------------------------------------------------------------------------
|
---|
175 | //IF we need to send away one byte, and ready to send
|
---|
176 |
|
---|
177 | if ( (usart_tx_buffer_index > 0) && (UCSRA & (1<<UDRE)) ) {
|
---|
178 | UDR = usart_tx_buffer[0];
|
---|
179 | // THis is shit
|
---|
180 | for (U08 i=0 ; i < USART_TX_BUFFER_SIZE; ++i) {
|
---|
181 | usart_tx_buffer[i] = usart_tx_buffer[i+1];
|
---|
182 | }
|
---|
183 | usart_tx_buffer_index--;
|
---|
184 | }
|
---|
185 | //----------------------------------------------------------------------------
|
---|
186 |
|
---|
187 | //IF we just received one byte, and there is enough space in the RX_buffer
|
---|
188 | if ( (UCSRA & (1<<RXC)) && (usart_rx_buffer_index < USART_RX_BUFFER_SIZE) ){
|
---|
189 | usart_last_char = UDR;
|
---|
190 | if (usart_last_char == '\n'){ // if EOL was received
|
---|
191 | usart_rx_ready = true;
|
---|
192 | }else {
|
---|
193 | usart_rx_buffer[usart_rx_buffer_index] = usart_last_char;
|
---|
194 | usart_rx_buffer_index++;
|
---|
195 | }
|
---|
196 | // here is still something strange .... better send an enter automatically
|
---|
197 | } else if (UCSRA & (1<<RXC)) { // if there is no scace in the buffer; read anyway.
|
---|
198 | usart_last_char = UDR;
|
---|
199 | usart_rx_buffer_index =0;
|
---|
200 | }
|
---|
201 | //----------------------------------------------------------------------------
|
---|
202 |
|
---|
203 | //IF USART DOR bit is set, PC is sending data to fast!!!
|
---|
204 | if ( UCSRA & (1<<DOR) ){
|
---|
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();
|
---|
223 | usart_rx_buffer_index = 0;
|
---|
224 | usart_rx_ready = false;
|
---|
225 | }
|
---|
226 | //----------------------------------------------------------------------------
|
---|
227 |
|
---|
228 | //IF ATmega internal ADC did finish a conversion --every 200us
|
---|
229 | if ( (ADCSRA & (1<<ADIF)) && !adc_measured_all) {
|
---|
230 | adc_current_reading = ADCH;
|
---|
231 | if (adc_readings_since_last_muxing == ADC_READINGS_UNTIL_SETTLED) {
|
---|
232 | adc_values[adc_current_channel] = adc_current_reading;
|
---|
233 | adc_readings_since_last_muxing=0;
|
---|
234 | // note that this channel is ready, now and
|
---|
235 | adc_output(adc_current_channel, adc_current_reading);
|
---|
236 | // proceed to the next enabled channel.
|
---|
237 | adc_channels_ready[adc_current_channel/8] |= (1<<(adc_current_channel%8));
|
---|
238 | adc_current_channel = increase_adc (adc_current_channel);
|
---|
239 | Set_V_Muxer(adc_current_channel);
|
---|
240 | } else { // the ADC did not settle yet, we discard the reading
|
---|
241 | ++adc_readings_since_last_muxing;
|
---|
242 | // current reading is not used for anything else
|
---|
243 | }
|
---|
244 | }
|
---|
245 | //----------------------------------------------------------------------------
|
---|
246 |
|
---|
247 | //IF AD7719 ADC just finished a conversion -- every 60ms
|
---|
248 |
|
---|
249 | if (AD7719_IS_READY()) {
|
---|
250 | ad7719_current_reading = read_adc(); // --takes at 4MHz SCLK speed about 6us
|
---|
251 | // AD7719 is only read out if settled. saves time.
|
---|
252 | if (ad7719_readings_since_last_muxing == AD7719_READINGS_UNTIL_SETTLED) {
|
---|
253 | ad7719_values[ad7719_current_channel] = ad7719_current_reading;
|
---|
254 | ad7719_readings_since_last_muxing=0;
|
---|
255 | // now prepare the data to be send away via USART.
|
---|
256 | //ad7719_output(ad7719_current_channel, ad7719_current_reading);
|
---|
257 | // note that this channel is ready, now and
|
---|
258 | // proceed to the next enabled channel.
|
---|
259 | ad7719_channels_ready[ad7719_current_channel/8] |= (1<<(ad7719_current_channel%8));
|
---|
260 | ad7719_current_channel = increase_ad7719 (ad7719_current_channel);
|
---|
261 | Set_T_Muxer(ad7719_current_channel);
|
---|
262 | } else { // the AD7719 did not settle yet, we discard the reading
|
---|
263 | ++ad7719_readings_since_last_muxing;
|
---|
264 |
|
---|
265 | // current reading is not used for anything else
|
---|
266 | }
|
---|
267 | }
|
---|
268 | //----------------------------------------------------------------------------
|
---|
269 | //IF one of the ADC measured all channels, we wanted to know.
|
---|
270 | check_if_measured_all();
|
---|
271 |
|
---|
272 | if (ad7719_measured_all && adc_measured_all)
|
---|
273 | adc_output_all();
|
---|
274 |
|
---|
275 | //----------------------------------------------------------------------------
|
---|
276 |
|
---|
277 | // if (verbose == true)
|
---|
278 | // talk() was just defined so the
|
---|
279 | // code is not at this place ... look down.
|
---|
280 | // talk();
|
---|
281 |
|
---|
282 |
|
---|
283 | } // end of MAIN LOOP
|
---|
284 | //-----------------------------------------------------------------------------
|
---|
285 | // E N D E N D E N D E N D E N D E N D E N D
|
---|
286 | //-----------------------------------------------------------------------------
|
---|
287 |
|
---|
288 | */
|
---|
289 |
|
---|
290 |
|
---|
291 |
|
---|
292 | ISR (TIMER2_COMP_vect)
|
---|
293 | {
|
---|
294 | ++local_ms;
|
---|
295 | }
|
---|
296 |
|
---|
297 |
|
---|
298 |
|
---|
299 |
|
---|