Project

General

Profile

Statistics
| Revision:

root / branches / encoders / code / projects / libdragonfly / spi.c @ 490

History | View | Annotate | Download (2.53 KB)

1 316 bpoole
/* @file spi.c
2
 * @brief
3
 * SPI module
4
 * @bug broken
5 252 kwoo
 */
6
7 316 bpoole
#include <avr/interrupt.h>
8
#include "ring_buffer.h"
9
#include "spi.h"
10
#include <dragonfly_lib.h>
11
12
13 252 kwoo
/*
14
SS = PB0
15
SCK = PB1
16
MOSI = PB2
17
MISO = PB3
18
*/
19 316 bpoole
/* Controls clock freq. see Table 72 of specs*/
20 252 kwoo
21
#define DOUBLE_SCK 1
22
#define SPR0_BIT 1
23 316 bpoole
#define SPR1_BIT 0
24 252 kwoo
#define LSB 1
25
#define MSB 0
26
27
28 350 kwoo
RING_BUFFER_NEW(spi_buffer, 16, char, spi_send_buff, spi_rec_buff);
29 316 bpoole
volatile char spi_status;
30 367 kwoo
char spi_mode;
31 316 bpoole
static spi_fun_recv_t  spi_recv_function;
32 367 kwoo
//static spi_fun_send_t  spi_send_function;
33 252 kwoo
34 316 bpoole
35 367 kwoo
void spi_init(char mode, spi_fun_recv_t recv_func) {
36 316 bpoole
    usb_puts("spi_init: start\n");
37 252 kwoo
38 367 kwoo
        spi_mode = mode;
39
40 252 kwoo
        RING_BUFFER_CLEAR(spi_send_buff);
41
        RING_BUFFER_CLEAR(spi_rec_buff);
42 316 bpoole
43
        spi_recv_function = recv_func;
44 367 kwoo
        //spi_send_function = send_func;
45 316 bpoole
46 350 kwoo
    /* 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 252 kwoo
54 490 bpoole
        spi_status = SPI_RECV;
55 316 bpoole
56 350 kwoo
    /* Set SCLK, SS, MOSI as outputs. MISO as input */
57
        if(mode == MASTER) {
58
            DDRB |= MOSI | SCLK | SS;
59
            DDRB &= ~MISO;
60 367 kwoo
                PORTB |= SS;        //Keep SS High until transmit
61 350 kwoo
        /* Set SCLK, SS, MOSI as inputs. MISO as output */
62
        } else {
63
            DDRB &= ~MOSI & ~SCLK & ~SS;
64
            DDRB |= MISO;
65
    }
66 252 kwoo
67 350 kwoo
        //sei();
68 316 bpoole
        usb_puts("spi_init: end\n");
69 252 kwoo
}
70
71 350 kwoo
int spi_send(char *data, size_t bytes) {
72
73
        int i;
74
75
    if(bytes == 0)
76
        return -1; /* ...needed?*/
77 316 bpoole
78 350 kwoo
    //Prevent race condition on the buffer
79 316 bpoole
    cli();
80
    for(i = 1; i < bytes; i++) {
81 350 kwoo
        // 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 252 kwoo
    }
89 350 kwoo
90 316 bpoole
    sei();
91 350 kwoo
92 490 bpoole
    spi_status |= SPI_SEND;
93 367 kwoo
94
        if (spi_mode == MASTER ){
95
                PORTB &= ~SS;        //Select slave
96
        }
97 316 bpoole
    SPDR = *data;
98 490 bpoole
        if(spi_mode)
99
                usb_puts("MASTER");
100
        else
101
                usb_puts("SLAVE");
102
        usb_puts(": sending [");usb_putc(*data);usb_puts("]\n\r");
103 350 kwoo
104 316 bpoole
        //sei();
105 367 kwoo
106
        return 1;
107 252 kwoo
}
108
109 490 bpoole
void spi_read_one(void)
110
{
111 367 kwoo
        PORTB &= ~SS;
112 490 bpoole
        SPDR = 'x';
113 367 kwoo
}
114
115 316 bpoole
ISR(SIG_SPI) {
116 367 kwoo
        char c;
117 490 bpoole
        if(!RING_BUFFER_EMPTY(spi_send_buff)) {//cheap way to test if SPI_SEND
118
                RING_BUFFER_REMOVE(spi_send_buff, c);
119
                SPDR = c;
120
                if(spi_mode)
121
                        usb_puts("MASTER");
122
                else
123
                        usb_puts("SLAVE");
124
                usb_puts(": sending [");usb_putc(c);usb_puts("]\n\r");
125
        } else if(spi_status & SPI_SEND) {
126
                spi_status ^= SPI_SEND;
127
                PORTB |= SS; //Sleep slave
128
        }
129
        if(spi_status & SPI_RECV){
130
                spi_recv_function(SPDR);
131
                if(spi_mode == MASTER)
132
                        PORTB |= SS;
133 316 bpoole
        }
134 490 bpoole
}