AIR32F103CBT6的存儲容量加上206MHz頻率, 跑RTOS才能充分利用它的性能. 關於FreeRTOS的介紹和集成, 網路上已經有不少文章, 可以直接百度搜索查看, 這裡主要介紹一下項目中的FreeRTOS集成步驟和代碼說明. ...
1. ping測試
協議棧提供ping工具,其頭文件為“net_tools/ping.h”,將其include進你的目標系統中即可使用這個工具。
……
#include "onps.h"
#include "net_tools/ping.h"
//* 回調函數,收到目標地址的應答報文後ping工具會調用這個函數完成用戶的特定處理邏輯
//* 針對這個測試,在這裡就是簡單地列印出了應答報文的內容以及ping的響應時間
static void ping_recv_handler(USHORT usIdentifier, //* ping的標識id,響應報文與探測報文這個id應該一致
in_addr_t unFromAddr, //* 響應報文的源地址
USHORT usSeqNum, //* 響應報文序號,其與探測報文一致
UCHAR *pubEchoData, //* 響應報文攜帶的響應數據,其與探測報文一致
UCHAR ubEchoDataLen, //* 響應報文攜帶的數據長度
UCHAR ubTTL, //* ttl值
UCHAR ubElapsedMSecs) //* 響應時長,單位:秒,從發送探測報文開始計時到收到響應報文結束計時
{
CHAR szSrcAddr[20];
struct in_addr stInAddr;
stInAddr.s_addr = unFromAddr;
printf("<Fr>%s, recv %d bytes, ID=%d, Sequence=%d, Data='%s', TTL=%d, time=%dms\r\n",
inet_ntoa_safe(stInAddr, szSrcAddr), //* 這是一個線程安全的ip地址轉ascii字元串函數
(UINT)ubEchoDataLen,
usIdentifier,
usSeqNum,
pubEchoData,
(UINT)ubTTL,
(UINT)ubElapsedMSecs);
}
int main(void)
{
if(open_npstack_load(&enErr))
{
printf("The open source network protocol stack (ver %s) is loaded successfully. \r\n", ONPS_VER);
//* 協議棧載入成功,在這裡初始化ethernet網卡或等待ppp鏈路就緒
#if 0
emac_init(); //* ethernet網卡初始化函數,並註冊網卡到協議棧
#else
while(!netif_is_ready("ppp0")) //* 等待ppp鏈路建立成功
os_sleep_secs(1);
#endif
}
else
{
printf("The open source network protocol stack failed to load, %s\r\n", onps_error(enErr));
return -1;
}
//* 啟動ping測試
USHORT usSeqNum = 0;
UINT unErrCount = 0;
INT nPing = ping_start(&enErr);
if(nPing < 0)
{
//* 啟動失敗,輸出一條日誌信息
printf("ping_start() failed, %s\r\n", onps_error(enErr));
return -1;
}
while(TRUE && usSeqNum < 100)
{
//* ping目標地址
INT nRtnVal = ping(nPing, inet_addr("192.168.0.2"), usSeqNum++, 64, GetElapsedMSecs, ping_recv_handler, 3, &enErr);
if(nRtnVal <= 0) //* ping返回一個錯誤
{
//* 累計ping錯誤數
unErrCount++;
//* 控制台列印當前錯誤數
printf("no reply received, the current number of errors is %d, current error: %s\r\n", unErrCount, nRtnVal ? onps_error(enErr) : "recv timeout");
}
os_sleep_secs(1);
}
//* 結束ping測試
ping_end(nPing);
return 0;
}
上述示例代碼調用了ping測試工具提供的幾個api函數。ping_start()函數的調用非常簡單,其功能就是開啟ping測試。結束ping測試需要調用ping_end()函數,否則ping測試會一直占用協議棧資源。這幾個函數的說明如下:
2. dns測試
這個測試需要為網卡設定好能夠訪問互聯網的網關、DNS伺服器地址等配置信息。當然如果採用dhcp動態地址申請的方式能夠得到這些信息那就更省事了。dns查詢工具的頭文件為"net_tools/dns.h"。
……
#include "onps.h"
#include "net_tools/ping.h"
int main(void)
{
if(open_npstack_load(&enErr))
{
printf("The open source network protocol stack (ver %s) is loaded successfully. \r\n", ONPS_VER);
//* 協議棧載入成功,在這裡初始化ethernet網卡或等待ppp鏈路就緒
#if 0
emac_init(); //* ethernet網卡初始化函數,並註冊網卡到協議棧
#else
while(!netif_is_ready("ppp0")) //* 等待ppp鏈路建立成功
os_sleep_secs(1);
#endif
}
else
{
printf("The open source network protocol stack failed to load, %s\r\n", onps_error(enErr));
return -1;
}
//* dns查詢測試
in_addr_t unPrimaryDNS, unSecondaryDNS;
INT nDnsClient = dns_client_start(&unPrimaryDNS, &unSecondaryDNS, 3, &enErr);
if(nDnsClient < 0)
{
//* dns客戶端啟動失敗,輸出一條錯誤日誌
printf("%s\r\n", onps_error(enErr));
}
else
{
//* 發送查詢請求並等待dns伺服器的應答
in_addr_t unIp = dns_client_query(nDnsClient, unPrimaryDNS, unSecondaryDNS, "gitee.com", &enErr);
if(unIp) //* 查詢成功
{
CHAR szAddr[20];
printf("The ip addr: %s\r\n", inet_ntoa_safe_ext(unIp, szAddr));
}
else
printf("%s\r\n", onps_error(enErr)); //* 查詢失敗
//* 結束dns查詢,釋放占用的協議棧資源
dns_client_end(nDnsClient);
}
return 0;
}
與ping測試工具相同,dns查詢工具同樣提供了一組簡單的api函數用於實現功能變數名稱查詢。這一組函數包括dns_client_start()、dns_client_end()以及dns_client_query(),其使用說明如下:
3. sntp網路校時測試
與dns的測試要求一樣,要進行這個測試依然要確保你的開發板在物理層能夠訪問互聯網,同時你的開發板支持rtc,並提供一組rtc操作函數,包括讀取、設置系統當前時間等api。這裡假設你的測試環境已經具備上述測試條件。sntp網路校時工具的頭文件為"net_tools/sntp.h"。
……
#include "onps.h"
#include "net_tools/sntp.h"
int main(void)
{
if(open_npstack_load(&enErr))
{
printf("The open source network protocol stack (ver %s) is loaded successfully. \r\n", ONPS_VER);
//* 協議棧載入成功,在這裡初始化ethernet網卡或等待ppp鏈路就緒
#if 0
emac_init(); //* ethernet網卡初始化函數,並註冊網卡到協議棧
#else
while(!netif_is_ready("ppp0")) //* 等待ppp鏈路建立成功
os_sleep_secs(1);
#endif
}
else
{
printf("The open source network protocol stack failed to load, %s\r\n", onps_error(enErr));
return -1;
}
//* 先設定個不合理的時間,以測試網路校時功能是否正常,由rtc驅動提供,負責修改系統當前時間
//* RTC首碼的函數為目標系統應提供的rtc時鐘操作函數
RTCSetSysTime(22, 9, 5, 17, 42, 30);
//* 開啟網路校時,sntp_update_by_ip()與sntp_update_by_dns()均可使用
ST_DATETIME stDateTime;
#if 1
if(sntp_update_by_ip("52.231.114.183", NULL, RTCSetSystemUnixTimestamp, 8, &enErr)) //* ntp伺服器地址直接校時
#else
if(sntp_update_by_dns("time.windows.com", Time, RTCSetSystemUnixTimestamp, 8, &enErr)) //* ntp伺服器功能變數名稱方式校時
#endif
{
//* 獲取系統時間,檢查校時結果
RTCGetSysTime(&stDateTime);
//* 控制台輸出當前系統時間
printf("The time is %d-%02d-%02d %02d:%02d:%02d\r\n", stDateTime.usYear, stDateTime.ubMonth,
stDateTime.ubDay, stDateTime.ubHour, stDateTime.ubMin, stDateTime.ubSec);
}
else
{
printf("%s\r\n", onps_error(enErr));
return -1;
}
return 0;
}
測試代碼首先把時間設定在了2022年9月5日17點42分30秒,目的是為了驗證目標系統時間是否會被成功校正。測試代碼用到了目標系統應提供的一組rtc時鐘操作函數。其中RTCSetSysTime()用於設置系統時間。RTCSetSystemUnixTimestamp()函數同樣也是設置系統時間,只不過是通過unix時間戳進行設置。RTCGetSysTime()函數用於讀取當前系統時間。相較於ping及dns工具,sntp網路校時工具只提供了一個介面函數sntp_update_by_xx()即可完成校時。我們可以通過ntp伺服器地址也可以通過ntp伺服器功能變數名稱進行校時。該函數的詳細使用說明如下:
sntp_update_by_dns()函數與sntp_update_by_ip()函數除了第一個入口參數變成了功能變數名稱外,其它完全相同,不再贅述。