Merge pull request #34 from DzikuVx/spi-improvements

Packet reading is now down in single SPI transaction
This commit is contained in:
Paweł Spychalski
2017-12-16 09:26:27 +01:00
committed by GitHub
6 changed files with 68 additions and 26 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
@@ -60,6 +60,8 @@ RxDeviceState_t rxDeviceState = {};
TxDeviceState_t txDeviceState = {}; TxDeviceState_t txDeviceState = {};
volatile RadioState_t radioState; volatile RadioState_t radioState;
uint8_t tmpBuffer[MAX_PACKET_SIZE];
uint8_t getRadioRssi(void) uint8_t getRadioRssi(void)
{ {
return 164 - constrain(LoRa.packetRssi() * -1, 0, 164); return 164 - constrain(LoRa.packetRssi() * -1, 0, 164);
@@ -263,10 +265,12 @@ void loop(void)
{ {
if (radioState.bytesToRead != NO_DATA_TO_READ) { if (radioState.bytesToRead != NO_DATA_TO_READ) {
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();
@@ -391,8 +395,12 @@ void loop(void)
if (qsp.canTransmit && transmitPayload) if (qsp.canTransmit && transmitPayload)
{ {
uint8_t size;
LoRa.beginPacket(); LoRa.beginPacket();
qspEncodeFrame(&qsp); //Prepare packet
qspEncodeFrame(&qsp, tmpBuffer, &size);
//Sent it to radio in one SPI transaction
LoRa.write(tmpBuffer, size);
LoRa.endPacket(); LoRa.endPacket();
//After ending packet, put device into receive mode again //After ending packet, put device into receive mode again
LoRa.receive(); LoRa.receive();

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:

17
qsp.cpp
View File

@@ -230,26 +230,27 @@ void qspDecodeIncomingFrame(
/** /**
* Encode frame is corrent format and write to hardware * Encode frame is corrent format and write to hardware
*/ */
void qspEncodeFrame(QspConfiguration_t *qsp) { void qspEncodeFrame(QspConfiguration_t *qsp, uint8_t buffer[], uint8_t *size) {
//Zero CRC //Zero CRC
qsp->crc = 0; qsp->crc = 0;
//Write CHANNEL_ID qspComputeCrc(qsp, CHANNEL_ID);
qsp->hardwareWriteFunction(CHANNEL_ID, qsp); buffer[0] = CHANNEL_ID;
//Write frame type and length //Write frame type and length
uint8_t data = qsp->payloadLength & 0x0f; uint8_t data = qsp->payloadLength & 0x0f;
data |= (qsp->frameToSend << 4) & 0xf0; data |= (qsp->frameToSend << 4) & 0xf0;
qsp->hardwareWriteFunction(data, qsp); qspComputeCrc(qsp, data);
buffer[1] = data;
//Write payload
for (uint8_t i = 0; i < qsp->payloadLength; i++) for (uint8_t i = 0; i < qsp->payloadLength; i++)
{ {
qsp->hardwareWriteFunction(qsp->payload[i], qsp); qspComputeCrc(qsp, qsp->payload[i]);
buffer[i + 2] = qsp->payload[i];
} }
//Finally write CRC buffer[qsp->payloadLength + 2] = qsp->crc;
qsp->hardwareWriteFunction(qsp->crc, qsp); *size = qsp->payloadLength + 3; //Total length of QSP frame
} }
void encodePingPayload(QspConfiguration_t *qsp, uint32_t currentMicros) { void encodePingPayload(QspConfiguration_t *qsp, uint32_t currentMicros) {

2
qsp.h
View File

@@ -17,6 +17,6 @@ void qspDecodeIncomingFrame(
RadioState_t *radioState RadioState_t *radioState
); );
void qspClearPayload(QspConfiguration_t *qsp); void qspClearPayload(QspConfiguration_t *qsp);
void qspEncodeFrame(QspConfiguration_t *qsp); void qspEncodeFrame(QspConfiguration_t *qsp, uint8_t buffer[], uint8_t *size);
void encodePingPayload(QspConfiguration_t *qsp, uint32_t currentMicros); void encodePingPayload(QspConfiguration_t *qsp, uint32_t currentMicros);