RadioHead
RHSPIDriver.h
1// RHSPIDriver.h
2// Author: Mike McCauley (mikem@airspayce.com)
3// Copyright (C) 2014 Mike McCauley
4// $Id: RHSPIDriver.h,v 1.16 2020/06/15 23:39:39 mikem Exp $
5
6#ifndef RHSPIDriver_h
7#define RHSPIDriver_h
8
9#include <RHGenericDriver.h>
10#include <RHHardwareSPI.h>
11
12// This is the bit in the SPI address that marks it as a write
13#define RH_SPI_WRITE_MASK 0x80
14
15class RHGenericSPI;
16
17/////////////////////////////////////////////////////////////////////
18/// \class RHSPIDriver RHSPIDriver.h <RHSPIDriver.h>
19/// \brief Base class for RadioHead drivers that use the SPI bus
20/// to communicate with its transport hardware.
21///
22/// This class can be subclassed by Drivers that require to use the SPI bus.
23/// It can be configured to use either the RHHardwareSPI class (if there is one available on the platform)
24/// of the bitbanged RHSoftwareSPI class. The default behaviour is to use a pre-instantiated built-in RHHardwareSPI
25/// interface.
26///
27/// SPI bus access is protected by ATOMIC_BLOCK_START and ATOMIC_BLOCK_END, which will ensure interrupts
28/// are disabled during access.
29///
30/// The read and write routines implement commonly used SPI conventions: specifically that the MSB
31/// of the first byte transmitted indicates that it is a write and the remaining bits indicate the rehgister to access)
32/// This can be overriden
33/// in subclasses if necessaryor an alternative class, RHNRFSPIDriver can be used to access devices like
34/// Nordic NRF series radios, which have different requirements.
35///
36/// Application developers are not expected to instantiate this class directly:
37/// it is for the use of Driver developers.
39{
40public:
41 /// Constructor
42 /// \param[in] slaveSelectPin The controler pin to use to select the desired SPI device. This pin will be driven LOW
43 /// during SPI communications with the SPI device that uis iused by this Driver.
44 /// \param[in] spi Reference to the SPI interface to use. The default is to use a default built-in Hardware interface.
45 RHSPIDriver(uint8_t slaveSelectPin = SS, RHGenericSPI& spi = hardware_spi);
46
47 /// Initialise the Driver transport hardware and software.
48 /// Make sure the Driver is properly configured before calling init().
49 /// \return true if initialisation succeeded.
50 bool init();
51
52 /// Reads a single register from the SPI device
53 /// \param[in] reg Register number
54 /// \return The value of the register
55 uint8_t spiRead(uint8_t reg);
56
57 /// Writes a single byte to the SPI device
58 /// \param[in] reg Register number
59 /// \param[in] val The value to write
60 /// \return Some devices return a status byte during the first data transfer. This byte is returned.
61 /// it may or may not be meaningfule depending on the the type of device being accessed.
62 uint8_t spiWrite(uint8_t reg, uint8_t val);
63
64 /// Reads a number of consecutive registers from the SPI device using burst read mode
65 /// \param[in] reg Register number of the first register
66 /// \param[in] dest Array to write the register values to. Must be at least len bytes
67 /// \param[in] len Number of bytes to read
68 /// \return Some devices return a status byte during the first data transfer. This byte is returned.
69 /// it may or may not be meaningfule depending on the the type of device being accessed.
70 uint8_t spiBurstRead(uint8_t reg, uint8_t* dest, uint8_t len);
71
72 /// Write a number of consecutive registers using burst write mode
73 /// \param[in] reg Register number of the first register
74 /// \param[in] src Array of new register values to write. Must be at least len bytes
75 /// \param[in] len Number of bytes to write
76 /// \return Some devices return a status byte during the first data transfer. This byte is returned.
77 /// it may or may not be meaningfule depending on the the type of device being accessed.
78 uint8_t spiBurstWrite(uint8_t reg, const uint8_t* src, uint8_t len);
79
80 /// Set or change the pin to be used for SPI slave select.
81 /// This can be called at any time to change the
82 /// pin that will be used for slave select in subsquent SPI operations.
83 /// \param[in] slaveSelectPin The pin to use
84 void setSlaveSelectPin(uint8_t slaveSelectPin);
85
86 /// Set the SPI interrupt number
87 /// If SPI transactions can occur within an interrupt, tell the low level SPI
88 /// interface which interrupt is used
89 /// \param[in] interruptNumber the interrupt number
90 void spiUsingInterrupt(uint8_t interruptNumber);
91
92 protected:
93
94 /// Signal the start of an SPI transaction that must not be interrupted by other SPI actions
95 /// In subclasses that support transactions this will ensure that other SPI transactions
96 /// are blocked until this one is completed by endTransaction().
97 /// Selects the slave with selectSlave()
98 virtual void beginTransaction();
99
100 /// Signal the end of an SPI transaction
101 /// Deelects the slave with deselectSlave()
102 virtual void endTransaction();
103
104 // Override this if you need an unusual way of selecting the slave before SPI transactions
105 // The default uses digitalWrite(_slaveSelectPin, LOW)
106 virtual void selectSlave();
107
108 // Override this if you need an unusual way of selecting the slave before SPI transactions
109 // The default uses digitalWrite(_slaveSelectPin, HIGH)
110 virtual void deselectSlave();
111
112 /// Reference to the RHGenericSPI instance to use to transfer data with the SPI device
114
115 /// The pin number of the Slave Select pin that is used to select the desired device.
117};
118
119#endif
Abstract base class for a RadioHead driver.
Definition: RHGenericDriver.h:42
Base class for SPI interfaces.
Definition: RHGenericSPI.h:31
Base class for RadioHead drivers that use the SPI bus to communicate with its transport hardware.
Definition: RHSPIDriver.h:39
virtual void beginTransaction()
Definition: RHSPIDriver.cpp:110
void spiUsingInterrupt(uint8_t interruptNumber)
Definition: RHSPIDriver.cpp:105
uint8_t spiWrite(uint8_t reg, uint8_t val)
Definition: RHSPIDriver.cpp:59
RHSPIDriver(uint8_t slaveSelectPin=SS, RHGenericSPI &spi=hardware_spi)
Definition: RHSPIDriver.cpp:8
bool init()
Definition: RHSPIDriver.cpp:15
uint8_t spiBurstWrite(uint8_t reg, const uint8_t *src, uint8_t len)
Definition: RHSPIDriver.cpp:87
void setSlaveSelectPin(uint8_t slaveSelectPin)
Definition: RHSPIDriver.cpp:100
uint8_t spiBurstRead(uint8_t reg, uint8_t *dest, uint8_t len)
Definition: RHSPIDriver.cpp:74
uint8_t _slaveSelectPin
The pin number of the Slave Select pin that is used to select the desired device.
Definition: RHSPIDriver.h:116
uint8_t spiRead(uint8_t reg)
Definition: RHSPIDriver.cpp:47
RHGenericSPI & _spi
Reference to the RHGenericSPI instance to use to transfer data with the SPI device.
Definition: RHSPIDriver.h:113
virtual void endTransaction()
Definition: RHSPIDriver.cpp:116