Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 83 additions & 42 deletions board-drivers/SX1280/inc/SX1280.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
#define SX1280_H_


// SX1280 command opcodes, etc can be defined here or in the .c file


//from SX1280 datasheet table 12-1
// SX1280 command opcodes (Datasheet Table 12-1)
#define SX1280_CMD_GET_STATUS 0xC0
#define SX1280_CMD_WRITE_REGISTER 0x18
#define SX1280_CMD_READ_REGISTER 0x19
Expand All @@ -38,6 +35,9 @@
#define SX1280_CMD_SET_PACKET_PARAMS 0x8C
#define SX1280_CMD_SET_BUFFER_BASE_ADDRESS 0x8F
#define SX1280_CMD_GET_RX_BUFFER_STATUS 0x17
#define SX1280_CMD_GET_IRQ_STATUS 0x15
#define SX1280_CMD_CLEAR_IRQ_STATUS 0x97
#define SX1280_CMD_SET_DIO_IRQ_PARAMS 0x8D

// Ramp time definitions for SetTxParams (Table 11-49)
#define SX1280_RAMP_02_US 0x00 // 2 us
Expand All @@ -50,7 +50,6 @@
#define SX1280_RAMP_20_US 0xE0 // 20 us

// PeriodBase definitions for SetTx/SetRx timeout (Table 11-24)
// These define the time step used for timeout calculations
#define SX1280_PERIODBASE_15_625_US 0x00 // 15.625 us steps
#define SX1280_PERIODBASE_62_5_US 0x01 // 62.5 us steps
#define SX1280_PERIODBASE_1_MS 0x02 // 1 ms steps
Expand All @@ -61,9 +60,11 @@
#define SX1280_RX_TIMEOUT_NONE 0x0000 // No timeout, Rx single mode
#define SX1280_RX_TIMEOUT_CONTINUOUS 0xFFFF // Continuous RX mode

#define SX1280_SPI_TIMEOUT_MS 100

// Standby modes for SET_STANDBY command
#define STDBY_RC 0x00
#define STDBY_XOSC 0x01

#define SX1280_SPI_TIMEOUT_MS 100


/**
Expand All @@ -76,13 +77,10 @@ typedef enum {
SX1280_TIMEOUT
} SX1280_Status_t;


/**
* @brief structure to handle all necessary hardware handles/pins for the SX1280
* instance of this struct should be passed to Initialize function
*/


