Project

General

Profile

Revision 546

Added by Kevin Woo about 16 years ago

Modified spi.c/h. Sends 1 packet before failing. Funny light blinking
thing happens when sending, no idea where that came from. It looks like
the orbs are used as transmit lights but there's no code for that as far
as I know.

View differences:

spi.c
51 51
    SPSR = 0x00;
52 52
	SPSR = _BV(SPI2X); 
53 53
	
54
	spi_status = SPI_RECV;
54
	spi_status = SPI_IDLE;
55 55

  
56 56
    /* Set SCLK, SS, MOSI as outputs. MISO as input */
57 57
	if(mode == MASTER) {
......
68 68
	usb_puts("spi_init: end\n");
69 69
}
70 70

  
71
int spi_send(char *data, size_t bytes) {
71
int spi_send(char *data, int bytes) {
72 72

  
73 73
	int i;
74 74

  
......
112 112
void spi_read(int bytes) {
113 113
    
114 114
    cli();
115
    for(i = 1; i < bytes; i++) {
115
    for(int i = 1; i < bytes; i++) {
116 116
        // Fail if the buffer is full
117 117
	    if(RING_BUFFER_FULL(spi_send_buff)) {
118 118
	        sei();
119
	        return -1;
119
	        return;
120 120
	    }
121 121
	
122
	    RING_BUFFER_ADD(spi_send_buff, '\0');
122
	    RING_BUFFER_ADD(spi_send_buff, '\n');
123 123
    }
124 124
    
125 125
    sei();
......
130 130
		PORTB &= ~SS;	//Select slave
131 131
	}
132 132
	
133
    SPDR = *data;
133
    SPDR = '\n';
134 134

  
135 135
}
136 136

  
......
142 142

  
143 143
ISR(SIG_SPI) {
144 144
	char c;
145
	
145

  
146
    //The clock is running so dequeue another byte to send	
146 147
	if(!RING_BUFFER_EMPTY(spi_send_buff)) {//cheap way to test if SPI_SEND
147 148
		RING_BUFFER_REMOVE(spi_send_buff, c);
148 149
		SPDR = c;
149
		
150
		if(spi_mode)
151
			usb_puts("MASTER");
152
		else
153
			usb_puts("SLAVE");
154
			
155
		usb_puts(": sending [");usb_putc(c);usb_puts("]\n\r");
156 150
	
157
	//If we were sending and we are done end the transmission
158
	} else if(spi_status == SPI_SEND) {
159
		spi_status ^= SPI_SEND;
160
		PORTB |= SS; //Sleep slave
161

  
162
    //End the transmission if we are not sending or recieving anymore
163
	} else if(spi_mode == MASTER) {
151
	//If we're the master and we're done sending, end the transmission
152
	} else if (spi_mode == MASTER) {
164 153
	    PORTB |= SS;
165 154
	}
166 155
	
156
	//You always receive something. You need to handle it.
157
    spi_recv_function(SPDR);	
158
    
159
	/*
160
	if(spi_status & SPI_SEND) {
161
		spi_status ^= SPI_SEND;
162
		
163
        if (spi_mode == MASTER) {
164
    		PORTB |= SS;
165
        }
166
    } else if (spi_status & SPI_RECV) {
167
           
168
    }
169
    */
170
    //End the transmission if we are not sending or recieving anymore
171
	//} else if(spi_mode == MASTER) {
172
	//   PORTB |= SS;
173
	//}
174
	
167 175
	//Call the recv function whenver we recieve a byte
168
	if(spi_status == SPI_RECV){
169
		spi_recv_function(SPDR);
176
	//if(spi_status == SPI_RECV){
177
		//spi_recv_function(SPDR);
170 178
		//if(spi_mode == MASTER)
171 179
			//PORTB |= SS;
172
	}
180
	//}
173 181
}		
174 182

  

Also available in: Unified diff