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

« Back to documentation index

Show/hide line numbers RHNRFSPIDriver.cpp Source File

RHNRFSPIDriver.cpp

00001 // RHNRFSPIDriver.cpp
00002 //
00003 // Copyright (C) 2014 Mike McCauley
00004 // $Id: RHNRFSPIDriver.cpp,v 1.2 2014/05/03 00:20:36 mikem Exp $
00005 
00006 #include <RHNRFSPIDriver.h>
00007 
00008 RHNRFSPIDriver::RHNRFSPIDriver(PINS slaveSelectPin, RHGenericSPI& spi)
00009     : 
00010     _spi(spi),
00011     _slaveSelectPin(slaveSelectPin)
00012 {
00013 }
00014 
00015 bool RHNRFSPIDriver::init()
00016 {
00017     // start the SPI library with the default speeds etc:
00018     // On Arduino Due this defaults to SPI1 on the central group of 6 SPI pins
00019     _spi.begin();
00020 
00021     // Initialise the slave select pin
00022     // On Maple, this must be _after_ spi.begin
00023 #if (RH_PLATFORM != RH_PLATFORM_MBED)
00024     pinMode(_slaveSelectPin, OUTPUT);
00025 #endif
00026     digitalWrite(_slaveSelectPin, HIGH);
00027 
00028     delay(100);
00029     return true;
00030 }
00031 
00032 // Low level commands for interfacing with the device
00033 uint8_t RHNRFSPIDriver::spiCommand(uint8_t command)
00034 {
00035     uint8_t status;
00036     ATOMIC_BLOCK_START;
00037     digitalWrite(_slaveSelectPin, LOW);
00038     status = _spi.transfer(command);
00039     digitalWrite(_slaveSelectPin, HIGH);
00040     ATOMIC_BLOCK_END;
00041     return status;
00042 }
00043 
00044 uint8_t RHNRFSPIDriver::spiRead(uint8_t reg)
00045 {
00046     uint8_t val;
00047     ATOMIC_BLOCK_START;
00048     digitalWrite(_slaveSelectPin, LOW);
00049     _spi.transfer(reg); // Send the address, discard the status
00050     val = _spi.transfer(0); // The written value is ignored, reg value is read
00051     digitalWrite(_slaveSelectPin, HIGH);
00052     ATOMIC_BLOCK_END;
00053     return val;
00054 }
00055 
00056 uint8_t RHNRFSPIDriver::spiWrite(uint8_t reg, uint8_t val)
00057 {
00058     uint8_t status = 0;
00059     ATOMIC_BLOCK_START;
00060     digitalWrite(_slaveSelectPin, LOW);
00061     status = _spi.transfer(reg); // Send the address
00062     _spi.transfer(val); // New value follows
00063     digitalWrite(_slaveSelectPin, HIGH);
00064     ATOMIC_BLOCK_END;
00065     return status;
00066 }
00067 
00068 uint8_t RHNRFSPIDriver::spiBurstRead(uint8_t reg, uint8_t* dest, uint8_t len)
00069 {
00070     uint8_t status = 0;
00071     ATOMIC_BLOCK_START;
00072     digitalWrite(_slaveSelectPin, LOW);
00073     status = _spi.transfer(reg); // Send the start address
00074     while (len--)
00075     *dest++ = _spi.transfer(0);
00076     digitalWrite(_slaveSelectPin, HIGH);
00077     ATOMIC_BLOCK_END;
00078     return status;
00079 }
00080 
00081 uint8_t RHNRFSPIDriver::spiBurstWrite(uint8_t reg, const uint8_t* src, uint8_t len)
00082 {
00083     uint8_t status = 0;
00084     ATOMIC_BLOCK_START;
00085     digitalWrite(_slaveSelectPin, LOW);
00086     status = _spi.transfer(reg); // Send the start address
00087     while (len--)
00088     _spi.transfer(*src++);
00089     digitalWrite(_slaveSelectPin, HIGH);
00090     ATOMIC_BLOCK_END;
00091     return status;
00092 }
00093 
00094 
00095