typedef struct {
SPI_HandleTypeDef* spiHandle;
GPIO_TypeDef* nssPort;
Expand All @@ -93,13 +91,10 @@ typedef struct {
uint16_t busyPin;
} SX1280_Hal_t;


/**
* @brief packet types supported by sx1280 (Table 11-42)
*
*/


typedef enum {
PACKET_TYPE_GFSK = 0x00,
PACKET_TYPE_LORA = 0x01,
Expand All @@ -108,9 +103,6 @@ typedef struct {
PACKET_TYPE_BLE = 0x04
} SX1280_PacketType_t;




/**
* @brief LoRa spreading factor (table 14-47)
*/
Expand All @@ -125,7 +117,6 @@ typedef struct {
LORA_SF12 = 0xC0
} SX1280_LoRa_SF_t;


/**
* @brief LoRa bandwidth settings (table 14-48)
*/
Expand All @@ -136,12 +127,9 @@ typedef struct {
LORA_BW_200 = 0x34, // 203.125
} SX1280_LoRa_BW_t;


/**
* @brief LoRa coding rate settings (table 14-49)
*/


typedef enum {
LORA_CR_4_5 = 0x01,
LORA_CR_4_6 = 0x02,
Expand All @@ -152,7 +140,6 @@ typedef struct {
LORA_CR_LI_4_8 = 0x07
} SX1280_LoRa_CR_t;


/**
* @brief LoRa packet header types (table 14-51)
*/
Expand All @@ -161,7 +148,6 @@ typedef struct {
LORA_IMPLICIT_HEADER = 0x80
} SX1280_LoRa_Header_Type_t;


/**
* @brief LoRa crc settings (table 14-53)
*/
Expand All @@ -170,7 +156,6 @@ typedef struct {
LORA_CRC_ON = 0x20
} SX1280_LoRa_CRC_t;


/**
* @brief LoRa IQ setting (table 14-54)
*/
Expand All @@ -181,8 +166,6 @@ typedef struct {


// PUBLIC API FUNCTION PROTOTYPES ------------------//


/**
* @brief initialize SX1280 radio transceiver
*
Expand All @@ -195,9 +178,23 @@ typedef struct {
* @return SX1280_Status_t status of initialization
*/
SX1280_Status_t SX1280_Init(SX1280_Hal_t* hal_config);

/**
* @brief Writes a buffer of data to the radio's TX FIFO.
* @param data Pointer to the data to write.
* @param length Number of bytes to write.
* @return SX1280_Status_t Status of operation.
*/
SX1280_Status_t SX1280_WriteBuffer(uint8_t* data, uint8_t length);
int16_t SX1280_ReadBuffer(uint8_t* data, uint8_t maxLength);

/**
* @brief Reads a received packet from the radio's RX FIFO.
* @note This function MUST be called after an RxDone IRQ.
* @param data Pointer to a buffer to store the received data.
* @param maxLength Max number of bytes to read into the buffer.
* @return int16_t Number of bytes read, or -1 on error.
*/
int16_t SX1280_ReadBuffer(uint8_t* data, uint8_t maxLength);

// config prototypes
/**
Expand All @@ -206,14 +203,12 @@ int16_t SX1280_ReadBuffer(uint8_t* data, uint8_t maxLength);
*/
SX1280_Status_t SX1280_SetPacketType(SX1280_PacketType_t packetType);


/**
* @brief set radio carrier freq
* @param frequency the frequency in Hz (ex. 2400000000 for 2.4 GHz)
*/
SX1280_Status_t SX1280_SetRfFrequency(uint32_t frequency);


/**
* @brief set modulation param for current packet type
* @param sf spreading factor (LoRa only)
Expand All @@ -225,14 +220,13 @@ SX1280_Status_t SX1280_SetModulationParams(SX1280_LoRa_SF_t sf, SX1280_LoRa_BW_t

/**
* @brief set packet params for current packet type
* @param preambleLength length of preamble in symbols
* @param preambleLengthVal The 8-bit *encoded* preamble length (see Table 14-50)
* @param headerType explicit or implicit header (LoRa only)
* @param payloadLength length of payload in bytes
* @param crcOn crc on or off (LoRa only)
* @param crc crc on or off (LoRa only)
* @param invertIQ standard or inverted IQ (LoRa only)
*/
SX1280_Status_t SX1280_SetPacketParams(uint16_t preambleLength, SX1280_LoRa_Header_Type_t headerType, uint8_t payloadLength, SX1280_LoRa_CRC_t crc, SX1280_LoRa_IQ_t invertIQ);

SX1280_Status_t SX1280_SetPacketParams(uint8_t preambleLengthVal, SX1280_LoRa_Header_Type_t headerType, uint8_t payloadLength, SX1280_LoRa_CRC_t crc, SX1280_LoRa_IQ_t invertIQ);

/**
* @brief set base address for TX and RX buffers
Expand All @@ -241,15 +235,13 @@ SX1280_Status_t SX1280_SetPacketParams(uint16_t preambleLength, SX1280_LoRa_Head
*/
SX1280_Status_t SX1280_SetBufferBaseAddress(uint8_t txBaseAddress, uint8_t rxBaseAddress);


/**
* @brief sets transmit output power and ramp time
* @param power output power in dBm (-18 to +13 dBm)
* @param rampTime Ramp time constant.
*/
SX1280_Status_t SX1280_SetTxParams(int8_t power, uint8_t rampTime);


/**
* @brief Enables the High Sensitivity Mode (LNA boost).
*
Expand All @@ -263,32 +255,66 @@ SX1280_Status_t SX1280_SetTxParams(int8_t power, uint8_t rampTime);
*/
SX1280_Status_t SX1280_SetHighSensitivityMode(void);


/**
* @brief Configures which interrupts are enabled and how they are routed to DIO pins.
* @param irqMask 16-bit mask of interrupts to *enable*. (e.g., 0xFFFF to enable all)
* @param dio1Mask 16-bit mask of interrupts to *route* to DIO1.
* @param dio2Mask 16-bit mask of interrupts to *route* to DIO2.
* @param dio3Mask 16-bit mask of interrupts to *route* to DIO3.
* @return SX1280_Status_t Status of operation.
*/
SX1280_Status_t SX1280_SetDioIrqParams(uint16_t irqMask, uint16_t dio1Mask, uint16_t dio2Mask, uint16_t dio3Mask);

/**
* @brief Put radio in TX mode to transmit packet
* @param timeout Timeout in milliseconds (uses 1ms steps)
* 0 = no timeout (single mode, returns to STDBY_RC after packet sent)
* >0 = timeout active (returns to STDBY_RC on timeout or after packet sent)
* 0 = no timeout (single mode, returns to STDBY_RC after packet sent)
* >0 = timeout active (returns to STDBY_RC on timeout or after packet sent)
* @return SX1280_Status_t Status of operation
*/
SX1280_Status_t SX1280_SetTx(uint16_t timeout);

/**
* @brief Put radio in RX mode to receive packets
* @param timeout Timeout in milliseconds (uses 1ms steps)
* 0x0000 = no timeout (single mode, returns to STDBY_RC after packet received)
* 0xFFFF = continuous mode (stays in RX, can receive multiple packets)
* other = timeout active (returns to STDBY_RC on timeout or after packet received)
* 0x0000 = no timeout (single mode, returns to STDBY_RC after packet received)
* 0xFFFF = continuous mode (stays in RX, can receive multiple packets)
* other = timeout active (returns to STDBY_RC on timeout or after packet received)
* @return SX1280_Status_t Status of operation
*/
SX1280_Status_t SX1280_SetRx(uint16_t timeout);

/**
* @brief Put radio in a standby mode (STDBY_RC or STDBY_XOSC).
* @param standbyConfig The standby mode to enter (STDBY_RC or STDBY_XOSC).
* @return SX1280_Status_t Status of operation
*/
SX1280_Status_t SX1280_SetStandby(uint8_t standbyConfig);


/**
* @brief Sets the LoRa SyncWord.
* @note From datasheet 14.4.1 (page 133). The 8-bit word 0xXY is split.
* X (high nibble) is written to 0x944 [7:4]
* Y (low nibble) is written to 0x945 [7:4]
* @param syncWord 8-bit sync word to use (e.g., 0x12)
* @return SX1280_Status_t Status of operation
*/
SX1280_Status_t SX1280_SetLoRaSyncWord(uint8_t syncWord);

/**
* @brief Clears pending radio interrupts.
* @param irqMask 16-bit mask of interrupts to clear. 0xFFFF clears all.
* @return SX1280_Status_t Status of operation
*/
SX1280_Status_t SX1280_ClearIrqStatus(uint16_t irqMask);


/**
* @brief Gets the 16-bit interrupt status register.
* @param irqStatus Pointer to a uint16_t to store the status.
* @return SX1280_Status_t Status of operation
*/
SX1280_Status_t SX1280_GetIrqStatus(uint16_t* irqStatus);


// low level API prototypes --------------------//
Expand All @@ -300,7 +326,23 @@ SX1280_Status_t SX1280_SetRx(uint16_t timeout);
* @param size number of param bytes
*/
SX1280_Status_t SX1280_SendCommand(uint8_t opcode, uint8_t* buffer, uint16_t size);

/**
* @brief Writes data to one or more registers.
* @param address 16-bit register start address.
* @param buffer Pointer to the data to write.
* @param size Number of bytes to write.
* @return SX1280_Status_t Status of operation.
*/
SX1280_Status_t SX1280_WriteRegister(uint16_t address, uint8_t* buffer, uint16_t size);

/**
* @brief Reads data from one or more registers.
* @param address 16-bit register start address.
* @param buffer Pointer to a buffer to store the read data.
* @param size Number of bytes to read.
* @return SX1280_Status_t Status of operation.
*/
SX1280_Status_t SX1280_ReadRegister(uint16_t address, uint8_t* buffer, uint16_t size);


Expand All @@ -310,4 +352,3 @@ SX1280_Status_t SX1280_ReadRegister(uint16_t address, uint8_t* buffer, uint16_t

// this marks the end of the header file, additional function prototypes can be added above this line
#endif // SX1280_H_

Loading