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.14 2019/09/06 04:40:40 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 
15 class 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 {
40 public:
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  /// Reference to the RHGenericSPI instance to use to transfer data with the SPI device
95 
96  /// The pin number of the Slave Select pin that is used to select the desired device.
97  uint8_t _slaveSelectPin;
98 };
99 
100 #endif
RHGenericSPI
Base class for SPI interfaces.
Definition: RHGenericSPI.h:30
RHGenericSPI::beginTransaction
virtual void beginTransaction()
Definition: RHGenericSPI.h:155
RHSPIDriver::spiWrite
uint8_t spiWrite(uint8_t reg, uint8_t val)
Definition: RHSPIDriver.cpp:42
RHSPIDriver
Base class for RadioHead drivers that use the SPI bus to communicate with its transport hardware.
Definition: RHSPIDriver.h:38
RHGenericSPI::usingInterrupt
virtual void usingInterrupt(uint8_t interruptNumber)
Definition: RHGenericSPI.h:168
RHGenericDriver
Abstract base class for a RadioHead driver.
Definition: RHGenericDriver.h:41
RHSPIDriver::spiUsingInterrupt
void spiUsingInterrupt(uint8_t interruptNumber)
Definition: RHSPIDriver.cpp:91
RHSPIDriver::spiBurstRead
uint8_t spiBurstRead(uint8_t reg, uint8_t *dest, uint8_t len)
Definition: RHSPIDriver.cpp:56
RHSPIDriver::spiBurstWrite
uint8_t spiBurstWrite(uint8_t reg, const uint8_t *src, uint8_t len)
Definition: RHSPIDriver.cpp:71
RHSPIDriver::init
bool init()
Definition: RHSPIDriver.cpp:15
RHSPIDriver::_slaveSelectPin
uint8_t _slaveSelectPin
The pin number of the Slave Select pin that is used to select the desired device.
Definition: RHSPIDriver.h:97
RHGenericSPI::begin
virtual void begin()=0
RHSPIDriver::setSlaveSelectPin
void setSlaveSelectPin(uint8_t slaveSelectPin)
Definition: RHSPIDriver.cpp:86
RHSPIDriver::RHSPIDriver
RHSPIDriver(uint8_t slaveSelectPin=SS, RHGenericSPI &spi=hardware_spi)
Definition: RHSPIDriver.cpp:8
RHSPIDriver::spiRead
uint8_t spiRead(uint8_t reg)
Definition: RHSPIDriver.cpp:30
RHSPIDriver::_spi
RHGenericSPI & _spi
Reference to the RHGenericSPI instance to use to transfer data with the SPI device.
Definition: RHSPIDriver.h:94
RHGenericSPI::endTransaction
virtual void endTransaction()
Definition: RHGenericSPI.h:160
RHGenericSPI::transfer
virtual uint8_t transfer(uint8_t data)=0