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
12class 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{
35public:
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
93protected:
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 virtual void beginTransaction();
98
99 /// Signal the end of an SPI transaction
100 virtual void endTransaction();
101
102 /// Reference to the RHGenericSPI instance to use to trasnfer data with teh SPI device
104
105 /// The pin number of the Slave Select pin that is used to select the desired device.
107};
108
109#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 NRF family transport ha...
Definition: RHNRFSPIDriver.h:34
virtual void beginTransaction()
Definition: RHNRFSPIDriver.cpp:128
bool init()
Definition: RHNRFSPIDriver.cpp:15
uint8_t spiRead(uint8_t reg)
Definition: RHNRFSPIDriver.cpp:46
uint8_t spiBurstRead(uint8_t reg, uint8_t *dest, uint8_t len)
Definition: RHNRFSPIDriver.cpp:84
uint8_t spiWrite(uint8_t reg, uint8_t val)
Definition: RHNRFSPIDriver.cpp:62
uint8_t _slaveSelectPin
The pin number of the Slave Select pin that is used to select the desired device.
Definition: RHNRFSPIDriver.h:106
uint8_t spiCommand(uint8_t command)
Definition: RHNRFSPIDriver.cpp:31
RHGenericSPI & _spi
Reference to the RHGenericSPI instance to use to trasnfer data with teh SPI device.
Definition: RHNRFSPIDriver.h:103
virtual void endTransaction()
Signal the end of an SPI transaction.
Definition: RHNRFSPIDriver.cpp:134
void spiUsingInterrupt(uint8_t interruptNumber)
Definition: RHNRFSPIDriver.cpp:123
uint8_t spiBurstWrite(uint8_t reg, const uint8_t *src, uint8_t len)
Definition: RHNRFSPIDriver.cpp:101
RHNRFSPIDriver(uint8_t slaveSelectPin=SS, RHGenericSPI &spi=hardware_spi)
Definition: RHNRFSPIDriver.cpp:8
void setSlaveSelectPin(uint8_t slaveSelectPin)
Definition: RHNRFSPIDriver.cpp:118