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

Parents
  • 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

Reply
  • 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

Children
No Data
Related