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

SLE串口1对8透传测试

该教程演示如何通过开发板实现SLE串口1对8透传测试,在测试中需要1块开发板做为Client端,另外8块开发板做为Server端,开发板配对后就可以互发消息了。

一、准备工作

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

二、编译 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 WIFI.
        [*]     Enable the Sample of products.
        [ ]         Enable all the sample of product, it's just for build. (NEW)
        [ ]         Support BLE UART sample. (NEW)
        [ ]         Support SLE UART sample. (NEW)
        [*]         Support SLE UART 1 vs 8 sample.
                        SLE UART  1 vs 8  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_1_vs_8\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);
    
        uint8_t *buff = (uint8_t *)buffer;
        char num_str[2] = {buff[0], '\0'};                                  //把第一个字符当作conn_id
        ssapc_write_param_t *sle_uart_send_param = get_g_sle_uart_send_param();
        uint16_t g_sle_uart_conn_id = atoi(num_str);                            
    
        osal_printk("\n sle_uart_client_read_int_handler: %d\r\n", g_sle_uart_conn_id);
        sle_uart_send_param->data_len = length - 1;
        sle_uart_send_param->data = (uint8_t *)buffer+1;                    // 调整数据指针,指向 buffer 的第二个字节
        ssapc_write_req(0, g_sle_uart_conn_id, sle_uart_send_param);
    }
    
    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. 编译烧录固件

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

三、编译 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 WIFI.
        [*]     Enable the Sample of products.
        [ ]         Enable all the sample of product, it's just for build. (NEW)
        [ ]         Support BLE UART sample. (NEW)
        [ ]         Support SLE UART sample. (NEW)
        [*]         Support SLE UART 1 vs 8 sample.
                        SLE UART  1 vs 8  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_1_vs_8\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. 修改服务端的mac地址,打开application\samples\products\sle_uart_1_vs_8\sle_uart_server\sle_uart_server_adv.c,修改第134行的mac地址,这里需要确保8个server端的mac地址都不相同。

    alt text

  6. 编译烧录固件

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

  7. 重复以上步骤,将8个server端都烧录代码,这里一定要注意每个server端的mac地址都要不相同。

四、测试

烧录固件后先启动8个 server设备,再启动client设备,在串口工具的输入框中输入数据并发送,测试server和client设备之间的数据收发,如下图所示。这里需要注意Client端的数据第一个字节表示con_id,例如1666 表示向con_id为1的设备发送666的数据。这里的con_id,是根据server连接上client端的先后顺序自动生成的,编号从0开始。

alt text