V148

Fork of RadioHead-148 by David Rimer

Committer:
davidr99
Date:
Thu Oct 15 01:27:00 2015 +0000
Revision:
0:ab4e012489ef
Messy start, but a port for RadioHead.; Currently the SPI modulus are the only ones that work.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
davidr99 0:ab4e012489ef 1 // RHNRFSPIDriver.cpp
davidr99 0:ab4e012489ef 2 //
davidr99 0:ab4e012489ef 3 // Copyright (C) 2014 Mike McCauley
davidr99 0:ab4e012489ef 4 // $Id: RHNRFSPIDriver.cpp,v 1.2 2014/05/03 00:20:36 mikem Exp $
davidr99 0:ab4e012489ef 5
davidr99 0:ab4e012489ef 6 #include <RHNRFSPIDriver.h>
davidr99 0:ab4e012489ef 7
davidr99 0:ab4e012489ef 8 RHNRFSPIDriver::RHNRFSPIDriver(PINS slaveSelectPin, RHGenericSPI& spi)
davidr99 0:ab4e012489ef 9 :
davidr99 0:ab4e012489ef 10 _spi(spi),
davidr99 0:ab4e012489ef 11 _slaveSelectPin(slaveSelectPin)
davidr99 0:ab4e012489ef 12 {
davidr99 0:ab4e012489ef 13 }
davidr99 0:ab4e012489ef 14
davidr99 0:ab4e012489ef 15 bool RHNRFSPIDriver::init()
davidr99 0:ab4e012489ef 16 {
davidr99 0:ab4e012489ef 17 // start the SPI library with the default speeds etc:
davidr99 0:ab4e012489ef 18 // On Arduino Due this defaults to SPI1 on the central group of 6 SPI pins
davidr99 0:ab4e012489ef 19 _spi.begin();
davidr99 0:ab4e012489ef 20
davidr99 0:ab4e012489ef 21 // Initialise the slave select pin
davidr99 0:ab4e012489ef 22 // On Maple, this must be _after_ spi.begin
davidr99 0:ab4e012489ef 23 #if (RH_PLATFORM != RH_PLATFORM_MBED)
davidr99 0:ab4e012489ef 24 pinMode(_slaveSelectPin, OUTPUT);
davidr99 0:ab4e012489ef 25 #endif
davidr99 0:ab4e012489ef 26 digitalWrite(_slaveSelectPin, HIGH);
davidr99 0:ab4e012489ef 27
davidr99 0:ab4e012489ef 28 delay(100);
davidr99 0:ab4e012489ef 29 return true;
davidr99 0:ab4e012489ef 30 }
davidr99 0:ab4e012489ef 31
davidr99 0:ab4e012489ef 32 // Low level commands for interfacing with the device
davidr99 0:ab4e012489ef 33 uint8_t RHNRFSPIDriver::spiCommand(uint8_t command)
davidr99 0:ab4e012489ef 34 {
davidr99 0:ab4e012489ef 35 uint8_t status;
davidr99 0:ab4e012489ef 36 ATOMIC_BLOCK_START;
davidr99 0:ab4e012489ef 37 digitalWrite(_slaveSelectPin, LOW);
davidr99 0:ab4e012489ef 38 status = _spi.transfer(command);
davidr99 0:ab4e012489ef 39 digitalWrite(_slaveSelectPin, HIGH);
davidr99 0:ab4e012489ef 40 ATOMIC_BLOCK_END;
davidr99 0:ab4e012489ef 41 return status;
davidr99 0:ab4e012489ef 42 }
davidr99 0:ab4e012489ef 43
davidr99 0:ab4e012489ef 44 uint8_t RHNRFSPIDriver::spiRead(uint8_t reg)
davidr99 0:ab4e012489ef 45 {
davidr99 0:ab4e012489ef 46 uint8_t val;
davidr99 0:ab4e012489ef 47 ATOMIC_BLOCK_START;
davidr99 0:ab4e012489ef 48 digitalWrite(_slaveSelectPin, LOW);
davidr99 0:ab4e012489ef 49 _spi.transfer(reg); // Send the address, discard the status
davidr99 0:ab4e012489ef 50 val = _spi.transfer(0); // The written value is ignored, reg value is read
davidr99 0:ab4e012489ef 51 digitalWrite(_slaveSelectPin, HIGH);
davidr99 0:ab4e012489ef 52 ATOMIC_BLOCK_END;
davidr99 0:ab4e012489ef 53 return val;
davidr99 0:ab4e012489ef 54 }
davidr99 0:ab4e012489ef 55
davidr99 0:ab4e012489ef 56 uint8_t RHNRFSPIDriver::spiWrite(uint8_t reg, uint8_t val)
davidr99 0:ab4e012489ef 57 {
davidr99 0:ab4e012489ef 58 uint8_t status = 0;
davidr99 0:ab4e012489ef 59 ATOMIC_BLOCK_START;
davidr99 0:ab4e012489ef 60 digitalWrite(_slaveSelectPin, LOW);
davidr99 0:ab4e012489ef 61 status = _spi.transfer(reg); // Send the address
davidr99 0:ab4e012489ef 62 _spi.transfer(val); // New value follows
davidr99 0:ab4e012489ef 63 digitalWrite(_slaveSelectPin, HIGH);
davidr99 0:ab4e012489ef 64 ATOMIC_BLOCK_END;
davidr99 0:ab4e012489ef 65 return status;
davidr99 0:ab4e012489ef 66 }
davidr99 0:ab4e012489ef 67
davidr99 0:ab4e012489ef 68 uint8_t RHNRFSPIDriver::spiBurstRead(uint8_t reg, uint8_t* dest, uint8_t len)
davidr99 0:ab4e012489ef 69 {
davidr99 0:ab4e012489ef 70 uint8_t status = 0;
davidr99 0:ab4e012489ef 71 ATOMIC_BLOCK_START;
davidr99 0:ab4e012489ef 72 digitalWrite(_slaveSelectPin, LOW);
davidr99 0:ab4e012489ef 73 status = _spi.transfer(reg); // Send the start address
davidr99 0:ab4e012489ef 74 while (len--)
davidr99 0:ab4e012489ef 75 *dest++ = _spi.transfer(0);
davidr99 0:ab4e012489ef 76 digitalWrite(_slaveSelectPin, HIGH);
davidr99 0:ab4e012489ef 77 ATOMIC_BLOCK_END;
davidr99 0:ab4e012489ef 78 return status;
davidr99 0:ab4e012489ef 79 }
davidr99 0:ab4e012489ef 80
davidr99 0:ab4e012489ef 81 uint8_t RHNRFSPIDriver::spiBurstWrite(uint8_t reg, const uint8_t* src, uint8_t len)
davidr99 0:ab4e012489ef 82 {
davidr99 0:ab4e012489ef 83 uint8_t status = 0;
davidr99 0:ab4e012489ef 84 ATOMIC_BLOCK_START;
davidr99 0:ab4e012489ef 85 digitalWrite(_slaveSelectPin, LOW);
davidr99 0:ab4e012489ef 86 status = _spi.transfer(reg); // Send the start address
davidr99 0:ab4e012489ef 87 while (len--)
davidr99 0:ab4e012489ef 88 _spi.transfer(*src++);
davidr99 0:ab4e012489ef 89 digitalWrite(_slaveSelectPin, HIGH);
davidr99 0:ab4e012489ef 90 ATOMIC_BLOCK_END;
davidr99 0:ab4e012489ef 91 return status;
davidr99 0:ab4e012489ef 92 }
davidr99 0:ab4e012489ef 93
davidr99 0:ab4e012489ef 94
davidr99 0:ab4e012489ef 95