forked from QUT-EESS/libRobotDev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRDSPI.h
188 lines (156 loc) · 3.71 KB
/
RDSPI.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* libRobotDev
* RDSPI.h
* Purpose: Abstracts all SPI functions
* Created: 04/12/2014
* Author(s): Arda Yilmaz, Jeremy Pearson
* Status: UNTESTED
*/
/*
* USAGE
*
* MASTER MODE
*
* #define SPI_MASTER 1 // Optional, defaults to 1
* #include "RDSPI.h"
*
* SLAVE MODE
*
* #define SPI_MASTER 0 // Required
* #include "RDSPI.h"
*
*/
#ifndef RDSPI_H_
/**
* Robot Development In-System Programming Header.
*/
#define RDSPI_H_
#include <avr/io.h>
#if SPI_MASTER == 0
#include <avr/interrupt.h>
/**
* Stores the current byte received over SPI.
*/
volatile uint8_t RDSPI_RxData;
#endif // SPI_MASTER
/**
* In-System Programming Port.
*/
#define SPIPORT DDRB
#ifndef SPI_MASTER
/**
* Setting for whether the mode will be master (1) or slave (0).
*/
#define SPI_MASTER 1
#endif // SPI_MASTER
/**
* Initialises SPI.
*
* @param endian
* 0: Little-endian (Most-Significant-Bit First),
* 1: Big-endian (Most-Significant-Bit Last).
*
* @param mode
* 0: (Sample Rising, Setup Falling),
* 1: (Setup Rising, Sample Falling),
* 2: (Sample Falling, Setup Rising),
* 3: (Setup Falling, Sample Rising).
*/
void RDSPIInit(uint8_t endian, uint8_t mode) {
// Set SPI port directions
SPIPORT = (SPIPORT & ~(1 << MOSI)) | (SPI_MASTER << MOSI);
SPIPORT = (SPIPORT & ~(1 << MISO)) | (!SPI_MASTER << MISO);
SPIPORT = (SPIPORT & ~(1 << SCLK)) | (SPI_MASTER << SCLK);
// Set endianness, master/slave mode, clock polarity and phase.
SPCR = (SPCR & ~(1 << DORD) ) | (endian << DORD) | (SPI_MASTER << MSTR) | (mode << CPHA);
SPCR |= (1 << SPE);
#if SPI_MASTER == 0
SPCR |= (1 << SPE) | (1 << SPIE);
sei();
#endif // SPI_MASTER
}
#if SPI_MASTER == 1
/**
* Sets the SPI Clock's frequency.
*
* @param frq
* The frequency to set the clock to.
* 0: (F_CPU / 4),
* 1: (F_CPU / 16),
* 2: (F_CPU / 64),
* 3: (F_CPU / 128),
* 4: (F_CPU / 2),
* 5: (F_CPU / 8),
* 6: (F_CPU / 32),
* 7: (F_CPU / 64).
*/
static inline void RDSPISetClock(uint8_t frq) {
SPCR = (SPCR & ~(3 << SPR0)) | (3 & frq);
SPSR = (SPSR & ~(1 << SPI2X)) | (frq >> 2);
}
/**
* Reads and writes using SPI.
*
* @param byte
* The byte to transmit.
*
* @param frq
* The frequency to set the clock to.
* 0: (F_CPU / 4),
* 1: (F_CPU / 16),
* 2: (F_CPU / 64),
* 3: (F_CPU / 128),
* 4: (F_CPU / 2),
* 5: (F_CPU / 8),
* 6: (F_CPU / 32),
* 7: (F_CPU / 64).
*
* @param port
* The port that the chip select pin is on.
*
* @param chipSelectPin
* The chip select pin.
*
* @return
* A byte from the slave, if present.
*/
uint8_t RDSPIRWByte(uint8_t byte, uint8_t frq, volatile uint8_t *port,
uint8_t chipSelectPin) {
// Disable SPI
//SPCR &= ~(1 << SPE);
// Set clock speed
RDSPISetClock(frq);
// Re-enable SPI
//SPCR |= (1 << SPE);
// Pull chip select (CS) low
*port &= ~(1 << chipSelectPin);
// Send byte
SPDR = byte;
while(!(SPSR & (1<<SPIF)));
// Pull chip select (CS) high
*port |= (1 << chipSelectPin);
// return anything shifted from slave
return SPDR;
}
#else
/**
* Transmits a byte from the slave using SPI.
*
* @param byte
* The byte to transmit.
*/
void RDSPISlaveTxByte(uint8_t byte) {
while(!(SPSR & (1<<SPIF)));
SPDR = byte;
}
/**
* Whenever a Serial Transfer Complete interrupt flag is triggered, this
* Interrupt Service Routine will store the data received, in RDSPI_RxData.
* Then it will reset SPDR to 0.
*/
ISR(SPI_STC_vect) {
RDSPI_RxData = SPDR;
SPDR = 0;
}
#endif // SPI_MASTER == 1
#endif // RDSPI_H_