David Rimer / RadioHead-148
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RHNRFSPIDriver.h Source File

RHNRFSPIDriver.h

00001 // RHNRFSPIDriver.h
00002 // Author: Mike McCauley (mikem@airspayce.com)
00003 // Copyright (C) 2014 Mike McCauley
00004 // $Id: RHNRFSPIDriver.h,v 1.2 2014/08/12 00:54:52 mikem Exp $
00005 
00006 #ifndef RHNRFSPIDriver_h
00007 #define RHNRFSPIDriver_h
00008 
00009 #include <RHGenericDriver.h>
00010 #include <RHHardwareSPI.h>
00011 
00012 class RHGenericSPI;
00013 
00014 /////////////////////////////////////////////////////////////////////
00015 /// \class RHNRFSPIDriver RHNRFSPIDriver.h <RHNRFSPIDriver.h>
00016 /// \brief Base class for a RadioHead driver that use the SPI bus
00017 /// to communicate with its transport hardware.
00018 ///
00019 /// This class can be subclassed by Drivers that require to use the SPI bus.
00020 /// It can be configured to use either the RHHardwareSPI class (if there is one available on the platform)
00021 /// of the bitbanged RHSoftwareSPI class. The dfault behaviour is to use a pre-instantiated built-in RHHardwareSPI
00022 /// interface.
00023 /// 
00024 /// SPI bus access is protected by ATOMIC_BLOCK_START and ATOMIC_BLOCK_END, which will ensure interrupts 
00025 /// are disabled during access.
00026 /// 
00027 /// The read and write routines use SPI conventions as used by Nordic NRF radios and otehr devices, 
00028 /// but these can be overriden 
00029 /// in subclasses if necessary.
00030 ///
00031 /// Application developers are not expected to instantiate this class directly: 
00032 /// it is for the use of Driver developers.
00033 class RHNRFSPIDriver : public RHGenericDriver
00034 {
00035 public:
00036     /// Constructor
00037     /// \param[in] slaveSelectPin The controller pin to use to select the desired SPI device. This pin will be driven LOW
00038     /// during SPI communications with the SPI device that uis iused by this Driver.
00039     /// \param[in] spi Reference to the SPI interface to use. The default is to use a default built-in Hardware interface.
00040     RHNRFSPIDriver(PINS slaveSelectPin, RHGenericSPI& spi = hardware_spi);
00041 
00042     /// Initialise the Driver transport hardware and software.
00043     /// Make sure the Driver is properly configured before calling init().
00044     /// \return true if initialisation succeeded.
00045     bool init();
00046 
00047     /// Sends a single command to the device
00048     /// \param[in] command The command code to send to the device.
00049     /// \return Some devices return a status byte during the first data transfer. This byte is returned.
00050     ///  it may or may not be meaningfule depending on the the type of device being accessed.
00051     uint8_t spiCommand(uint8_t command);
00052 
00053     /// Reads a single register from the SPI device
00054     /// \param[in] reg Register number
00055     /// \return The value of the register
00056     uint8_t        spiRead(uint8_t reg);
00057 
00058     /// Writes a single byte to the SPI device
00059     /// \param[in] reg Register number
00060     /// \param[in] val The value to write
00061     /// \return Some devices return a status byte during the first data transfer. This byte is returned.
00062     ///  it may or may not be meaningfule depending on the the type of device being accessed.
00063     uint8_t           spiWrite(uint8_t reg, uint8_t val);
00064 
00065     /// Reads a number of consecutive registers from the SPI device using burst read mode
00066     /// \param[in] reg Register number of the first register
00067     /// \param[in] dest Array to write the register values to. Must be at least len bytes
00068     /// \param[in] len Number of bytes to read
00069     /// \return Some devices return a status byte during the first data transfer. This byte is returned.
00070     ///  it may or may not be meaningfule depending on the the type of device being accessed.
00071     uint8_t           spiBurstRead(uint8_t reg, uint8_t* dest, uint8_t len);
00072 
00073     /// Write a number of consecutive registers using burst write mode
00074     /// \param[in] reg Register number of the first register
00075     /// \param[in] src Array of new register values to write. Must be at least len bytes
00076     /// \param[in] len Number of bytes to write
00077     /// \return Some devices return a status byte during the first data transfer. This byte is returned.
00078     ///  it may or may not be meaningfule depending on the the type of device being accessed.
00079     uint8_t           spiBurstWrite(uint8_t reg, const uint8_t* src, uint8_t len);
00080 
00081 protected:
00082     /// Reference to the RHGenericSPI instance to use to trasnfer data with teh SPI device
00083     RHGenericSPI&       _spi;
00084 
00085     /// The pin number of the Slave Selct pin that is used to select the desired device.
00086 #if (RH_PLATFORM == RH_PLATFORM_MBED)
00087     DigitalOut          _slaveSelectPin;
00088 #else
00089     uint8_t             _slaveSelectPin;
00090 #endif
00091 };
00092 
00093 #endif