RadioHead
RHNRFSPIDriver.h
1 // RHNRFSPIDriver.h
2 // Author: Mike McCauley (mikem@airspayce.com)
3 // Copyright (C) 2014 Mike McCauley
4 // $Id: RHNRFSPIDriver.h,v 1.5 2017/11/06 00:04:08 mikem Exp $
5 
6 #ifndef RHNRFSPIDriver_h
7 #define RHNRFSPIDriver_h
8 
9 #include <RHGenericDriver.h>
10 #include <RHHardwareSPI.h>
11 
12 class RHGenericSPI;
13 
14 /////////////////////////////////////////////////////////////////////
15 /// \class RHNRFSPIDriver RHNRFSPIDriver.h <RHNRFSPIDriver.h>
16 /// \brief Base class for RadioHead drivers that use the SPI bus
17 /// to communicate with its NRF family transport hardware.
18 ///
19 /// This class can be subclassed by Drivers that require to use the SPI bus.
20 /// It can be configured to use either the RHHardwareSPI class (if there is one available on the platform)
21 /// of the bitbanged RHSoftwareSPI class. The dfault behaviour is to use a pre-instantiated built-in RHHardwareSPI
22 /// interface.
23 ///
24 /// SPI bus access is protected by ATOMIC_BLOCK_START and ATOMIC_BLOCK_END, which will ensure interrupts
25 /// are disabled during access.
26 ///
27 /// The read and write routines use SPI conventions as used by Nordic NRF radios and otehr devices,
28 /// but these can be overriden
29 /// in subclasses if necessary.
30 ///
31 /// Application developers are not expected to instantiate this class directly:
32 /// it is for the use of Driver developers.
34 {
35 public:
36  /// Constructor
37  /// \param[in] slaveSelectPin The controller pin to use to select the desired SPI device. This pin will be driven LOW
38  /// during SPI communications with the SPI device that uis iused by this Driver.
39  /// \param[in] spi Reference to the SPI interface to use. The default is to use a default built-in Hardware interface.
40  RHNRFSPIDriver(uint8_t slaveSelectPin = SS, RHGenericSPI& spi = hardware_spi);
41 
42  /// Initialise the Driver transport hardware and software.
43  /// Make sure the Driver is properly configured before calling init().
44  /// \return true if initialisation succeeded.
45  bool init();
46 
47  /// Sends a single command to the device
48  /// \param[in] command The command code to send to the device.
49  /// \return Some devices return a status byte during the first data transfer. This byte is returned.
50  /// it may or may not be meaningfule depending on the the type of device being accessed.
51  uint8_t spiCommand(uint8_t command);
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  /// Reference to the RHGenericSPI instance to use to trasnfer data with teh SPI device
96 
97  /// The pin number of the Slave Select pin that is used to select the desired device.
98  uint8_t _slaveSelectPin;
99 };
100 
101 #endif
RHNRFSPIDriver::spiCommand
uint8_t spiCommand(uint8_t command)
Definition: RHNRFSPIDriver.cpp:31
RHGenericSPI
Base class for SPI interfaces.
Definition: RHGenericSPI.h:30
RHNRFSPIDriver::spiRead
uint8_t spiRead(uint8_t reg)
Definition: RHNRFSPIDriver.cpp:48
RHGenericSPI::beginTransaction
virtual void beginTransaction()
Definition: RHGenericSPI.h:155
RHNRFSPIDriver::setSlaveSelectPin
void setSlaveSelectPin(uint8_t slaveSelectPin)
Definition: RHNRFSPIDriver.cpp:128
RHNRFSPIDriver::RHNRFSPIDriver
RHNRFSPIDriver(uint8_t slaveSelectPin=SS, RHGenericSPI &spi=hardware_spi)
Definition: RHNRFSPIDriver.cpp:8
RHGenericSPI::usingInterrupt
virtual void usingInterrupt(uint8_t interruptNumber)
Definition: RHGenericSPI.h:168
RHGenericDriver
Abstract base class for a RadioHead driver.
Definition: RHGenericDriver.h:41
RHNRFSPIDriver::init
bool init()
Definition: RHNRFSPIDriver.cpp:15
RHNRFSPIDriver::spiBurstWrite
uint8_t spiBurstWrite(uint8_t reg, const uint8_t *src, uint8_t len)
Definition: RHNRFSPIDriver.cpp:109
RHNRFSPIDriver::spiUsingInterrupt
void spiUsingInterrupt(uint8_t interruptNumber)
Definition: RHNRFSPIDriver.cpp:133
RHNRFSPIDriver::_slaveSelectPin
uint8_t _slaveSelectPin
The pin number of the Slave Select pin that is used to select the desired device.
Definition: RHNRFSPIDriver.h:98
RHGenericSPI::begin
virtual void begin()=0
RHNRFSPIDriver
Base class for RadioHead drivers that use the SPI bus to communicate with its NRF family transport ha...
Definition: RHNRFSPIDriver.h:33
RHNRFSPIDriver::spiWrite
uint8_t spiWrite(uint8_t reg, uint8_t val)
Definition: RHNRFSPIDriver.cpp:66
RHNRFSPIDriver::spiBurstRead
uint8_t spiBurstRead(uint8_t reg, uint8_t *dest, uint8_t len)
Definition: RHNRFSPIDriver.cpp:90
RHNRFSPIDriver::_spi
RHGenericSPI & _spi
Reference to the RHGenericSPI instance to use to trasnfer data with teh SPI device.
Definition: RHNRFSPIDriver.h:95
RHGenericSPI::endTransaction
virtual void endTransaction()
Definition: RHGenericSPI.h:160
RHGenericSPI::transfer
virtual uint8_t transfer(uint8_t data)=0