Continue to Site

Reply to thread

[CODE=cpp]#ifndef STM32_SX1281_H

#define STM32_SX1281_H


#include "stm32f1xx_hal.h"


//----------------------------------------------------------------------------- Registers


//                                                      Address   DefaultValue  Bit7         Bit6         Bit5         Bit4         Bit3         Bit2         Bit1         Bit0

#define CodingRate                                      0x950     //NA          ----         CR           CR           CR           ----         ----         ----         ----                  0x534C


//----------------------------------------------------------------------------- Commands

//                                                      OpCode

#define CMD_GetStatus                                   0xC0

#define CMD_SetStandby                                  0x80

#define CMD_SetPacketType                               0x8A


//----------------------------------------------------------------------------- Command Details

#define Mode_STDBY_RC                                   0x00

#define Mode_STDBY_XOSC                                 0x01

#define Packet_Type_GFSK                                0x00

#define Packet_Type_LORA                                0x01

#define Packet_Type_RANGING                             0x02

#define Packet_Type_FLRC                                0x03

#define Packet_Type_BLE                                 0x04


//----------------------------------------------------------------------------- Command Response

#define CMD_GetStatusResponse_CircuitMode_STDBY_RC      (0x02 << 5)

#define CMD_GetStatusResponse_CircuitMode_STDBY_XOSC    (0x03 << 5)

#define CMD_GetStatusResponse_CircuitMode_FS            (0x04 << 5)

#define CMD_GetStatusResponse_CircuitMode_Rx            (0x05 << 5)

#define CMD_GetStatusResponse_CircuitMode_Tx            (0x06 << 5)


//-----------------------------------------------------------------------------

#define TimeOutCounterforBusyPin                         50


class SX1281

{


private :

  uint16_t            ResetPin  ;

  GPIO_TypeDef*       ResetPort ;

  uint16_t            BusyPin   ;

  GPIO_TypeDef*       BusyPort  ;

  uint16_t            CTSPin    ;

  GPIO_TypeDef*       CTSPort   ;

  UART_HandleTypeDef *UARTBusNo ;


public :

  //-------- Constructor

  SX1281(UART_HandleTypeDef *_UARTBusNo, GPIO_TypeDef* _ResetPort, uint16_t _ResetPin, GPIO_TypeDef* _BusyPort, uint16_t _BusyPin, GPIO_TypeDef* _CTSPort, uint16_t _CTSPin);


  //-------- Methods

  bool SX1281_Reset(uint16_t *Error);

  bool SX1281_CheckBusyPin(uint16_t *Error) ;

 

  void SX1281_Init_LoRa(uint16_t *Error);

  void SX1281_GetStatus(uint8_t *CircuitMode, uint8_t *CommandStatus, uint16_t *Error);

 

  void SX1281_SetStandby(uint8_t StandbyConfig, uint16_t *Error);

  void SX1281_SetPacketType(uint8_t PacketType, uint16_t *Error);

 

 

  enum SX1281_Error

  {

    NoError = 0,

    BusyPinTogglingTimeout      = 0x0001          

  };

 

}; //END Class


#endif[/CODE]


this is .h code



[CODE=cpp]#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) ;

}[/CODE]


this is .c code


[ATTACH=full]197306[/ATTACH]


Part and Inventory Search

Back
Top