Packet reading is now down in single SPI transaction

This commit is contained in:
Pawel Spychalski (DzikuVx)
2017-12-14 23:41:14 +01:00
parent c97c5f2b22
commit 50e297b3d6
4 changed files with 55 additions and 18 deletions

View File

@@ -1,6 +1,6 @@
{ {
"board": "bsfrance:avr:lora32u4", "board": "bsfrance:avr:lora32u4",
"sketch": "crossbow.ino", "sketch": "crossbow.ino",
"port": "COM17", "port": "COM4",
"output": "./build" "output": "./build"
} }

View File

@@ -1,10 +1,10 @@
#define DEVICE_MODE_TX // #define DEVICE_MODE_TX
// #define DEVICE_MODE_RX #define DEVICE_MODE_RX
#define FEATURE_TX_OLED // #define FEATURE_TX_OLED
// #define FORCE_TX_WITHOUT_INPUT #define FORCE_TX_WITHOUT_INPUT
// #define DEBUG_SERIAL #define DEBUG_SERIAL
// #define DEBUG_PING_PONG // #define DEBUG_PING_PONG
// #define DEBUG_LED // #define DEBUG_LED
@@ -263,10 +263,14 @@ void loop(void)
{ {
if (radioState.bytesToRead != NO_DATA_TO_READ) { if (radioState.bytesToRead != NO_DATA_TO_READ) {
static uint8_t tmpBuffer[20];
LoRa.read(tmpBuffer, radioState.bytesToRead);
for (int i = 0; i < radioState.bytesToRead; i++) { for (int i = 0; i < radioState.bytesToRead; i++) {
qspDecodeIncomingFrame(&qsp, LoRa.fastRead(), &rxDeviceState, &txDeviceState, &radioState); qspDecodeIncomingFrame(&qsp, tmpBuffer[i], &rxDeviceState, &txDeviceState, &radioState);
} }
radioState.rssi = getRadioRssi(); radioState.rssi = getRadioRssi();
radioState.snr = getRadioSnr(); radioState.snr = getRadioSnr();

View File

@@ -208,12 +208,17 @@ float LoRaClass::packetSnr()
return ((int8_t)readRegister(REG_PKT_SNR_VALUE)) * 0.25; return ((int8_t)readRegister(REG_PKT_SNR_VALUE)) * 0.25;
} }
size_t LoRaClass::write(uint8_t byte) void LoRaClass::write(uint8_t data)
{ {
return write(&byte, sizeof(byte)); int currentLength = readRegister(REG_PAYLOAD_LENGTH);
writeRegister(REG_FIFO, data);
// update length
writeRegister(REG_PAYLOAD_LENGTH, currentLength + 1);
} }
size_t LoRaClass::write(const uint8_t *buffer, size_t size) void LoRaClass::write(uint8_t buffer[], size_t size)
{ {
int currentLength = readRegister(REG_PAYLOAD_LENGTH); int currentLength = readRegister(REG_PAYLOAD_LENGTH);
@@ -222,15 +227,10 @@ size_t LoRaClass::write(const uint8_t *buffer, size_t size)
size = MAX_PKT_LENGTH - currentLength; size = MAX_PKT_LENGTH - currentLength;
} }
// write data writeRegister(REG_FIFO, buffer, size);
for (size_t i = 0; i < size; i++) {
writeRegister(REG_FIFO, buffer[i]);
}
// update length // update length
writeRegister(REG_PAYLOAD_LENGTH, currentLength + size); writeRegister(REG_PAYLOAD_LENGTH, currentLength + size);
return size;
} }
int LoRaClass::available() int LoRaClass::available()
@@ -244,6 +244,11 @@ int LoRaClass::fastRead() {
return readRegister(REG_FIFO); return readRegister(REG_FIFO);
} }
void LoRaClass::read(uint8_t buffer[], uint8_t size) {
_packetIndex += size;
return readRegister(REG_FIFO, buffer, size);
}
int LoRaClass::read() int LoRaClass::read()
{ {
if (!available()) { if (!available()) {
@@ -498,6 +503,29 @@ uint8_t LoRaClass::singleTransfer(uint8_t address, uint8_t value)
return response; return response;
} }
void LoRaClass::writeRegister(uint8_t address, uint8_t buffer[], size_t size)
{
bufferTransfer(address | 0x80, buffer, size);
}
void LoRaClass::readRegister(uint8_t address, uint8_t buffer[], size_t size)
{
bufferTransfer(address & 0x7f, buffer, size);
}
void LoRaClass::bufferTransfer(uint8_t address, uint8_t buffer[], uint8_t size) {
uint8_t response;
digitalWrite(_ss, LOW);
SPI.beginTransaction(_spiSettings);
SPI.transfer(address);
SPI.transfer(buffer, size);
SPI.endTransaction();
digitalWrite(_ss, HIGH);
}
void LoRaClass::onDio0Rise() void LoRaClass::onDio0Rise()
{ {
LoRa.handleDio0Rise(); LoRa.handleDio0Rise();

9
lora.h
View File

@@ -30,11 +30,12 @@ public:
int packetRssi(); int packetRssi();
float packetSnr(); float packetSnr();
size_t write(uint8_t byte); void write(uint8_t byte);
size_t write(const uint8_t *buffer, size_t size); void write(uint8_t buffer[], size_t size);
int available(); int available();
int read(); int read();
int fastRead(); int fastRead();
void read(uint8_t buffer[], uint8_t size);
void onReceive(void(*callback)(int)); void onReceive(void(*callback)(int));
@@ -69,6 +70,10 @@ private:
void writeRegister(uint8_t address, uint8_t value); void writeRegister(uint8_t address, uint8_t value);
uint8_t singleTransfer(uint8_t address, uint8_t value); uint8_t singleTransfer(uint8_t address, uint8_t value);
void readRegister(uint8_t address, uint8_t buffer[], size_t size);
void writeRegister(uint8_t address, uint8_t buffer[], size_t size);
void bufferTransfer(uint8_t address, uint8_t buffer[], uint8_t size);
static void onDio0Rise(); static void onDio0Rise();
private: private: