This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nrf52840 NUS and UART

Hi,

Iam working on a nrf52840 and trying to achieve  data flow from BLE device using NUS and transfer this data through UART.Uart is configured as (TX:P1.01,RX:P1.02).Iam receiving data in 

 nus_data_handler, and expecting app_uart_put will send data to TX which can be received by UART connected device  RX.I can see that data is looping back to sender.Iam using  NUS added 

ble_peripheral\ble_app_template 

static void uart_init(void) {
  uint32_t err_code;
  app_uart_comm_params_t const comm_params =
  {
    // .rx_pin_no    = RX_PIN_NUMBER,
    // .tx_pin_no    = TX_PIN_NUMBER,
    .rx_pin_no = NRF_GPIO_PIN_MAP(1, 1),
    .tx_pin_no = NRF_GPIO_PIN_MAP(1, 2),
    .rts_pin_no = RTS_PIN_NUMBER,
    .cts_pin_no = CTS_PIN_NUMBER,
    .flow_control = APP_UART_FLOW_CONTROL_DISABLED,
    .use_parity = false,
#if defined(UART_PRESENT)
    .baud_rate = NRF_UART_BAUDRATE_115200
#else
    .baud_rate = NRF_UARTE_BAUDRATE_115200
#endif
  };

  APP_UART_FIFO_INIT(&comm_params,
      UART_RX_BUF_SIZE,
      UART_TX_BUF_SIZE,
      uart_event_handle,
      APP_IRQ_PRIORITY_LOWEST,
      err_code);
  APP_ERROR_CHECK(err_code);
}

static void nus_data_handler(ble_nus_evt_t *p_evt) {
  ret_code_t ret_val;
  if (p_evt->type == BLE_NUS_EVT_RX_DATA) {
    uint32_t err_code;
   
    NRF_LOG_INFO("Received data from BLE NUS. Writing data on UART.");
    NRF_LOG_HEXDUMP_DEBUG(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);

    for (uint32_t i = 0; i < p_evt->params.rx_data.length; i++) {
      do {
        err_code = app_uart_put(p_evt->params.rx_data.p_data[i]);

        if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_BUSY)) {
          NRF_LOG_ERROR("Failed receiving NUS message. Error 0x%x. ", err_code);
          APP_ERROR_CHECK(err_code);
        }
    
      } while (err_code == NRF_ERROR_BUSY);
    
    }

    if (p_evt->params.rx_data.p_data[p_evt->params.rx_data.length - 1] == '\r') {
      while (app_uart_put('\n') == NRF_ERROR_BUSY)
        ;
    }
  } else if (p_evt->type == BLE_NUS_EVT_TX_RDY) {
    flag_transmitted = 1;

    NRF_LOG_INFO("TX OK");
  
  } else if (p_evt->type == BLE_NUS_EVT_COMM_STARTED) {

    NRF_LOG_INFO("BLE_NUS_EVT_COMM_STARTED");
    //TODO : add send START command if required
    //   SendData();
    flag_connected = 1;
 
  }
}

Please advice whether and how can I use this.

Thanks

Syam

  • Hello Syam,

    Iam receiving data in 

     nus_data_handler, and expecting app_uart_put will send data to TX which can be received by UART connected device  RX.I can see that data is looping back to sender.

    Please advice whether and how can I use this.

    I am not sure I understand what you are asking me about. 

    From your description above, it seems to me that you are using the Nordic UART central and Nordic UART peripheral example to successfully transfer data between two devices. Furthermore, you have connected the peripheral device in loopback mode, where the UART data received is set to be transmitted back again to the central. From your description, it sounds like the loopback test is also successful, but in that case I am not sure what you are having trouble with.

    Are you having trouble seeing the loopbacked data in your central device again?
    If so, please make sure that you have not opened the virtual com port on the peripheral device - because this will route the UART data to your terminal, rather than to your TX.

    If this is not what you are having trouble with, please elaborate on what you are attempting to do, and how the results differ from your expectations.

    Best regards,
    Karl

  • Iam trying to use  Nordic peripheral example to route the incoming NUS BLE data through hardware UART for which  Iam using pin configuration as explained.

    .rx_pin_no = NRF_GPIO_PIN_MAP(1, 1),
    .tx_pin_no = NRF_GPIO_PIN_MAP(1, 2),

    Iam able to get BLE NUS data ,would like to know the app_uart_put(app_uart_put(p_evt->params.rx_data.p_data[i]); will send the data to Uart connected device or whether it will send it to .tx_pin_no,so that RX of receiver  will get it.Iam not getting the same now. 

  • Hello Syam,

    syamkrishnan said:
    .rx_pin_no = NRF_GPIO_PIN_MAP(1, 1),
    .tx_pin_no = NRF_GPIO_PIN_MAP(1, 2),

    Be advised that according to the Pin Assignment P1.01 and P1.02 are designated as low-frequency(< 10 kHz) pins, to avoid degrading radio performance.

    syamkrishnan said:
    Iam able to get BLE NUS data ,would like to know the app_uart_put(app_uart_put(p_evt->params.rx_data.p_data[i]); will send the data to Uart connected device or whether it will send it to .tx_pin_no,so that RX of receiver  will get it.Iam not getting the same now. 

    The data from app_uart_put will be outputted to the Tx pin, and app_uart_get will read from the Rx pin.
    My previous comment regarding the Virtual port is only applicable if you were using the pins that are connected to the interface MCU(the default configuration).

    Have you made any modifications to the uart peripheral example other than what you have included above?
    I am not sure I understand what the "TODO" and "SendData()" is aimed at, but if you loopback your Tx pin to your Rx pin, then the BLE received data will be sent back to the central, without having to make a separate function for this.

    What do you mean by that last part, "I am not getting the same now"?
    Could you please elaborate more on what behavior you are seeing, and how it is different from what you would have expected?

    Best regards,
    Karl

  • Hello Karl,

    I may not be able to change this configuration,as this is a third party hardware ,and Iam trying to create exact setup using NRF52840 DK and ESP.I will confirm it with the hardware contact regarding the pins.

    "  TODO:" is for ihandshaking to ask sender to send data when connection is established function is as below.Is it  right way to do this?

    void SendData(void)
    {
    ret_code_t err_code ;
    NRF_LOG_INFO("SENDING DATA");
    char buf[6];
    snprintf(buf, sizeof(buf), "START");
    static uint16_t length = sizeof(buf);
    err_code = ble_nus_data_send(&m_nus, buf, &length, m_conn_handle);
    }

    I was not getting the app_uart _put data in the TX of receiver. Imeant "I am not getting the same now" by this.I will do more on this by debugging.

    Thanks,

    Syam

     

  • Hello Syam,

    syamkrishnan said:
    I may not be able to change this configuration,as this is a third party hardware ,and Iam trying to create exact setup using NRF52840 DK and ESP.I will confirm it with the hardware contact regarding the pins.

    The nRF52840 SoC allows you to configure all the pins as you would like - however there might be an issue with what pinsout are available on your third party hardware. Is it a custom module you are working with, or one from a retailer/manufacturer?

    syamkrishnan said:
    I was not getting the app_uart _put data in the TX of receiver. Imeant "I am not getting the same now" by this.I will do more on this by debugging.

     I still do not understand what you are telling me here. Are you saying that when you call app_uart_put, you are not seeing anything being transmitted on the TX pin of the device? If so, could you tell me how you are reading the TX pin - are you using a Logic analyzer, is it connected to another device, or is it connected to a serial COM port terminal?
     

    syamkrishnan said:

    "  TODO:" is for ihandshaking to ask sender to send data when connection is established function is as below.Is it  right way to do this?

    void SendData(void)
    {
    ret_code_t err_code ;
    NRF_LOG_INFO("SENDING DATA");
    char buf[6];
    snprintf(buf, sizeof(buf), "START");
    static uint16_t length = sizeof(buf);
    err_code = ble_nus_data_send(&m_nus, buf, &length, m_conn_handle);
    }

    That depends if you using the Nordic UART central or peripheral example. The central uses the function ble_nus_c_string_send while the peripheral example uses ble_nus_data_send.
    If you are using the peripheral example, your approach seems correct. Are you experiencing any problems or unexpected behavior when doing this?

    Best regards,
    Karl

Related