全能小网关|CH32V208--第五篇:ETH与BLE的联动
<div class='showpostmsg'> 本帖最后由 xiaolinen 于 2024-7-20 19:07 编辑<p><span style="font-size:22px;"><strong>一:说明</strong></span></p>
<p><span style="font-size:20px;"> (1)本次实验是在BLE_UART例程中,添加TCP客户端和SLEEP功能下进行的;</span></p>
<p><span style="font-size:20px;"> (2)ETH和BLE共存的情况下,将系统核心时钟设置为120MHz:</span></p>
<p><span style="font-size:20px;"> 既:uint32_t SystemCoreClock=SYSCLK_FREQ_120MHz_HSE; </span></p>
<p> </p>
<p><strong><span style="font-size:22px;">二:设置SLEEP功能</span></strong></p>
<p><span style="font-size:20px;"> 2.1,sleep功能进入逻辑</span></p>
<p><span style="font-size:20px;"> 当蓝牙接收到“AT+SLEEP=1\r\n”时,进入低功耗模式;</span></p>
<p><span style="font-size:20px;"> 2.2,sleep功能使能和低功耗模式下GPIO的状态设置函数</span></p>
<pre>
<code class="language-cpp">/*********************************************************************
* @fn sleep_modem_gpio_state_func
*
* <a href="home.php?mod=space&uid=159083" target="_blank">@brief </a>sleep休眠模式时,GPIO的状态
*
* <a href="home.php?mod=space&uid=784970" target="_blank">@return </a>none
*/
void sleep_modem_gpio_state_func(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //enable clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //enable clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //enable clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); //enable clock
GPIO_InitTypeDef GPIO_InitStructure = {0}; //gpio init struct
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; //gpio pin
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //gpio mode
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //gpio speed
GPIO_Init(GPIOA, &GPIO_InitStructure); //gpio init
GPIO_Init(GPIOB, &GPIO_InitStructure); //gpio init
GPIO_Init(GPIOC, &GPIO_InitStructure); //gpio init
GPIO_Init(GPIOD, &GPIO_InitStructure); //gpio init
}
/*********************************************************************
* @fn sleep_mode_enable
*
* @brief 使能sleep休眠模式
*
* @returnnone
*/
void sleep_mode_enable(void)
{
if(((RCC->CFGR0 & RCC_SWS)==0x04)||((RCC->CFGR0 & RCC_PLLSRC) == RCC_PLLSRC))
{
RCC_HSICmd(DISABLE);
}
__WFI();
}</code></pre>
<p> </p>
<p><strong><span style="font-size:22px;">三:添加TCP客户端功能</span></strong></p>
<p><span style="font-size:20px;"> 3.1,添加wchnet库文件</span></p>
<p><span style="font-size:20px;"> 3.1.1,工程中添加操作,如下图所示:</span></p>
<div style="text-align: center;"></div>
<div style="text-align: center;"><span style="color:#e74c3c;">备注:步骤4为添加后的结果</span></div>
<div><span style="font-size:20px;"> 3.1.2,“包含”添加操作,如下图所示:</span></div>
<div>
<div style="text-align: center;"></div>
<p><span style="font-size:20px;"> 3.1.3,“库”添加操作,如下图所示:</span></p>
<div style="text-align: center;"></div>
<p><span style="font-size:20px;"> 3.1.4,“库路径”添加操作,如下图所示:</span></p>
<div style="text-align: center;"></div>
<p><span style="font-size:20px;"> 3.1.5,TCP部分代码记录,如下所示:</span></p>
<pre>
<code class="language-cpp">/*********************************************************************
* @fn WCHNET_CreateTcpSocket
*
* @brief Create TCP Socket
*
* @returnnone
*/
static u8 socket_test;
/*********************************************************************
* @fn WCHNET_CreateTcpSocket
*
* @brief Create TCP Socket
*
* @returnnone
*/
void WCHNET_CreateTcpSocket(void)
{
u8 i;
SOCK_INF TmpSocketInf;
memset((void *) &TmpSocketInf, 0, sizeof(SOCK_INF));
memcpy((void *) TmpSocketInf.IPAddr, DESIP, 4);
TmpSocketInf.DesPort = desport;
TmpSocketInf.SourPort = srcport++;
TmpSocketInf.ProtoType = PROTO_TYPE_TCP;
TmpSocketInf.RecvBufLen = RECE_BUF_LEN;
i = WCHNET_SocketCreat(&SocketId, &TmpSocketInf);
printf("WCHNET_SocketCreat %d\r\n", SocketId);
mStopIfError(i);
i = WCHNET_SocketConnect(SocketId); //make a TCP connection
mStopIfError(i);
}
/*********************************************************************
* @fn WCH_TCP_CLIENT_SEND
*
* @brief TCP客户端发送数据
*
* @param void
*
* @returnnone
*/
uint8_t wch_tcp_client_send_func(uint8_t *buf, uint32_t *len)
{
return WCHNET_SocketSend(socket_test, buf, len);
}
/*********************************************************************
* @fn on_bleuartServiceEvt
*
* @brief ble uart service callback handler
*
* @returnNULL
*/
extern void wch_tcp_client_send_func(uint8_t *buf, uint32_t *len);
void on_bleuartServiceEvt(uint16_t connection_handle, ble_uart_evt_t *p_evt)
{
switch(p_evt->type)
{
case BLE_UART_EVT_TX_NOTI_DISABLED:
PRINT("%02x:bleuart_EVT_TX_NOTI_DISABLED\r\n", connection_handle);
break;
case BLE_UART_EVT_TX_NOTI_ENABLED:
PRINT("%02x:bleuart_EVT_TX_NOTI_ENABLED\r\n", connection_handle);
break;
case BLE_UART_EVT_BLE_DATA_RECIEVED://BLE数据接收事件
PRINT("BLE RX DATA len:%d\r\n", p_evt->data.length);
PRINT("BLE RX DATA :%s\r\n", p_evt->data.p_data);
//此处,将数据发送到TCP服务器端
if(wch_tcp_client_send_func((uint8_t *)p_evt->data.p_data,(uint32_t*)&p_evt->data.length) == p_evt->data.length)
{
printf("data sent successfully!\r\n");
}
break;
default:
break;
}
}</code></pre>
<p> </p>
</div>
<p><span style="font-size: 22px;"><b>四:实验现象</b></span></p>
<p><span style="font-size:20px;"> 4.1,数据传输</span></p>
<p><span style="font-size:20px;"> 4.1.1,TCP服务器端记录,如下:</span></p>
<div style="text-align: center;"></div>
<div><span style="font-size:20px;"> 4.1.2,设备运行日志记录,如下:</span></div>
<div>
<pre>
<code class="language-cpp">/* 上电初始化 */
CH32V20x_BLE_LIB_V1.40
SystemClk:120000000
net version:19
WCHNET_LibInit Success
WCHNET_SocketCreat 0
PHY Link Success
/* BLE连接成功 */
Initialized..
AdvertisConn 1 - Int 18
Connected..
Update 1 - Int 6
Update 1 - Int 18
Update 1 - Int 10
/* TCP第一次连接超时 */
TCP Timeout
WCHNET_SocketCreat 0
/* TCP第二次连接超时 */
TCP Timeout
WCHNET_SocketCreat 0
/* TCP连接成功 */
PHY Link Success
TCP Connect Success
socket id: 0
/* 数据转发 */
BLE RX DATA len:19
BLE RX DATA :welcome to eeworld!
data sent successfully!
BLE RX DATA len:19
BLE RX DATA :welcome to eeworld!
data sent successfully!
BLE RX DATA len:19
BLE RX DATA :welcome to eeworld!
data sent successfully!
BLE RX DATA len:19
BLE RX DATA :welcome to eeworld!
data sent successfully!
BLE RX DATA len:19
BLE RX DATA :welcome to eeworld!
data sent successfully!
BLE RX DATA len:19
BLE RX DATA :welcome to eeworld!
data sent successfully!</code></pre>
<p> </p>
</div>
<p><span style="font-size:20px;"> 4.2,功耗记录</span></p>
<div style="text-align: center;"></div>
<p> </p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> <p>蓝牙转wlan,确实是比较牛的功能了。</p>
lugl4313820 发表于 2024-7-21 09:07
蓝牙转wlan,确实是比较牛的功能了。
<p>因为工作需要用到这个转换,也算是提前熟悉这部分功能了。</p>
页:
[1]