central_uart example UART Rx can not receive all data sometimes

Hi,

NCS v2.4.3

nRF52DK (nRF52832)

Based on central_uart example, I extend MTU to 247 and UART_BUF_SIZE to 500,

and I send UART hex data from PC to DK with 61 bytes to test,

0001020304050607080910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758590a

I output log in uart_cb(), case UART_RX_RDY:

	case UART_RX_RDY:
		LOG_DBG("UART_RX_RDY");
		buf = CONTAINER_OF(evt->data.rx.buf, struct uart_data_t, data);
		buf->len += evt->data.rx.len;

		if (disable_req) {
			return;
		}

		if ((evt->data.rx.buf[buf->len - 1] == '\n') ||
		    (evt->data.rx.buf[buf->len - 1] == '\r')) {

			disable_req = true;
			uart_rx_disable(uart);
		}

		else
		{
			LOG_INF("*** len = %d\n", buf->len);

			//Hexdump some data
    		LOG_HEXDUMP_INF(buf->data, buf->len, "UART Rx data"); 
		}
		break;

Sometime RX receive less than 61 bytes, like the following log:

00> [00:00:17.269,805] <inf> central_uart: *** len = 60
00> 
00> [00:00:17.269,836] <inf> central_uart: UART Rx data
00>                                        00 01 02 03 04 05 06 07  08 09 10 11 12 13 14 15 |........ ........
00>                                        16 17 18 19 20 21 22 23  24 25 26 27 28 29 30 31 |.... !"# $%&'()01
00>                                        32 33 34 35 36 37 38 39  40 41 42 43 44 45 46 47 |23456789 @ABCDEFG
00>                                        48 49 50 51 52 53 54 55  56 57 58 59             |HIPQRSTU VWXY    
00> [00:00:18.274,780] <inf> central_uart: *** len = 121
00> 
00> [00:00:18.274,810] <inf> central_uart: UART Rx data
00>                                        00 01 02 03 04 05 06 07  08 09 10 11 12 13 14 15 |........ ........
00>                                        16 17 18 19 20 21 22 23  24 25 26 27 28 29 30 31 |.... !"# $%&'()01
00>                                        32 33 34 35 36 37 38 39  40 41 42 43 44 45 46 47 |23456789 @ABCDEFG
00>                                        48 49 50 51 52 53 54 55  56 57 58 59 0a 00 01 02 |HIPQRSTU VWXY....
00>                                        03 04 05 06 07 08 09 10  11 12 13 14 15 16 17 18 |........ ........
00>                                        19 20 21 22 23 24 25 26  27 28 29 30 31 32 33 34 |. !"#$%& '()01234
00>                                        35 36 37 38 39 40 41 42  43 44 45 46 47 48 49 50 |56789@AB CDEFGHIP
00>                                        51 52 53 54 55 56 57 58  59                      |QRSTUVWX Y       
00> [00:00:19.272,094] <inf> central_uart: *** len = 181
00> 
00> [00:00:19.272,125] <inf> central_uart: UART Rx data
00>                                        00 01 02 03 04 05 06 07  08 09 10 11 12 13 14 15 |........ ........
00>                                        16 17 18 19 20 21 22 23  24 25 26 27 28 29 30 31 |.... !"# $%&'()01
00>                                        32 33 34 35 36 37 38 39  40 41 42 43 44 45 46 47 |23456789 @ABCDEFG
00>                                        48 49 50 51 52 53 54 55  56 57 58 59 0a 00 01 02 |HIPQRSTU VWXY....
00>                                        03 04 05 06 07 08 09 10  11 12 13 14 15 16 17 18 |........ ........
00>                                        19 20 21 22 23 24 25 26  27 28 29 30 31 32 33 34 |. !"#$%& '()01234
00>                                        35 36 37 38 39 40 41 42  43 44 45 46 47 48 49 50 |56789@AB CDEFGHIP
00>                                        51 52 53 54 55 56 57 58  59 0a 00 01 02 03 04 05 |QRSTUVWX Y.......
00>                                        06 07 08 09 10 11 12 13  14 15 16 17 18 19 20 21 |........ ...... !
00>                                        22 23 24 25 26 27 28 29  30 31 32 33 34 35 36 37 |"#$%&'() 01234567
00>                                        38 39 40 41 42 43 44 45  46 47 48 49 50 51 52 53 |89@ABCDE FGHIPQRS
00>                                        54 55 56 57 58                                   |TUVWX            

It receive just 60 bytes and not receive the last byte 0x0a.

