1 | //-----------------------------------------------------------------------------
|
---|
2 |
|
---|
3 | #include "application.h"
|
---|
4 | #include "usart.h"
|
---|
5 | #include <avr/wdt.h>
|
---|
6 |
|
---|
7 |
|
---|
8 | //-----------------------------------------------------------------------------
|
---|
9 |
|
---|
10 | volatile U08 app_reset_source;
|
---|
11 | //-----------------------------------------------------------------------------
|
---|
12 |
|
---|
13 | void app_init(void) {
|
---|
14 | app_reset_source = MCUSR; // Save last reset source
|
---|
15 | MCUSR = 0x00; // Clear reset source for next reset cycle
|
---|
16 |
|
---|
17 |
|
---|
18 | // Dangerous here: I still do not know much about the watchdog.
|
---|
19 | // This code is still from Udo Juerss.
|
---|
20 |
|
---|
21 | // The watchdog timer is disabled by default ("startup.asm")
|
---|
22 | #ifdef USE_WATCHDOG
|
---|
23 | WDTCSR = WDTOE | (1 << WDE); // Enable watchdog reset (~16ms)
|
---|
24 | #endif
|
---|
25 |
|
---|
26 | // define PORTS
|
---|
27 | // USART
|
---|
28 | DDRD &= ~(1<<PD0); // PD0 = RXD is input
|
---|
29 | DDRD |= 1<<PD1; // PD1 = TXD is output
|
---|
30 |
|
---|
31 |
|
---|
32 | // SPARE OUT/-INPUTS
|
---|
33 | DDRB |= (1<<PB2) | (1<<PB3); // set Out1_spare & out2_spare as outputs
|
---|
34 | DDRA &= ~(1<<PA7); // set In1_spare as input
|
---|
35 | DDRC &= ~(1<<PC7); // set In2_spare as input
|
---|
36 | //PORTA |= (1<<PA7); // swtich on pullup on In1_spare
|
---|
37 | //PORTC |= (1<<PC7); // swtich on pullup on In2_spare
|
---|
38 |
|
---|
39 | // ATmega internal ADC input
|
---|
40 | DDRA &= ~(1<<PA6);
|
---|
41 |
|
---|
42 | // MUXER ADDRESS OUTs
|
---|
43 | DDRA |= 0x3F; // SA-pins -> output
|
---|
44 | DDRC |= 0x7F; // SB-pins -> output
|
---|
45 |
|
---|
46 | // SPI
|
---|
47 | // set all CS's: output
|
---|
48 | DDRB |= (1 << SPI_E_CS);
|
---|
49 | DDRD |= (1 << SPI_AD_CS) |(1 << SPI_M_CS) |(1 << SPI_A_CS);
|
---|
50 |
|
---|
51 | // set all Chips selects HIGH
|
---|
52 | PORTB |= (1 << SPI_E_CS);
|
---|
53 | PORTD |= (1 << SPI_AD_CS) |(1 << SPI_M_CS) |(1 << SPI_A_CS);
|
---|
54 |
|
---|
55 | // set MOSI and SCK: output & // set MISO: input
|
---|
56 | SPI_DDR |= (1 << SPI_MOSI);
|
---|
57 | SPI_DDR |= (1 << SPI_SCLK);
|
---|
58 | SPI_DDR &= ~(1 << SPI_MISO);
|
---|
59 |
|
---|
60 | // set MOSI, SCK: HIGH. MISO leave alone.
|
---|
61 | SPI_PRT |= (1 << SPI_MOSI);
|
---|
62 | SPI_PRT |= (1 << SPI_SCLK);
|
---|
63 | //SPI_PRT |= (1 << SPI_MISO);
|
---|
64 |
|
---|
65 | // ADC
|
---|
66 | DDRD &= ~(1<<PD6); // PD6 is AD_READY input
|
---|
67 | DDRD |= 1<<PD7; // PD7 is AD_RESET output
|
---|
68 |
|
---|
69 | // ACCELEROMETER
|
---|
70 | DDRD &= ~(1<<PD2); // PD2 is ACC_READY input
|
---|
71 |
|
---|
72 | //MAX6662 <--- not assembled
|
---|
73 | // DDRB &= ~(1<<PB0); // PB0 is over temperature alert input
|
---|
74 | // DDRB &= ~(1<<PB1); // PB1 is general temperature altert input
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 |
|
---|
79 | //-----------------------------------------------------------------------------
|
---|
80 |
|
---|
81 | void app_set_watchdog_prescaler(tWDT_PRESCALE wdt_prescale) // Set watchdog prescale
|
---|
82 | {
|
---|
83 | U08 sreg_backup = SREG; // Copy status register to variable
|
---|
84 | U08 wdtcsr_value = WDE + wdt_prescale; // Set new prescale value to variable
|
---|
85 |
|
---|
86 | cli(); // Disable interrups
|
---|
87 | wdt_reset(); // Reset watchdog
|
---|
88 |
|
---|
89 | WDTCR |= (1 << WDTOE) | (1 << WDE); // Unlock register access, 4 cycles to store new value
|
---|
90 | WDTCR = wdtcsr_value; // Set new watchdog prescaler
|
---|
91 | SREG = sreg_backup; // Restore status register
|
---|
92 | }
|
---|
93 |
|
---|
94 | void set_ad7719_enable_register() {
|
---|
95 |
|
---|
96 | usart_write_str((pU08)"\n set enable bits of AD7719 Port ");
|
---|
97 | if ((usart_received_chars>=5) &&
|
---|
98 | (usart_rx_buffer[2] >= 'A' && usart_rx_buffer[2] <= 'H'))
|
---|
99 | {
|
---|
100 | usart_write_char(usart_rx_buffer[2]);
|
---|
101 | usart_write_str((pU08)" to ");
|
---|
102 | usart_write_U08_hex(usart_rx_buffer[4]);
|
---|
103 | usart_write_char('\n');
|
---|
104 | ad7719_enables[usart_rx_buffer[2]-'A']=usart_rx_buffer[4];
|
---|
105 | ad7719_channels_ready[usart_rx_buffer[2]-'A']=0x00;
|
---|
106 | }
|
---|
107 | else if ((usart_received_chars=3) &&
|
---|
108 | (usart_rx_buffer[1] >= 'A' && usart_rx_buffer[1] <= 'H'))
|
---|
109 | {
|
---|
110 | usart_write_char(usart_rx_buffer[1]);
|
---|
111 | if (usart_rx_buffer[2]!='0') {
|
---|
112 | usart_write_str((pU08)" to 0xFF\n");
|
---|
113 | ad7719_enables[usart_rx_buffer[1]-'A']=0xFF;
|
---|
114 | } else
|
---|
115 | {
|
---|
116 | usart_write_str((pU08)" to 0x00\n");
|
---|
117 | ad7719_enables[usart_rx_buffer[1]-'A']=0x00;
|
---|
118 | }
|
---|
119 | ad7719_channels_ready[usart_rx_buffer[1]-'A']=0x00;
|
---|
120 | }
|
---|
121 | else
|
---|
122 | {
|
---|
123 | usart_write_str((pU08)"\n something wrong\n");
|
---|
124 | usart_write_str((pU08)"usart_rx_buffer_index: ");
|
---|
125 | usart_write_U08(usart_received_chars, 3);
|
---|
126 | usart_write_str((pU08)"\n usart_rx_buffer[2]: ");
|
---|
127 | usart_write_char(usart_rx_buffer[2]);
|
---|
128 | usart_write_str((pU08)"\n usart_rx_buffer[4]: ");
|
---|
129 | usart_write_U08_hex(usart_rx_buffer[4]);
|
---|
130 | usart_write_char('\n');
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | void set_adc_enable_register() {
|
---|
135 | // TODO
|
---|
136 | usart_write_str((pU08)"setting of ATmega internal ADC enable registers is not supported. yet.\n");
|
---|
137 | }
|
---|
138 |
|
---|
139 | U08 increase_adc (U08 channel){
|
---|
140 | U08 effective_channel;
|
---|
141 | for ( U08 increase = 1 ; increase <= V_CHANNELS + I_CHANNELS + H_CHANNELS; increase++)
|
---|
142 | {
|
---|
143 | effective_channel = (channel + increase) % (V_CHANNELS + I_CHANNELS + H_CHANNELS);
|
---|
144 | if (adc_enables[effective_channel/8] & (1<<effective_channel%8))
|
---|
145 | return effective_channel;
|
---|
146 | }
|
---|
147 | return channel;
|
---|
148 | } // end if increase_adc;
|
---|
149 |
|
---|
150 | U08 increase_ad7719 (U08 channel){
|
---|
151 | U08 effective_channel;
|
---|
152 | for ( U08 increase = 1 ; increase <= TEMP_CHANNELS; increase++)
|
---|
153 | {
|
---|
154 | effective_channel = (channel + increase) % (TEMP_CHANNELS);
|
---|
155 | if (ad7719_enables[effective_channel/8] & (1<<effective_channel%8))
|
---|
156 | return effective_channel;
|
---|
157 | }
|
---|
158 | return channel;
|
---|
159 | } // end if increase_adc;
|
---|
160 |
|
---|
161 | void check_if_measured_all() {
|
---|
162 | adc_measured_all = true;
|
---|
163 | for ( U08 i=0; i<V_BITMAP + I_BITMAP + H_BITMAP; ++i ) {
|
---|
164 | if ((adc_enables[i] ^ adc_channels_ready[i]) != 0x00) {
|
---|
165 | adc_measured_all = false;
|
---|
166 | break;
|
---|
167 | }
|
---|
168 | }
|
---|
169 | ad7719_measured_all = true;
|
---|
170 | for ( U08 i=0; i<CHANNEL_BITMAP; ++i ) {
|
---|
171 | if ((ad7719_enables[i] ^ ad7719_channels_ready[i]) != 0x00) {
|
---|
172 | ad7719_measured_all = false;
|
---|
173 | break;
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | }
|
---|