This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[参考译文] CC2340R2:如果 UART 读取缓冲区退出、则 UART 读取回调不会再次发生

Guru**** 2350610 points
Other Parts Discussed in Thread: CC2340R2
请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/wireless-connectivity/bluetooth-group/bluetooth/f/bluetooth-forum/1419661/cc2340r2-uart-read-callback-not-occurring-again-if-uart-read-buffer-exits

器件型号:CC2340R2

工具与软件:

您好!

  目前我正在处理示例 basic_ble 外设配置文件项目。

#define UART_MAX_READ_SIZE  248 //MTU exchange value

uint8_t uartReadBuffer[UART_MAX_READ_SIZE];

void UART_Init()
{

    /* Create a UART in CALLBACK read mode */
    UART2_Params_init(&uartParams);
    uartParams.readMode = UART2_Mode_CALLBACK;
    uartParams.readCallback = UARTCallback;
    uartParams.readReturnMode = UART2_ReadReturnMode_PARTIAL;
    uartParams.baudRate = 115200;
    uartParams.eventCallback = UARTEventCallback;
    uartParams.eventMask = UART2_EVENT_OVERRUN;
    uart = UART2_open(CONFIG_UART2_0, &uartParams);

    if (uart == NULL)
    {
        /* UART2_open() failed */
        while (1)
        {
        }
    }
    // Setup an initial read
     UART2_read(uart, &uartReadBuffer, UART_MAX_READ_SIZE, 0);

}

void UARTCallback(UART2_Handle handle, void *buffer, size_t count, void *userArg, int_fast16_t status)
{
    uartPacketSize = count;
    BLEAppUtil_invokeFunction(HandlingFunction,buffer);
}
typedef struct 
{
  uint8_t pkt_len;
  uint8_t *data;
}api_data;
void HandlingFunction(char *pData)
{
        api_data *api_pkt_ptr = (api_data*)uartReadBuffer;
        uint16_t datalen = uartPacketSize;
		
   SimpleGattProfile_setParameter(SIMPLEGATTPROFILE_CHAR4, RemainDataLen,&uartReadBuffer[datalen]);
		
	memset(&uartReadBuffer[0], 0, sizeof(UART_MAX_READ_SIZE));
	UART2_read(uart, uartReadBuffer, UART_MAX_READ_SIZE, 0);
}

在从模块向移动设备发送248字节时、在移动设备中以单个数据块接收248 (MTU 交换值)。 如果我从 模块向移动设备发送250字节、则 在移动设备中以 单个块248 (MTU 交换值)接收剩余2个字节的数据将 不会接收。 这2个字节将在下一次数据包发送时接收。 其中 这2个字节没有发生 UART 读取回调。

 不发生多达16字节的 UART 读取回调。 如果进行的回调字节多于16字节。

SDK 版本:simplelink_lowpower_f3_sdk_8_10_01_02 (外设)

CCS 版本:CCS 12.7.1

谢谢!

