1.首先找一个空白的工程模板,在工程里面建立两个文件夹(组)radio和platform,向platform中添加,fifo.c, led.c, sx1276-Hal.c, sx12xxEiger.c;向radio中添加radio.c, sx1276-Fsk.c, sx1276-FskMisc.c, sx1276-LoRa.c, sx1276-LoRaMisc.c, sx1276.c; 并在工程中添加其对应的头文件路径。编译整个工程,出现错误。
2.第一处修改,在开发软件中添加宏定义PLATFORM=SX12xxEiger,此宏定义在platform.h文件的开头调用,用于条件编译。
3.找不到头文件stm32f10x.h。这里我们在sx12xxEiger.h和fifo.h中的开始部分修改宏定义,两处一样,如图1所示。因为我用的是103平台,所以根据需求修改。
图1 修包含改头文件
3.注释部分用不到的代码,根据平台修改,入门级学习,此处直接注释。将sx12xxEiger.c中的下面代码注释。
//#if( defined( STM32F4XX ) || defined( STM32F2XX ) || defined( STM32F429_439xx ) )
//
//#include "usbd_cdc_core.h"
//#include "usbd_usr.h"
//#include "usbd_desc.h"
//
//#else
//
//#include "usb_regs.h"
//#include "usb_core.h"
//#include "usb_init.h"
//#include "usb_pwr.h"
//#include "usb_bsp.h"
//#include "usb_sil.h"
//
//#endif
//#if( defined( STM32F4XX ) || defined( STM32F2XX ) || defined( STM32F429_439xx ) )
//
//#ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
// #if defined ( __ICCARM__ ) /*!< IAR Compiler */
// #pragma data_alignment=4
// #endif
//#endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
//
//__ALIGN_BEGIN USB_OTG_CORE_HANDLE USB_OTG_dev __ALIGN_END ;
//
//#else
//
//#endif
将 void BoardInit(void)函数中的代码全部注释,注意不要注释函数名字。因为我使用的是Cube MX配置的HAL库所以不需要进行初始化了,并且sx1276驱动源码是在标准的库函数上编写的。
4.修改led相关内容。首先修改led.h文件中的IO口相关定义,不用关心条件编译中的其他平台,我们只关心#else里面的,改动如下
图2 LED端口修改
接下来修改led.c中的时钟和初始化,用不到时钟所以直接注释,将void LedInit( tLed led )内部代码全部注释如图3.
图3 led初始化修改
将void LedOn( tLed led )和void LedOff( tLed led )内部的标准库换成HAL库函数代码如下
void LedOn( tLed led )
{
HAL_GPIO_WritePin( LedPort[led], LedPin[led], LED_ON );
}
void LedOff( tLed led )
{
HAL_GPIO_WritePin( LedPort[led], LedPin[led], LED_OFF );
}
5.修改sx1276-Hal.c文件。
根据自己的平台修改reset,NSS, DIO0到DIO5,我这里RESET使用的是硬件复位,直接注释掉,DIO使用0到3,其余注释掉,注意只关心条件编译#else下的端口就行,代码如下
/*!
* SX1276 SPI NSS I/O definitions
*/
#if defined( STM32F4XX ) || defined( STM32F2XX )
#define NSS_IOPORT GPIOA
#define NSS_PIN GPIO_Pin_15
#elif defined( STM32F429_439xx )
#define NSS_IOPORT GPIOA
#define NSS_PIN GPIO_Pin_4
#else
#define NSS_IOPORT GPIOC
#define NSS_PIN GPIO_PIN_4
#endif
/*!
* SX1276 DIO pins I/O definitions
*/
#if defined( STM32F4XX ) || defined( STM32F2XX )
#define DIO0_IOPORT GPIOG
#define DIO0_PIN GPIO_Pin_13
#elif defined( STM32F429_439xx )
#define DIO0_IOPORT GPIOG
#define DIO0_PIN GPIO_Pin_13
#else
#define DIO0_IOPORT GPIOB
#define DIO0_PIN GPIO_PIN_12
#endif
#if defined( STM32F4XX ) || defined( STM32F2XX )
#define DIO1_IOPORT GPIOB
#define DIO1_PIN GPIO_Pin_8
#elif defined( STM32F429_439xx )
#define DIO1_IOPORT GPIOB
#define DIO1_PIN GPIO_Pin_7
#else
#define DIO1_IOPORT GPIOB
#define DIO1_PIN GPIO_PIN_13
#endif
#if defined( STM32F4XX ) || defined( STM32F2XX )
#define DIO2_IOPORT GPIOA
#define DIO2_PIN GPIO_Pin_2
#elif defined( STM32F429_439xx )
#define DIO2_IOPORT GPIOA
#define DIO2_PIN GPIO_Pin_2
#else
#define DIO2_IOPORT GPIOB
#define DIO2_PIN GPIO_PIN_14
#endif
//#if defined( STM32F4XX ) || defined( STM32F2XX ) || defined( STM32F429_439xx )
//#define DIO3_IOPORT
//#define DIO3_PIN RF_DIO3_PIN
//#else
//#define DIO3_IOPORT GPIOB
//#define DIO3_PIN GPIO_PIN_15
//#endif
//
//#if defined( STM32F4XX ) || defined( STM32F2XX ) || defined( STM32F429_439xx )
//#define DIO4_IOPORT
//#define DIO4_PIN RF_DIO4_PIN
//#else
//#define DIO4_IOPORT
//#define DIO4_PIN RF_DIO4_PIN
//#endif
//
//#if defined( STM32F4XX ) || defined( STM32F2XX ) || defined( STM32F429_439xx )
//#define DIO5_IOPORT
//#define DIO5_PIN RF_DIO5_PIN
//#else
//#define DIO5_IOPORT
//#define DIO5_PIN RF_DIO5_PIN
//#endif
//
//#if defined( STM32F4XX ) || defined( STM32F2XX ) || defined( STM32F429_439xx )
//#define RXTX_IOPORT
//#define RXTX_PIN FEM_CTX_PIN
//#else
//#define RXTX_IOPORT
//#define RXTX_PIN FEM_CTX_PIN
//#endif
void SX1276InitIo( void )
{
// GPIO_InitTypeDef GPIO_InitStructure;
//
//#if defined( STM32F4XX ) || defined( STM32F2XX ) || defined( STM32F429_439xx )
// RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB |
// RCC_AHB1Periph_GPIOG, ENABLE );
//#else
// RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
// RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE );
//#endif
//
//#if defined( STM32F4XX ) || defined( STM32F2XX ) || defined( STM32F429_439xx )
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
//#else
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//#endif
//
// // Configure NSS as output
// GPIO_WriteBit( NSS_IOPORT, NSS_PIN, Bit_SET );
// GPIO_InitStructure.GPIO_Pin = NSS_PIN;
// GPIO_Init( NSS_IOPORT, &GPIO_InitStructure );
//
// // Configure radio DIO as inputs
//#if defined( STM32F4XX ) || defined( STM32F2XX ) || defined( STM32F429_439xx )
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//#else
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//#endif
//
// // Configure DIO0
// GPIO_InitStructure.GPIO_Pin = DIO0_PIN;
// GPIO_Init( DIO0_IOPORT, &GPIO_InitStructure );
//
// // Configure DIO1
// GPIO_InitStructure.GPIO_Pin = DIO1_PIN;
// GPIO_Init( DIO1_IOPORT, &GPIO_InitStructure );
//
// // Configure DIO2
// GPIO_InitStructure.GPIO_Pin = DIO2_PIN;
// GPIO_Init( DIO2_IOPORT, &GPIO_InitStructure );
//
// // REAMARK: DIO3/4/5 configured are connected to IO expander
//
// // Configure DIO3 as input
//
// // Configure DIO4 as input
//
// // Configure DIO5 as input
}
void SX1276SetReset( uint8_t state )
{
// GPIO_InitTypeDef GPIO_InitStructure;
if( state == RADIO_RESET_ON )
{
// Set RESET pin to 0
HAL_GPIO_WritePin( RESET_IOPORT, RESET_PIN, GPIO_PIN_RESET );
// Configure RESET as output
//#if defined( STM32F4XX ) || defined( STM32F2XX ) || defined( STM32F429_439xx )
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
// GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
//#else
//
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
//#endif
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_InitStructure.GPIO_Pin = RESET_PIN;
// GPIO_Init( RESET_IOPORT, &GPIO_InitStructure );
}
else
{
//#if FPGA == 0
// // Configure RESET as input
//#if defined( STM32F4XX ) || defined( STM32F2XX ) || defined( STM32F429_439xx )
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
//#else
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//#endif
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_InitStructure.GPIO_Pin = RESET_PIN;
// GPIO_Init( RESET_IOPORT, &GPIO_InitStructure );
//#else
HAL_GPIO_WritePin( RESET_IOPORT, RESET_PIN, GPIO_PIN_RESET );
//#endif
}
}
注意下面的读写Buffer需要改为HAL库函数,代码如下
void SX1276WriteBuffer( uint8_t addr, uint8_t *buffer, uint8_t size )
{
uint8_t i;
//NSS = 0;
HAL_GPIO_WritePin( NSS_IOPORT, NSS_PIN, GPIO_PIN_RESET );
SpiInOut( addr | 0x80 );
for( i = 0; i < size; i++ )
{
SpiInOut( buffer[i] );
}
//NSS = 1;
HAL_GPIO_WritePin( NSS_IOPORT, NSS_PIN, GPIO_PIN_SET );
}
void SX1276ReadBuffer( uint8_t addr, uint8_t *buffer, uint8_t size )
{
uint8_t i;
//NSS = 0;
HAL_GPIO_WritePin( NSS_IOPORT, NSS_PIN, GPIO_PIN_RESET );
SpiInOut( addr & 0x7F );
for( i = 0; i < size; i++ )
{
buffer[i] = SpiInOut( 0 );
}
//NSS = 1;
HAL_GPIO_WritePin( NSS_IOPORT, NSS_PIN, GPIO_PIN_SET );
}
将没有用到的DIO读取函数返回值修改为1,防止出错,注释掉下面的硬件控制收发。
6.修改sx1276-Hal.h中的tick获取宏定义,使用HAL库函数,代码如下
#define GET_TICK_COUNT( ) ( HAL_GetTick() )
7 实现uint8_t SpiInOut( uint8_t outData )函数
我们将实现函数编写在SPI相关文件里面,如spi.c。代码如下
uint8_t SpiInOut( uint8_t outData )
{
uint8_t pData = 0;
if(HAL_SPI_TransmitReceive(&hspi1, &outData, &pData, 1, 0xffff)!= HAL_OK)
return ERROR;
else
return pData;
}
8.上电自检操作,证明移植成功
在主函数初始化中添加一下代码,注意引入相关头文件。
SX1276Read( REG_LR_VERSION, &RegVersion );
if(RegVersion != 0x12)
{
printf("LoRa Read Error!!!\r\n");
printf("LoRa RegVersion: %#x\r\n", RegVersion);
printf("Init Error!!!\r\n");
}
else
{
printf("LoRa Read OK!!!\r\n");
printf("LoRa RegVersion: %#x\r\n", RegVersion);
printf("Init Ok!!!\r\n");
//LedToggle(LED_GREEN);
}
到此移植结束,亲测可用,更多学习交流,请加入QQ群:1095431780