Project

General

Profile

Revision 490

Added by Ben Poole over 14 years ago

slave -> master kind of works

View differences:

branches/encoders/code/projects/template/main.c
2 2
#include "spi.h"
3 3

  
4 4
#define MODE 0
5
#define TEST 1
6
extern char spi_status;
5 7

  
6 8
void recv_func(char c) {
7
	usb_puts("Received: ");
8
	usb_putc(c);
9
	usb_puts("\r\n");
9
	if(MODE)
10
		usb_puts("MASTER");
11
	else
12
		usb_puts("SLAVE");
13
	usb_puts(": received [");usb_putc(c);usb_puts("]\n\r");
10 14
	return;
11 15
}
12 16

  
......
16 20
{
17 21
	dragonfly_init(ALL_ON);
18 22
	spi_init(MODE, recv_func);
19
	usb_puts("Init done\r\n");
23
	usb_puts("Init done\n\r");
20 24
	while(1) {
21
	//usb_puts("loop\r\n");
22
		if (MODE) {
23
			spi_master_recv_on();
24
		}else {
25
			SPDR = 'b';
26
		//	delay_ms(10);
25
		if(TEST){
26
			if(MODE){
27
				spi_read_one();
28
				delay_ms(1000);
29
			} else {
30
				if(!(spi_status &SPI_SEND))
31
				spi_send("SLAVE",6);
32
			}
33
		} else {
34
			if (MODE) {
35
				spi_send("MASTER",6);
36
			}
27 37
		}
28 38
		
29 39
	}
branches/encoders/code/projects/libdragonfly/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
}		
branches/encoders/code/projects/libdragonfly/spi.h
1
#ifndef __SPI_H__
2
#define __SPI_H__
3

  
1 4
#define DOUBLE_SCK 1
2 5
#define SPR0_BIT 1
3 6
#define LSB 1
4 7
#define MSB 0
5 8
#define MASTER 1
6 9
#define SLAVE 0
7
#define SPI_SENDING 1
8
#define SPI_VOID 0
9
#define SPI_MASTER_RECV 2
10 10

  
11
#define SPI_IDLE _BV(0)
12
#define SPI_SEND _BV(1)
13
#define SPI_RECV _BV(2)
14
#define SPI_BOTH _BV(3)
15

  
16

  
11 17
/* Pin Defines */
12 18
#define MOSI _BV(PB2)
13 19
#define MISO _BV(PB3)
......
21 27
int spi_send(char *data, size_t bytes);
22 28
void spi_master_recv_on(void);
23 29
void spi_master_recv_off(void);
30
void spi_read_one(void);
31

  
32
#endif

Also available in: Unified diff