Project

General

Profile

Revision 490

Added by Ben Poole over 14 years ago

slave -> master kind of works

View differences:

spi.c
37 37

  
38 38
	spi_mode = mode;
39 39

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

  
......
52 51
    SPSR = 0x00;
53 52
	SPSR = _BV(SPI2X); 
54 53
	
55
	spi_status = SPI_VOID;
54
	spi_status = SPI_RECV;
56 55

  
57 56
    /* Set SCLK, SS, MOSI as outputs. MISO as input */
58 57
	if(mode == MASTER) {
......
72 71
int spi_send(char *data, size_t bytes) {
73 72

  
74 73
	int i;
75
	    
76
	usb_puts("spi_send: start\n");
77 74

  
78 75
    if(bytes == 0) 
79 76
        return -1; /* ...needed?*/
......
92 89
    
93 90
    sei();
94 91
    
95
    spi_status = SPI_SENDING;
92
    spi_status |= SPI_SEND;
96 93
	
97 94
	if (spi_mode == MASTER ){
98 95
		PORTB &= ~SS;	//Select slave
99 96
	}
100 97
    SPDR = *data;
98
	if(spi_mode)
99
		usb_puts("MASTER");
100
	else
101
		usb_puts("SLAVE");
102
	usb_puts(": sending [");usb_putc(*data);usb_puts("]\n\r");
101 103
	
102
	usb_puts("spi_send: end\n");
103 104
	//sei();
104 105
	
105 106
	return 1;
106 107
}
107 108

  
108
void spi_master_recv_on() {
109
	spi_status = SPI_VOID;
109
void spi_read_one(void)
110
{
110 111
	PORTB &= ~SS;
112
	SPDR = 'x';
111 113
}
112 114

  
113
void spi_master_recv_off() {
114
	spi_status = SPI_VOID;
115
	PORTB |= SS;
116
}
117

  
118 115
ISR(SIG_SPI) {
119
    usb_puts("ISR: start\n\r");
120 116
	char c;
121

  
122
	//usb_putc('['); usb_puti(c);usb_putc(',');usb_puti(spi_status);usb_puts("]\n\r");
123
	switch(spi_status){
124
		case SPI_SENDING:
125
			if(!RING_BUFFER_EMPTY(spi_send_buff)) {
126
				RING_BUFFER_REMOVE(spi_send_buff, c);
127
				//usb_puts("SPDR=["); usb_puti(c);usb_putc(',');usb_puti(spi_status);usb_puts("]\n\r");
128
				SPDR = c;
129
				//c = SPDR;
130
				//usb_puts("c=["); usb_puti(c);usb_putc(',');usb_puti(spi_status);usb_puts("]\n");
131
				
132
			} else {
133
				if (spi_mode == MASTER)
134
					PORTB |= SS;		//Turn off Slave select
135
				spi_status = SPI_VOID;
136
			}
137
			break;
138
		case SPI_VOID:
139
			spi_recv_function(SPDR);
140
			break;
117
	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;
141 133
	}
134
}		
142 135

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

Also available in: Unified diff