#include "STM32_SX1281.hpp"
//------- Constructor
SX1281::SX1281(UART_HandleTypeDef *_UARTBusNo, GPIO_TypeDef* _ResetPort, uint16_t _ResetPin, GPIO_TypeDef* _BusyPort, uint16_t _BusyPin, GPIO_TypeDef* _CTSPort, uint16_t _CTSPin)
{
UARTBusNo = _UARTBusNo ;
ResetPort = _ResetPort ;
ResetPin = _ResetPin ;
BusyPort = _BusyPort ;
BusyPin = _BusyPin ;
CTSPort = _CTSPort ;
CTSPin = _CTSPin ;
}
/**
* @brief Check value of busy pin to
* @param ---
* @param ---
* @retval true if busy pin goes to low and false if timeout
*/
bool SX1281::SX1281_CheckBusyPin(uint16_t *Error)
{
uint32_t TimeOut = 0 ;
//------- Wait Until Busy Pin goes to low voltage and SX1281 Ready to accept command
do
{
TimeOut++ ;
if (TimeOut > TimeOutCounterforBusyPin)
{
*Error |= SX1281::SX1281_Error::BusyPinTogglingTimeout;
return false ;
}
//------- Read Busy Pin Value
}while((HAL_GPIO_ReadPin(BusyPort, BusyPin)) == 1) ;
return true ;
}
/**
* @brief Hardware Reset, after reset we should wait for busy pin to go low
but we do this by waiting for IC to go to Stand By mode state
* @param ---
* @param ---
* @retval None
*/
bool SX1281::SX1281_Reset(uint16_t *Error)
{
//------- Perform Hardware Reset
HAL_GPIO_WritePin(ResetPort, ResetPin, GPIO_PIN_RESET);
HAL_Delay(10) ;
HAL_GPIO_WritePin(ResetPort, ResetPin, GPIO_PIN_SET);
HAL_Delay(10) ;
return (SX1281_CheckBusyPin(Error)) ;
}
/**
* @brief Perform GetStatus command
* @param CircuitMode : Call by reference variable, initiated by 5 modes
* @param CommandStatus : Call by reference variable, initiated by command process status
* @retval None
*/
void SX1281::SX1281_GetStatus(uint8_t *CircuitMode, uint8_t *CommandStatus, uint16_t *Error)
{
uint8_t DataToSend[1] = {CMD_GetStatus} ;
uint8_t DataToReceive[1] = {0x00} ;
if (SX1281_CheckBusyPin(Error))
{
//------- Send GetStatus() Command
HAL_UART_Transmit(UARTBusNo, DataToSend, sizeof(DataToSend), 10) ;
}
//------- Receive GetStatus() Respone and Examine Return Value
HAL_UART_Receive(UARTBusNo, DataToReceive, sizeof(DataToSend), 10) ;
*CircuitMode = DataToReceive[0] & 0xE0 ; //[7:5]
*CommandStatus = DataToReceive[0] & 0x1C ; //[4:2]
}
/**
* @brief Set to Standby RC (13MHz) mode or Standby XOSC mode(52MHz)
* @param StandbyConfig : Should set by rc mode or xosc mode
Mode_STDBY_RC
Mode_STDBY_XOSC
* @retval None
*/
void SX1281::SX1281_SetStandby(uint8_t StandbyConfig, uint16_t *Error)
{
uint8_t DataToSend[3] = {CMD_SetStandby, 0x01, StandbyConfig} ;
if (SX1281_CheckBusyPin(Error))
{
//------- Send SetStandby() Command
HAL_UART_Transmit(UARTBusNo, DataToSend, sizeof(DataToSend), 10) ;
}
}
/**
* @brief The command SetPacketType() sets the transceiver radio frame
* @param PacketType : Should set one of 5 different type
* @retval None
*/
void SX1281::SX1281_SetPacketType(uint8_t PacketType, uint16_t *Error)
{
uint8_t DataToSend[3] = {CMD_SetPacketType, 0x01, PacketType} ;
if (SX1281_CheckBusyPin(Error))
{
//------- Send SetPacketType() Command
HAL_UART_Transmit(UARTBusNo, DataToSend, sizeof(DataToSend), 10) ;
}
}
/**
* @brief Initial Standby mode, Packet Type,
* @param ---
* @param ---
* @retval None
*/
void SX1281::SX1281_Init_LoRa(uint16_t *Error)
{
uint8_t mode = 0x00 ;
uint8_t Cstatus = 0x00;
SX1281_GetStatus(&mode, &Cstatus, Error) ;
//HAL_GPIO_WritePin(CTSPort, CTSPin, GPIO_PIN_SET);
//------- Set Standby Mode
SX1281_SetStandby(Mode_STDBY_RC, Error);
//------- Set Packet Type
//SX1281_SetPacketType(Packet_Type_LORA, Error);
//SX1281_GetStatus(&mode, &Cstatus, Error) ;
}