-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathavrbus.c
395 lines (312 loc) · 9.11 KB
/
avrbus.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
* SPI Expansion bus driver - addressable spi bus with 1Mbs throughput at 16Mhz F_CPU
*
* Martin Schröder, [email protected]
* http://github.com/mkschreder
*
* Released under GPLv3
*/
#include "avrbus.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#define SPI_is_master (SPCR & _BV(MSTR))
#define SPI_is_slave (!SPI_is_master)
#define SShi {SPI_PORT |= _BV(SPI_SS);}
#define SSlo {SPI_PORT &= ~_BV(SPI_SS);}
#define CEhi {SPI_PORT |= _BV(SPI_CE);}
#define CElo {SPI_PORT &= ~_BV(SPI_CE);}
#define SSout {SPI_DDR |= _BV(SPI_SS);}
#define SSin {SPI_DDR &= ~_BV(SPI_SS);}
#define CEout {SPI_DDR |= _BV(SPI_CE);}
#define CEin {SPI_DDR &= ~_BV(SPI_CE);}
#define MISOout {SPI_DDR |= _BV(SPI_MISO);}
#define MISOin {SPI_DDR &= ~_BV(SPI_MISO);}
#define SS (SPI_PIN & _BV(SPI_SS))
#define CE (SPI_PIN & _BV(SPI_CE))
#define LEDon {PORTC |= _BV(PC0); DDRC |= _BV(PC0); }
#define LEDoff {PORTC &= ~_BV(PC0); DDRC &= ~_BV(PC0); }
#define E_NOTREADY 0
#define E_READY 0x02
// start condition is CE going from high to low and SS being low
#define START { \
SPI_DDR |= (_BV(SPI_SS) | _BV(SPI_CE)); \
SPI_PORT &= ~(_BV(SPI_SS) | _BV(SPI_CE)); \
}
#define EXT_RD (0x20)
#define EXT_WR (0x30)
enum states {
BUS_IDLE,
BUS_ADDRESS,
BUS_COMMAND,
BUS_RX_DATA,
BUS_TX_DATA,
BUS_CHECKSUM,
BUS_FINISH,
BUS_STATE_COUNT
};
enum events {
EV_BUS_START,
EV_BUS_END,
EV_BUS_DATA,
EV_COUNT
};
typedef struct bus_s {
uint8_t rx_bytes;
uint16_t address;
uint8_t state;
uint8_t command;
uint8_t command_size;
uint8_t out_byte;
uint8_t in_byte;
uint8_t sum;
} bus_t;
bus_t _bus = {
.rx_bytes = 0,
.address = 0,
.state = BUS_IDLE,
.command = 0
};
static void _bus_reset(void);
// used by slave
static volatile char *_slave_buffer;
static volatile uint8_t _slave_buffer_size = 0;
static volatile uint8_t _slave_buffer_ptr;
static volatile uint16_t _slave_addr = 0;
//static packet_t _packet;
//static volatile uint8_t _rx_count = 0;
static volatile uint8_t _prev_pinb = 0;
void bus_master_init(void){
SPI_DDR &= ~((1<<SPI_MISO)); //input
SPI_DDR |= (_BV(SPI_MOSI) | _BV(SPI_SS) | _BV(SPI_SCK) | _BV(SPI_CE)); //output
// enable pullups
SPI_PORT |= (_BV(SPI_SS) | _BV(SPI_CE));
SPCR = ((1<<SPE)| // SPI Enable
(0<<SPIE)| // SPI Interupt Enable
(0<<DORD)| // Data Order (0:MSB first / 1:LSB first)
(1<<MSTR)| // Master/Slave select
(0<<SPR1)|(0<<SPR0)| // SPI Clock Rate
(0<<CPOL)| // Clock Polarity (0:SCK low / 1:SCK hi when idle)
(0<<CPHA)); // Clock Phase (0:leading / 1:trailing edge sampling)
SPSR = (0<<SPI2X); // Double SPI Speed Bit
// default is SS high and CE high
SPI_PORT |= _BV(SPI_CE) | _BV(SPI_SS);
}
/** initialize spi interface in SPI_EXT mode **/
void bus_slave_init(uint16_t base_addr, char *buf, uint8_t size){
//_slave_addr = base_addr & 0x7fff;
_slave_addr = base_addr;
_slave_buffer = buf;
_slave_buffer_size = size;
// Enable PCINT interrupt on PB0 (int0) and PB1 (int1)
PCICR |= (1<<PCIE0);
PCMSK0 = (1<<PCINT0) | (1<<PCINT2);
SPI_DDR &= ~(_BV(SPI_MOSI) | _BV(SPI_SS) | _BV(SPI_CE) | _BV(SPI_SCK)); //input
// enable pullups on CE and SS
SPI_PORT |= (_BV(SPI_CE) | _BV(SPI_SS));
// MISO is also input by default so that we don't interfere with other slaves.
MISOin;
SPCR = ((1<<SPE)| // SPI Enable
(1<<SPIE)| // SPI Interupt Enable
(0<<DORD)| // Data Order (0:MSB first / 1:LSB first)
(0<<MSTR)| // Master/Slave select
(0<<SPR1)|(0<<SPR0)| // SPI Clock Rate
(0<<CPOL)| // Clock Polarity (0:SCK low / 1:SCK hi when idle)
(0<<CPHA)); // Clock Phase (0:leading / 1:trailing edge sampling)
SPSR = (0<<SPI2X); // Double SPI Speed Bit
}
uint8_t bus_exchange_byte(uint8_t data) {
SPDR = data;
while((SPSR & (1<<SPIF)) == 0);
return SPDR;
}
uint16_t ext_get_address(void){ return _bus.address; }
uint16_t ext_state(void){ return 0;}
#define BYTE_DELAY_US 5
// writes data to an expansion slot peripheral
uint8_t bus_write(uint16_t addr, const char *data, uint8_t size){
//if(!ext_start(addr & 0x7fff)) return 0;
if(size > 0x0f) size = 0x0f;
SSout;
SSlo;
_delay_us(10);
bus_exchange_byte(addr);
_delay_us(5);
bus_exchange_byte(addr >> 8);
_delay_us(5);
bus_exchange_byte(EXT_WR | (size & 0x0f));
_delay_us(5);
//uint8_t stat = bus_exchange_byte(0);
//_delay_us(BYTE_DELAY_US);
// transfer data
uint8_t c = 0;
for(c = 0; c < size; c++){
//SSlo;
bus_exchange_byte(data[c]);
//SShi;
_delay_us(10);
}
SShi;
_delay_us(10);
return c;
}
// reads data from expansion slot peripheral
uint8_t bus_read(uint16_t addr, char *data, uint8_t size){
uint8_t retry = 10;
uint8_t rx_count = 0;
while(retry--){
cli();
rx_count = 0;
SSlo;
_delay_us(BYTE_DELAY_US);
bus_exchange_byte(addr);
_delay_us(BYTE_DELAY_US);
bus_exchange_byte(addr >> 8);
_delay_us(BYTE_DELAY_US);
bus_exchange_byte(EXT_RD | (size & 0x0f));
_delay_us(BYTE_DELAY_US);
// when slave gets the last address byte, it loads it's data register with previous
// "next" byte. When it gets the command, it loads it with our status.
uint8_t stat = bus_exchange_byte(0);
_delay_us(BYTE_DELAY_US);
if(stat != E_READY){
SShi;
sei();
_delay_us(50);
continue;
}
//bus_exchange_byte(0);
__asm("nop"); __asm("nop"); __asm("nop");
// transfer data
uint8_t sum = 0;
uint8_t buffer[16];
while(rx_count < size){
buffer[rx_count] = bus_exchange_byte(0);
sum += buffer[rx_count];
rx_count++;
_delay_us(BYTE_DELAY_US);
}
uint8_t sum2 = bus_exchange_byte(0);
if(sum != sum2) {
SShi;
sei();
_delay_us(2);
continue;
}
for(uint8_t c = 0; c < 16; c++){
data[c] = buffer[c];
}
break;
}
_delay_us(BYTE_DELAY_US);
SShi;
sei();
return rx_count;
}
static void _bus_idle_start(void){
// called when ss goes low
}
static void _bus_idle_rx_byte(void){
_bus.address |= _bus.in_byte;
_bus.sum = 0;
_bus.state = BUS_ADDRESS;
}
static void _bus_address_rx_byte(void){
_bus.address |= _bus.in_byte << 8;
if((_bus.address >= _slave_addr) &&
(_bus.address < (_slave_addr + _slave_buffer_size))){
// if the address matches our address then we enter command state
MISOout;
_bus.state = BUS_COMMAND;
} else {
_bus_reset();
}
}
static void _bus_command_rx_byte(void){
_bus.command = _bus.in_byte & 0xf0;
_bus.command_size = _bus.in_byte & 0x0f;
if(_bus.command == EXT_RD)
_bus.state = BUS_TX_DATA;
else if(_bus.command == EXT_WR)
_bus.state = BUS_RX_DATA;
_bus.out_byte = E_READY;
_slave_buffer_ptr = 0;
}
static void _bus_rx_data(void){
if(_slave_buffer_ptr < _slave_buffer_size){
_slave_buffer[_slave_buffer_ptr++] = _bus.in_byte;
_bus.out_byte = _bus.in_byte; // send it back to master for inspection
} else {
_bus_reset();
}
}
static void _bus_checksum_rx_byte(void){
_bus.out_byte = _bus.sum;
_bus.state = BUS_FINISH;
}
static void _bus_tx_data(void){
if(_slave_buffer_ptr < _slave_buffer_size){
_bus.out_byte = _slave_buffer[_slave_buffer_ptr++];
_bus.sum += _bus.out_byte;
//_bus.out_byte = _bus.sum;
if(_slave_buffer_ptr == _bus.command_size){
_bus.state = BUS_CHECKSUM;
}
} else {
_bus.state = BUS_FINISH;
//_bus_reset();
}
}
static void _bus_reset(void){
_bus.address = 0;
_bus.command = 0;
_bus.sum = 0;
_bus.state = BUS_IDLE;
MISOin;
}
static void _bus_nop(void){
// does nothing :)
}
// Bus state machine state/events callback table
// start, end, data
void (*const state_table [BUS_STATE_COUNT][EV_COUNT]) (void) = {
{ _bus_idle_start, _bus_reset, _bus_idle_rx_byte, }, // idle
{ _bus_nop, _bus_reset, _bus_address_rx_byte }, // address
{ _bus_nop, _bus_reset, _bus_command_rx_byte }, // command
{ _bus_nop, _bus_reset, _bus_rx_data }, // rx
{ _bus_nop, _bus_reset, _bus_tx_data }, // tx
{ _bus_nop, _bus_reset, _bus_checksum_rx_byte}, // checksum
{ _bus_nop, _bus_reset, _bus_reset} // finish
};
/// Enabled for the bus transfers
/*
ISR(PCINT0_vect){
//DDRB |= _BV(1);
uint8_t changed = _prev_pinb ^ SPI_PIN;
_prev_pinb = SPI_PIN;
if(SPI_is_slave && (changed & _BV(SPI_SS))){
if(!SS){
state_table[_bus.state][EV_BUS_START]();
//_bus.sync_timeout = timeout(5);
} else {
//if(!timeout_expired(_bus.timeout)){
// sync bus arbitration
//}
state_table[_bus.state][EV_BUS_END]();
}
}
}
*/
/// This ISR is only used when data is received in slave mode.
/// Takes 1.5us
/// Interrupt latency: 2us
/// SPI transmission time: 4us at clk/8
ISR(SPI_STC_vect)
{
//PORTB |= _BV(1);
//DDRB |= _BV(1);
_bus.in_byte = SPDR;
state_table[_bus.state][EV_BUS_DATA]();
SPDR = _bus.out_byte;
//PORTB &= ~_BV(1);
}