How to improve UART Rx unstable condition?

  • Hi,

    Which baudrate are you using? Do you have HW flow control pins enabled?

    Have you tested different serial terminals on the PC to see if that makes any difference?

    Are you testing with the unmodified central_uart sample? If not, what changes have you made?

    Best regards,
    Jørgen

  • Hi,

    1) 115200 8-n-1

    2) use nRF52DK, its VCOM, no HW flow control

    3) another serial terminal still have the same issue

    4) the original unmodified NCS sample limit to 20 bytes, it happened in large data size, need to extend UART buffer and MTU.

    5) I found this thread

     Data Loss With Nordic Uart Service 

    add 

    CONFIG_UART_0_NRF_HW_ASYNC=y

    CONFIG_UART_0_NRF_HW_ASYNC_TIMER=1

    to prj.conf seem fix UART Rx issue.

    But when I send data size to 207 bytes in 1000ms interval, the BLE failed to send data, and cause not run k_free() in 

    ble_data_sent() then UART can not k_malloc() success. 
    How to improve it?
    00> [00:00:01.344,512] <inf> central_uart: Service discovery completed
    00> [00:00:31.065,216] <wrn> central_uart: NUS send timeout
    00> [00:00:31.905,487] <wrn> central_uart: Failed to send data over BLE connection(err -120)
    00> [00:00:33.053,070] <wrn> central_uart: NUS send timeout
    00> [00:00:33.916,748] <wrn> central_uart: Failed to send data over BLE connection(err -120)
    00> [00:00:35.065,765] <wrn> central_uart: NUS send timeout
    00> [00:00:35.915,405] <wrn> central_uart: Failed to send data over BLE connection(err -120)
    00> [00:00:37.058,563] <wrn> central_uart: NUS send timeout
    00> [00:00:37.905,853] <wrn> central_uart: Failed to send data over BLE connection(err -120)
    00> [00:00:39.055,389] <wrn> central_uart: NUS send timeout
    00> [00:00:39.901,641] <wrn> central_uart: Failed to send data over BLE connection(err -120)
    00> [00:00:41.064,086] <wrn> central_uart: NUS send timeout
    00> [00:00:41.913,696] <wrn> central_uart: Failed to send data over BLE connection(err -120)
    00> [00:00:43.063,781] <wrn> central_uart: NUS send timeout
    00> [00:00:43.912,200] <wrn> central_uart: Failed to send data over BLE connection(err -120)
    00> [00:00:45.060,913] <wrn> central_uart: NUS send timeout
    00> [00:00:45.905,578] <wrn> central_uart: Failed to send data over BLE connection(err -120)
    00> [00:00:47.056,152] <wrn> central_uart: NUS send timeout
    00> [00:00:47.901,214] <wrn> central_uart: Failed to send data over BLE connection(err -120)
    00> [00:00:49.066,131] <wrn> central_uart: NUS send timeout
    00> [00:00:49.912,597] <wrn> central_uart: Failed to send data over BLE connection(err -120)
    00> [00:00:51.063,079] <wrn> central_uart: NUS send timeout
    00> [00:00:51.912,811] <wrn> central_uart: Failed to send data over BLE connection(err -120)
    00> [00:00:53.058,593] <wrn> central_uart: NUS send timeout
    00> [00:00:53.905,120] <wrn> central_uart: Failed to send data over BLE connection(err -120)
    00> [00:00:55.053,314] <wrn> central_uart: NUS send timeout
    00> [00:00:55.901,123] <wrn> central_uart: Not able to allocate UART receive buffer
    00> [00:00:55.901,184] <wrn> central_uart: Failed to send data over BLE connection(err -120)
    00> [00:00:56.914,154] <wrn> central_uart: Not able to allocate UART receive buffer
    00> [00:00:57.065,704] <wrn> central_uart: NUS send timeout
    00> [00:00:57.913,330] <wrn> central_uart: Not able to allocate UART receive buffer
    00> [00:00:57.913,391] <wrn> central_uart: Failed to send data over BLE connection(err -120)
    00> [00:00:57.963,470] <wrn> central_uart: Not able to allocate UART receive buffer
    00> [00:00:58.013,580] <wrn> central_uart: Not able to allocate UART receive buffer
    00> [00:00:58.063,720] <wrn> central_uart: Not able to allocate UART receive buffer
    00> [00:00:58.913,940] <wrn> central_uart: Not able to allocate UART receive buffer
    00> [00:00:58.964,080] <wrn> central_uart: Not able to allocate UART receive buffer
    00> [00:00:59.014,190] <wrn> central_uart: Not able to allocate UART receive buffer
    00> [00:00:59.064,300] <wrn> central_uart: Not able to allocate UART receive buffer
    00> [00:00:59.065,124] <wrn> central_uart: NUS send timeout
    00> [00:00:59.114,410] <wrn> central_uart: Not able to allocate UART receive buffer
    00> [00:00:59.164,520] <wrn> central_uart: Not able to allocate UART receive buffer
    00> [00:00:59.214,630] <wrn> central_uart: Not able to allocate UART receive buffer
  • Felix Shih said:
    But when I send data size to 207 bytes in 1000ms interval, the BLE failed to send data,

    Does it work for the first packets, or it never works from the first connection?

    What is the connection interval for the BLE connection? Can you provide a sniffer trace?

    Error -120 means "Operation already in progress".

  • Hi,

    1) I update to NCS v2.6.0, so it need to use send interval less than 800ms to reproduce the issue now. If send with 800ms interval, it send a few packets normally and then show error messages, but if less than 700ms interval, it show the error messages at the first.

    2) the connection interval should be default values, I don't change it.

    #define BT_GAP_INIT_CONN_INT_MIN                0x0018  /* 30 ms    */
    #define BT_GAP_INIT_CONN_INT_MAX                0x0028  /* 50 ms    */
     
    3) I send 207 bytes from central UART to test it, the test hex value string as the following:
    41542b0051c801020304050607080910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989900010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899000a

     the RTT log:

    00> [00:00:00.006,896] <inf> fs_nvs: 6 Sectors of 4096 bytes
    00> [00:00:00.006,927] <inf> fs_nvs: alloc wra: 0, f70
    00> [00:00:00.006,927] <inf> fs_nvs: data wra: 0, 118
    00> [00:00:00.007,110] <inf> bt_sdc_hci_driver: SoftDevice Controller build revision: 
    00>                                             36 f0 e5 0e 87 68 48 fb  02 fd 9f 82 cc 32 e5 7b |6....hH. .....2.{
    00>                                             91 b1 5c ed                                      |..\.             
    00> [00:00:00.010,711] <inf> bt_hci_core: HW Platform: Nordic Semiconductor (0x0002)
    00> [00:00:00.010,742] <inf> bt_hci_core: HW Variant: nRF52x (0x0002)
    00> [00:00:00.010,772] <inf> bt_hci_core: Firmware: Standard Bluetooth controller (0x00) Version 54.58864 Build 1214809870
    00> [00:00:00.011,413] <inf> bt_hci_core: No ID address. App must call settings_load()
    00> [00:00:00.011,413] <inf> central_uart: Bluetooth initialized
    00> [00:00:00.012,329] <inf> bt_hci_core: Identity: C1:69:8A:90:[00:00:13.908,386] <wrn> central_uart: NUS send timeout
    00> [00:00:14.459,625] <wrn> central_uart: Failed to send data over BLE connection(err -120)
    00> [00:00:15.321,746] <wrn> central_uart: NUS send timeout
    00> [00:00:15.862,976] <wrn> central_uart: Failed to send data over BLE connection(err -120)
    00> [00:00:16.725,067] <wrn> central_uart: NUS send timeout
    00> [00:00:17.266,265] <wrn> central_uart: Failed to send data over BLE connection(err -120)
    00> [00:00:18.108,367] <wrn> central_uart: NUS send timeout
    00> [00:00:18.669,586] <wrn> central_uart: Failed to send data over BLE connection(err -120)
    00> [00:00:19.521,697] <wrn> central_uart: NUS send timeout
    00> [00:00:20.062,896] <wrn> central_uart: Failed to send data over BLE connection(err -120)
    00> [00:00:20.904,998] <wrn> central_uart: NUS send timeout
    00> [00:00:21.466,217] <wrn> central_uart: Failed to send data over BLE connection(err -120)
    00> [00:00:22.308,319] <wrn> central_uart: NUS send timeout
    00> [00:00:22.869,537] <wrn> central_uart: Failed to send data over BLE connection(err -120)
    00> [00:00:23.711,639] <wrn> central_uart: NUS send timeout
    00> [00:00:24.252,868] <wrn> central_uart: Failed to send data over BLE connection(err -120)
    00> [00:00:25.114,959] <wrn> central_uart: NUS send timeout
    00> [00:00:25.656,158] <wrn> central_uart: Failed to send data over BLE connection(err -120)

    I am not sure captured the data what you need to watch, the sniffer log is as the follows.

    240326_NUS send timeout.pcapng

  • You might have to increase the data length in order to send the packets correctly. Try adding something like this to your prj.conf:

    CONFIG_BT_DATA_LEN_UPDATE=y
    CONFIG_BT_HCI_ACL_FLOW_CONTROL=y
    CONFIG_BT_L2CAP_TX_MTU=247
    CONFIG_BT_BUF_ACL_RX_SIZE=251
    CONFIG_BT_CTLR=y
    CONFIG_BT_CTLR_RX_BUFFERS=10
    CONFIG_BT_BUF_ACL_TX_COUNT=10
    CONFIG_BT_BUF_ACL_TX_SIZE=251
    CONFIG_BT_CTLR_DATA_LENGTH_MAX=251

Related