-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.c
369 lines (344 loc) · 8 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* main.c
*
* Created: 18.08.2014 16:45:53
* Author: Hubert
*/
#include <avr/io.h>
#include <stdbool.h>
#include <util/delay.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <string.h>
#include "main.h"
#include "adc.h"
#include "spi.h"
#include "uart.h"
#include "buttons.h"
#include "rotary.h"
#define WAIT 250
extern volatile buttons_t buttons, buttons_old, buttons_vold;
volatile bool mfd_active = false;
volatile uint16_t pi_shutdown_count = 0;
volatile uint16_t pi_start_count = 0;
volatile uint8_t data_3lb[20];
volatile bool status = false;
uint8_t count_3lb = 0;
volatile uint32_t no_amp_timeout;
volatile uint32_t spi_timeout;
volatile uint16_t mp_cnt = 0;
volatile bool buttons_active = false;
volatile uint8_t buttons_active_count = 0;
volatile int32_t ZV_count;
volatile bool ready_3lb = false;
volatile uint8_t count_3lb_max = 0xFF;
volatile bool shutdown_irq = false;
volatile bool dont_start = false;
void timer1_init(void){
TCCR1B |= (1<<WGM12) | (1<<CS11) | (1<<CS10); // CTC, XTAL / 64
OCR1A = (uint8_t)(XTAL / 64.0 * 1e-3 - 0.5); // 1ms
TIMSK |= 1<<OCIE1A;
}
void pi_shutdown_init(void){
PISTARTPDDR |= (1<<PISTART);
PIDDR |= (1<<PISHUTDOWN);
pi_shutdown_count = 0;
}
void pi_shutdown_task(void){
if(!(PISTARTPORT & (1<<PISTART))) return; // pi ausgeschaltet? dann zurück!
//pi herunterfahren
if(PIPORT & (1<<PISHUTDOWN)){
// pi hat shutdown signal bekommen: ist er schon aus?
if(PIPIN & (1<<PIACTIVE)){
//noch an... da machen wir nix.
return;
}
//hier isser schon aus.
PIPORT &= ~(1<<PISHUTDOWN);
_delay_ms(10000);//sicherheit
PISTARTPORT &= ~(1<<PISTART);// und abschalten
_delay_ms(10000);//sicherheit
}else{
// pi ist an - soll jetzt abgeschaltet werden
if(!(PIPIN & (1<<PIREADY))){
uint8_t x = 0;
do{
USART_Transmit(0xFF); // MFD ausgeschaltet. Der Pi möge seine Arbeit einstellen.
_delay_ms(200);
x++;
if(x>50) break;
}while(PIPIN & (1<<PIREADY));
}
PIPORT |= (1<<PISHUTDOWN);
}
}
void pi_cooling_init(){
//timer2 init
ASSR = 0x00;
TCNT2 = 0x01;
OCR2 = 0xFF;
//TCCR2 = 0x00; // Einfacher Timer
TCCR2 |= (1<<WGM20) | (1<<COM21); // phase-correct, non-inverted PWM
TCCR2 |= (1<<CS21);
}
void pi_cooling_task(void){
uint16_t temperature = read_adc(PI_TEMP);
if(temperature > TEMP_HIGH){
PWM = PWM_HIGH;
return;
}
if(temperature > TEMP_MED){
PWM = PWM_MED;
return;
}
if(temperature >TEMP_LOW){
PWM = PWM_LOW;
return;
}
PWM = PWM_OFF;
}
void init_3lb(void){
spi_init(false,1,0);
}
void start_pi(void){
if(dont_start) return;
if(!(PISTARTPORT & (1<<PISTART))){
cli();
int i = 0;
PIPORT &= ~(1<<PISHUTDOWN);
_delay_ms(100);
PISTARTPORT |= (1<<PISTART);
for(;i<30;i++){
_delay_ms(1000);
}
sei();
}
}
void uart_task(){
//*
if(buttons_old._1) USART_Transmit('1');
if(buttons_old._2) USART_Transmit('2');
if(buttons_old._3) USART_Transmit('3');
if(buttons_old._4) USART_Transmit('4');
if(buttons_old._5) USART_Transmit('5');
if(buttons_old._6) USART_Transmit('6');
if(buttons_old.next) USART_Transmit('n');
if(buttons_old.prev) USART_Transmit('p');
if(buttons_old.eject) USART_Transmit('e');
if(buttons_old.tim) USART_Transmit('t');
if(buttons_old.info) USART_Transmit('i');
if(buttons_old.as) USART_Transmit('a');
if(buttons_old.scan) USART_Transmit('s');
if(buttons_old.flag) USART_Transmit('f');
if(buttons_old.light) USART_Transmit('l');
if(buttons_old.navi) USART_Transmit('N');
if(buttons_old.back) USART_Transmit('b');
if(buttons_old.audio) USART_Transmit('A');
if(buttons_old.tone) USART_Transmit('T');
if(buttons_old.left_right){
int8_t c = buttons_old.left_right;
if(c > 0){
//while(c--){
USART_Transmit('+');
_delay_us(WAIT);
USART_Transmit(c);
_delay_us(WAIT);
USART_Transmit('0');
//}
}else{
//while(c++){
c *= -1;
USART_Transmit('-');
_delay_us(WAIT);
USART_Transmit(c);
_delay_us(WAIT);
USART_Transmit('0');
//}
}
}
if(buttons_old.enter) USART_Transmit('E');
if(buttons_old.traffic){
if(PIPIN & (1<<PIACTIVE)){
PIPORT |= (1<<PISHUTDOWN);
_delay_ms(1000);
PIPORT &= ~(1<<PISHUTDOWN);
dont_start = true;
}else{
PISTARTPORT &= ~(1<<PISTART);
_delay_ms(1000);
dont_start = false;
}
}
if(buttons.next == false && buttons.prev == false && buttons.eject == false && buttons.tim == false && buttons.info == false && buttons.as == false && buttons.scan == false && buttons._1 == false && buttons._2 == false && buttons._3 == false && buttons.flag == false && buttons.light == false && buttons.navi == false && buttons.back == false && buttons.audio == false && buttons.tone == false && buttons.left_right == 0 && buttons.enter == false && buttons._4 == false && buttons._5 == false && buttons._6 == false && buttons.traffic == false){
USART_Transmit('0');
}
buttons_old = buttons;
//*/
buttons_init();
}
uint8_t aux_check(void){
// 01234567 89012345
// [ AUX ]
// [ AUX TP]
// [ AUX INFO TP]
//return AUX;
//if(ready_3lb){
if(strncmp( (char*) &data_3lb[1], " AUX ", 8) == 0){
if(strncmp( (char*) &data_3lb[9], "INFO TP", 8) == 0){
return AUX_INFO;
}else{
return AUX;
}
}else{
spi_init(false, 0,0);
return NOT_AUX;
}
//}
return AUX_NR; // not ready
}
int main(void){
adc_init();
init_3lb();
timer1_init();
buttons_init();
pi_shutdown_init();
set_sleep_mode(SLEEP_MODE_IDLE);
ZV_count = 10;
no_amp_timeout = 0;
USART_Init(MYUBRR);
sei();
while(1){
//*
if(buttons_active){
if((AMP_PIN & (1<<AMP_ON))){
no_amp_timeout = 0;
start_pi();
switch(aux_check()){
case AUX:{
buttons_task();
uart_task();
break;
}
case NOT_AUX:{
USART_Transmit(0x01);
_delay_ms(250);
uint8_t i;
for(i=1; i<17; i++){
USART_Transmit(data_3lb[i]);
_delay_ms(25);
}
USART_Transmit(0x00);
_delay_ms(250);
break;
}
case AUX_INFO:{
USART_Transmit(0x01);
_delay_ms(250);
break;
}
}
mfd_active = true;
}else{
// pi_shutdown_task();
//*
if(ZV_PIN & (1<<ZV_ZU)){
pi_shutdown_task();
}else{
if(no_amp_timeout > TIMEOUT_10MIN){
pi_shutdown_task();
no_amp_timeout = TIMEOUT_10MIN + 1;
}else{
start_pi();
USART_Transmit(0x02);
_delay_ms(250);
}
}
//*/
}
buttons_active = false;
}
if(!(PISTARTPORT & (1<<PISTART))){
sleep_mode();
buttons_active = true;
}
//*/
}
return 0;
}
ISR(TIMER1_COMPA_vect){// 1ms Timer
pi_start_count++;
pi_shutdown_count++;
mp_cnt++;
if(mp_cnt == 100){
no_amp_timeout++;
mp_cnt = 0;
buttons_active = true;
}
}
ISR(SPI_STC_vect){
//*
uint8_t data = 0xFF-SPDR;
SPSR &= ~(1<<SPIF);
if(data == (0x81) || data == 0xC1){// data == 0x56 || data == 0x53 für navi frames
uint8_t i = 0;
_delay_us(150);
PORT_3LB |= (1<<EN);
DDR_3LB |= (1<<EN);
SPSR &= ~(1<<SPIF);
uint16_t timeout_us = 500;
while(!(SPSR & (1<<SPIF)) && timeout_us > 0){
_delay_us(10);
timeout_us -= 20;
}
data = 0xFF-SPDR;
_delay_us(20);
DDR_3LB &= ~(1<<EN);
PORT_3LB &= ~(1<<EN);
while(data--){
_delay_us(150);
PORT_3LB |= (1<<EN);
DDR_3LB |= (1<<EN);
SPSR &= ~(1<<SPIF);
timeout_us = 500;
while(!(SPSR & (1<<SPIF)) && timeout_us > 0){
_delay_us(10);
timeout_us -= 20;
}
data_3lb[i++] = 0xFF-SPDR;
_delay_us(20);
DDR_3LB &= ~(1<<EN);
PORT_3LB &= ~(1<<EN);
}
}else if(data == 0xC3){
if(data_3lb[1] == 0){
PORT_3LB |= (1<<EN);
DDR_3LB |= (1<<EN);
_delay_us(3500);
DDR_3LB &= ~(1<<EN);
PORT_3LB &= ~(1<<EN);
_delay_ms(2);
PORT_3LB |= (1<<EN);
DDR_3LB |= (1<<EN);
_delay_us(3500);
DDR_3LB &= ~(1<<EN);
PORT_3LB &= ~(1<<EN);
}
ready_3lb = true;
}else{
ready_3lb = false;
}
//*/
DDR_3LB &= ~(1<<EN);
PORT_3LB &= ~(1<<EN);
}
//ISR( TIMER0_COMP_vect )// 1ms for manual movement
ISR(USART_RXC_vect){/*
uint8_t data = UDR;
if(data == 0xFF){
shutdown_irq = true;
//mfd_active = true;
}*/
}
ISR(INT0_vect){
start_pi();
}