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

Last change on this file since 10698 was 10697, checked in by neise, 14 years ago
File size: 7.4 KB
Line 
1//-----------------------------------------------------------------------------
2
3#include "application.h"
4#include "usart.h"
5#include <avr/wdt.h>
6
7// in order to implement the "registers" I work with a quite long
8// char-array like this:
9
10U08 FSCregister[FSC_REGISTER_LENGTH];
11// but this register is not only accessible by
12// FSCregister[i], but as well by special pointers like this:
13U32 *status = (U32*)&FSCregister[0];
14U32 *time_sec = (U32*)&(FSCregister[4]);
15U16 *time_ms = (U16*)&(FSCregister[6]);
16U16 *FR_period = (U16*)&(FSCregister[8]);
17U16 *ref_resistor = (U16*)&(FSCregister[10]);
18
19
20U32 *ad7719_values = (U32*)&FSCregister[68];
21U08 *ad7719_enables = &FSCregister[32];
22U08 *ad7719_channels_ready = &FSCregister[50];
23U08 *ad7719_readings_since_last_muxing = &FSCregister[14];
24U08 *ad7719_current_channel = &FSCregister[15];
25U32 *ad7719_current_reading = (U32*)&FSCregister[16];
26
27U16 *adc_values = (U16*) &FSCregister[324];
28U08 *adc_enables = &FSCregister[40];
29U08 *adc_channels_ready = &FSCregister[58];
30U08 *adc_readings_since_last_muxing = &FSCregister[20];
31U08 *adc_current_channel = &FSCregister[21];
32U16 *adc_current_reading = (U16*) &FSCregister[22];
33
34
35
36// using these pointers one can manipulate measurement values like this:
37// res_value[3] = 453212;
38// and then readout the most significant byte of this same value by accessing:
39// FSCregister[92];
40// I like this very much for asking the boards status ... this is just a copy of the registers,
41// into the W5100 TX FIFO.
42// submitting the measurement values is just a partial copy...
43
44//-----------------------------------------------------------------------------
45
46//-----------------------------------------------------------------------------
47
48volatile U08 app_reset_source;
49//-----------------------------------------------------------------------------
50
51void app_init(void) {
52 app_reset_source = MCUSR; // Save last reset source
53 MCUSR = 0x00; // Clear reset source for next reset cycle
54
55
56 // Dangerous here: I still do not know much about the watchdog.
57 // This code is still from Udo Juerss.
58
59 // The watchdog timer is disabled by default ("startup.asm")
60 #ifdef USE_WATCHDOG
61 WDTCSR = WDTOE | (1 << WDE); // Enable watchdog reset (~16ms)
62 #endif
63
64 // define PORTS
65 // USART
66 DDRD &= ~(1<<PD0); // PD0 = RXD is input
67 DDRD |= 1<<PD1; // PD1 = TXD is output
68
69
70 // SPARE OUT/-INPUTS
71 DDRB |= (1<<PB2) | (1<<PB3); // set Out1_spare & out2_spare as outputs
72 DDRA &= ~(1<<PA7); // set In1_spare as input
73 DDRC &= ~(1<<PC7); // set In2_spare as input
74 //PORTA |= (1<<PA7); // swtich on pullup on In1_spare
75 //PORTC |= (1<<PC7); // swtich on pullup on In2_spare
76
77 // ATmega internal ADC input
78 DDRA &= ~(1<<PA6);
79
80 // MUXER ADDRESS OUTs
81 DDRA |= 0x3F; // SA-pins -> output
82 DDRC |= 0x7F; // SB-pins -> output
83
84 // SPI
85 // set all CS's: output
86 DDRB |= (1 << SPI_E_CS);
87 DDRD |= (1 << SPI_AD_CS) |(1 << SPI_M_CS) |(1 << SPI_A_CS);
88
89 // set all Chips selects HIGH
90 PORTB |= (1 << SPI_E_CS);
91 PORTD |= (1 << SPI_AD_CS) |(1 << SPI_M_CS) |(1 << SPI_A_CS);
92
93 // set MOSI and SCK: output & // set MISO: input
94 SPI_DDR |= (1 << SPI_MOSI);
95 SPI_DDR |= (1 << SPI_SCLK);
96 SPI_DDR &= ~(1 << SPI_MISO);
97
98 // set MOSI, SCK: HIGH. MISO leave alone.
99 SPI_PRT |= (1 << SPI_MOSI);
100 SPI_PRT |= (1 << SPI_SCLK);
101 //SPI_PRT |= (1 << SPI_MISO);
102
103 // ADC
104 DDRD &= ~(1<<PD6); // PD6 is AD_READY input
105 DDRD |= 1<<PD7; // PD7 is AD_RESET output
106
107 // ACCELEROMETER
108 DDRD &= ~(1<<PD2); // PD2 is ACC_READY input
109
110 //MAX6662 <--- not assembled
111 // DDRB &= ~(1<<PB0); // PB0 is over temperature alert input
112 // DDRB &= ~(1<<PB1); // PB1 is general temperature altert input
113}
114
115
116
117//-----------------------------------------------------------------------------
118
119void app_set_watchdog_prescaler(tWDT_PRESCALE wdt_prescale) // Set watchdog prescale
120{
121 U08 sreg_backup = SREG; // Copy status register to variable
122 U08 wdtcsr_value = WDE + wdt_prescale; // Set new prescale value to variable
123
124 cli(); // Disable interrups
125 wdt_reset(); // Reset watchdog
126
127 WDTCR |= (1 << WDTOE) | (1 << WDE); // Unlock register access, 4 cycles to store new value
128 WDTCR = wdtcsr_value; // Set new watchdog prescaler
129 SREG = sreg_backup; // Restore status register
130}
131
132void set_ad7719_enable_register() {
133
134 usart_write_str((pU08)"\n set enable bits of AD7719 Port ");
135 if ((usart_received_chars>=5) &&
136 (usart_rx_buffer[2] >= 'A' && usart_rx_buffer[2] <= 'H'))
137 {
138 usart_write_char(usart_rx_buffer[2]);
139 usart_write_str((pU08)" to ");
140 usart_write_U08_hex(usart_rx_buffer[4]);
141 usart_write_char('\n');
142 ad7719_enables[usart_rx_buffer[2]-'A']=usart_rx_buffer[4];
143 ad7719_channels_ready[usart_rx_buffer[2]-'A']=0x00;
144 }
145 else if ((usart_received_chars=3) &&
146 (usart_rx_buffer[1] >= 'A' && usart_rx_buffer[1] <= 'H'))
147 {
148 usart_write_char(usart_rx_buffer[1]);
149 if (usart_rx_buffer[2]!='0') {
150 usart_write_str((pU08)" to 0xFF\n");
151 ad7719_enables[usart_rx_buffer[1]-'A']=0xFF;
152 } else
153 {
154 usart_write_str((pU08)" to 0x00\n");
155 ad7719_enables[usart_rx_buffer[1]-'A']=0x00;
156 }
157 ad7719_channels_ready[usart_rx_buffer[1]-'A']=0x00;
158 }
159 else
160 {
161 usart_write_str((pU08)"\n something wrong\n");
162 usart_write_str((pU08)"usart_rx_buffer_index: ");
163 usart_write_U08(usart_received_chars, 3);
164 usart_write_str((pU08)"\n usart_rx_buffer[2]: ");
165 usart_write_char(usart_rx_buffer[2]);
166 usart_write_str((pU08)"\n usart_rx_buffer[4]: ");
167 usart_write_U08_hex(usart_rx_buffer[4]);
168 usart_write_char('\n');
169 }
170}
171
172void set_adc_enable_register() {
173 // TODO
174 usart_write_str((pU08)"setting of ATmega internal ADC enable registers is not supported. yet.\n");
175}
176
177U08 increase_adc (U08 channel){
178U08 effective_channel;
179 for ( U08 increase = 1 ; increase <= VOLTAGE_CHANNELS; increase++)
180 {
181 effective_channel = (channel + increase) % (VOLTAGE_CHANNELS);
182 if (adc_enables[effective_channel/8] & (1<<effective_channel%8))
183 return effective_channel;
184 }
185 return channel;
186} // end if increase_adc;
187
188U08 increase_ad7719 (U08 channel){
189U08 effective_channel;
190 for ( U08 increase = 1 ; increase <= RESISTANCE_CHANNELS; increase++)
191 {
192 effective_channel = (channel + increase) % (RESISTANCE_CHANNELS);
193 if (ad7719_enables[effective_channel/8] & (1<<effective_channel%8))
194 return effective_channel;
195 }
196 return channel;
197} // end if increase_adc;
198
199void check_if_measured_all() {
200 adc_measured_all = true;
201 for ( U08 i=0; i<(VOLTAGE_CHANNELS/8); ++i ) {
202 if ((adc_enables[i] ^ adc_channels_ready[i]) != 0x00) {
203 adc_measured_all = false;
204 break;
205 }
206 }
207 ad7719_measured_all = true;
208 for ( U08 i=0; i<(RESISTANCE_CHANNELS/8); ++i ) {
209 if ((ad7719_enables[i] ^ ad7719_channels_ready[i]) != 0x00) {
210 ad7719_measured_all = false;
211 break;
212 }
213 }
214
215
216}
217
218
219bool check_if_adc_measurement_done(){
220 adc_measured_all = true;
221 for ( U08 i=0; i<VOLTAGE_CHANNELS/8; ++i ) {
222 if ((adc_enables[i] ^ adc_channels_ready[i]) != 0x00) {
223 adc_measured_all = false;
224 break;
225 }
226 }
227 return adc_measured_all;
228}
229
230bool check_if_ad7719_measurement_done(){
231 ad7719_measured_all = true;
232 for ( U08 i=0; i<RESISTANCE_CHANNELS/8; ++i ) {
233 if ((ad7719_enables[i] ^ ad7719_channels_ready[i]) != 0x00) {
234 ad7719_measured_all = false;
235 break;
236 }
237 }
238 return ad7719_measured_all;
239}
Note: See TracBrowser for help on using the repository browser.