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

How to send data using gpio (rx, tx pin)?

Hi, all

I'm using nrf52832 pca10040, s132.. 

I want to send and receive data over Tx , Rx pin.. with my custom board, cm1106 model..

   <<<<<< I've connected the two devices directly.

So, what I want to do is..

i want to send data, 0x11 0x01 0x01 0xED to custom device (cm1106)

( If I send those data correctly, custom device will send 'respond data'.. )

1. Is it correct to use 'app_uart_put' to send data?..

2. Even if i do not receive the response data,,  how can I check that those 0x11 ... data send well? (using nRF connect?)

3. Am I have to initialize custom device's gpio, too?

( This is uart initialize.. Could you also look at the wrong part? )

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,
        .rts_pin_no   = RTS_PIN_NUMBER,
        .cts_pin_no   = CTS_PIN_NUMBER,
        .flow_control = APP_UART_FLOW_CONTROL_DISABLED,
        .use_parity   = false,
        .baud_rate    = NRF_UART_BAUDRATE_9600
    };
    nrf_gpio_cfg_input(RX_PIN_NUMBER, NRF_GPIO_PIN_NOPULL);
    nrf_gpio_cfg_output(TX_PIN_NUMBER);

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

Any answer can be helpful as the explanation or question itself may have been ambiguous due to lack of foundation. Thank you.

BR, 

lyrics

Parents
  • Hi,

    1. Is it correct to use 'app_uart_put' to send data?..

    Yes this is the function to send data over uart, this function sends one byte at a time so you need to make a for loop for sending all the bytes you mentioned above.

    2. Even if i do not receive the response data,,  how can I check that those 0x11 ... data send well? (using nRF connect?)

    You can use a UART to USB converter, plug it to your PC an use a terminal to open the right Comport to see the sent bytes from the board over UART.

    3. Am I have to initialize custom device's gpio, too?

    You don't need to use custom gpios if you use the code and pins for pca10040

    Hope this was helpful.

    Best Regards

    JK

  • Hi, jawadk

    Thank you for your reply..

    I sent the data through 'app_uart_put'.

    If the data was sent well, shouldn't 'APP_UART_DATA_READY' be called on the code?..

    Why isn't the event happening?

    int main(void)
    {
        bool erase_bonds;
    
        // Initialize.
        uart_init();
    
        send_data();
    
        // Enter main loop.
        while(true)
        {
              //receive_data();
        }
    }

    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,
            .rts_pin_no   = RTS_PIN_NUMBER,
            .cts_pin_no   = CTS_PIN_NUMBER,
            .flow_control = APP_UART_FLOW_CONTROL_DISABLED,
            .use_parity   = false,
            .baud_rate    = NRF_UART_BAUDRATE_9600
        };
    //    nrf_gpio_cfg_input(RX_PIN_NUMBER, NRF_GPIO_PIN_NOPULL);
    //    nrf_gpio_cfg_output(TX_PIN_NUMBER);
    
        APP_UART_FIFO_INIT(&comm_params,
                           UART_RX_BUF_SIZE,
                           UART_TX_BUF_SIZE,
                           uart_evt_callback,
                           APP_IRQ_PRIORITY_LOWEST,
                           err_code);
        APP_ERROR_CHECK(err_code);
    }

    void send_data()
    {
        uint32_t    err_code;
        uint8_t     send_data[UART_TX_BUF_SIZE];
        uint32_t    length = 4;
    
        send_data[0] = 0x11;
        send_data[1] = 0x01;
        send_data[2] = 0x01;
        send_data[3] = 0xed;
    
        for(uint32_t i = 0; i < length; i++)
        {
            {
                err_code = app_uart_put(send_data[i]);
                if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_BUSY))
                {
                    NRF_LOG_ERROR("Failed Error 0x%x. ", err_code);
                    APP_ERROR_CHECK(err_code);
                }
            } while (err_code == NRF_ERROR_BUSY);
        }
    }

    void uart_evt_callback(app_uart_evt_t * uart_evt) 
    {
        uint8_t   byte;
        uint32_t  err_code;
        switch (uart_evt->evt_type)
        {
            case APP_UART_DATA_READY:
                printf("b\r\n"); 
                err_code = app_uart_get(&byte);
                if(err_code == NRF_SUCCESS)
                {
                    printf("success..\r\n");
                }
            break;
            
            ...
            
        }
    }

  • Hi Lyics,

    you will got the "APP_UART_DATA_READY" event when you receive data over UART and not when you finish sending data, instead you will got the event "APP_UART_TX_EMPTY", this event indicating that UART has completed transmission of all available data in the TX FIFO, so use it as Nordix example uses the "APP_UART_DATA_READY" event .

    Plus pay attention to what is the returned value you got from the function "app_uart_put" to see if you have any problem regarding the uard driver.

    BR

    JK

Reply
  • Hi Lyics,

    you will got the "APP_UART_DATA_READY" event when you receive data over UART and not when you finish sending data, instead you will got the event "APP_UART_TX_EMPTY", this event indicating that UART has completed transmission of all available data in the TX FIFO, so use it as Nordix example uses the "APP_UART_DATA_READY" event .

    Plus pay attention to what is the returned value you got from the function "app_uart_put" to see if you have any problem regarding the uard driver.

    BR

    JK

Children
  • Hi,

    I got the event "APP_UART_TX_EMPTY" and looked at the flow with breakpoint,,

    Will TX_EMPTY be called when all the values of 0x11 are sent?
    And since app_uart_get is defined in TX_EMPTY, err_code is not SUCCESS, where should I define it?

  • lyrics said:
    Will TX_EMPTY be called when all the values of 0x11 are sent?

    The event will be generated whenever the FIFO is empty. When this is depends on how fast the application puts bytes into the FIFO, and how fast the UART peripheral is able to transmit the bytes.

    lyrics said:
    And since app_uart_get is defined in TX_EMPTY, err_code is not SUCCESS, where should I define it?

    You should call app_uart_get() in the APP_UART_DATA_READY event case, if you want to read the data as soon as it is received by the UART peripheral.

Related