LoRa点对点系统7 剖析核心代码
1 引言
在LoRa点对点系统中,核心是Network进程的处理代码,它除了响应用户发送无线数据包外,还需要驱动射频和响应多种中断信号。为此,我们列出该进程的逻辑,并且剖析它的代码。
2 核心代码逻辑
有2个特别注意的地方:
1. 如果RF_Rx_Error,需要重新启动RF接收功能,以避免引发宕机错误;
2. RF缓冲区中有多个数据包时,给自身发poll消息,让其他进程有机会运行。
3 剖析核心代码
PROCESS_THREAD(NetworkProcess, ev, data)
{
/*ATTENTION: stack variables can NOT cross any “XX_YIELD()”. */
uint8_t byDataSize;
uint16_t wCalcCRC16;
uint16_t wSrcCRC16;
PROCESS_BEGIN();
SX1278Receive(0);
while(TRUE)
{
PROCESS_YIELD();
if (NETWORK_EVENT_TX_RF_PACKAGE == ev) 1
{
/* Make RF package as well as TX it. */
wCalcCRC16= util_CRC16( s_stTxRFPackage.a_byBuf,
s_stTxRFPackage.byRFTxSize );
*(uint16_t *)&s_stTxRFPackage.a_byBuf[s_stTxRFPackage.byRFTxSize] =
wCalcCRC16;
SX1278SetSleep(); 2
SX1278Send( s_stTxRFPackage.a_byBuf,
s_stTxRFPackage.byRFTxSize +sizeof(uint16_t)); 3
/* Waiting until TX is done. */
PROCESS_YIELD_UNTIL( RF_Tx_Done == s_tRFResult ||
RF_Tx_Timeout ==s_tRFResult ); 4
/* Enable RF_RX. */
SX1278Receive(0); 5
}
else 6
{
if (RF_Rx_Error == s_tRFResult)
{
/*EXPLAIN: Restore the RX of RFif Rx-Error or frame is NOT integrity. */
SX1278SetSleep(); 7
SX1278Receive(0);
}
/* EXPLAIN: process all received packets for simplicity. */
if (!IsRadioBufEmpty())
{
/* Fetch data from RF buffer, CheckCRC16. */
s_stParseRFBuf.bySize = GetRadioBuf(s_stParseRFBuf.a_byBuf,RF_FIFO_SIZE); 8
if (sizeof(uint16_t) < s_stParseRFBuf.bySize)
{
byDataSize = s_stParseRFBuf.bySize – sizeof(uint16_t);
wSrcCRC16 = *(uint16_t *)&s_stParseRFBuf.a_byBuf[byDataSize];
wCalcCRC16 = util_CRC16(s_stParseRFBuf.a_byBuf, byDataSize);
if (wSrcCRC16 == wCalcCRC16) 9
{
/* Deliver data to Comm2Trm-Process */
comm2trm_RxRFPackage(s_stParseRFBuf.a_byBuf, byDataSize); 10
}
else
{
/*EXPLAIN: Restore the RX of RF if Rx-Error orframe is NOT integrity. */
SX1278SetSleep(); 11
SX1278Receive(0);
}
}/*if*/
/* Check whether some packages still stay in buffer. */
if (!IsRadioBufEmpty()) 12
{
/* Don’t disturb the scheduling ofother processes. */
process_poll(&NetworkProcess);
}
}/*if (!IsRadioBufEmpty())*/
}
}/*while(TRUE)*/
PROCESS_END();
}
Line#1:用户需要发送RF数据包;
Line#2:将处于持续接收的RF设置休眠状态;
Line#3:通过SX1278发送该RF数据包;
Line#4:阻塞进程,直到RF发送数据包完毕;
Line#5:将RF切换到持续接收模式;
Line#6:射频中断服务程序发来消息:接收到数据包,或,接收错误;
Line#7:RF接收出错,重新启动RF接收功能,避免宕机错误;
Line#8:从RF缓冲区复制数据包到解析工作区;
Line#9:检查接收数据包的CRC16是否正确;
Line#10:交付接收数据给Comm2Trm进程;
Line#11:如果数据包的CRC16错误,重新启动RF接收功能,避免宕机错误;
Line#12:如果RF缓冲区还有数据包,给自身发poll消息,允许Contiki调度其他进程运行。
您的留言或需求: