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

Parents
  • Hi

    If you only need to use one device at a time you could keep switching the pin selection in the UART around in software by uninitializing and initializing the peripheral with different TX/RX pins. For example, have a UART_INIT1 and a UART_INIT2 function that uses different TX/RX pins and, when uninitialized, calls to initialize the other function. This won't help if you need access to all the devices constantly though. In that case you can try bitbanging the UART; but that's not something we would recommend.

    Best regards,

    Simon

Reply
  • Hi

    If you only need to use one device at a time you could keep switching the pin selection in the UART around in software by uninitializing and initializing the peripheral with different TX/RX pins. For example, have a UART_INIT1 and a UART_INIT2 function that uses different TX/RX pins and, when uninitialized, calls to initialize the other function. This won't help if you need access to all the devices constantly though. In that case you can try bitbanging the UART; but that's not something we would recommend.

    Best regards,

    Simon

Children
No Data
Related