root / trunk / code / projects / libdragonfly / spi.c @ 1461
History | View | Annotate | Download (2.35 KB)
1 | 868 | justin | /**
|
---|---|---|---|
2 | * @file spi.c
|
||
3 | * @brief Basic SPI module to handle encoders
|
||
4 | * @author Colony Project, CMU Robotics Club
|
||
5 | * Need to move spi.h include into dragonfly_lib.h when stable
|
||
6 | **/
|
||
7 | |||
8 | #include <avr/interrupt.h> |
||
9 | #include "spi.h" |
||
10 | |||
11 | 1461 | bneuman | unsigned char spi_initd=0; |
12 | 868 | justin | |
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 */ |
||
16 | |||
17 | /**
|
||
18 | * @brief Initialize SPI hardware for communication.
|
||
19 | *
|
||
20 | * @param recv_func The function to be called for each byte of data received.
|
||
21 | * @param recv_complete_func The function to be called at the end of a complete transmission.
|
||
22 | 1461 | bneuman | *
|
23 | * @return 0 if init succesfull, an error code otherwise
|
||
24 | 868 | justin | */
|
25 | 1461 | bneuman | int spi_init (spi_fun_recv_t recv_func, spi_fun_recv_complete_t recv_complete_func)
|
26 | 868 | justin | { |
27 | 1461 | bneuman | if(spi_initd) {
|
28 | return ERROR_INIT_ALREADY_INITD;
|
||
29 | } |
||
30 | |||
31 | 868 | justin | /* Enable Interrupt, Enable SPI Module, MSB First, Master Mode, Clock div = 64 */
|
32 | SPCR = _BV(SPE) | _BV(SPIE) /*| _BV(DORD)*/ | _BV(MSTR) | _BV(SPR1) | _BV(SPR0);
|
||
33 | SPSR = _BV(SPI2X); |
||
34 | |||
35 | /* Set SCLK, SS, MOSI as outputs. MISO as input */
|
||
36 | DDRB |= MOSI | SCLK | SS; |
||
37 | DDRB &= ~MISO; |
||
38 | |||
39 | /* Keep SS high until transmit */
|
||
40 | PORTB |= SS; |
||
41 | |||
42 | /* set function to be executed when we receive a byte */
|
||
43 | spi_recv_func = recv_func; |
||
44 | spi_recv_complete_func = recv_complete_func; |
||
45 | spi_bytes = 0;
|
||
46 | //usb_puts("\tspi.c Debug: SPI INITIALIZED\n");
|
||
47 | 1461 | bneuman | |
48 | spi_initd=1;
|
||
49 | return 0; |
||
50 | 868 | justin | } |
51 | |||
52 | /**
|
||
53 | * @brief Transfer a given byte to slave and receive a byte
|
||
54 | *
|
||
55 | * @param bytes The number of bytes to be transferred.
|
||
56 | **/
|
||
57 | 1461 | bneuman | int spi_transfer(char bytes) |
58 | 868 | justin | { |
59 | 1461 | bneuman | if(!spi_initd)
|
60 | return ERROR_LIBRARY_NOT_INITD;
|
||
61 | |||
62 | 868 | justin | spi_bytes = bytes; |
63 | PORTB &= ~SS; /* Set SS low to initiate transmission */
|
||
64 | SPDR = 0xff; /* Initiate data transmision */ |
||
65 | 1461 | bneuman | |
66 | return 0; |
||
67 | 868 | justin | } |
68 | |||
69 | ISR(SIG_SPI) |
||
70 | { |
||
71 | //usb_puts("Interrupt");
|
||
72 | /* only handle intterupt when we are expecting data */
|
||
73 | if(spi_bytes > 0){ |
||
74 | /* process byte */
|
||
75 | spi_recv_func(SPDR); |
||
76 | /* if we've read all the bytes, set SS high to end transmission,
|
||
77 | * otherwise get the next byte */
|
||
78 | if(--spi_bytes == 0){ |
||
79 | //usb_puts("Read all bytes\r\n");
|
||
80 | PORTB |= SS; |
||
81 | if(spi_recv_complete_func)
|
||
82 | spi_recv_complete_func(); |
||
83 | }else {
|
||
84 | //usb_puts("There are this many bytes left: "); usb_puti(spi_bytes);usb_puts("\r\n");
|
||
85 | SPDR = 0xff;
|
||
86 | } |
||
87 | } |
||
88 | } |