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

« Back to documentation index

Show/hide line numbers RHSPIDriver.cpp Source File

RHSPIDriver.cpp

00001 // RHSPIDriver.cpp
00002 //
00003 // Copyright (C) 2014 Mike McCauley
00004 // $Id: RHSPIDriver.cpp,v 1.9 2014/05/03 00:20:36 mikem Exp $
00005 
00006 #include <RHSPIDriver.h>
00007 
00008 RHSPIDriver::RHSPIDriver(PINS slaveSelectPin, RHGenericSPI& spi)
00009     : 
00010     _spi(spi),
00011     _slaveSelectPin(slaveSelectPin)
00012 {
00013 }
00014 
00015 bool RHSPIDriver::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 uint8_t RHSPIDriver::spiRead(uint8_t reg)
00033 {
00034     uint8_t val;
00035     ATOMIC_BLOCK_START;
00036     digitalWrite(_slaveSelectPin, LOW);
00037     _spi.transfer(reg & ~RH_SPI_WRITE_MASK); // Send the address with the write mask off
00038     val = _spi.transfer(0); // The written value is ignored, reg value is read
00039     digitalWrite(_slaveSelectPin, HIGH);
00040     ATOMIC_BLOCK_END;
00041     return val;
00042 }
00043 
00044 uint8_t RHSPIDriver::spiWrite(uint8_t reg, uint8_t val)
00045 {
00046     uint8_t status = 0;
00047     ATOMIC_BLOCK_START;
00048     digitalWrite(_slaveSelectPin, LOW);
00049     status = _spi.transfer(reg | RH_SPI_WRITE_MASK); // Send the address with the write mask on
00050     _spi.transfer(val); // New value follows
00051     digitalWrite(_slaveSelectPin, HIGH);
00052     ATOMIC_BLOCK_END;
00053     return status;
00054 }
00055 
00056 uint8_t RHSPIDriver::spiBurstRead(uint8_t reg, uint8_t* dest, uint8_t len)
00057 {
00058     uint8_t status = 0;
00059     ATOMIC_BLOCK_START;
00060     digitalWrite(_slaveSelectPin, LOW);
00061     status = _spi.transfer(reg & ~RH_SPI_WRITE_MASK); // Send the start address with the write mask off
00062     while (len--)
00063     *dest++ = _spi.transfer(0);
00064     digitalWrite(_slaveSelectPin, HIGH);
00065     ATOMIC_BLOCK_END;
00066     return status;
00067 }
00068 
00069 uint8_t RHSPIDriver::spiBurstWrite(uint8_t reg, const uint8_t* src, uint8_t len)
00070 {
00071     uint8_t status = 0;
00072     ATOMIC_BLOCK_START;
00073     digitalWrite(_slaveSelectPin, LOW);
00074     status = _spi.transfer(reg | RH_SPI_WRITE_MASK); // Send the start address with the write mask on
00075     while (len--)
00076     _spi.transfer(*src++);
00077     digitalWrite(_slaveSelectPin, HIGH);
00078     ATOMIC_BLOCK_END;
00079     return status;
00080 }
00081 
00082 
00083