xiaolinen 发表于 2024-7-20 19:07

全能小网关|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;">&nbsp; &nbsp; &nbsp; &nbsp; (1)本次实验是在BLE_UART例程中,添加TCP客户端和SLEEP功能下进行的;</span></p>

<p><span style="font-size:20px;">&nbsp; &nbsp; &nbsp; &nbsp; (2)ETH和BLE共存的情况下,将系统核心时钟设置为120MHz:</span></p>

<p><span style="font-size:20px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;既:uint32_t SystemCoreClock=SYSCLK_FREQ_120MHz_HSE;&nbsp;</span></p>

<p>&nbsp;</p>

<p><strong><span style="font-size:22px;">二:设置SLEEP功能</span></strong></p>

<p><span style="font-size:20px;">&nbsp; &nbsp; &nbsp; &nbsp;2.1,sleep功能进入逻辑</span></p>

<p><span style="font-size:20px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 当蓝牙接收到&ldquo;AT+SLEEP=1\r\n&rdquo;时,进入低功耗模式;</span></p>

<p><span style="font-size:20px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2.2,sleep功能使能和低功耗模式下GPIO的状态设置函数</span></p>

<pre>
<code class="language-cpp">/*********************************************************************
* @fn      sleep_modem_gpio_state_func
*
* <a href="home.php?mod=space&amp;uid=159083" target="_blank">@brief </a>sleep休眠模式时,GPIO的状态
*
* <a href="home.php?mod=space&amp;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, &amp;GPIO_InitStructure);                //gpio init
    GPIO_Init(GPIOB, &amp;GPIO_InitStructure);                //gpio init
    GPIO_Init(GPIOC, &amp;GPIO_InitStructure);                //gpio init
    GPIO_Init(GPIOD, &amp;GPIO_InitStructure);                //gpio init
}

/*********************************************************************
* @fn      sleep_mode_enable
*
* @brief   使能sleep休眠模式
*
* @returnnone
*/
void sleep_mode_enable(void)
{
    if(((RCC-&gt;CFGR0 &amp; RCC_SWS)==0x04)||((RCC-&gt;CFGR0 &amp; RCC_PLLSRC) == RCC_PLLSRC))
       {
         RCC_HSICmd(DISABLE);
       }
    __WFI();
}</code></pre>

<p>&nbsp;</p>

<p><strong><span style="font-size:22px;">三:添加TCP客户端功能</span></strong></p>

<p><span style="font-size:20px;">&nbsp; &nbsp; &nbsp; &nbsp; 3.1,添加wchnet库文件</span></p>

<p><span style="font-size:20px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 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;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3.1.2,&ldquo;包含&rdquo;添加操作,如下图所示:</span></div>

<div>
<div style="text-align: center;"></div>

<p><span style="font-size:20px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3.1.3,&ldquo;库&rdquo;添加操作,如下图所示:</span></p>

<div style="text-align: center;"></div>

<p><span style="font-size:20px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3.1.4,&ldquo;库路径&rdquo;添加操作,如下图所示:</span></p>

<div style="text-align: center;"></div>

<p><span style="font-size:20px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 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 *) &amp;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(&amp;SocketId, &amp;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-&gt;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-&gt;data.length);
            PRINT("BLE RX DATA :%s\r\n", p_evt-&gt;data.p_data);

            //此处,将数据发送到TCP服务器端
            if(wch_tcp_client_send_func((uint8_t *)p_evt-&gt;data.p_data,(uint32_t*)&amp;p_evt-&gt;data.length) == p_evt-&gt;data.length)
            {
                printf("data sent successfully!\r\n");
            }
            break;
      default:
            break;
    }
}</code></pre>

<p>&nbsp;</p>
</div>

<p><span style="font-size: 22px;"><b>四:实验现象</b></span></p>

<p><span style="font-size:20px;">&nbsp; &nbsp; &nbsp; &nbsp; 4.1,数据传输</span></p>

<p><span style="font-size:20px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4.1.1,TCP服务器端记录,如下:</span></p>

<div style="text-align: center;"></div>

<div><span style="font-size:20px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 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>&nbsp;</p>
</div>

<p><span style="font-size:20px;">&nbsp; &nbsp; &nbsp; &nbsp; 4.2,功耗记录</span></p>

<div style="text-align: center;"></div>

<p>&nbsp;</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>

lugl4313820 发表于 2024-7-21 09:07

<p>蓝牙转wlan,确实是比较牛的功能了。</p>

xiaolinen 发表于 2024-7-21 11:51

lugl4313820 发表于 2024-7-21 09:07
蓝牙转wlan,确实是比较牛的功能了。

<p>因为工作需要用到这个转换,也算是提前熟悉这部分功能了。</p>
页: [1]
查看完整版本: 全能小网关|CH32V208--第五篇:ETH与BLE的联动