Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (2.78 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 350 kwoo
        //cli();
41 252 kwoo
        RING_BUFFER_CLEAR(spi_send_buff);
42
        RING_BUFFER_CLEAR(spi_rec_buff);
43 316 bpoole
44
        spi_recv_function = recv_func;
45 367 kwoo
        //spi_send_function = send_func;
46 316 bpoole
47 350 kwoo
    /* Enables the SPI module
48
     * Enable Interrupt, Enable SPI Module, LSB First, Master Mode, Clock div = 64
49
     */
50
    SPCR = 0x00;
51
        SPCR = _BV(SPIE) | _BV(SPE) | _BV(DORD) | _BV(MSTR)| _BV(SPR1) | _BV(SPR0);
52
    SPSR = 0x00;
53
        SPSR = _BV(SPI2X);
54 252 kwoo
55 316 bpoole
        spi_status = SPI_VOID;
56
57 350 kwoo
    /* Set SCLK, SS, MOSI as outputs. MISO as input */
58
        if(mode == MASTER) {
59
            DDRB |= MOSI | SCLK | SS;
60
            DDRB &= ~MISO;
61 367 kwoo
                PORTB |= SS;        //Keep SS High until transmit
62 350 kwoo
        /* Set SCLK, SS, MOSI as inputs. MISO as output */
63
        } else {
64
            DDRB &= ~MOSI & ~SCLK & ~SS;
65
            DDRB |= MISO;
66
    }
67 252 kwoo
68 350 kwoo
        //sei();
69 316 bpoole
        usb_puts("spi_init: end\n");
70 252 kwoo
}
71
72 350 kwoo
int spi_send(char *data, size_t bytes) {
73
74
        int i;
75
76
        usb_puts("spi_send: start\n");
77
78
    if(bytes == 0)
79
        return -1; /* ...needed?*/
80 316 bpoole
81 350 kwoo
    //Prevent race condition on the buffer
82 316 bpoole
    cli();
83
    for(i = 1; i < bytes; i++) {
84 350 kwoo
        // Fail if the buffer is full
85
            if(RING_BUFFER_FULL(spi_send_buff)) {
86
                sei();
87
                return -1;
88
            }
89
90
            RING_BUFFER_ADD(spi_send_buff, data[i]);
91 252 kwoo
    }
92 350 kwoo
93 316 bpoole
    sei();
94 350 kwoo
95 316 bpoole
    spi_status = SPI_SENDING;
96 367 kwoo
97
        if (spi_mode == MASTER ){
98
                PORTB &= ~SS;        //Select slave
99
        }
100 316 bpoole
    SPDR = *data;
101 350 kwoo
102 316 bpoole
        usb_puts("spi_send: end\n");
103
        //sei();
104 367 kwoo
105
        return 1;
106 252 kwoo
}
107
108 367 kwoo
void spi_master_recv_on() {
109
        spi_status = SPI_VOID;
110
        PORTB &= ~SS;
111
}
112
113
void spi_master_recv_off() {
114
        spi_status = SPI_VOID;
115
        PORTB |= SS;
116
}
117
118 316 bpoole
ISR(SIG_SPI) {
119 350 kwoo
    usb_puts("ISR: start\n\r");
120 367 kwoo
        char c;
121 252 kwoo
122 367 kwoo
        //usb_putc('['); usb_puti(c);usb_putc(',');usb_puti(spi_status);usb_puts("]\n\r");
123 252 kwoo
        switch(spi_status){
124 316 bpoole
                case SPI_SENDING:
125 252 kwoo
                        if(!RING_BUFFER_EMPTY(spi_send_buff)) {
126
                                RING_BUFFER_REMOVE(spi_send_buff, c);
127 367 kwoo
                                //usb_puts("SPDR=["); usb_puti(c);usb_putc(',');usb_puti(spi_status);usb_puts("]\n\r");
128 252 kwoo
                                SPDR = c;
129 367 kwoo
                                //c = SPDR;
130
                                //usb_puts("c=["); usb_puti(c);usb_putc(',');usb_puti(spi_status);usb_puts("]\n");
131 316 bpoole
132 252 kwoo
                        } else {
133 367 kwoo
                                if (spi_mode == MASTER)
134
                                        PORTB |= SS;                //Turn off Slave select
135 316 bpoole
                                spi_status = SPI_VOID;
136 252 kwoo
                        }
137
                        break;
138 316 bpoole
                case SPI_VOID:
139 367 kwoo
                        spi_recv_function(SPDR);
140 252 kwoo
                        break;
141 316 bpoole
        }
142 367 kwoo
143 316 bpoole
        usb_puts("ISR: end\n");
144 252 kwoo
}