Skip to content

Instantly share code, notes, and snippets.

@AndrewCarterUK
Created July 19, 2017 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewCarterUK/74e851c984fc605f31cfaf8a47409307 to your computer and use it in GitHub Desktop.
Save AndrewCarterUK/74e851c984fc605f31cfaf8a47409307 to your computer and use it in GitHub Desktop.
LoRa Confusion
/*
This program attempts to change the operation mode of a SX1276.
SPI communication is verified as working, as the program is able to select and verify sleep mode.
The SX1276 refuses to enter RXCONTINUOS mode and instead reverts back to STDBY·
Compile:
gcc test.c -lwiringPi -o test
Run
./test
Output from program:
Setting stdby mode...
Got mode: STDBY
Setting sleep mode...
Got mode: SLEEP
Setting receive mode...
Got mode: STDBY
*/
#include <stdio.h>
#include <stdint.h>
#include <wiringPiSPI.h>
#define CHANNEL 0
#define SPEED 500000
#define LORA_SPI_WRITE 0x80
#define LORA_REG_MODE 0x01
#define LORA_MODE_FLAG_LONG_RANGE 0x80
#define LORA_MODE_FLAG_ACCESS_SHARED_REG 0x40
#define LORA_MODE_SLEEP 0x00
#define LORA_MODE_STDBY 0x01
#define LORA_MODE_FSTX 0x02
#define LORA_MODE_TX 0x03
#define LORA_MODE_FSRX 0x04
#define LORA_MODE_RXCONTINUOUS 0x05
#define LORA_MODE_RXSINGLE 0x06
#define LORA_MODE_CAD 0x07
uint8_t spiRead(uint8_t reg)
{
uint8_t block[2];
block[0] = reg & ~LORA_SPI_WRITE;
block[1] = 0;
wiringPiSPIDataRW(CHANNEL, block, 2);
return block[1];
}
uint8_t spiWrite(uint8_t reg, uint8_t val)
{
uint8_t block[2];
block[0] = reg | LORA_SPI_WRITE;
block[1] = val;
wiringPiSPIDataRW(CHANNEL, block, 2);
return block[0];
}
uint8_t getMode(void)
{
return spiRead(LORA_REG_MODE) & ~LORA_MODE_FLAG_LONG_RANGE;
}
void setMode(uint8_t mode)
{
spiWrite(LORA_REG_MODE, mode | LORA_MODE_FLAG_LONG_RANGE);
}
const char * modeToString(uint8_t mode)
{
switch (mode) {
case LORA_MODE_SLEEP: return "SLEEP";
case LORA_MODE_STDBY: return "STDBY";
case LORA_MODE_FSTX: return "FSTX";
case LORA_MODE_TX: return "TX";
case LORA_MODE_FSRX: return "FSRX";
case LORA_MODE_RXCONTINUOUS: return "RXCONTINUOUS";
case LORA_MODE_RXSINGLE: return "RXSINGLE";
case LORA_MODE_CAD: return "CAD";
}
return "UNKNOWN";
}
int main(int argc, char *argv[])
{
if (-1 == wiringPiSPISetup(CHANNEL, SPEED)) {
puts("Failed to setup SPI channel");
return 1;
}
puts("Setting stdby mode...");
setMode(LORA_MODE_STDBY);
printf("Got mode: %s\n\n", modeToString(getMode()));
puts("Setting sleep mode...");
setMode(LORA_MODE_SLEEP);
printf("Got mode: %s\n\n", modeToString(getMode()));
puts("Setting receive mode...");
setMode(LORA_MODE_RXCONTINUOUS);
printf("Got mode: %s\n\n", modeToString(getMode()));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment