This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to use BLE UART and normal UART

Hello,

In my project i am using nrf52840 with custom board, here i am using BLE UART to send the data from the mobile to device and sending data from another sensor board device to controller board over RS232 UART communication but i can able to send the data from the mobile to device over BLE UART but i couldn't receive the data sensor board to controller board this is the hurdle i am facing i have used the app_uart_get function also but it didn't work for me, In uart event handler also i have checked there is no any data received from the sensor board. 

1. First thing how can i configure the TX,RX pins for example if i have used TX,RX pins as 6,8 then BLE UART is working fine but if i have changed it to custom board pins(RS232) 3,4 then its not work so please let me know how to configure the pins.??

2. is it possible to use two UARTs.??

thank you. 

void uart_event_handle(app_uart_evt_t * p_event)
{
    static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
    static uint8_t index = 0;
    uint32_t       err_code;
    NRF_LOG_INFO("I AM IN UART_EVENT_HANDLER");
    NRF_LOG_FLUSH();
    NRF_LOG_INFO("data_array:%d",data_array);
    NRF_LOG_FLUSH();
    switch (p_event->evt_type)
    {
        case APP_UART_DATA_READY:
            UNUSED_VARIABLE(app_uart_get(&data_array[index]));
            NRF_LOG_INFO("s:%d",data_array);
            NRF_LOG_FLUSH();
            index++;

            if ((data_array[index - 1] == '\n') ||
                (data_array[index - 1] == '\r') ||
                (index >= m_ble_nus_max_data_len))
            {
                if (index > 1)
                {
                    NRF_LOG_DEBUG("Ready to send data over BLE NUS");
                    NRF_LOG_HEXDUMP_DEBUG(data_array, index);

                    do
                    {
                        uint16_t length = (uint16_t)index;
                        err_code = ble_nus_data_send(&m_nus, data_array, &length, m_conn_handle);
                        if ((err_code != NRF_ERROR_INVALID_STATE) &&
                            (err_code != NRF_ERROR_RESOURCES) &&
                            (err_code != NRF_ERROR_NOT_FOUND))
                        {
                            APP_ERROR_CHECK(err_code);
                        }
                    } while (err_code == NRF_ERROR_RESOURCES);
                }

                index = 0;
            }
            break;

        case APP_UART_COMMUNICATION_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_communication);
            NRF_LOG_INFO("Communication error");
             NRF_LOG_FLUSH();
            break;

        case APP_UART_FIFO_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_code);
            NRF_LOG_INFO("FIFO error");
             NRF_LOG_FLUSH();
            break;

        default:
            NRF_LOG_INFO(" defualt break");
            break;
    }

   
    data_init(data_array);
 //   memset(data_array,0,20);
}

Parents
  • Hi,

    1. First thing how can i configure the TX,RX pins for example if i have used TX,RX pins as 6,8 then BLE UART is working fine but if i have changed it to custom board pins(RS232) 3,4 then its not work so please let me know how to configure the pins.??

    The pins are set in the app_uart_comm_params_t struct, passed to APP_UART_FIFO_INIT() macro during initialization. What is not working? Have you checked if any data is input/ouput on the pins using a logic analyzer?

    2. is it possible to use two UARTs.??

    nRF52840 have two UARTE instances, but only one can be used with app_uart library. To utilize both UARTE instances, you need to use the UART driver or libUARTE for one or both instances. See for instance this example.

    Best regards,
    Jørgen

  • The "UART" from the mobile app to the device over BLE does not use any HW UART instances, this is a purely virtual UART service that can send ASCII strings over BLE. The ble_app_uart example also uses a HW UART instance to allow you to use a serial terminal on a PC to send/receive the strings to/from the board, but this UART instance can be repurposed to communicate with your sensor, if you do not have a requirement to send/receive strings from a PC serial terminal.

    Data received over the HW UART instance is handled in uart_event_handle() in ble_app_uart example, and the default behavior is to pass the received string to ble_nus_data_send(), to send it over the BLE UART service. Data received over BLE UART service is handled in nus_data_handler(), and the default behavior is to pass the received data to the HW UART instance by calling app_uart_put(). If you remove these two function calls, you can use the HW UART instance for handling data to/from your sensor.

    If you require high throughput on the HW UART instance to/from your sensor, I would highly recommend you to use libUARTE instead of app_uart. It supports sending larger buffers using EasyDMA, while app_uart only sends/receivec a single byte at a time before the CPU needs to change the buffer. libUARTE also supports timeouts for RX, to alert the application if a string is received, without having to wait for the full RX buffer to be filled.

Reply
  • The "UART" from the mobile app to the device over BLE does not use any HW UART instances, this is a purely virtual UART service that can send ASCII strings over BLE. The ble_app_uart example also uses a HW UART instance to allow you to use a serial terminal on a PC to send/receive the strings to/from the board, but this UART instance can be repurposed to communicate with your sensor, if you do not have a requirement to send/receive strings from a PC serial terminal.

    Data received over the HW UART instance is handled in uart_event_handle() in ble_app_uart example, and the default behavior is to pass the received string to ble_nus_data_send(), to send it over the BLE UART service. Data received over BLE UART service is handled in nus_data_handler(), and the default behavior is to pass the received data to the HW UART instance by calling app_uart_put(). If you remove these two function calls, you can use the HW UART instance for handling data to/from your sensor.

    If you require high throughput on the HW UART instance to/from your sensor, I would highly recommend you to use libUARTE instead of app_uart. It supports sending larger buffers using EasyDMA, while app_uart only sends/receivec a single byte at a time before the CPU needs to change the buffer. libUARTE also supports timeouts for RX, to alert the application if a string is received, without having to wait for the full RX buffer to be filled.

Children
  • Hello, 

    thanks for your reply,

    this is a purely virtual UART service that can send ASCII strings over BLE. The ble_app_uart example also uses a HW UART instance to allow you to use a serial terminal on a PC to send/receive the strings to/from the board,

    Yes, i knew this concept but the thing is i want to send/ receive the data over BLE  so for this app_ble_uart example has been used just to send the command data to device to control the motor and at the same time the sensor data should be continuously send to device over HW UART so that's why i didn't change the uart_init() function only the TX,RX pins gets changed in my case as P1.3(TX), P1.4(RX) so then when i send command data to device it should receive over BLE and sensor data should be received over UART then why it showing me uart_communication_error .??

    so if its possible to do with single UART by  changing tx,rx pin numbers for HW UART then BLE should work separately as well as HW UART should work separately then why its giving me this error i don't know, 

     while debugging i got this

    if BLE data and HW UART data received with the same same RX pin if i am not wrong is this a problem.

    please let me know your suggestion to implement this.

    thank you. 

  • If you build the application in "debug" configuration (or add the DEBUG/DEBUG_NRF symbols to the projects preprocessor symbols), the log should output the error code and the location where the error occurred on the long, instead of just "Fatal error".

    My guess is that the application pass the error source from the communication error to the error handler, and the GPIO you set as RX pin is in the wrong state when you enable the UART RX, generating a FRAMING/BREAK error:

    case APP_UART_COMMUNICATION_ERROR:
        APP_ERROR_HANDLER(p_event->data.error_communication);
        break;

    If you have this APP_ERROR_CHECK in your application, please try to comment this out and see if it resolves the fatal error.

  • Hello,

    Jorgen thanks for your reply,

    As you said i have commented the APP_ERROR_HANDLER and now no error  i can able to receive the data from the MAX232 to MCU over  HW_UART and BLE is connected now i need to send the data over BLE but this works via Virtual UART right. here i observed something like when i send a command data from the MOBILE APP to MCU  to control the motor and its working fine but at the same time if i connect the TX,RX pins of HW_UART physically data send from the sensor board to MCU via MAX232 but there is unwanted noise from the motor while receiving the data from the MAX232 to MCU i mean in the sense motor stops for nano sec and after receiving the data it continuous for motor running this is happening fraction of sec so i can able to here that sound.

    so how can i reduce this and why its happening while receiving the data  from the MAX232 to MCU.??

    Please let me know your feedback .

    thank you.

  • How is the motor powered and controlled? Do you have a scope image of the control signal when you hear the noise?

    If you can post your schematics and layout files, our HW team may be able to review it and see if there is any issues.

Related