Revision 749
added another function pointer to encoders which gets called after all bytes are recv'd.
working on a behavior to drive in a straight line, but encoders are giving invalid on robot 5
spi.c | ||
---|---|---|
12 | 12 |
|
13 | 13 |
static volatile char spi_bytes; /* number of bytes to read */ |
14 | 14 |
static spi_fun_recv_t spi_recv_func; /* byte handler */ |
15 |
static spi_fun_recv_complete_t spi_recv_complete_func; /*transmission completion handler */ |
|
15 | 16 |
|
16 |
void spi_init (spi_fun_recv_t recv_func) |
|
17 |
void spi_init (spi_fun_recv_t recv_func, spi_fun_recv_complete_t recv_complete_func)
|
|
17 | 18 |
{ |
18 | 19 |
/* Enable Interrupt, Enable SPI Module, MSB First, Master Mode, Clock div = 64 */ |
19 | 20 |
SPCR = _BV(SPE) | _BV(SPIE) | _BV(DORD) | _BV(MSTR) | _BV(SPR1) | _BV(SPR0); |
... | ... | |
28 | 29 |
|
29 | 30 |
/* set function to be executed when we receive a byte */ |
30 | 31 |
spi_recv_func = recv_func; |
32 |
spi_recv_complete_func = recv_complete_func; |
|
31 | 33 |
spi_bytes = 0; |
32 | 34 |
} |
33 | 35 |
|
... | ... | |
41 | 43 |
|
42 | 44 |
ISR(SIG_SPI) |
43 | 45 |
{ |
46 |
//usb_puts("Interrupt"); |
|
44 | 47 |
/* only handle intterupt when we are expecting data */ |
45 | 48 |
if(spi_bytes > 0){ |
46 | 49 |
/* process byte */ |
47 | 50 |
spi_recv_func(SPDR); |
48 | 51 |
/* if we've read all the bytes, set SS high to end transmission, |
49 | 52 |
* otherwise get the next byte */ |
50 |
if(--spi_bytes == 0) |
|
53 |
if(--spi_bytes == 0){ |
|
54 |
//usb_puts("Read all bytes\r\n"); |
|
51 | 55 |
PORTB |= SS; |
52 |
else |
|
56 |
spi_recv_complete_func(); |
|
57 |
}else { |
|
58 |
//usb_puts("There are this many bytes left: "); usb_puti(spi_bytes);usb_puts("\r\n"); |
|
53 | 59 |
SPDR = 0xff; |
60 |
} |
|
54 | 61 |
} |
55 | 62 |
} |
Also available in: Unified diff