diff --git a/Inc/sx127x.h b/Inc/sx127x.h index 4cd7e19..73505ca 100644 --- a/Inc/sx127x.h +++ b/Inc/sx127x.h @@ -32,5 +32,8 @@ typedef struct sx127x_s { int32_t sx127x_init(sx127x_t *sx127x, SPI_HandleTypeDef *hspi, GPIO_TypeDef *port, uint16_t pin); int32_t sx127x_tx(sx127x_t *sx127x, uint8_t *buf, uint16_t len); int32_t sx127x_rx(sx127x_t *sx127x, uint8_t *buf); +int32_t sx127x_snr(sx127x_t *sx127x); +int32_t sx127x_rssi(sx127x_t *sx127x); +int32_t sx127x_rand(sx127x_t *sx127x); #endif /* SX127X_INC_SX127X_H_ */ diff --git a/Src/sx127x.c b/Src/sx127x.c index 40c131a..0ca6c4e 100644 --- a/Src/sx127x.c +++ b/Src/sx127x.c @@ -269,6 +269,9 @@ int32_t sx127x_tx(sx127x_t *sx127x, uint8_t *buf, uint16_t len) uint16_t i; uint8_t tmp; + return_val_if_fail(sx127x, -1); + return_val_if_fail(buf, -1); + /* * FIFO can be prefilled in Sleep/Standby but must be refilled “on-the-fly” during Tx with the rest of the payload. * 1) Pre-fill FIFO (in Sleep/Standby first or directly in Tx mode) until FifoThreshold or FifoFull is set @@ -306,6 +309,9 @@ int32_t sx127x_rx(sx127x_t *sx127x, uint8_t *buf) uint8_t len; uint8_t ptr; + return_val_if_fail(sx127x, -1); + return_val_if_fail(buf, -1); + /* * In continuous mode the received packet processing sequence is given below. * 1 Whilst in Sleep or Standby mode select RXCONT mode. @@ -375,6 +381,61 @@ int32_t sx127x_rx(sx127x_t *sx127x, uint8_t *buf) return len; } +int32_t sx127x_snr(sx127x_t *sx127x) +{ + uint8_t tmp; + + return_val_if_fail(sx127x, INT32_MIN); + + /* read SNR from register as two's complement, multiplied by 4 */ + return_val_if_fail(sx127x_readbyte(sx127x, SX127X_REG_PKTSNRVALUE, &tmp) == 0, -1); + + return (int32_t) (int8_t) tmp; +} + +int32_t sx127x_rssi(sx127x_t *sx127x) +{ + uint8_t tmp; + int32_t snr = sx127x_snr(sx127x); + int32_t rssi; + + return_val_if_fail(sx127x, INT32_MIN); + + /* check if in LF or HF mode */ + return_val_if_fail(sx127x_readbyte(sx127x, SX127X_REG_PKTSNRVALUE, &tmp) == 0, -1); + if(tmp & SX127X_OPMODE_LFON) + { + rssi = -164; + } + else + { + rssi = -157; + } + /* read raw RSSI of latest received packet */ + return_val_if_fail(sx127x_readbyte(sx127x, SX127X_REG_PKTSNRVALUE, &tmp) == 0, -1); + /* compute RSSI */ + rssi += (int8_t) tmp; + rssi <<= 2; /* rssi *= 4 */ + if(snr < 0) + { + rssi += snr; + } + + return rssi; +} + +int32_t sx127x_rand(sx127x_t *sx127x) +{ + uint8_t tmp; + + return_val_if_fail(sx127x, INT32_MIN); + + /* read raw wideband RSSI */ + return_val_if_fail(sx127x_readbyte(sx127x, SX127X_REG_RSSIWIDEBAND, &tmp) == 0, -1); + + return tmp; +} + static int32_t sx127x_writebyte(sx127x_t *sx127x, uint8_t reg, uint8_t byte) { uint8_t txdata[2] = { WREG(reg), byte };