V148
Fork of RadioHead-148 by
RHSPIDriver.h@1:b7641da2b203, 2017-10-25 (annotated)
- Committer:
- ilkaykozak
- Date:
- Wed Oct 25 05:14:09 2017 +0000
- Revision:
- 1:b7641da2b203
- Parent:
- 0:ab4e012489ef
V148
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
davidr99 | 0:ab4e012489ef | 1 | // RHSPIDriver.h |
davidr99 | 0:ab4e012489ef | 2 | // Author: Mike McCauley (mikem@airspayce.com) |
davidr99 | 0:ab4e012489ef | 3 | // Copyright (C) 2014 Mike McCauley |
davidr99 | 0:ab4e012489ef | 4 | // $Id: RHSPIDriver.h,v 1.9 2014/04/23 06:00:59 mikem Exp $ |
davidr99 | 0:ab4e012489ef | 5 | |
davidr99 | 0:ab4e012489ef | 6 | #ifndef RHSPIDriver_h |
davidr99 | 0:ab4e012489ef | 7 | #define RHSPIDriver_h |
davidr99 | 0:ab4e012489ef | 8 | |
davidr99 | 0:ab4e012489ef | 9 | #include <RHGenericDriver.h> |
davidr99 | 0:ab4e012489ef | 10 | #include <RHHardwareSPI.h> |
davidr99 | 0:ab4e012489ef | 11 | |
davidr99 | 0:ab4e012489ef | 12 | // This is the bit in the SPI address that marks it as a write |
davidr99 | 0:ab4e012489ef | 13 | #define RH_SPI_WRITE_MASK 0x80 |
davidr99 | 0:ab4e012489ef | 14 | |
davidr99 | 0:ab4e012489ef | 15 | class RHGenericSPI; |
davidr99 | 0:ab4e012489ef | 16 | |
davidr99 | 0:ab4e012489ef | 17 | ///////////////////////////////////////////////////////////////////// |
davidr99 | 0:ab4e012489ef | 18 | /// \class RHSPIDriver RHSPIDriver.h <RHSPIDriver.h> |
davidr99 | 0:ab4e012489ef | 19 | /// \brief Base class for a RadioHead drivers that use the SPI bus |
davidr99 | 0:ab4e012489ef | 20 | /// to communicate with its transport hardware. |
davidr99 | 0:ab4e012489ef | 21 | /// |
davidr99 | 0:ab4e012489ef | 22 | /// This class can be subclassed by Drivers that require to use the SPI bus. |
davidr99 | 0:ab4e012489ef | 23 | /// It can be configured to use either the RHHardwareSPI class (if there is one available on the platform) |
davidr99 | 0:ab4e012489ef | 24 | /// of the bitbanged RHSoftwareSPI class. The default behaviour is to use a pre-instantiated built-in RHHardwareSPI |
davidr99 | 0:ab4e012489ef | 25 | /// interface. |
davidr99 | 0:ab4e012489ef | 26 | /// |
davidr99 | 0:ab4e012489ef | 27 | /// SPI bus access is protected by ATOMIC_BLOCK_START and ATOMIC_BLOCK_END, which will ensure interrupts |
davidr99 | 0:ab4e012489ef | 28 | /// are disabled during access. |
davidr99 | 0:ab4e012489ef | 29 | /// |
davidr99 | 0:ab4e012489ef | 30 | /// The read and write routines implement commonly used SPI conventions: specifically that the MSB |
davidr99 | 0:ab4e012489ef | 31 | /// of the first byte transmitted indicates that it is a write and the remaining bits indicate the rehgister to access) |
davidr99 | 0:ab4e012489ef | 32 | /// This can be overriden |
davidr99 | 0:ab4e012489ef | 33 | /// in subclasses if necessaryor an alternative class, RHNRFSPIDriver can be used to access devices like |
davidr99 | 0:ab4e012489ef | 34 | /// Nordic NRF series radios, which have different requirements. |
davidr99 | 0:ab4e012489ef | 35 | /// |
davidr99 | 0:ab4e012489ef | 36 | /// Application developers are not expected to instantiate this class directly: |
davidr99 | 0:ab4e012489ef | 37 | /// it is for the use of Driver developers. |
davidr99 | 0:ab4e012489ef | 38 | class RHSPIDriver : public RHGenericDriver |
davidr99 | 0:ab4e012489ef | 39 | { |
davidr99 | 0:ab4e012489ef | 40 | public: |
davidr99 | 0:ab4e012489ef | 41 | /// Constructor |
davidr99 | 0:ab4e012489ef | 42 | /// \param[in] slaveSelectPin The controler pin to use to select the desired SPI device. This pin will be driven LOW |
davidr99 | 0:ab4e012489ef | 43 | /// during SPI communications with the SPI device that uis iused by this Driver. |
davidr99 | 0:ab4e012489ef | 44 | /// \param[in] spi Reference to the SPI interface to use. The default is to use a default built-in Hardware interface. |
davidr99 | 0:ab4e012489ef | 45 | RHSPIDriver(PINS slaveSelectPin, RHGenericSPI& spi = hardware_spi); |
davidr99 | 0:ab4e012489ef | 46 | |
davidr99 | 0:ab4e012489ef | 47 | /// Initialise the Driver transport hardware and software. |
davidr99 | 0:ab4e012489ef | 48 | /// Make sure the Driver is properly configured before calling init(). |
davidr99 | 0:ab4e012489ef | 49 | /// \return true if initialisation succeeded. |
davidr99 | 0:ab4e012489ef | 50 | bool init(); |
davidr99 | 0:ab4e012489ef | 51 | |
davidr99 | 0:ab4e012489ef | 52 | /// Reads a single register from the SPI device |
davidr99 | 0:ab4e012489ef | 53 | /// \param[in] reg Register number |
davidr99 | 0:ab4e012489ef | 54 | /// \return The value of the register |
davidr99 | 0:ab4e012489ef | 55 | uint8_t spiRead(uint8_t reg); |
davidr99 | 0:ab4e012489ef | 56 | |
davidr99 | 0:ab4e012489ef | 57 | /// Writes a single byte to the SPI device |
davidr99 | 0:ab4e012489ef | 58 | /// \param[in] reg Register number |
davidr99 | 0:ab4e012489ef | 59 | /// \param[in] val The value to write |
davidr99 | 0:ab4e012489ef | 60 | /// \return Some devices return a status byte during the first data transfer. This byte is returned. |
davidr99 | 0:ab4e012489ef | 61 | /// it may or may not be meaningfule depending on the the type of device being accessed. |
davidr99 | 0:ab4e012489ef | 62 | uint8_t spiWrite(uint8_t reg, uint8_t val); |
davidr99 | 0:ab4e012489ef | 63 | |
davidr99 | 0:ab4e012489ef | 64 | /// Reads a number of consecutive registers from the SPI device using burst read mode |
davidr99 | 0:ab4e012489ef | 65 | /// \param[in] reg Register number of the first register |
davidr99 | 0:ab4e012489ef | 66 | /// \param[in] dest Array to write the register values to. Must be at least len bytes |
davidr99 | 0:ab4e012489ef | 67 | /// \param[in] len Number of bytes to read |
davidr99 | 0:ab4e012489ef | 68 | /// \return Some devices return a status byte during the first data transfer. This byte is returned. |
davidr99 | 0:ab4e012489ef | 69 | /// it may or may not be meaningfule depending on the the type of device being accessed. |
davidr99 | 0:ab4e012489ef | 70 | uint8_t spiBurstRead(uint8_t reg, uint8_t* dest, uint8_t len); |
davidr99 | 0:ab4e012489ef | 71 | |
davidr99 | 0:ab4e012489ef | 72 | /// Write a number of consecutive registers using burst write mode |
davidr99 | 0:ab4e012489ef | 73 | /// \param[in] reg Register number of the first register |
davidr99 | 0:ab4e012489ef | 74 | /// \param[in] src Array of new register values to write. Must be at least len bytes |
davidr99 | 0:ab4e012489ef | 75 | /// \param[in] len Number of bytes to write |
davidr99 | 0:ab4e012489ef | 76 | /// \return Some devices return a status byte during the first data transfer. This byte is returned. |
davidr99 | 0:ab4e012489ef | 77 | /// it may or may not be meaningfule depending on the the type of device being accessed. |
davidr99 | 0:ab4e012489ef | 78 | uint8_t spiBurstWrite(uint8_t reg, const uint8_t* src, uint8_t len); |
davidr99 | 0:ab4e012489ef | 79 | |
davidr99 | 0:ab4e012489ef | 80 | protected: |
davidr99 | 0:ab4e012489ef | 81 | /// Reference to the RHGenericSPI instance to use to trasnfer data with teh SPI device |
davidr99 | 0:ab4e012489ef | 82 | RHGenericSPI& _spi; |
davidr99 | 0:ab4e012489ef | 83 | |
davidr99 | 0:ab4e012489ef | 84 | /// The pin number of the Slave Selct pin that is used to select the desired device. |
davidr99 | 0:ab4e012489ef | 85 | #if (RH_PLATFORM == RH_PLATFORM_MBED) |
davidr99 | 0:ab4e012489ef | 86 | DigitalOut _slaveSelectPin; |
davidr99 | 0:ab4e012489ef | 87 | #else |
davidr99 | 0:ab4e012489ef | 88 | uint8_t _slaveSelectPin; |
davidr99 | 0:ab4e012489ef | 89 | #endif |
davidr99 | 0:ab4e012489ef | 90 | }; |
davidr99 | 0:ab4e012489ef | 91 | |
davidr99 | 0:ab4e012489ef | 92 | #endif |