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 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. 

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

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

Related