Architecture of Bluetooth Peripheral Sample

Hello guys,

I'm trying to clearly understand the Bluetooth Peripheral Sample github.com/.../peripheral_uart and how it exactly works.. so i'm curious if there are some software diagrams or architectures that describes this code ? and also i'm curious about applying some unit tests on some functions, so any example for for that ? looking forward for you help !

Thanks in advance !

best regards

  • Hi,

    Please take a look at the Serial Communication UART course on the DevAcademy. There are also other basic samples there, which are explained in detailed.

    Regards,

    Priyanka

  • I'm curious to know more about the k_work_init_delayable(&uart_work, uart_work_handler); in the code of Bluetooth Peripherla UART ( this line ) : https://github.com/nrfconnect/sdk-nrf/blob/main/samples/bluetooth/central_uart/src/main.c#L278 and what its uart_work_handler supposed to do in this sample ?

    Thanks in advance

  • Hi,

    The k_work_init_delayable(&uart_work, uart_work_handler); is a function call in Zephyr  which is used to initialize a delayable work item.
    This function takes two arguments: a pointer to the delayable work item and a pointer to the work handler function. In this case, &uart_work is the delayable work item and uart_work_handler is the work handler function.
    The uart_work_handler function is a callback function that is scheduled to run in the system workqueue context. In the Peripheral UART sample, this function is responsible for handling UART work like, allocating memory for a uart_data_t structure, which is used to store UART data , after that, setting the length of the data to 0. And if the memory allocation fails, it logs a warning message and reschedules the UART work after a delay. It also enables the UART receiver with the allocated buffer.

    Regards,
    Priyanka
  • Thank you so much.

    I have discouvred a weird behaviour of the UART Driver which consists of calling UART_RX_BUF_RELEASED two times ( i think it's because of using uart_rx_buf_rsp(uart, buf->data, sizeof(buf->data)); ), so is it something normal ? as UART_RX_BUF_REQUEST creates a new empty buffer, if my current buffer (filled) is released and the second buffer is provided but is empty directly it calls UART_RX_BUF_RELEASED ? because in the documentation i did not see in any case the UART_RX_BUF_RELEASED  could happen twice. Some logs i took :

    [00:00:42.523,590] <dbg> peripheral_uart: uart_cb: UART_RX_RDY
    [00:00:42.529,663] <dbg> peripheral_uart: uart_cb: UART_RX_BUF_RELEASED //first call
    [00:00:42.529,663] <dbg> peripheral_uart: uart_cb: UART_RX_BUF_RELEASED: buf=0x2000b180 len=2
    [00:00:42.529,693] <dbg> peripheral_uart: uart_cb: Buffer added to FIFO: buf=0x2000b180
    [00:00:42.529,724] <dbg> peripheral_uart: uart_cb: UART_RX_BUF_RELEASED //second call
    [00:00:42.529,724] <dbg> peripheral_uart: uart_cb: UART_RX_BUF_RELEASED: buf=0x2000b198 len=0
    [00:00:42.529,754] <dbg> peripheral_uart: uart_cb: Buffer freed: buf=0x2000b198
    [00:00:42.529,754] <dbg> peripheral_uart: uart_cb: UART_RX_DISABLED
    [00:00:42.529,785] <dbg> peripheral_uart: uart_cb: UART_RX_BUF_REQUEST
    [00:00:42.529,815] <dbg> peripheral_uart: uart_cb: Buffer response with new buffer: buf=0x2000b198

  • This is an expected behaviour, and is part of the UART asynchronous API's functionality. When you call uart_rx_buf_rsp(uart, buf->data, sizeof(buf->data));, you're providing a second buffer for the UART driver to use, once the first buffer is filled.
    The UART_RX_BUF_RELEASED event is generated when the current buffer is no longer used by the driver. This event can occur multiple times for the same buffer, depending on the implementation. The buffer may be released when it is completely or partially filled.
    In your logs, the first UART_RX_BUF_RELEASED event is triggered when the first buffer (buf=0x2000b180) is filled and no longer used by the driver. The second UART_RX_BUF_RELEASED event is triggered when the second buffer (buf=0x2000b198) is no longer used by the driver. The second buffer is empty (len=0), which is why it's released immediately. Hope this clears your doubt.
    Regards,
    Priyanka
Related