本帖最后由 xiaolinen 于 2024-7-20 19:07 编辑
一:说明
(1)本次实验是在BLE_UART例程中,添加TCP客户端和SLEEP功能下进行的;
(2)ETH和BLE共存的情况下,将系统核心时钟设置为120MHz:
既:uint32_t SystemCoreClock=SYSCLK_FREQ_120MHz_HSE;
二:设置SLEEP功能
2.1,sleep功能进入逻辑
当蓝牙接收到“AT+SLEEP=1\r\n”时,进入低功耗模式;
2.2,sleep功能使能和低功耗模式下GPIO的状态设置函数
/*********************************************************************
* @fn sleep_modem_gpio_state_func
*
* @brief sleep休眠模式时,GPIO的状态
*
* @return 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休眠模式
*
* @return none
*/
void sleep_mode_enable(void)
{
if(((RCC->CFGR0 & RCC_SWS)==0x04)||((RCC->CFGR0 & RCC_PLLSRC) == RCC_PLLSRC))
{
RCC_HSICmd(DISABLE);
}
__WFI();
}
三:添加TCP客户端功能
3.1,添加wchnet库文件
3.1.1,工程中添加操作,如下图所示:
备注:步骤4为添加后的结果
3.1.2,“包含”添加操作,如下图所示:
3.1.3,“库”添加操作,如下图所示:
3.1.4,“库路径”添加操作,如下图所示:
3.1.5,TCP部分代码记录,如下所示:
/*********************************************************************
* @fn WCHNET_CreateTcpSocket
*
* @brief Create TCP Socket
*
* @return none
*/
static u8 socket_test;
/*********************************************************************
* @fn WCHNET_CreateTcpSocket
*
* @brief Create TCP Socket
*
* @return none
*/
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
*
* @return none
*/
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
*
* @return NULL
*/
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;
}
}
四:实验现象
4.1,数据传输
4.1.1,TCP服务器端记录,如下:
4.1.2,设备运行日志记录,如下:
/* 上电初始化 */
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!
4.2,功耗记录