/*
|
______ _
|
/ _____) _ | |
|
( (____ _____ ____ _| |_ _____ ____| |__
|
\____ \| ___ | (_ _) ___ |/ ___) _ \
|
_____) ) ____| | | || |_| ____( (___| | | |
|
(______/|_____)_|_|_| \__)_____)\____)_| |_|
|
(C)2013 Semtech
|
|
Description: SX126x driver specific target board functions implementation
|
|
License: Revised BSD License, see LICENSE.TXT file include in the project
|
|
Maintainer: Miguel Luis and Gregory Cristian
|
*/
|
//#include "stm32f0xx.h"
|
#include "delay.h"
|
//#include "gpio.h"
|
//#include "Spi.h"
|
//#include "spi.h"
|
#include "radio.h"
|
#include "sx126x.h"
|
#include "sx126x-board.h"
|
#include "main.h"
|
#include "stm32l0xx_hal_spi.h"
|
#include "Spi.h"
|
extern SPI_HandleTypeDef hspi1;
|
|
|
//void HAL_Delay_nMS( uint32_t Delay )
|
//{
|
// uint32_t tickstart = 0;
|
// tickstart = HAL_GetTick( );
|
// while( ( HAL_GetTick( ) - tickstart ) < Delay );
|
//}
|
void SX126x_CS_Di( )
|
{
|
if(!HAL_GPIO_ReadPin( SPIx_CS_GPIO, SPIx_CS))
|
{
|
HAL_GPIO_WritePin( SPIx_CS_GPIO, SPIx_CS,GPIO_PIN_SET);
|
printf("uwb_cs_error\r\n");
|
}
|
|
}
|
|
/*!
|
* @brief Sends txBuffer and receives rxBuffer
|
*
|
* @param [IN] txBuffer Byte to be sent
|
* @param [OUT] rxBuffer Byte to be sent
|
* @param [IN] size Byte to be sent
|
*/
|
uint8_t SpiInOut( uint8_t txBuffer)
|
{
|
uint8_t rxData = 0;
|
HAL_SPI_TransmitReceive(&hspi1,&txBuffer,&rxData,1, 1000);
|
return( rxData );
|
}
|
|
|
|
void SX126xReset( void )
|
{
|
HAL_Delay_nMS( 10 );
|
HAL_GPIO_WritePin( RADIO_nRESET_GPIO_Port, RADIO_nRESET_Pin,GPIO_PIN_RESET);
|
delay_us( 2000 );
|
HAL_GPIO_WritePin( RADIO_nRESET_GPIO_Port, RADIO_nRESET_Pin,GPIO_PIN_SET);
|
HAL_Delay_nMS( 10 );
|
}
|
|
void SX126xWaitOnBusy( void )
|
{
|
while(HAL_GPIO_ReadPin(RADIO_BUSY_GPIO_Port,RADIO_BUSY_Pin)==GPIO_PIN_SET);
|
}
|
|
|
void SX126xWakeup( void )
|
{
|
SX126x_CS_Di( );
|
HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin,GPIO_PIN_RESET);
|
SpiInOut(RADIO_GET_STATUS);
|
SpiInOut(0);
|
|
HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin,GPIO_PIN_SET);
|
|
// Wait for chip to be ready.
|
SX126xWaitOnBusy( );
|
}
|
|
void SX126xWriteCommand( RadioCommands_t command, uint8_t *buffer, uint16_t size )
|
{
|
|
SX126xCheckDeviceReady( );
|
SX126x_CS_Di( );
|
HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin,GPIO_PIN_RESET);
|
|
SpiInOut(( uint8_t )command );
|
|
for( uint16_t i = 0; i < size; i++ )
|
{
|
SpiInOut(buffer[i] );
|
}
|
|
HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin,GPIO_PIN_SET);
|
|
if( command != RADIO_SET_SLEEP )
|
{
|
SX126xWaitOnBusy( );
|
}
|
}
|
|
void SX126xReadCommand( RadioCommands_t command, uint8_t *buffer, uint16_t size )
|
{
|
SX126xCheckDeviceReady( );
|
SX126x_CS_Di( );
|
HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin,GPIO_PIN_RESET);
|
|
SpiInOut(( uint8_t )command );
|
SpiInOut(0x00 );
|
for( uint16_t i = 0; i < size; i++ )
|
{
|
buffer[i] = SpiInOut(0 );
|
}
|
|
HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin,GPIO_PIN_SET);
|
|
SX126xWaitOnBusy( );
|
}
|
|
void SX126xWriteRegisters( uint16_t address, uint8_t *buffer, uint16_t size )
|
{
|
SX126xCheckDeviceReady( );
|
SX126x_CS_Di( );
|
HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin,GPIO_PIN_RESET);
|
|
SpiInOut(RADIO_WRITE_REGISTER );
|
SpiInOut(( address & 0xFF00 ) >> 8 );
|
SpiInOut( address & 0x00FF );
|
|
for( uint16_t i = 0; i < size; i++ )
|
{
|
SpiInOut(buffer[i] );
|
}
|
|
|
HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin,GPIO_PIN_SET);
|
|
SX126xWaitOnBusy( );
|
}
|
|
void SX126xWriteRegister( uint16_t address, uint8_t value )
|
{
|
SX126xWriteRegisters( address, &value, 1 );
|
}
|
|
void SX126xReadRegisters( uint16_t address, uint8_t *buffer, uint16_t size )
|
{
|
SX126xCheckDeviceReady( );
|
SX126x_CS_Di( );
|
HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin,GPIO_PIN_RESET);
|
|
SpiInOut(RADIO_READ_REGISTER );
|
SpiInOut(( address & 0xFF00 ) >> 8 );
|
SpiInOut( address & 0x00FF );
|
SpiInOut( 0 );
|
for( uint16_t i = 0; i < size; i++ )
|
{
|
buffer[i] = SpiInOut(0 );
|
}
|
|
HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin,GPIO_PIN_SET);
|
|
SX126xWaitOnBusy( );
|
}
|
|
uint8_t SX126xReadRegister( uint16_t address )
|
{
|
uint8_t data;
|
SX126xReadRegisters( address, &data, 1 );
|
return data;
|
}
|
|
void SX126xWriteBuffer( uint8_t offset, uint8_t *buffer, uint8_t size )
|
{
|
SX126xCheckDeviceReady( );
|
SX126x_CS_Di( );
|
HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin,GPIO_PIN_RESET);
|
|
SpiInOut( RADIO_WRITE_BUFFER );
|
SpiInOut( offset );
|
for( uint16_t i = 0; i < size; i++ )
|
{
|
SpiInOut( buffer[i] );
|
}
|
|
HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin,GPIO_PIN_SET);
|
|
SX126xWaitOnBusy( );
|
}
|
|
void SX126xReadBuffer( uint8_t offset, uint8_t *buffer, uint8_t size )
|
{
|
SX126xCheckDeviceReady( );
|
SX126x_CS_Di( );
|
HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin,GPIO_PIN_RESET);
|
|
SpiInOut( RADIO_READ_BUFFER );
|
SpiInOut( offset );
|
SpiInOut( 0 );
|
for( uint16_t i = 0; i < size; i++ )
|
{
|
buffer[i] = SpiInOut( 0 );
|
}
|
|
HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin,GPIO_PIN_SET);
|
|
SX126xWaitOnBusy( );
|
}
|
|
void SX126xSetRfTxPower( int8_t power )
|
{
|
SX126xSetTxParams( power, RADIO_RAMP_40_US );
|
}
|
|
uint8_t SX126xGetPaSelect( uint32_t channel )
|
{
|
// if( GpioRead( &DeviceSel ) == 1 )
|
// {
|
// return SX1261;
|
// }
|
// else
|
// {
|
// return SX1262;
|
// }
|
|
return SX1262;
|
}
|
|
void SX126xAntSwOn( void )
|
{
|
//GpioInit( &AntPow, ANT_SWITCH_POWER, PIN_OUTPUT, PIN_PUSH_PULL, PIN_PULL_UP, 1 );
|
}
|
|
void SX126xAntSwOff( void )
|
{
|
// GpioInit( &AntPow, ANT_SWITCH_POWER, PIN_ANALOGIC, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
|
}
|
|
bool SX126xCheckRfFrequency( uint32_t frequency )
|
{
|
// Implement check. Currently all frequencies are supported
|
return true;
|
}
|