一、lora芯片126x、127x差异
1、支持频段
SX1278芯片支持的频段为137-525MHz,射频发射输出脚为第27脚(PA_BOOST)或第28脚(RFO_LF),射频接收输入脚为第1脚(RFI_LF);
SX1276支持的频段为137-1020MHz,当使用137-525MHz时,射频发射输出脚必须为第27脚(PA_BOOST)或第28脚(RFO_LF),射频接收输入脚必须为第1脚(RFI_LF),当使用862-1020MHz时,射频发射输出脚必须为第27脚(PA_BOOST)或第22脚(RFO_HF),射频接收输入脚必须为第21脚(RFI_HF)。
SX1268支持的频段为150-960MHz,射频发射输出脚为第23脚(RFO),射频接收差分输入脚分别为第21脚(RFI_P)、第22脚(RFI_N)。更换频段时,无需更换引脚,只需调整射频电路参数。
2、晶振电路
SX1268芯片与SX1278/6芯片三者均可以采用TCXO晶振,若采用TCXO晶振,则XTB引脚不接,但SX1268芯片的第6脚(DIO3)可用来为TCXO晶振供电,只需通过软件配置。采用XTAL时,SX1278/6外部需添加匹配电容;而SX1268外部无需添加匹配电容,内部已自带,可直接通过软件调节。
3、lora无线模块配电及发射功率、接收灵敏度、电流
SX1278/6芯片仅有一种配电方式,最大发射功率20dBm,要达到最大发射功率,需使用第27脚(PA_BOOST),发射电流120mA@20dBm。
SX1268芯片最大发射功率可达22dBm,带有两种配电方式,低压差稳压器(LDO)以及高效率降压DC-DC转换器,可选择DC-DC形式,发射电流118mA@22dBm。
SX1278/6芯片的接收电流约为12mA左右,SX1268芯片在DC-DC方式下,接收电流约为5mA左右;三者能达到的最高灵敏度为-148dBm。
4、扩频因子、空速等参数
在LoRa调制下,SX1278/6的扩频因子6-12,BW 7.8-500kHz,空中速率0.018-37.5kbps。而SX1268的扩频因子5-12,BW 7.81-500kHz,空中速率0.018-62.5kbps。可以看出在LoRa调制技术下,SX1268芯片可以达到的空中速率要比SX1278/6大得多。
二、sx126x,127x寄存器,接口都是基本一致,分享一段通用性、结构性都比较好的lora驱动程序,自适应126x、127x
typedef struct {void (*DioPin_top_half)(void);/*!* \brief Tx Done callback prototype.*/void (*TxDone_topHalf)(void); // read irqAt for timestamp of interruptvoid (*TxDone_botHalf)(void); // read irqAt for timestamp of interrupt/*!* \brief Tx Timeout callback prototype.*/void ( *TxTimeout )( void );/*!* \brief Rx Done callback prototype.** \param [IN] payload Received buffer pointer* \param [IN] size Received buffer size* \param [IN] rssi RSSI value computed while receiving the frame [dBm]* \param [IN] snr Raw SNR value given by the radio hardware* FSK : N/A ( set to 0 )* LoRa: SNR value in dB* \param [IN] curTime captured time at RxDone event occurance*///void ( *RxDone )(uint16_t size, int16_t rssi, int8_t snr);void ( *RxDone )(uint8_t size, float rssi, float snr); // read radio.rx_buf for payload, irqAt for timestamp of interrupt/*!* \brief Rx Timeout callback prototype.*/void ( *RxTimeout )( void );/*!* \brief Rx Error callback prototype.*/void ( *RxError )( void );/*!* \brief FHSS Change Channel callback prototype.** \param [IN] currentChannel Index number of the current channel*/void ( *FhssChangeChannel )( uint8_t currentChannel );/*!* \brief CAD Done callback prototype.** \param [IN] channelDetected Channel Activity detected during the CAD*/void ( *CadDone ) ( bool channelActivityDetected );
} RadioEvents_t;
事件回调结构
DioPin_top_half:DIO事件回调函数
TxDone_topHalf:发送上半部处理
TxDone_botHalf:发送下半部处理,猜测类似linux中断分成上下半部处理提升发送效率
TxTimeout:发送超时回调
RxDone:接收完成
RxTimeout:接收超时
RxError:接收错误
FhssChangeChannel:改变频率
CadDone:CAD前导码回调
typedef struct {void (*init)(const RadioEvents_t*);void (*standby)(void);void (*loRaModemConfig)(unsigned bwKHz, uint8_t sf, uint8_t cr);void (*setChannel)(unsigned hz);void (*set_tx_dbm)(int8_t dbm);// preambleLen, fixLen, crcOn, invIQvoid (*loRaPacketConfig)(unsigned preambleLen, bool fixLen, bool crcOn, bool invIQ);int (*send)(uint8_t size/*, timestamp_t maxListenTime, timestamp_t channelFreeTime, int rssiThresh*/);void (*printOpMode)(void);bool (*service)(void);void (*rx)(unsigned timeout);void (*setLoRaSymbolTimeout)(uint16_t symbs);void (*irqTopHalf)(void);uint16_t irq_pin;void (*postcfgreadback)(void);bool (*bw_at_highest)(void);bool (*bw_at_lowest)(void);bool (*sf_at_slowest)(void);bool (*sf_at_fastest)(void);uint8_t *rx_buf;uint8_t *tx_buf;
} lorahal_t;
HAL接口结构定义
init:初始化,将上面定义的事件回调结构作为入口
standby:待机处理
loRaModemConfig:配置lora参数
setChannel:设置频段
set_tx_dbm:设置功率
loRaPacketConfig:设置lora数据参数
send:发送函数
printOpMode:获取工作模式
service:根据相应中断状态调用处理函数
rx:接收
setLoRaSymbolTimeout:
irqTopHalf:中断上半部处理
irq_pin:中断引脚
postcfgreadback:配置读回
bw_at_highest
bw_at_lowest
sf_at_slowest
sf_at_fastest
这四个应该是带宽与扩频因子相关函数,具体不清楚什么用
rx_buf
tx_buf
lora的发送接收缓冲
整个驱动都是围绕这两个结构转,对上层应用也不需因为切换不同芯片而修改,操作系统驱动设计思想,但这种实现方式更加简洁
要使用127x驱动,将其初始化到127x的相关函数
void sethal_sx127x()
{lorahal.init = Init_sx127x;lorahal.standby = Standby_sx127x;lorahal.loRaModemConfig = LoRaModemConfig_sx127x;lorahal.setChannel = SetChannel_sx127x;lorahal.set_tx_dbm = set_tx_dbm_sx127x;lorahal.loRaPacketConfig = LoRaPacketConfig_sx127x;lorahal.send = Send_sx127x;lorahal.printOpMode = printOpMode_sx127x;lorahal.service = service_sx127x;lorahal.rx = Rx_sx127x;lorahal.setLoRaSymbolTimeout = SetLoRaSymbolTimeout_sx127x;lorahal.irqTopHalf = SX127x_dio0_topHalf;lorahal.irq_pin = DIO0_PIN;lorahal.postcfgreadback = PostConfigReadBack;lorahal.bw_at_highest = is_bw_at_highest;lorahal.bw_at_lowest = is_bw_at_lowest;lorahal.sf_at_slowest = is_sf_at_slowest;lorahal.sf_at_fastest = is_sf_at_fastest;lorahal.rx_buf = SX127x_rx_buf;lorahal.tx_buf = SX127x_tx_buf;
}
要使用126x驱动,将其初始化到126x的相关函数
void sethal_sx126x()
{lorahal.init = Init_sx126x;lorahal.standby = Standby_sx126x;lorahal.loRaModemConfig = LoRaModemConfig_sx126x;lorahal.setChannel = SetChannel_sx126x;lorahal.set_tx_dbm = set_tx_dbm_sx126x;lorahal.loRaPacketConfig = LoRaPacketConfig_sx126x;lorahal.send = Send_sx126x;lorahal.printOpMode = printOpMode_sx126x;lorahal.service = service_sx126x;lorahal.rx = Rx_sx126x;lorahal.setLoRaSymbolTimeout = SetLoRaSymbolTimeout_sx126x;lorahal.irqTopHalf = SX126x_dio1_topHalf;lorahal.irq_pin = DIO1_PIN;lorahal.postcfgreadback = PostConfigReadBack;lorahal.bw_at_highest = is_bw_at_highest;lorahal.bw_at_lowest = is_bw_at_lowest;lorahal.sf_at_slowest = is_sf_at_slowest;lorahal.sf_at_fastest = is_sf_at_fastest;lorahal.rx_buf = SX126x_rx_buf;lorahal.tx_buf = SX126x_tx_buf;
}
事件回调函数结构在上层定义,
void radio_irq_callback()
{tickAtIrq = uwTick;
}void txDoneCB()
{txing = 0;appHal.lcd_printOpMode(false);lcd_print_tx_duration((int)(tickAtIrq - txStartAt), (unsigned)cycleDur);if (rx_start_at_tx_done) { lora_rx_begin();rx_start_at_tx_done = 0;}
}void rxTimeoutCB()
{
}const RadioEvents_t rev = {/* DioPin_top_half */ radio_irq_callback,/* TxDone_topHalf */ NULL,/* TxDone_botHalf */ txDoneCB,/* TxTimeout */ NULL,/* RxDone */ rxDoneCB,/* RxTimeout */ rxTimeoutCB,/* RxError */ NULL,/* FhssChangeChannel */NULL,/* CadDone */ NULL
};
const RadioEvents_t rev = {/* DioPin_top_half */ radio_irq_callback,/* TxDone_topHalf */ NULL,/* TxDone_botHalf */ txDoneCB,/* TxTimeout */ NULL,/* RxDone */ rxDoneCB,/* RxTimeout */ rxTimeoutCB,/* RxError */ NULL,/* FhssChangeChannel */NULL,/* CadDone */ NULL
};
通过读取OPMODE寄存器识别126x、127x芯片,通过lorahal.init(&rev);
将2种芯片的共同事件回调函数结构传入,完成初始化
芯片识别,接口初始化RegOpMode.octet = read_reg(REG_OPMODE);if (RegOpMode.octet != 0xff) {/* radio is sx127x: status.octet==00, sx127xopmode==1 */printf("is sx127x\r\n");sethal_sx127x();} else {/* radio is sx126x: status.octet==45, sx127xopmode==0xff */printf("IS sx126x\r\n");sethal_sx126x();}上层应用的调用,统一的接口lorahal.init(&rev);lorahal.standby();lorahal.loRaModemConfig(LORA_BW_KHZ, sf_at_500KHz, 1);lorahal.setChannel(CF_HZ);lorahal.set_tx_dbm(TX_DBM);// preambleLen, fixLen, crcOn, invIQlorahal.loRaPacketConfig(8, false, false, false); // crcOfflorahal.postcfgreadback();
上一篇:异常情况下的生命周期