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 | ||
---|---|---|
1 |
/* @file spi.c |
|
2 |
* @brief |
|
3 |
* SPI module |
|
4 |
* @bug broken |
|
5 |
*/ |
|
1 |
/** |
|
2 |
* @file spi.c |
|
3 |
* @brief Basic SPI module to handle encoders |
|
4 |
* @author Colony Project, CMU Robotics Club |
|
5 |
* @bug Not tested |
|
6 |
* Need to move spi.h include into dragonfly_lib.h when stable |
|
7 |
**/ |
|
6 | 8 |
|
7 | 9 |
#include <avr/interrupt.h> |
8 |
#include "ring_buffer.h"
|
|
10 |
#include <dragonfly_lib.h>
|
|
9 | 11 |
#include "spi.h" |
10 |
#include <dragonfly_lib.h> |
|
11 | 12 |
|
13 |
static volatile char spi_bytes; /* number of bytes to read */ |
|
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 */ |
|
12 | 16 |
|
13 |
/* |
|
14 |
SS = PB0 |
|
15 |
SCK = PB1 |
|
16 |
MOSI = PB2 |
|
17 |
MISO = PB3 |
|
18 |
*/ |
|
19 |
/* Controls clock freq. see Table 72 of specs*/ |
|
17 |
void spi_init (spi_fun_recv_t recv_func, spi_fun_recv_complete_t recv_complete_func) |
|
18 |
{ |
|
19 |
/* Enable Interrupt, Enable SPI Module, MSB First, Master Mode, Clock div = 64 */ |
|
20 |
SPCR = _BV(SPE) | _BV(SPIE) | _BV(DORD) | _BV(MSTR) | _BV(SPR1) | _BV(SPR0); |
|
21 |
SPSR = _BV(SPI2X); |
|
20 | 22 |
|
21 |
#define DOUBLE_SCK 1 |
|
22 |
#define SPR0_BIT 1 |
|
23 |
#define SPR1_BIT 0 |
|
24 |
#define LSB 1 |
|
25 |
#define MSB 0 |
|
26 |
|
|
27 |
|
|
28 |
RING_BUFFER_NEW(spi_buffer, 16, char, spi_send_buff, spi_rec_buff); |
|
29 |
volatile char spi_status; |
|
30 |
char spi_mode; |
|
31 |
static spi_fun_recv_t spi_recv_function; |
|
32 |
//static spi_fun_send_t spi_send_function; |
|
33 |
|
|
34 |
|
|
35 |
void spi_init(char mode, spi_fun_recv_t recv_func) { |
|
36 |
usb_puts("spi_init: start\n"); |
|
37 |
|
|
38 |
spi_mode = mode; |
|
39 |
|
|
40 |
RING_BUFFER_CLEAR(spi_send_buff); |
|
41 |
RING_BUFFER_CLEAR(spi_rec_buff); |
|
42 |
|
|
43 |
spi_recv_function = recv_func; |
|
44 |
//spi_send_function = send_func; |
|
45 |
|
|
46 |
/* Enables the SPI module |
|
47 |
* Enable Interrupt, Enable SPI Module, LSB First, Master Mode, Clock div = 64 |
|
48 |
*/ |
|
49 |
SPCR = 0x00; |
|
50 |
SPCR = _BV(SPIE) | _BV(SPE) | _BV(DORD) | _BV(MSTR)| _BV(SPR1) | _BV(SPR0); |
|
51 |
SPSR = 0x00; |
|
52 |
SPSR = _BV(SPI2X); |
|
53 |
|
|
54 |
spi_status = SPI_IDLE; |
|
55 |
|
|
56 | 23 |
/* Set SCLK, SS, MOSI as outputs. MISO as input */ |
57 |
if(mode == MASTER) { |
|
58 |
DDRB |= MOSI | SCLK | SS; |
|
59 |
DDRB &= ~MISO; |
|
60 |
PORTB |= SS; //Keep SS High until transmit |
|
61 |
/* Set SCLK, SS, MOSI as inputs. MISO as output */ |
|
62 |
} else { |
|
63 |
DDRB &= ~MOSI & ~SCLK & ~SS; |
|
64 |
DDRB |= MISO; |
|
65 |
} |
|
66 |
|
|
67 |
//sei(); |
|
68 |
usb_puts("spi_init: end\n"); |
|
69 |
} |
|
70 |
|
|
71 |
int spi_send(char *data, int bytes) { |
|
72 |
|
|
73 |
int i; |
|
74 |
|
|
75 |
if(bytes == 0) |
|
76 |
return -1; /* ...needed?*/ |
|
24 |
DDRB = MOSI | SCLK | SS; |
|
25 |
DDRB &= ~MISO; |
|
77 | 26 |
|
78 |
//Prevent race condition on the buffer |
|
79 |
cli(); |
|
80 |
for(i = 1; i < bytes; i++) { |
|
81 |
// Fail if the buffer is full |
|
82 |
if(RING_BUFFER_FULL(spi_send_buff)) { |
|
83 |
sei(); |
|
84 |
return -1; |
|
85 |
} |
|
86 |
|
|
87 |
RING_BUFFER_ADD(spi_send_buff, data[i]); |
|
88 |
} |
|
89 |
|
|
90 |
sei(); |
|
91 |
|
|
92 |
spi_status |= SPI_SEND; |
|
93 |
|
|
94 |
if (spi_mode == MASTER ){ |
|
95 |
PORTB &= ~SS; //Select slave |
|
96 |
} |
|
97 |
|
|
98 |
SPDR = *data; |
|
99 |
|
|
100 |
if(spi_mode) |
|
101 |
usb_puts("MASTER"); |
|
102 |
else |
|
103 |
usb_puts("SLAVE"); |
|
104 |
|
|
105 |
usb_puts(": sending [");usb_putc(*data);usb_puts("]\n\r"); |
|
106 |
|
|
107 |
//sei(); |
|
108 |
|
|
109 |
return 1; |
|
110 |
} |
|
27 |
/* Keep SS high until transmit */ |
|
28 |
PORTB |= SS; |
|
111 | 29 |
|
112 |
void spi_read(int bytes) { |
|
113 |
|
|
114 |
cli(); |
|
115 |
for(int i = 1; i < bytes; i++) { |
|
116 |
// Fail if the buffer is full |
|
117 |
if(RING_BUFFER_FULL(spi_send_buff)) { |
|
118 |
sei(); |
|
119 |
return; |
|
120 |
} |
|
121 |
|
|
122 |
RING_BUFFER_ADD(spi_send_buff, '\n'); |
|
123 |
} |
|
124 |
|
|
125 |
sei(); |
|
126 |
|
|
127 |
spi_status |= SPI_RECV; |
|
128 |
|
|
129 |
if (spi_mode == MASTER ){ |
|
130 |
PORTB &= ~SS; //Select slave |
|
131 |
} |
|
132 |
|
|
133 |
SPDR = '\n'; |
|
134 |
|
|
30 |
/* set function to be executed when we receive a byte */ |
|
31 |
spi_recv_func = recv_func; |
|
32 |
spi_recv_complete_func = recv_complete_func; |
|
33 |
spi_bytes = 0; |
|
135 | 34 |
} |
136 | 35 |
|
137 |
void spi_read_one(void) |
|
36 |
/* Transfer a given byte to slave and receive a byte */ |
|
37 |
void spi_transfer(char bytes) |
|
138 | 38 |
{ |
139 |
PORTB &= ~SS; |
|
140 |
SPDR = 'x'; |
|
39 |
spi_bytes = bytes; |
|
40 |
PORTB &= ~SS; /* Set SS low to initiate transmission */ |
|
41 |
SPDR = 0xff; /* Initiate data transmision */ |
|
141 | 42 |
} |
142 | 43 |
|
143 |
ISR(SIG_SPI) { |
|
144 |
char c; |
|
145 |
|
|
146 |
//The clock is running so dequeue another byte to send |
|
147 |
if(!RING_BUFFER_EMPTY(spi_send_buff)) {//cheap way to test if SPI_SEND |
|
148 |
RING_BUFFER_REMOVE(spi_send_buff, c); |
|
149 |
SPDR = c; |
|
150 |
|
|
151 |
//If we're the master and we're done sending, end the transmission |
|
152 |
} else if (spi_mode == MASTER) { |
|
44 |
ISR(SIG_SPI) |
|
45 |
{ |
|
46 |
//usb_puts("Interrupt"); |
|
47 |
/* only handle intterupt when we are expecting data */ |
|
48 |
if(spi_bytes > 0){ |
|
49 |
/* process byte */ |
|
50 |
spi_recv_func(SPDR); |
|
51 |
/* if we've read all the bytes, set SS high to end transmission, |
|
52 |
* otherwise get the next byte */ |
|
53 |
if(--spi_bytes == 0){ |
|
54 |
//usb_puts("Read all bytes\r\n"); |
|
153 | 55 |
PORTB |= SS; |
154 |
} |
|
155 |
|
|
156 |
//You always receive something. You need to handle it. |
|
157 |
spi_recv_function(SPDR); |
|
158 |
|
|
159 |
/* |
|
160 |
if(spi_status & SPI_SEND) { |
|
161 |
spi_status ^= SPI_SEND; |
|
162 |
|
|
163 |
if (spi_mode == MASTER) { |
|
164 |
PORTB |= SS; |
|
165 |
} |
|
166 |
} else if (spi_status & SPI_RECV) { |
|
167 |
|
|
56 |
spi_recv_complete_func(); |
|
57 |
}else { |
|
58 |
//usb_puts("There are this many bytes left: "); usb_puti(spi_bytes);usb_puts("\r\n"); |
|
59 |
SPDR = 0xff; |
|
60 |
} |
|
168 | 61 |
} |
169 |
*/ |
|
170 |
//End the transmission if we are not sending or recieving anymore |
|
171 |
//} else if(spi_mode == MASTER) { |
|
172 |
// PORTB |= SS; |
|
173 |
//} |
|
174 |
|
|
175 |
//Call the recv function whenver we recieve a byte |
|
176 |
//if(spi_status == SPI_RECV){ |
|
177 |
//spi_recv_function(SPDR); |
|
178 |
//if(spi_mode == MASTER) |
|
179 |
//PORTB |= SS; |
|
180 |
//} |
|
181 | 62 |
} |
182 |
|
Also available in: Unified diff