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

2 UART sample code not working as expected

Hello,

We are using the Serial port library with two UARTEs. It defines two instances of the serial port interface: one for receiving data, and one for sending data.
After power-up, the example sends a "Hello nrf_serial!" string to the J-Link serial port. Then, it starts operating in loopback, which means that it sends every
byte that it receives through the first serial instance back to the serial port through the second serial instance.it's woking properly

when we send data from tx pin of  both UART`S UART0 and UART1  to Hterm  then data send successfully and vice varsa,but when we use single UART0 with our Device (meter)
we are not getting response in Rx buffer .if we short TX and RX pin we got data in Rx buffer.

example:


NRF_SERIAL_DRV_UART_CONFIG_DEF(m_uarte0_drv_config,
                      RX_PIN_NUMBER,TX_PIN_NUMBER ,
                      RTS_PIN_NUMBER, CTS_PIN_NUMBER,
                      NRF_UART_HWFC_DISABLED, NRF_UART_PARITY_EXCLUDED,
                      NRF_UART_BAUDRATE_115200,
                      UART_DEFAULT_CONFIG_IRQ_PRIORITY);

NRF_SERIAL_DRV_UART_CONFIG_DEF(m_uarte1_drv_config,
                      ARDUINO_SDA_PIN,ARDUINO_SCL_PIN,
                      RTS_PIN_NUMBER, CTS_PIN_NUMBER,
                      NRF_UART_HWFC_DISABLED, NRF_UART_PARITY_EXCLUDED,
                      NRF_UART_BAUDRATE_115200,
                      UART_DEFAULT_CONFIG_IRQ_PRIORITY);
/*
NRF_SERIAL_DRV_UART_CONFIG_DEF(m_uarte0_drv_config,
                      RX_PIN_NUMBER, ARDUINO_SCL_PIN,
                      RTS_PIN_NUMBER, CTS_PIN_NUMBER,
                      NRF_UART_HWFC_DISABLED, NRF_UART_PARITY_EXCLUDED,
                      NRF_UART_BAUDRATE_115200,
                      UART_DEFAULT_CONFIG_IRQ_PRIORITY);

NRF_SERIAL_DRV_UART_CONFIG_DEF(m_uarte1_drv_config,
                      ARDUINO_SDA_PIN, TX_PIN_NUMBER,
                      RTS_PIN_NUMBER, CTS_PIN_NUMBER,
                      NRF_UART_HWFC_DISABLED, NRF_UART_PARITY_EXCLUDED,
                      NRF_UART_BAUDRATE_115200,
                      UART_DEFAULT_CONFIG_IRQ_PRIORITY);
*/
#define SERIAL_FIFO_TX_SIZE 1024
#define SERIAL_FIFO_RX_SIZE 1024

NRF_SERIAL_QUEUES_DEF(serial0_queues, SERIAL_FIFO_TX_SIZE, SERIAL_FIFO_RX_SIZE);
NRF_SERIAL_QUEUES_DEF(serial1_queues, SERIAL_FIFO_TX_SIZE, SERIAL_FIFO_RX_SIZE);


#define SERIAL_BUFF_TX_SIZE 1
#define SERIAL_BUFF_RX_SIZE 1

NRF_SERIAL_BUFFERS_DEF(serial0_buffs, SERIAL_BUFF_TX_SIZE, SERIAL_BUFF_RX_SIZE);
NRF_SERIAL_BUFFERS_DEF(serial1_buffs, SERIAL_BUFF_TX_SIZE, SERIAL_BUFF_RX_SIZE);


NRF_SERIAL_CONFIG_DEF(serial0_config, NRF_SERIAL_MODE_DMA,
                      &serial0_queues, &serial0_buffs, NULL, sleep_handler);
NRF_SERIAL_CONFIG_DEF(serial1_config, NRF_SERIAL_MODE_DMA,
                      &serial1_queues, &serial1_buffs, NULL, sleep_handler);


NRF_SERIAL_UART_DEF(serial0_uarte, 0);
NRF_SERIAL_UART_DEF(serial1_uarte, 1);


int main(void)
{
    ret_code_t ret;

    ret = nrf_drv_clock_init();
    APP_ERROR_CHECK(ret);
    ret = nrf_drv_power_init(NULL);
    APP_ERROR_CHECK(ret);

    nrf_drv_clock_lfclk_request(NULL);
    ret = app_timer_init();
    APP_ERROR_CHECK(ret);

    // Initialize LEDs and buttons.
    bsp_board_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS);

    ret = nrf_serial_init(&serial0_uarte, &m_uarte0_drv_config, &serial0_config);
    APP_ERROR_CHECK(ret);

    ret = nrf_serial_init(&serial1_uarte, &m_uarte1_drv_config, &serial1_config);
    APP_ERROR_CHECK(ret);

    uint8_t tx_message[34] ={0x7e, 0xa0, 0x20, 0x03, 0x21, 0x93, 0x7d, 0xd9, 0x81, 0x80, 0x14, 0x05, 0x02, 0x02, 0x00, 0x06, 0x02, 0x02, 0x00, 0x07, 0x04, 0x00, 0x00, 0x00, 0x01, 0x08, 0x04, 0x00, 0x00, 0x00, 0x01, 0x6f, 0xef, 0x7e};
     
      unsigned char c[100];                                                     // Rx Buffer   

      ret = nrf_serial_write(&serial0_uarte,
                           tx_message,
                           sizeof(tx_message),
                           NULL,
                           NRF_SERIAL_MAX_TIMEOUT);                             // use only UART0 
    
          (void)nrf_serial_flush(&serial0_uarte, 0);
       
        memset(&c,0,100);
  
        /*ret = nrf_serial_read(&serial0_uarte, &c, 34, NULL, 10000);
 
        (void)nrf_serial_flush(&serial0_uarte, 0);
        if( strlen(c))
        {
        (void)nrf_serial_write(&serial0_uarte, &c, 100, NULL, 10000);
        (void)nrf_serial_flush(&serial0_uarte, 0);
        }*/
  
     while (true)
     {
              
        ret = nrf_serial_read(&serial0_uarte, &c, 34, NULL, 10000);
        if (ret != NRF_SUCCESS)
        {
            continue;
        }
        
        if( strlen(c))
        {
        
        (void)nrf_serial_write(&serial0_uarte, &c, 100, NULL, 10000);
        (void)nrf_serial_flush(&serial0_uarte, 0);     
        }
            
     }
          
  }   
   

Please support us

  • Hi

    I'm sorry, but what exactly are you connecting the UART0 TX pin to? Have you tried connecting just the UART1 TX pin to this device to see if that works at all? What voltage is the VDD_GPIO on the nRF and on this other device set to?

    Best regards,

    Simon

  • Hi Simon,

    When we use the single UART example code from the SDK we are able to communicate with our external device. But what we need to do is to read data from the external device and send it to another device over UART. Basically communicate over UART0 to receive data and then retransmit the data over UART1 to another device.

    When we interface with the single UART example the communicate with the external device is fine but when we try to communicate with the same device using the serial communication APIs from the two UART example code we are only able to send the data but not receive anything back.

    We believe this is because the single UART example code works on an interrupt basis while the two UART example code doesn't.

    Is there something you can do to help us here?

    The hardware has been checked and it works thoroughly well. We did this by running the example code.

    Neel.

  • Hi Neel

    Thank you for specifying. Have you checked out our serialization setup guide here.

    Best regards,

    Simon

  • Hi Simon,

    When we run the example UART code it is working fine so we know how to use the serial communication. The example code UART code uses interrupts and it is working fine for us.

    Compared to that the example Serial UART code is the code with 2 UARTs. The example Serial UART code doesn't work for us since the code doesn't use interrupts.

    Is there an example code with two UARTs (similar to the Serial UART code) that uses interrupts?

    Please advice.

    Neel.

  • Hi Neel

    No, we don't have a serial example using interrupts I'm afraid. I think it should be possible to tweak it to be interrupt based, but we don't have any resources on how to do this I'm afraid.

    Best regards,

    Simon

Related