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 /// If slaveSelectPin is 0xff, then the pin will not be initialised or activated by this class.
45 /// \param[in] spi Reference to the SPI interface to use. The default is to use a default built-in Hardware interface.
46 RHSPIDriver(uint8_t slaveSelectPin = SS, RHGenericSPI& spi = hardware_spi);
47
48 /// Initialise the Driver transport hardware and software.
49 /// Make sure the Driver is properly configured before calling init().
50 /// \return true if initialisation succeeded.
51 bool init();
52
53 /// Reads a single register from the SPI device
54 /// \param[in] reg Register number
55 /// \return The value of the register
56 uint8_t spiRead(uint8_t reg);
57
58 /// Writes a single byte to the SPI device
59 /// \param[in] reg Register number
60 /// \param[in] val The value to write
61 /// \return Some devices return a status byte during the first data transfer. This byte is returned.
62 /// it may or may not be meaningfule depending on the the type of device being accessed.
63 uint8_t spiWrite(uint8_t reg, uint8_t val);
64
65 /// Reads a number of consecutive registers from the SPI device using burst read mode
66 /// \param[in] reg Register number of the first register
67 /// \param[in] dest Array to write the register values to. Must be at least len bytes
68 /// \param[in] len Number of bytes to read
69 /// \return Some devices return a status byte during the first data transfer. This byte is returned.
70 /// it may or may not be meaningfule depending on the the type of device being accessed.
71 uint8_t spiBurstRead(uint8_t reg, uint8_t* dest, uint8_t len);
72
73 /// Write a number of consecutive registers using burst write mode
74 /// \param[in] reg Register number of the first register
75 /// \param[in] src Array of new register values to write. Must be at least len bytes
76 /// \param[in] len Number of bytes to write
77 /// \return Some devices return a status byte during the first data transfer. This byte is returned.
78 /// it may or may not be meaningfule depending on the the type of device being accessed.
79 uint8_t spiBurstWrite(uint8_t reg, const uint8_t* src, uint8_t len);
80
81 /// Set or change the pin to be used for SPI slave select.
82 /// This can be called at any time to change the
83 /// pin that will be used for slave select in subsquent SPI operations.
84 /// \param[in] slaveSelectPin The pin to use
85 void setSlaveSelectPin(uint8_t slaveSelectPin);
86
87 /// Set the SPI interrupt number
88 /// If SPI transactions can occur within an interrupt, tell the low level SPI
89 /// interface which interrupt is used
90 /// \param[in] interruptNumber the interrupt number
91 void spiUsingInterrupt(uint8_t interruptNumber);
92
93 protected:
94
95 /// Signal the start of an SPI transaction that must not be interrupted by other SPI actions
96 /// In subclasses that support transactions this will ensure that other SPI transactions
97 /// are blocked until this one is completed by endTransaction().
98 /// Selects the slave with selectSlave()
99 virtual void beginTransaction();
100
101 /// Signal the end of an SPI transaction
102 /// Deelects the slave with deselectSlave()
103 virtual void endTransaction();
104
105 // Override this if you need an unusual way of selecting the slave before SPI transactions
106 // The default uses digitalWrite(_slaveSelectPin, LOW)
107 virtual void selectSlave();
108
109 // Override this if you need an unusual way of selecting the slave before SPI transactions
110 // The default uses digitalWrite(_slaveSelectPin, HIGH)
111 virtual void deselectSlave();
112
113 /// Reference to the RHGenericSPI instance to use to transfer data with the SPI device
115
116 /// The pin number of the Slave Select pin that is used to select the desired device.
118};
119
120#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
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:117
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:114
virtual void endTransaction()
Definition RHSPIDriver.cpp:116