-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathXboxRfModule.c
114 lines (88 loc) · 2.4 KB
/
XboxRfModule.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
/*
Xbox 360 Rf Module
*/
#define F_CPU 1200000 // clock 1.2MHz, internal oscillator
#include <util/delay.h>
#include <avr/io.h>
#include <avr/sleep.h>
#include <stdint.h>
#include <avr/interrupt.h>
#define readPin(x,y) x & _BV(y)
#define BIT_L(port,pin) port &= ~(1<<pin)
#define BIT_H(port,pin) port |= (1<<pin)
#define OUTPUT 1
#define INPUT 0
#define HI 1
#define LO 0
#define LOW 0
#define HIGH 1
typedef struct
{
unsigned int bit0:1;
unsigned int bit1:1;
unsigned int bit2:1;
unsigned int bit3:1;
unsigned int bit4:1;
unsigned int bit5:1;
unsigned int bit6:1;
unsigned int bit7:1;
} _io_reg;
#define REGISTER_BIT(rg,bt) ((volatile _io_reg*)&rg)->bit##bt
#define DATA PB3
#define CLOCK PB4
#define SYNC PB0
//#define DEBUG PB1
#define DATA_PIN REGISTER_BIT(PINB,3)
#define CLOCK_PIN REGISTER_BIT(PINB,4)
#define SYNC_PIN REGISTER_BIT(PINB,0)
//#define DEBUG_PIN REGISTER_BIT(PINB,1)
#define DATA_PORT REGISTER_BIT(PORTB,3)
#define CLOCK_PORT REGISTER_BIT(PORTB,4)
#define SYNC_PORT REGISTER_BIT(PORTB,0)
//#define DEBUG_PORT REGISTER_BIT(PORTB,1)
#define DATA_DIR REGISTER_BIT(DDRB,3)
#define CLOCK_DIR REGISTER_BIT(DDRB,4)
#define SYNC_DIR REGISTER_BIT(DDRB,0)
//#define DEBUG_DIR REGISTER_BIT(DDRB,1)
char led_cmd[10] = {0,0,1,0,0,0,0,1,0,0}; //Activates/initialises the LEDs, leaving the center LED lit.
char anim_cmd[10] = {0,0,1,0,0,0,0,1,0,1}; //Makes the startup animation on the ring of light.
char sync_cmd[10] = {0,0,0,0,0,0,0,1,0,0}; //Initiates the sync process.
volatile int sync_enable = 0;
ISR (PCINT0_vect) // Interrupt on Int0 vector
{
sync_enable = 1;
}
void sendData(char cmd_do[]) {
int prev = 1;
int i=0;
DATA_DIR = OUTPUT;
DATA_PORT = LOW;
for(; i < 11; i++)
{
while (prev == CLOCK_PIN){} //detects change in clock
prev = CLOCK_PIN;
DATA_PORT = cmd_do[i];
while (prev == CLOCK_PIN){} //detects upward edge of clock
prev = CLOCK_PIN;
}
DATA_PORT = HI;
DATA_DIR = INPUT;
}
int main(void){
DATA_DIR = INPUT;
CLOCK_DIR = INPUT;
PCMSK |= (1<<PCINT0); // pin change mask: listen to portb bit 2
GIMSK |= (1<<PCIE); // enable PCINT interrupt
MCUCR |= (1<<ISC00);
sei(); // enable all interrupts
_delay_ms(100);
sendData(anim_cmd);
while(1)
{
if(sync_enable==1) {
sendData(sync_cmd);
_delay_ms(1000);
sync_enable = 0;
}
}
}