Vignesh。

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    尊敬的 Vignesh:

    我们在此处有一个 uart2callback 示例: https://dev.ti.com/tirex/explore/node?node=A__AKdlpPkvwtCAe6soBzKbpQ__com.ti.SIMPLELINK_LOWPOWER_F3_SDK__58mgN04__LATEST

    对于这部分、我刚刚对其进行了修改、使输入为248字节(UART 读取缓冲区)。

    如果接收到的数据少于16个字节、我仍然可以看到正在调用回调(具体来说、它获取键盘输入、因此正在注册任何1键按压操作并将其打印到串行终端)。

    /*
     * Copyright (c) 2020, Texas Instruments Incorporated
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * *  Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * *  Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     *
     * *  Neither the name of Texas Instruments Incorporated nor the names of
     *    its contributors may be used to endorse or promote products derived
     *    from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     */
    
    /*
     *  ======== uart2callback.c ========
     */
    #include <stdint.h>
    #include <stddef.h>
    
    /* POSIX Header files */
    #include <semaphore.h>
    
    /* Driver Header files */
    #include <ti/drivers/GPIO.h>
    #include <ti/drivers/UART2.h>
    
    /* Driver configuration */
    #include "ti_drivers_config.h"
    
    static sem_t sem;
    static volatile size_t numBytesRead;
    
    /*
     *  ======== callbackFxn ========
     */
    void callbackFxn(UART2_Handle handle, void *buffer, size_t count, void *userArg, int_fast16_t status)
    {
        if (status != UART2_STATUS_SUCCESS)
        {
            /* RX error occured in UART2_read() */
            while (1) {}
        }
    
        numBytesRead = count;
        sem_post(&sem);
    }
    
    /*
     *  ======== mainThread ========
     */
    void *mainThread(void *arg0)
    {
        char input[248];
        const char echoPrompt[] = "Echoing characters:\r\n";
        UART2_Handle uart;
        UART2_Params uartParams;
        int32_t semStatus;
        uint32_t status = UART2_STATUS_SUCCESS;
    
        /* Call driver init functions */
        GPIO_init();
    
        /* Configure the LED pin */
        GPIO_setConfig(CONFIG_GPIO_LED_0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
    
        /* Create semaphore */
        semStatus = sem_init(&sem, 0, 0);
    
        if (semStatus != 0)
        {
            /* Error creating semaphore */
            while (1) {}
        }
    
        /* Create a UART in CALLBACK read mode */
        UART2_Params_init(&uartParams);
        uartParams.readMode     = UART2_Mode_CALLBACK;
        uartParams.readCallback = callbackFxn;
        uartParams.baudRate     = 115200;
    
        uart = UART2_open(CONFIG_UART2_0, &uartParams);
    
        if (uart == NULL)
        {
            /* UART2_open() failed */
            while (1) {}
        }
    
        /* Turn on user LED to indicate successful initialization */
        GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON);
    
        /* Pass NULL for bytesWritten since it's not used in this example */
        UART2_write(uart, echoPrompt, sizeof(echoPrompt), NULL);
    
        /* Loop forever echoing */
        while (1)
        {
            numBytesRead = 0;
    
            /* Pass NULL for bytesRead since it's not used in this example */
            status = UART2_read(uart, &input, sizeof(input), NULL);
    
            if (status != UART2_STATUS_SUCCESS)
            {
                /* UART2_read() failed */
                while (1) {}
            }
    
            /* Do not write until read callback executes */
            sem_wait(&sem);
    
            if (numBytesRead > 0)
            {
                // print number of bytes no greater than input size
                uint8_t printBytes;
                if (numBytesRead > sizeof(input))
                    printBytes = sizeof(input);
                else
                    printBytes = numBytesRead;
    
                status = UART2_write(uart, &input, numBytesRead, NULL);
    
                if (status != UART2_STATUS_SUCCESS)
                {
                    /* UART2_write() failed */
                    while (1) {}
                }
            }
        }
    }
    

    若要自行尝试、您可以将代码粘贴到 uart2Callback 示例中。

    您可以在没有 BLE 的情况下运行代码吗? 查看在接收16个字节或更少字节时是否正在进行 UART 回调。
    如果问题 仅 在 BLE 运行时发生、那么我们需要咨询 BLE 专家。

    谢谢!
    Toby

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    尊敬的 Toby:

    我在  uart2Callback 示例中尝试了上述代码。 我已经发送了250个字节、并且在 UART 上打印的数据在发送接下来的250个字节时为248个字节、之前的数据包剩余字节在 UART 上打印。  

    在 我发送250 个字节后 、将进行248个字节的 UART 读取、数据将打印在 UART 上、 对于这2个字节、同样必须进行回调。  

    谢谢!

    Vignesh。

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    尊敬的 Vignesh:

    这与 BLE 问题是否相同?

    Unknown 说:
     未发生最多16字节的 UART 读回调。 如果进行的回调超过16字节。

    ^您是否也确认了以下大约16字节?
    例如、尝试发送

    1. 不超过16个字节-->是否发生回调?
    2. 是否超过16字节-->发生回调?

    对于 uart2Callback 实验:

    这与您在 BLE 项目中看到的行为是否相同?

    您能否确认这是否符合您的意思?

    1. 器件:DUT = CC2340R2、UTX = UART 发送器
    2. UTX 向 DUT 发送250个字节  
    3. DUT 接收字节并执行 UART 回调、然后 DUT 输出248字节
    4. 对于 UTX 在步骤2中发送的其余2个未打印的字节(称为 UTX-STEP-2-LAST-2字节)、DUT 没有 UART 回调
    5. UTX 向 DUT 发送接下来的250个字节
    6. DUT 接收字节并执行 UART 回调、然后 DUT 输出 UTX-STEP-2-LAST 2个字节以及第5步产生的246个字节(因此2+246 =总共打印248个字节)。 这意味着 UTX-STEP-5中的最后4个字节未打印输出。

    谢谢!
    Toby

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    尊敬的 Toby:

    [报价用户 id="324727" url="~/support/wireless-connectivity/bluetooth-group/bluetooth/f/bluetooth-forum/1419661/cc2340r2-uart-read-callback-not-occurring-again-if-uart-read-buffer-exits/5440960 #5440960"]这是否与 BLE 存在同样的问题?[/QUOT]

    是的、BLE 具有同样的问题。

    [报价用户 id="324727" url="~/support/wireless-connectivity/bluetooth-group/bluetooth/f/bluetooth-forum/1419661/cc2340r2-uart-read-callback-not-occurring-again-if-uart-read-buffer-exits/5440960 #5440960"]

    例如、尝试发送

    1. 不超过16个字节-->是否发生回调?
    [报价]

     - 在 BLE 代码 248字节,超过5字节- 回调发生。

    超过16字节-->发生回调?

     -是的,正在进行回叫。

    [报价用户 id="324727" url="~/support/wireless-connectivity/bluetooth-group/bluetooth/f/bluetooth-forum/1419661/cc2340r2-uart-read-callback-not-occurring-again-if-uart-read-buffer-exits/5440960 #5440960"]

    对于 uart2Callback 实验:

    这与您在 BLE 项目中看到的行为是否相同?

    您能否确认这是否符合您的意思?

    1. 器件:DUT = CC2340R2、UTX = UART 发送器
    2. UTX 向 DUT 发送250个字节  
    3. DUT 接收字节并执行 UART 回调、然后 DUT 输出248字节
    4. 对于 UTX 在步骤2中发送的其余2个未打印的字节(称为 UTX-STEP-2-LAST-2字节)、DUT 没有 UART 回调
    5. UTX 向 DUT 发送接下来的250个字节
    6. DUT 接收字节并执行 UART 回调、然后 DUT 输出 UTX-STEP-2-LAST 2个字节以及第5步产生的246个字节(因此2+246 =总共打印248个字节)。 这意味着 UTX-STEP-5中的最后4个字节未打印输出。
    [报价]

    正确、上述序列同时在 uart2Callback.c 和 BLE 代码中发生。

    谢谢!

    Vignesh。

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    尊敬的 Vignesh:

    请注意、对于非阻塞模式、有一个 RingBuffer 被使用(请参阅 /Debug/syscfg/ti_drivers_config.c ->uart2RxRingBuffer0 )。
    由于 UTX 发送250个字节而与在 UART2_READ 中请求的248个字节相比、这里可能有剩余字节。

    我认为该问题与发送超出缓冲区所能容纳的字节有关。

    我建议尝试增加 UART 缓冲区的大小、使其至少与预期的最大数据大小一样大。 在你的情况下、它似乎是250、因此将缓冲区大小调整为250 (而不是248)。 请注意、BLE 传输仍应限制为248。

    谢谢!
    Toby