Compare commits

...

4 Commits

Author SHA1 Message Date
d57a2a85df Remove addr check for some unknown error 2021-10-17 14:46:06 +08:00
fdc67e0d83 Remove the end '\0' add 2021-10-16 10:36:09 +08:00
a7a4236052 Update to adapt no DIO0 situation 2021-10-15 22:59:00 +08:00
Wojciech Domski
d6d89363ce
Merge pull request #4 from wdomski/develop
Updated documentation
2020-10-21 20:22:20 +02:00
4 changed files with 519 additions and 444 deletions

View File

@ -9,7 +9,10 @@
#include "SX1278.h"
#include <string.h>
uint8_t SX1278_SPIRead(SX1278_t *module, uint8_t addr) {
SoftSPI_TypeDef *sx1278_spix;
uint8_t SX1278_SPIRead(SX1278_t *module, uint8_t addr)
{
uint8_t tmp;
SX1278_hw_SPICommand(module->hw, addr);
tmp = SX1278_hw_SPIReadByte(module->hw);
@ -17,7 +20,8 @@ uint8_t SX1278_SPIRead(SX1278_t *module, uint8_t addr) {
return tmp;
}
void SX1278_SPIWrite(SX1278_t *module, uint8_t addr, uint8_t cmd) {
void SX1278_SPIWrite(SX1278_t *module, uint8_t addr, uint8_t cmd)
{
SX1278_hw_SetNSS(module->hw, 0);
SX1278_hw_SPICommand(module->hw, addr | 0x80);
SX1278_hw_SPICommand(module->hw, cmd);
@ -25,14 +29,20 @@ void SX1278_SPIWrite(SX1278_t *module, uint8_t addr, uint8_t cmd) {
}
void SX1278_SPIBurstRead(SX1278_t *module, uint8_t addr, uint8_t *rxBuf,
uint8_t length) {
uint8_t length)
{
uint8_t i;
if (length <= 1) {
// if (length <= 1)
if (length < 1)
{
return;
} else {
}
else
{
SX1278_hw_SetNSS(module->hw, 0);
SX1278_hw_SPICommand(module->hw, addr);
for (i = 0; i < length; i++) {
for (i = 0; i < length; i++)
{
*(rxBuf + i) = SX1278_hw_SPIReadByte(module->hw);
}
SX1278_hw_SetNSS(module->hw, 1);
@ -40,21 +50,27 @@ void SX1278_SPIBurstRead(SX1278_t *module, uint8_t addr, uint8_t *rxBuf,
}
void SX1278_SPIBurstWrite(SX1278_t *module, uint8_t addr, uint8_t *txBuf,
uint8_t length) {
uint8_t length)
{
unsigned char i;
if (length <= 1) {
if (length <= 1)
{
return;
} else {
}
else
{
SX1278_hw_SetNSS(module->hw, 0);
SX1278_hw_SPICommand(module->hw, addr | 0x80);
for (i = 0; i < length; i++) {
for (i = 0; i < length; i++)
{
SX1278_hw_SPICommand(module->hw, *(txBuf + i));
}
SX1278_hw_SetNSS(module->hw, 1);
}
}
void SX1278_config(SX1278_t *module) {
void SX1278_config(SX1278_t *module)
{
SX1278_sleep(module); // Change modem mode Must in Sleep mode
SX1278_hw_DelayMs(15);
@ -75,33 +91,32 @@ void SX1278_config(SX1278_t *module) {
SX1278_SPIWrite(module, LR_RegOcp, 0x0B); // RegOcp,Close Ocp
SX1278_SPIWrite(module, LR_RegLna, 0x23); // RegLNA,High & LNA Enable
if (SX1278_SpreadFactor[module->LoRa_SF] == 6) { //SFactor=6
if (SX1278_SpreadFactor[module->LoRa_SF] == 6)
{ // SFactor=6
uint8_t tmp;
SX1278_SPIWrite(module,
LR_RegModemConfig1,
((SX1278_LoRaBandwidth[module->LoRa_BW] << 4)
+ (SX1278_CodingRate[module->LoRa_CR] << 1) + 0x01)); //Implicit Enable CRC Enable(0x02) & Error Coding rate 4/5(0x01), 4/6(0x02), 4/7(0x03), 4/8(0x04)
((SX1278_LoRaBandwidth[module->LoRa_BW] << 4) + (SX1278_CodingRate[module->LoRa_CR] << 1) + 0x01)); // Implicit Enable CRC Enable(0x02) & Error Coding rate 4/5(0x01), 4/6(0x02), 4/7(0x03), 4/8(0x04)
SX1278_SPIWrite(module,
LR_RegModemConfig2,
((SX1278_SpreadFactor[module->LoRa_SF] << 4)
+ (SX1278_CRC_Sum[module->LoRa_CRC_sum] << 2) + 0x03));
((SX1278_SpreadFactor[module->LoRa_SF] << 4) + (SX1278_CRC_Sum[module->LoRa_CRC_sum] << 2) + 0x03));
tmp = SX1278_SPIRead(module, 0x31);
tmp &= 0xF8;
tmp |= 0x05;
SX1278_SPIWrite(module, 0x31, tmp);
SX1278_SPIWrite(module, 0x37, 0x0C);
} else {
}
else
{
SX1278_SPIWrite(module,
LR_RegModemConfig1,
((SX1278_LoRaBandwidth[module->LoRa_BW] << 4)
+ (SX1278_CodingRate[module->LoRa_CR] << 1) + 0x00)); //Explicit Enable CRC Enable(0x02) & Error Coding rate 4/5(0x01), 4/6(0x02), 4/7(0x03), 4/8(0x04)
((SX1278_LoRaBandwidth[module->LoRa_BW] << 4) + (SX1278_CodingRate[module->LoRa_CR] << 1) + 0x00)); // Explicit Enable CRC Enable(0x02) & Error Coding rate 4/5(0x01), 4/6(0x02), 4/7(0x03), 4/8(0x04)
SX1278_SPIWrite(module,
LR_RegModemConfig2,
((SX1278_SpreadFactor[module->LoRa_SF] << 4)
+ (SX1278_CRC_Sum[module->LoRa_CRC_sum] << 2) + 0x00)); //SFactor & LNA gain set by the internal AGC loop
((SX1278_SpreadFactor[module->LoRa_SF] << 4) + (SX1278_CRC_Sum[module->LoRa_CRC_sum] << 2) + 0x00)); // SFactor & LNA gain set by the internal AGC loop
}
SX1278_SPIWrite(module, LR_RegModemConfig3, 0x04);
@ -113,25 +128,30 @@ void SX1278_config(SX1278_t *module) {
SX1278_standby(module); // Entry standby mode
}
void SX1278_standby(SX1278_t *module) {
void SX1278_standby(SX1278_t *module)
{
SX1278_SPIWrite(module, LR_RegOpMode, 0x09);
module->status = STANDBY;
}
void SX1278_sleep(SX1278_t *module) {
void SX1278_sleep(SX1278_t *module)
{
SX1278_SPIWrite(module, LR_RegOpMode, 0x08);
module->status = SLEEP;
}
void SX1278_entryLoRa(SX1278_t *module) {
void SX1278_entryLoRa(SX1278_t *module)
{
SX1278_SPIWrite(module, LR_RegOpMode, 0x88);
}
void SX1278_clearLoRaIrq(SX1278_t *module) {
void SX1278_clearLoRaIrq(SX1278_t *module)
{
SX1278_SPIWrite(module, LR_RegIrqFlags, 0xFF);
}
int SX1278_LoRaEntryRx(SX1278_t *module, uint8_t length, uint32_t timeout) {
int SX1278_LoRaEntryRx(SX1278_t *module, uint8_t length, uint32_t timeout)
{
uint8_t addr;
module->packetLength = length;
@ -149,12 +169,15 @@ int SX1278_LoRaEntryRx(SX1278_t *module, uint8_t length, uint32_t timeout) {
// SX1278_SPIWrite(module, LR_RegOpMode,0x05); //Continuous Rx Mode //High Frequency Mode
module->readBytes = 0;
while (1) {
if ((SX1278_SPIRead(module, LR_RegModemStat) & 0x04) == 0x04) { //Rx-on going RegModemStat
while (1)
{
if ((SX1278_SPIRead(module, LR_RegModemStat) & 0x04) == 0x04)
{ // Rx-on going RegModemStat
module->status = RX;
return 1;
}
if (--timeout == 0) {
if (--timeout == 0)
{
SX1278_hw_Reset(module->hw);
SX1278_config(module);
return 0;
@ -163,21 +186,36 @@ int SX1278_LoRaEntryRx(SX1278_t *module, uint8_t length, uint32_t timeout) {
}
}
uint8_t SX1278_LoRaRxPacket(SX1278_t *module) {
uint8_t SX1278_LoRaRxPacket(SX1278_t *module)
{
unsigned char addr;
unsigned char packet_size;
static unsigned char last_addr = 0xff;
// if (SX1278_hw_GetDIO0(module->hw))
{
if (module->LoRa_SF == SX1278_LORA_SF_6)
{ // When SpreadFactor is six,will used Implicit Header mode(Excluding internal packet length)
packet_size = module->packetLength;
}
else
{
packet_size = SX1278_SPIRead(module, LR_RegRxNbBytes); // Number for received bytes
}
if (packet_size == 0)
return 0;
if (SX1278_hw_GetDIO0(module->hw)) {
memset(module->rxBuffer, 0x00, SX1278_MAX_PACKET);
addr = SX1278_SPIRead(module, LR_RegFifoRxCurrentaddr); // last packet addr
SX1278_SPIWrite(module, LR_RegFifoAddrPtr, addr); //RxBaseAddr -> FiFoAddrPtr
if (module->LoRa_SF == SX1278_LORA_SF_6) { //When SpreadFactor is six,will used Implicit Header mode(Excluding internal packet length)
packet_size = module->packetLength;
} else {
packet_size = SX1278_SPIRead(module, LR_RegRxNbBytes); //Number for received bytes
}
// if (addr == last_addr) // Judge if new packet arrived
// return 0;
// else
// last_addr = addr;
SX1278_SPIWrite(module, LR_RegFifoAddrPtr, addr); // RxBaseAddr -> FiFoAddrPtr
SX1278_SPIBurstRead(module, 0x00, module->rxBuffer, packet_size);
module->readBytes = packet_size;
@ -186,7 +224,8 @@ uint8_t SX1278_LoRaRxPacket(SX1278_t *module) {
return module->readBytes;
}
int SX1278_LoRaEntryTx(SX1278_t *module, uint8_t length, uint32_t timeout) {
int SX1278_LoRaEntryTx(SX1278_t *module, uint8_t length, uint32_t timeout)
{
uint8_t addr;
uint8_t temp;
@ -202,14 +241,17 @@ int SX1278_LoRaEntryTx(SX1278_t *module, uint8_t length, uint32_t timeout) {
addr = SX1278_SPIRead(module, LR_RegFifoTxBaseAddr); // RegFiFoTxBaseAddr
SX1278_SPIWrite(module, LR_RegFifoAddrPtr, addr); // RegFifoAddrPtr
while (1) {
while (1)
{
temp = SX1278_SPIRead(module, LR_RegPayloadLength);
if (temp == length) {
if (temp == length)
{
module->status = TX;
return 1;
}
if (--timeout == 0) {
if (--timeout == 0)
{
SX1278_hw_Reset(module->hw);
SX1278_config(module);
return 0;
@ -218,29 +260,36 @@ int SX1278_LoRaEntryTx(SX1278_t *module, uint8_t length, uint32_t timeout) {
}
int SX1278_LoRaTxPacket(SX1278_t *module, uint8_t *txBuffer, uint8_t length,
uint32_t timeout) {
uint32_t timeout)
{
SX1278_SPIBurstWrite(module, 0x00, txBuffer, length);
SX1278_SPIWrite(module, LR_RegOpMode, 0x8b); // Tx Mode
while (1) {
if (SX1278_hw_GetDIO0(module->hw)) { //if(Get_NIRQ()) //Packet send over
{
// if (SX1278_hw_GetDIO0(module->hw))
{ // if(Get_NIRQ()) //Packet send over
SX1278_SPIRead(module, LR_RegIrqFlags);
SX1278_clearLoRaIrq(module); // Clear irq
SX1278_standby(module); // Entry Standby mode
return 1;
}
if (--timeout == 0) {
SX1278_hw_Reset(module->hw);
SX1278_config(module);
return 0;
}
SX1278_hw_DelayMs(1);
// if (--timeout == 0)
// {
// SX1278_hw_Reset(module->hw);
// SX1278_config(module);
// return 0;
// }
// SX1278_hw_DelayMs(1);
}
}
void SX1278_init(SX1278_t *module, uint64_t frequency, uint8_t power,
uint8_t LoRa_SF, uint8_t LoRa_BW, uint8_t LoRa_CR,
uint8_t LoRa_CRC_sum, uint8_t packetLength) {
uint8_t LoRa_CRC_sum, uint8_t packetLength)
{
sx1278_spix = module->hw->spi;
SX1278_hw_init(module->hw);
module->frequency = frequency;
module->power = power;
@ -253,38 +302,45 @@ void SX1278_init(SX1278_t *module, uint64_t frequency, uint8_t power,
}
int SX1278_transmit(SX1278_t *module, uint8_t *txBuf, uint8_t length,
uint32_t timeout) {
if (SX1278_LoRaEntryTx(module, length, timeout)) {
uint32_t timeout)
{
if (SX1278_LoRaEntryTx(module, length, timeout))
{
return SX1278_LoRaTxPacket(module, txBuf, length, timeout);
}
return 0;
}
int SX1278_receive(SX1278_t *module, uint8_t length, uint32_t timeout) {
int SX1278_receive(SX1278_t *module, uint8_t length, uint32_t timeout)
{
return SX1278_LoRaEntryRx(module, length, timeout);
}
uint8_t SX1278_available(SX1278_t *module) {
uint8_t SX1278_available(SX1278_t *module)
{
return SX1278_LoRaRxPacket(module);
}
uint8_t SX1278_read(SX1278_t *module, uint8_t *rxBuf, uint8_t length) {
uint8_t SX1278_read(SX1278_t *module, uint8_t *rxBuf, uint8_t length)
{
if (length != module->readBytes)
length = module->readBytes;
memcpy(rxBuf, module->rxBuffer, length);
rxBuf[length] = '\0';
// rxBuf[length] = '\0';
module->readBytes = 0;
return length;
}
uint8_t SX1278_RSSI_LoRa(SX1278_t *module) {
uint8_t SX1278_RSSI_LoRa(SX1278_t *module)
{
uint32_t temp = 10;
temp = SX1278_SPIRead(module, LR_RegRssiValue); // Read RegRssiValue, Rssi value
temp = temp + 127 - 137; // 127:Max RSSI, 137:RSSI offset
return (uint8_t)temp;
}
uint8_t SX1278_RSSI(SX1278_t *module) {
uint8_t SX1278_RSSI(SX1278_t *module)
{
uint8_t temp = 0xff;
temp = SX1278_SPIRead(module, RegRssiValue);
temp = 127 - (temp >> 1); // 127:Max RSSI

View File

@ -152,7 +152,8 @@
#define SX1278_POWER_14DBM 2
#define SX1278_POWER_11DBM 3
static const uint8_t SX1278_Power[4] = { 0xFF, //20dbm
static const uint8_t SX1278_Power[4] = {
0xFF, // 20dbm
0xFC, // 17dbm
0xF9, // 14dbm
0xF6, // 11dbm
@ -179,7 +180,8 @@ static const uint8_t SX1278_SpreadFactor[7] = { 6, 7, 8, 9, 10, 11, 12 };
#define SX1278_LORA_BW_250KHZ 8
#define SX1278_LORA_BW_500KHZ 9
static const uint8_t SX1278_LoRaBandwidth[10] = { 0, // 7.8KHz,
static const uint8_t SX1278_LoRaBandwidth[10] = {
0, // 7.8KHz,
1, // 10.4KHz,
2, // 15.6KHz,
3, // 20.8KHz,
@ -205,11 +207,18 @@ static const uint8_t SX1278_CodingRate[4] = { 0x01, 0x02, 0x03, 0x04 };
static const uint8_t SX1278_CRC_Sum[2] = {0x01, 0x00};
typedef enum _SX1278_STATUS {
SLEEP, STANDBY, TX, RX
typedef enum _SX1278_STATUS
{
SLEEP,
STANDBY,
TX,
RX
} SX1278_Status_t;
typedef struct {
#define RECEIVE_PACKET_LENGTH 1
typedef struct
{
SX1278_hw_t *hw;
uint64_t frequency;

View File

@ -8,20 +8,28 @@
#include "SX1278_hw.h"
#include <string.h>
#include "gpio.h"
#include "spi.h"
#include "systick.h"
#include "softspi.h"
__weak void SX1278_hw_init(SX1278_hw_t *hw) {
#define SetSS SoftSPI_SetSS(sx1278_spix)
#define ClrSS SoftSPI_ClrSS(sx1278_spix)
#define SPI_WriteRead(__byte__) SoftSPI_WriteRead(sx1278_spix, __byte__)
extern SoftSPI_TypeDef *sx1278_spix;
void SX1278_hw_init(SX1278_hw_t *hw)
{
SX1278_hw_SetNSS(hw, 1);
HAL_GPIO_WritePin(hw->reset.port, hw->reset.pin, GPIO_PIN_SET);
}
__weak void SX1278_hw_SetNSS(SX1278_hw_t *hw, int value) {
HAL_GPIO_WritePin(hw->nss.port, hw->nss.pin,
(value == 1) ? GPIO_PIN_SET : GPIO_PIN_RESET);
void SX1278_hw_SetNSS(SX1278_hw_t *hw, int value)
{
(value == 1) ? SetSS : ClrSS;
}
__weak void SX1278_hw_Reset(SX1278_hw_t *hw) {
void SX1278_hw_Reset(SX1278_hw_t *hw)
{
SX1278_hw_SetNSS(hw, 1);
HAL_GPIO_WritePin(hw->reset.port, hw->reset.pin, GPIO_PIN_RESET);
@ -32,29 +40,29 @@ __weak void SX1278_hw_Reset(SX1278_hw_t *hw) {
SX1278_hw_DelayMs(100);
}
__weak void SX1278_hw_SPICommand(SX1278_hw_t *hw, uint8_t cmd) {
void SX1278_hw_SPICommand(SX1278_hw_t *hw, uint8_t cmd)
{
SX1278_hw_SetNSS(hw, 0);
HAL_SPI_Transmit(hw->spi, &cmd, 1, 1000);
while (HAL_SPI_GetState(hw->spi) != HAL_SPI_STATE_READY)
;
SPI_WriteRead(cmd);
}
__weak uint8_t SX1278_hw_SPIReadByte(SX1278_hw_t *hw) {
uint8_t SX1278_hw_SPIReadByte(SX1278_hw_t *hw)
{
uint8_t txByte = 0x00;
uint8_t rxByte = 0x00;
SX1278_hw_SetNSS(hw, 0);
HAL_SPI_TransmitReceive(hw->spi, &txByte, &rxByte, 1, 1000);
while (HAL_SPI_GetState(hw->spi) != HAL_SPI_STATE_READY)
;
rxByte = SPI_WriteRead(txByte);
return rxByte;
}
__weak void SX1278_hw_DelayMs(uint32_t msec) {
HAL_Delay(msec);
}
__weak int SX1278_hw_GetDIO0(SX1278_hw_t *hw) {
return (HAL_GPIO_ReadPin(hw->dio0.port, hw->dio0.pin) == GPIO_PIN_SET);
void SX1278_hw_DelayMs(uint32_t msec)
{
Delay_ms(msec);
}
// int SX1278_hw_GetDIO0(SX1278_hw_t *hw)
// {
// return (HAL_GPIO_ReadPin(hw->dio0.port, hw->dio0.pin) == GPIO_PIN_SET);
// }

View File

@ -9,17 +9,20 @@
#define __SX1278_HW_HEADER
#include <stdint.h>
#include "softspi.h"
typedef struct {
typedef struct
{
int pin;
void *port;
} SX1278_hw_dio_t;
typedef struct {
typedef struct
{
SX1278_hw_dio_t reset;
SX1278_hw_dio_t dio0;
SX1278_hw_dio_t nss;
void *spi;
SoftSPI_TypeDef *spi;
} SX1278_hw_t;
/**
@ -89,7 +92,6 @@ void SX1278_hw_DelayMs(uint32_t msec);
*
* \return 0 if DIO0 low, 1 if DIO high
*/
int SX1278_hw_GetDIO0(SX1278_hw_t *hw);
// int SX1278_hw_GetDIO0(SX1278_hw_t *hw);
#endif