Project

General

Profile

Revision 367

Added by Kevin Woo about 16 years ago

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

View differences:

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

  
4
#define MODE 1
3 5

  
4
void a1()
5
{
6
	usb_puts("a\n");
6
void recv_func(char c) {
7
	usb_puts("Received: ");
8
	usb_putc(c);
9
	usb_puts("\r\n");
10
	return;
7 11
}
8 12

  
9
void a2()
10
{
11
	usb_puts("b\n");
12
}
13 13

  
14
void a3()
15
{
16
	usb_puts("c\n");
17
}
18 14

  
19
void a4()
20
{
21
	usb_puts("4\n");
22
}
23

  
24
void a5()
25
{
26
	usb_puts("5\n");
27
}
28

  
29
void a6()
30
{
31
	usb_puts("6\n");
32
}
33

  
34
void a7()
35
{
36
	usb_puts("7\n");
37
}
38

  
39
void a8()
40
{
41
	usb_puts("8\n");
42
}
43

  
44
void a9()
45
{
46
	usb_puts("9\n");
47
}
48

  
49
void a10()
50
{
51
	usb_puts("A");
52
}
53

  
54 15
int main(void)
55 16
{
56 17
	dragonfly_init(ALL_ON);
57
	void (*functions[])(void) = {a1};
58
	void (*allF[])(void) = {a1,a2,a3,a4,a5,a6,a7,a8,a9,a10};
59
	int test[] = {20};
18
	spi_init(MODE, recv_func);
19
	usb_puts("Init done\r\n");
20
	while(1) {
21
	//usb_puts("loop\r\n");
22
		if (MODE) {
60 23
	
61
	rtc_multi_init(test, functions, 1);
62
	
63
	while(1){
64
		if(button1_click() == 1)
65
		{
66
			int time = rtc_get_function_count();
67
			usb_puts("number of functions: ");
68
			usb_puti(rtc_add_function(allF[time], time*10));
69
			usb_puts("\n");
24
				spi_send("b", 1);
25
				delay_ms(1000);
70 26
		}
71
		if(button2_click() == 1)
72
		{
73
			int time = rtc_get_function_count();
74
			if(time > 0)
75
			{
76
				usb_puts("number of functions: ");
77
				usb_puti(rtc_remove_function(allF[time-1]));
78
				usb_puts("\n");
79
			}
80
		}
81
	};
27
		
28
	}
82 29
	
83 30
	return 0;
84 31
	
branches/encoders/code/projects/template/Makefile
116 116
endif
117 117

  
118 118
# Place -I, -L options here
119
CINCS = -I$(COLONYROOT)/code/lib/include/libdragonfly 
120
CINCS += -L$(COLONYROOT)/code/lib/bin
121
ifdef USE_WIRELESS
122
	CINCS += -I$(COLONYROOT)/code/lib/include/libwireless
123
endif
119
CINCS = -I../libdragonfly 
120
CINCS += -L../libdragonfly
124 121

  
125 122
#---------------- Compiler Options ----------------
126 123
#  -g*:          generate debugging information
branches/encoders/code/projects/libdragonfly/dragonfly_lib.h
65 65
#include <bom.h>
66 66
#include <move.h>
67 67
#include <reset.h>
68
//#include "spi.h"
68 69
#include <math.h>
69 70

  
70 71
#endif
branches/encoders/code/projects/libdragonfly/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
}		
branches/encoders/code/projects/libdragonfly/spi.h
6 6
#define SLAVE 0
7 7
#define SPI_SENDING 1
8 8
#define SPI_VOID 0
9
#define SPI_MASTER_RECV 2
9 10

  
10 11
/* Pin Defines */
11 12
#define MOSI _BV(PB2)
......
16 17
#define size_t int
17 18
typedef void (*spi_fun_recv_t)(char);
18 19
typedef void (*spi_fun_send_t)(char);
19
int spi_init(char mode, spi_fun_send_t send_func, spi_fun_recv_t recv_func);
20
void spi_init(char mode, spi_fun_recv_t recv_func);
20 21
int spi_send(char *data, size_t bytes);
21

  
22
void spi_master_recv_on(void);
23
void spi_master_recv_off(void);

Also available in: Unified diff