Project

General

Profile

Revision 367

Added by Kevin Woo over 14 years ago

SPI Master->Slave works. Slave->Master not working yet. /template/main.c has the test bench. Yay!

View differences:

spi.c
27 27

  
28 28
RING_BUFFER_NEW(spi_buffer, 16, char, spi_send_buff, spi_rec_buff);
29 29
volatile char spi_status;
30
char spi_mode;
30 31
static spi_fun_recv_t  spi_recv_function;
31
static spi_fun_send_t  spi_send_function;
32
//static spi_fun_send_t  spi_send_function;
32 33

  
33 34

  
34
int spi_init(char mode, spi_fun_send_t send_func, spi_fun_recv_t recv_func) {
35
void spi_init(char mode, spi_fun_recv_t recv_func) {
35 36
    usb_puts("spi_init: start\n");
36 37

  
38
	spi_mode = mode;
39

  
37 40
	//cli();
38 41
	RING_BUFFER_CLEAR(spi_send_buff);
39 42
	RING_BUFFER_CLEAR(spi_rec_buff);
40 43

  
41 44
	spi_recv_function = recv_func;
42
	spi_send_function = send_func;
45
	//spi_send_function = send_func;
43 46

  
44 47
    /* Enables the SPI module
45 48
     * Enable Interrupt, Enable SPI Module, LSB First, Master Mode, Clock div = 64
......
55 58
	if(mode == MASTER) {
56 59
	    DDRB |= MOSI | SCLK | SS;
57 60
	    DDRB &= ~MISO;
61
		PORTB |= SS;	//Keep SS High until transmit
58 62
	/* Set SCLK, SS, MOSI as inputs. MISO as output */
59 63
	} else {
60 64
	    DDRB &= ~MOSI & ~SCLK & ~SS;
......
89 93
    sei();
90 94
    
91 95
    spi_status = SPI_SENDING;
96
	
97
	if (spi_mode == MASTER ){
98
		PORTB &= ~SS;	//Select slave
99
	}
92 100
    SPDR = *data;
93 101
	
94 102
	usb_puts("spi_send: end\n");
95 103
	//sei();
104
	
105
	return 1;
96 106
}
97 107

  
108
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

  
98 118
ISR(SIG_SPI) {
99 119
    usb_puts("ISR: start\n\r");
100
	char c = SPDR;
120
	char c;
101 121

  
102
	usb_putc('['); usb_puti(c);usb_putc(',');usb_puti(spi_status);usb_puts("]\n\r");
122
	//usb_putc('['); usb_puti(c);usb_putc(',');usb_puti(spi_status);usb_puts("]\n\r");
103 123
	switch(spi_status){
104 124
		case SPI_SENDING:
105 125
			if(!RING_BUFFER_EMPTY(spi_send_buff)) {
106 126
				RING_BUFFER_REMOVE(spi_send_buff, c);
107
				usb_puts("SPDR=["); usb_puti(c);usb_putc(',');usb_puti(spi_status);usb_puts("]\n\r");
127
				//usb_puts("SPDR=["); usb_puti(c);usb_putc(',');usb_puti(spi_status);usb_puts("]\n\r");
108 128
				SPDR = c;
109
				c = SPDR;
110
				usb_puts("c=["); usb_puti(c);usb_putc(',');usb_puti(spi_status);usb_puts("]\n");
129
				//c = SPDR;
130
				//usb_puts("c=["); usb_puti(c);usb_putc(',');usb_puti(spi_status);usb_puts("]\n");
111 131
				
112 132
			} else {
133
				if (spi_mode == MASTER)
134
					PORTB |= SS;		//Turn off Slave select
113 135
				spi_status = SPI_VOID;
114 136
			}
115 137
			break;
116 138
		case SPI_VOID:
117
			spi_recv_function(c);
139
			spi_recv_function(SPDR);
118 140
			break;
119 141
	}
120
		SPSR ^= (1 << SPIF);
142

  
121 143
	usb_puts("ISR: end\n");
122 144
}		

Also available in: Unified diff