目录
  • 一、准备工作
  • 二、编译 SLE UART Server代码
  • 三、编译 SLE UART Client代码
  • 四、测试

SLE串口透传测试

该教程演示如何通过两块开发板实现SLE数据传输测试,A开发板通过串口接收数据,然后通过SLE传输给B开发板,B开发板通过串口将接收到的数据打印出来;同样,B开发板通过串口接收数据,然后通过SLE传输给A开发板,A开发板通过串口将接收到的数据打印出来。在测试中需要一块开发板做为Server端,另外一块开发板做为Client端,两块开发板配对后就可以互发消息了。

一、准备工作

  • 准备2块BearPi-Pico H3863开发板

二、编译 SLE UART Server代码

在Windows下编译操作
  1. 点击工具栏中的“系统配置”,打开配置界面.

    Alt text

  2. 选择Enable SLE UART Server sample

    Application  --->
        [*] Enable Sample.
        [ ]     Enable the Sample of peripheral.
        [*]     Enable the Sample of products.
        [ ]         Enable all the sample of product, it's just for build.
        [ ]         Support BLE UART sample.
        [*]         Support SLE UART sample.
                        SLE UART Sample Configuration  --->
                            Select sle uart type (Enable SLE UART Server sample.)  --->
                                    (X) Enable SLE UART Server sample.
                                    ( ) Enable SLE UART Client sample.
    

    Alt textAlt text

  3. 按下"Save"键保存配置。

    Alt text

  4. 关键代码,位于application\samples\products\sle_uart\sle_uart.c

    
    static void ssaps_server_write_request_cbk(uint8_t server_id, uint16_t conn_id, ssaps_req_write_cb_t *write_cb_para,
        errcode_t status)
    {
        osal_printk("%s ssaps write request callback cbk server_id:%x, conn_id:%x, handle:%x, status:%x\r\n",
            SLE_UART_SERVER_LOG, server_id, conn_id, write_cb_para->handle, status);
        if ((write_cb_para->length > 0) && write_cb_para->value) {
            osal_printk("\n sle uart recived data : %s\r\n", write_cb_para->value);
            uapi_uart_write(CONFIG_SLE_UART_BUS, (uint8_t *)write_cb_para->value, write_cb_para->length, 0); //将服务端接收到的数据通过UART发送出来
        }
    }
    
    
    static void sle_uart_server_read_int_handler(const void *buffer, uint16_t length, bool error)
    {
        unused(error);
        if (sle_uart_client_is_connected()) {
    #ifdef CONFIG_SAMPLE_SUPPORT_LOW_LATENCY_TYPE
        g_buff_data_valid = 1;
        g_uart_buff_len = 0;
        (void)memcpy_s(g_buff, SLE_UART_SERVER_SEND_BUFF_MAX_LEN, buffer, length);
        g_uart_buff_len = length;
    #else
        sle_uart_server_send_report_by_handle(buffer, length);                          //将UART中断回调接收到的数据发送给客户端
    #endif
        } else {
            osal_printk("%s sle client is not connected! \r\n", SLE_UART_SERVER_LOG);
        }
    }
    
    static void *sle_uart_server_task(const char *arg)
    {
        ...
    
        sle_uart_server_init(ssaps_server_read_request_cbk, ssaps_server_write_request_cbk);    //初始化服务端并注册接收中断回调
    
        ...
        errcode_t ret = uapi_uart_register_rx_callback(CONFIG_SLE_UART_BUS,                     //注册UART中断回调
                                                    UART_RX_CONDITION_FULL_OR_SUFFICIENT_DATA_OR_IDLE,
                                                    1, sle_uart_server_read_int_handler);
        ...
    }
    
  5. 编译烧录固件

    参考环境搭建教程编译烧录代码

三、编译 SLE UART Client代码

在Windows下编译操作
  1. 点击工具栏中的“系统配置”,打开配置界面.

    Alt text

  2. 选择Enable SLE UART Client sample

    Application  --->
        [*] Enable Sample.
        [ ]     Enable the Sample of peripheral.
        [*]     Enable the Sample of products.
        [ ]         Enable all the sample of product, it's just for build.
        [ ]         Support BLE UART sample.
        [*]         Support SLE UART sample.
                        SLE UART Sample Configuration  --->
                            Select sle uart type (Enable SLE UART Server sample.)  --->
                                    ( ) Enable SLE UART Server sample.
                                    (X) Enable SLE UART Client sample.
    

    Alt textAlt text

  3. 按下"Save"键保存配置。

    Alt text

  4. 关键代码,位于application\samples\products\sle_uart\sle_uart.c

    void sle_uart_notification_cb(uint8_t client_id, uint16_t conn_id, ssapc_handle_value_t *data,
        errcode_t status)
    {
        unused(client_id);
        unused(conn_id);
        unused(status);
        osal_printk("\n sle uart recived data : %s\r\n", data->data);
        uapi_uart_write(CONFIG_SLE_UART_BUS, (uint8_t *)(data->data), data->data_len, 0);       //将客户端接收到的数据通过UART发送出来
    }
    
    void sle_uart_indication_cb(uint8_t client_id, uint16_t conn_id, ssapc_handle_value_t *data,
        errcode_t status)
    {
        unused(client_id);
        unused(conn_id);
        unused(status);
        osal_printk("\n sle uart recived data : %s\r\n", data->data);
        uapi_uart_write(CONFIG_SLE_UART_BUS, (uint8_t *)(data->data), data->data_len, 0);
    }
    
    static void sle_uart_client_read_int_handler(const void *buffer, uint16_t length, bool error)
    {
        unused(error);
        ssapc_write_param_t *sle_uart_send_param = get_g_sle_uart_send_param();
        uint16_t g_sle_uart_conn_id = get_g_sle_uart_conn_id();
        sle_uart_send_param->data_len = length;
        sle_uart_send_param->data = (uint8_t *)buffer;
        ssapc_write_req(0, g_sle_uart_conn_id, sle_uart_send_param);            //将UART中断回调接收到的数据发送给服务端
    }
    
    static void *sle_uart_client_task(const char *arg)
    {
        unused(arg);
        /* UART pinmux. */
        uart_init_pin();
    
        /* UART init config. */
        uart_init_config();
    
        uapi_uart_unregister_rx_callback(CONFIG_SLE_UART_BUS);
        errcode_t ret = uapi_uart_register_rx_callback(CONFIG_SLE_UART_BUS,             //注册UART中断回调
                                                    UART_RX_CONDITION_FULL_OR_SUFFICIENT_DATA_OR_IDLE,
                                                    1, sle_uart_client_read_int_handler);
        sle_uart_client_init(sle_uart_notification_cb, sle_uart_indication_cb);         //初始化客户端并注册接收中断回调
        
        if (ret != ERRCODE_SUCC) {
            osal_printk("Register uart callback fail.");
            return NULL;
        }
    
        return NULL;
    }
    
  5. 编译烧录固件

    参考环境搭建教程编译烧录代码

四、测试

烧录固件后先启动server设备,再启动client设备,在串口工具的输入框中输入数据并发送,测试server和client设备之间的数据收发,如下图所示。

Alt text