(deleted)
(deleted)
(deleted)
I am using an nRF24 with an Attiny1634 using the SpenceKonde core and I have made my own SPI and nRF24 functions - drawing heavily on the TMRh20 RF24 library.
This is my SPI code
// SPI code for Attiny1634 - same as 84 except for pinouts
// this should be API interchangeable with my R2spi.h
#include <Arduino.h>
#define SCK_PIN 12 // Physical 16
#define DO_PIN 15 // 19
#define DI_PIN 16 // 20
// code based on TMRh20RF24.cpp
void R2spiInitialize(byte settings) {
// settings is ignored - for compatibility with 328 version
pinMode(DO_PIN, OUTPUT);
pinMode(SCK_PIN, OUTPUT);
pinMode(DI_PIN, INPUT);
USICR = 0b00010000; // _BV(USIWM0); // wire mode for 3 wire
}
byte R2spiTransfer(byte val) {
USIDR = val;
USISR = 0b01000000; // _BV(USIOIF); // writing 1 clears interrupt flag. Also writes counter to zero
do {
USICR = 0b00011011; // _BV(USIWM0) | _BV(USICS1) | _BV(USICLK) | _BV(USITC);
// 3 wire mode, ext pos software CLK, toggle Clock
// this gets repeated often enough to send all the bits
// Note that the transfer speed is determined by how fast the WHILE repeats
} while ((USISR & 0b01000000) == 0); // ((USISR & _BV(USIOIF)) == 0); // check for overflow
return USIDR;
}
and this is my nRF24 code
// an attempt to write my own nRF24 access functions
// this version uses my SPI functions rather than the standard library
// #includeX "Definitions.h"
byte R2rfStatus;
byte R2rfPayloadLength = 32;
byte writeBytes[4];
byte readBytes[32];
// some function prototypes
byte R2rfResetStatus();
//======================
void R2rfInitialize() {
R2spiInitialize(0);
pinMode(CE_PIN, OUTPUT);
pinMode(CSN_PIN, OUTPUT);
}
//======================
// basic functions to interface with nRF24
// 3 are required - read a register, write a register, send a stand-alone command
// this version with pointers - drawn from TMRh20RF24.cpp
// this reads one byte
byte R2rfReadRegister(byte reg) {
byte regCmd = 0x00 + reg;
byte regVal;
digitalWrite(CSN_PIN, LOW);
R2rfStatus = R2spiTransfer( regCmd );
regVal = R2spiTransfer(0xEE);
digitalWrite(CSN_PIN, HIGH);
return regVal;
}
//======================
// this writes one byte
void R2rfWriteRegister(byte reg, byte regVal) {
byte regCmd = 0x20 + reg;
digitalWrite(CSN_PIN, LOW);
R2rfStatus = R2spiTransfer( regCmd );
R2spiTransfer(regVal);
digitalWrite(CSN_PIN, HIGH);
}
//======================
// write 5 address bytes
void R2rfWriteAddress5(byte reg, void* buf) {
byte* tempBuf = reinterpret_cast <byte*>(buf);
byte regCmd = 0x20 + reg;
digitalWrite(CSN_PIN, LOW);
R2rfStatus = R2spiTransfer( regCmd );
for (byte n = 0; n < 5; n++) {
R2spiTransfer(tempBuf[n]);
}
digitalWrite(CSN_PIN, HIGH);
}
//======================
void R2rfWriteTxPld(void* buf, byte numBytes) {
byte numBlanks = R2rfPayloadLength - numBytes;
byte* tempBuf = reinterpret_cast <byte*>(buf);
digitalWrite(CSN_PIN, LOW);
R2rfStatus = R2spiTransfer( W_TX_PAYLOAD );
for (byte n = 0; n < numBytes; n++) {
R2spiTransfer(tempBuf[n]);
}
for (byte n = 0; n < numBlanks; n++) {
R2spiTransfer(0);
}
digitalWrite(CSN_PIN, HIGH);
}
//======================
void R2rfWriteAckPld(byte pipe, void* buf, byte numBytes) {
byte numBlanks = R2rfPayloadLength - numBytes;
byte* tempBuf = reinterpret_cast <byte*>(buf);
byte cmd = W_ACK_PAYLOAD + pipe;
digitalWrite(CSN_PIN, LOW);
R2rfStatus = R2spiTransfer( cmd );
for (byte n = 0; n < numBytes; n++) {
R2spiTransfer(tempBuf[n]);
}
//~ for (byte n = 0; n < numBlanks; n++) { // think dynamic size works
//~ R2spiTransfer(0);
//~ }
digitalWrite(CSN_PIN, HIGH);
}
//======================
void R2rfReadRxPld(void* buf, byte numBytes) {
byte* tempBuf = reinterpret_cast <byte*>(buf);
digitalWrite(CSN_PIN, LOW);
R2rfStatus = R2spiTransfer( R_RX_PAYLOAD );
for (byte n = 0; n < numBytes; n++) {
tempBuf[n] = R2spiTransfer(0xFF);
}
digitalWrite(CSN_PIN, HIGH);
}
//======================
byte R2rfSingleByteCmd(byte cmd){
digitalWrite(CSN_PIN, LOW);
R2rfStatus = R2spiTransfer( cmd );
digitalWrite(CSN_PIN, HIGH);
}
//======================
void R2rfStartListeningPipe1() {
// set as Primary Rx
R2rfWriteRegister(CONFIG, 0b00001111);
// reset status
R2rfResetStatus();
// CE high
digitalWrite(CE_PIN, HIGH);
// Flush Tx
R2rfSingleByteCmd(FLUSH_TX);
// Flush Rx
R2rfSingleByteCmd(FLUSH_RX);
}
//======================
byte R2rfDataAvailable() {
R2rfSingleByteCmd(NOP); // get status
byte avail = (R2rfStatus & 0b01000000); // register bit will be 1 if data is available
return (avail > 0);
}
//======================
byte XXR2rfDataAvailable() {
byte avail = ! (R2rfReadRegister(FIFO_STATUS) & 0b00000001); // register bit will be 1 if RX is empty - invert for avail
return avail;
}
//======================
byte R2rfResetStatus() {
// reset status
R2rfWriteRegister(NRF_STATUS, 0b01110000);
}
I also use the nRF24 register definitions from the TMRh20 code.
I am not claiming anything for any of this apart from the fact that it works for me.
...R
(deleted)
jaskamakkara:
So do I understand correctly that you have made your own, smaller, SPI and RF24 libraries?
That is partly what I had in mind. But I also wanted to be very clear about what I/O pins are being used and the RF24 library is rather confusing with lots of IF DEFINED to check for different hardware.
However I never tested to see if my code is actually smaller. The compiler and linker is actually very good at excluding unnecessary code.
...R
(deleted)
jaskamakkara:
Right. So do you think your libraries would work with an ATTiny2313/4313?
I have not the slightest idea. But I posted the code so you can try it if you wish. Quite possibly you will need to change some of the details. You will need to study the Attiny datasheet carefully.
By the way, the code I posted is not a library. It is just a collection of functions that can be copied into your .ino file.
...R