forked from epsilonrt/RadioHead
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRHSPIDriver.cpp
More file actions
126 lines (115 loc) · 3.12 KB
/
Copy pathRHSPIDriver.cpp
File metadata and controls
126 lines (115 loc) · 3.12 KB
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
// RHSPIDriver.cpp
//
// Copyright (C) 2014 Mike McCauley
// $Id: RHSPIDriver.cpp,v 1.11 2017/11/06 00:04:08 mikem Exp $
#include <RHSPIDriver.h>
RHSPIDriver::RHSPIDriver (uint8_t slaveSelectPin, RHGenericSPI& spi)
:
_spi (spi),
_slaveSelectPin (slaveSelectPin)
{
}
bool RHSPIDriver::init()
{
// start the SPI library with the default speeds etc:
// On Arduino Due this defaults to SPI1 on the central group of 6 SPI pins
_spi.begin();
// Initialise the slave select pin
// On Maple, this must be _after_ spi.begin
pinMode (_slaveSelectPin, OUTPUT);
digitalWrite (_slaveSelectPin, HIGH);
delay (100);
return true;
}
uint8_t RHSPIDriver::spiRead (uint8_t reg)
{
uint8_t buf[2];
buf[0] = reg & ~RH_SPI_WRITE_MASK;
buf[1] = 0;
ATOMIC_BLOCK_START;
_spi.beginTransaction();
digitalWrite (_slaveSelectPin, LOW);
_spi.transfer (buf, 2); // Send the address with the write mask on read the value
digitalWrite (_slaveSelectPin, HIGH);
_spi.endTransaction();
ATOMIC_BLOCK_END;
return buf[1];
}
uint8_t RHSPIDriver::spiWrite (uint8_t reg, uint8_t val)
{
uint8_t buf[2];
buf[0] = reg | RH_SPI_WRITE_MASK;
buf[1] = val;
ATOMIC_BLOCK_START;
_spi.beginTransaction();
digitalWrite (_slaveSelectPin, LOW);
_spi.transfer (buf, 2); // Send the address with the write mask on and value
digitalWrite (_slaveSelectPin, HIGH);
_spi.endTransaction();
ATOMIC_BLOCK_END;
return buf[0];
}
uint8_t RHSPIDriver::spiBurstRead (uint8_t reg, uint8_t* dest, uint8_t len)
{
#if (RH_PLATFORM != RH_PLATFORM_PIDUINO)
uint8_t status = 0;
ATOMIC_BLOCK_START;
_spi.beginTransaction();
digitalWrite (_slaveSelectPin, LOW);
status = _spi.transfer (reg & ~RH_SPI_WRITE_MASK); // Send the start address with the write mask off
while (len--)
*dest++ = _spi.transfer (0);
digitalWrite (_slaveSelectPin, HIGH);
_spi.endTransaction();
ATOMIC_BLOCK_END;
return status;
#else
uint8_t * buf = new uint8_t[len + 1];
buf[0] = reg & ~RH_SPI_WRITE_MASK;
_spi.beginTransaction();
digitalWrite (_slaveSelectPin, LOW);
_spi.transfer (buf, len + 1);
digitalWrite (_slaveSelectPin, HIGH);
_spi.endTransaction();
memcpy (dest, &buf[1], len);
reg = buf[0];
delete[] buf;
return reg;
#endif
}
uint8_t RHSPIDriver::spiBurstWrite (uint8_t reg, const uint8_t* src, uint8_t len)
{
#if (RH_PLATFORM != RH_PLATFORM_PIDUINO)
uint8_t status = 0;
ATOMIC_BLOCK_START;
_spi.beginTransaction();
digitalWrite (_slaveSelectPin, LOW);
status = _spi.transfer (reg | RH_SPI_WRITE_MASK); // Send the start address with the write mask on
while (len--)
_spi.transfer (*src++);
digitalWrite (_slaveSelectPin, HIGH);
_spi.endTransaction();
ATOMIC_BLOCK_END;
return status;
#else
uint8_t * buf = new uint8_t[len + 1];
buf[0] = reg | RH_SPI_WRITE_MASK;
memcpy (&buf[1], src, len);
_spi.beginTransaction();
digitalWrite (_slaveSelectPin, LOW);
_spi.transfer (buf, len + 1);
digitalWrite (_slaveSelectPin, HIGH);
_spi.endTransaction();
reg = buf[0];
delete[] buf;
return reg;
#endif
}
void RHSPIDriver::setSlaveSelectPin (uint8_t slaveSelectPin)
{
_slaveSelectPin = slaveSelectPin;
}
void RHSPIDriver::spiUsingInterrupt (uint8_t interruptNumber)
{
_spi.usingInterrupt (interruptNumber);
}