-
Notifications
You must be signed in to change notification settings - Fork 124
/
CsrSpiDriver.ino
158 lines (144 loc) · 3.45 KB
/
CsrSpiDriver.ino
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
/*
* Attempt to talk to CSR Bluetooth chip SPI
*/
#include <inttypes.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <util/delay.h>
#define PIN_CS _BV(2)
#define PIN_MOSI _BV(3)
#define PIN_MISO _BV(4)
#define PIN_CLK _BV(5)
int nShiftPeriod=1;//5814;
unsigned short g_nCmdReadBits=0;
unsigned short g_nCmdWriteBits=0;
void setup()
{
//Set MISO as input, CLK, CS, MOSI as output
DDRB=(DDRB&~PIN_MISO)|PIN_CS|PIN_MOSI|PIN_CLK;
//Set MOSI and CLK low, CS and MISO high (MISO as pull-up)
PORTB=(PORTB&~(PIN_MOSI|PIN_CLK))|PIN_CS|PIN_MISO;
// start serial port at 256000 bps:
Serial.begin(256000);
//Send init message
Serial.print("CSRSPI1");
}
uint8_t ReadByte() {
while (Serial.available()<1);
return (uint8_t)Serial.read();
}
void CSRReset() {
//Chip select high
PORTB|=PIN_CS;_delay_us(nShiftPeriod);
//Two clock cycles to reset the device
PORTB|=PIN_CLK; _delay_us(nShiftPeriod);
PORTB&=~PIN_CLK; _delay_us(nShiftPeriod);
PORTB|=PIN_CLK; _delay_us(nShiftPeriod);
PORTB&=~PIN_CLK; _delay_us(nShiftPeriod);
}
void CSRStart() {
CSRReset();
//Chip select low
PORTB&=~PIN_CS;
_delay_us(nShiftPeriod);
}
void CSRStop() {
PORTB|=PIN_CS;
}
uint8_t CSRTransfer(uint8_t nInput) {
uint8_t ret=0;
for (uint8_t b=0; b<8; b++) {
if (nInput & 0x80) PORTB|=PIN_MOSI;
else PORTB&=~PIN_MOSI;
nInput<<=1;
if (PINB&PIN_MISO) nInput|=1;
_delay_us(nShiftPeriod);
PORTB|=PIN_CLK;
_delay_us(nShiftPeriod);
PORTB&=~PIN_CLK;
}
return nInput;
}
void command_read() {
uint8_t nCmd,nAddressH,nAddressL,nControlH,nControlL;
nCmd=3; //Command read
nAddressH=ReadByte();
nAddressL=ReadByte();
CSRStart();
CSRTransfer(nCmd); //Command read
CSRTransfer(nAddressH); //Transfer in address
CSRTransfer(nAddressL);
//Get control data
nControlH=CSRTransfer(0);
nControlL=CSRTransfer(0);
if (nControlH != nCmd || nControlL != nAddressH) {
//Read failed
Serial.write((uint8_t)1);
return;
}
Serial.write((uint8_t)0);
while (true) {
uint8_t nRead=ReadByte();
if (nRead==0)
break;
while (nRead--) {
Serial.write(CSRTransfer(0));
Serial.write(CSRTransfer(0));
}
}
CSRStop();
}
void command_write() {
uint8_t nAddressH,nAddressL;
//Read data from serial
nAddressH=ReadByte();
nAddressL=ReadByte();
CSRStart();
CSRTransfer(2); //Command write
CSRTransfer(nAddressH);
CSRTransfer(nAddressL);
while (1) {
Serial.write(31);
uint8_t nBlockLength=ReadByte();
if (nBlockLength==0)
break;
while (nBlockLength--) {
CSRTransfer(ReadByte());
CSRTransfer(ReadByte());
}
}
CSRStop();
}
void command_xap_stopped() {
//Check to see if chip responds to a write (see if it's even connected properly)
uint8_t nControlH,nControlL;
CSRStart();
uint8_t nCheck=CSRTransfer(3);
CSRTransfer(0xFF);
CSRTransfer(0x9A);
nControlH=CSRTransfer(0);
nControlL=CSRTransfer(0);
CSRStop();
if (nControlH!=3 || nControlL!=0xFF) {
//No chip present or not responding correctly, no way to find out.
Serial.write(0xFF);
return;
}
//Reading works, if the chip is running it'll pull MISO low during the first command byte, else it'll leave it high.
Serial.write((uint8_t)(nCheck?1:0));
return;
}
void loop()
{
switch (ReadByte()) {
case 1: //Read
command_read();
break;
case 2: //Write
command_write();
break;
case 3: //XAP Stopped
command_xap_stopped();
break;
}
}