forked from kevinkessler/TelemetryBridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spektrum.c
128 lines (109 loc) · 2 KB
/
Spektrum.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
/*
* Spektrum.c
*
* Created on: Jan 15, 2014
* Author: Kevin
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <string.h>
#include <util/twi.h>
#define MSG_LENGTH 16
void TWISlaveInit();
extern uint8_t alt_buf[16];
extern uint8_t cur_buf[16];
extern uint8_t pwr_buf[16];
extern uint8_t air_buf[16];
extern uint8_t gps_buf16[16];
extern uint8_t gps_buf17[16];
uint8_t addresses[]={0x03,0x0A,0x11,0x12,0x16,0x17,0x00};
uint8_t initializing=1;
uint8_t addrIdx=0;
uint8_t twi_buf[17];
volatile uint8_t twi_buf_ptr=0;
volatile uint8_t addrCount=0;
void TWISlaveInit()
{
TWAR=addresses[0]<<1;
TWSR=0;
TWBR=0;
TWCR=(1<<TWEN)|(1<<TWEA)|(1<<TWIE);
for(int n=0;n<16;n++)
twi_buf[n]=alt_buf[n];
sei();
}
ISR(TWI_vect)
{
uint8_t dummy;
switch (TW_STATUS)
{
case TW_SR_SLA_ACK:
dummy=TWDR;
TWCR |= (1<<TWINT);
break;
case TW_ST_SLA_ACK:
if(initializing)
{
addrCount++;
if(addrCount==2)
{
addrCount=0;
if(addresses[++addrIdx]==0)
{
TWAR=addresses[0]<<1;
initializing=0;
}
else
TWAR=addresses[addrIdx]<<1;
}
}
else
TWAMR=0xFF;
uint8_t funct=TWDR>>1;
switch(funct)
{
case 0x03:
memcpy(twi_buf,cur_buf,16);
break;
case 0x0A:
memcpy(twi_buf,pwr_buf,16);
break;
case 0x11:
memcpy(twi_buf,air_buf,16);
break;
case 0x12:
memcpy(twi_buf,alt_buf,16);
break;
case 0x16:
memcpy(twi_buf,gps_buf16,16);
break;
case 0x17:
memcpy(twi_buf,gps_buf17,16);
break;
default:
for (int n=0;n<16;n++)
twi_buf[n]=0;
break;
}
case TW_ST_DATA_ACK:
TWDR=twi_buf[twi_buf_ptr++];
if(twi_buf_ptr==MSG_LENGTH)
{
twi_buf_ptr=0;
TWCR = (1<<TWINT) | (1<<TWEN) | (0<<TWEA) | (1<<TWIE);
}
else
{
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA) | (1<<TWIE);
}
break;
case TW_ST_LAST_DATA:
case TW_ST_DATA_NACK:
TWCR=(1<<TWEN)|(1<<TWEA)|(1<<TWINT)| (1<<TWIE);
break;
default:
TWCR|=(1<<TWINT);
break;
}
sei();
}