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

how to write a functions(program) for transmitting and receiving data over uart using nrf 51822?

Hi, there i am using nrf51822 to communicate with quectel ec25 .i want to send AT commands over uart and receive some response from quectel.for eg. i am sending a "AT" command i should get "ok" response in return.i am referring nrf5 sdk10.0.0 uart example and in that example there is loop back test and i have disabled that but i didnt understand the code flow and the functions that are used in this example.what are the changes i have to make in my code to transmit and receive data over uart .This is the example:can somebody explain me which part of code is sending and receiving data.please urgent help needed.and i dont want to use ble _uart example just want to use a simple uart communication.

#include <stdbool.h> #include <stdint.h> #include <stdio.h> #include "app_uart.h" #include "app_error.h" #include "nrf_delay.h" #include "nrf.h" #include "bsp.h"

//#define ENABLE_LOOPBACK_TEST /**< if defined, then this example will be a loopback test, which means that TX should be connected to RX to get data loopback. */

#define MAX_TEST_DATA_BYTES (15U) /< max number of test bytes to be used for tx and rx. */ #define UART_TX_BUF_SIZE 256 /< UART TX buffer size. */ #define UART_RX_BUF_SIZE 1 /**< UART RX buffer size. */

void uart_error_handle(app_uart_evt_t * p_event) { if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR) { APP_ERROR_HANDLER(p_event->data.error_communication); } else if (p_event->evt_type == APP_UART_FIFO_ERROR) { APP_ERROR_HANDLER(p_event->data.error_code); } }

//#ifdef ENABLE_LOOPBACK_TEST /** @brief Function for setting the @ref ERROR_PIN high, and then enter an infinite loop. */ static void show_error(void) {

LEDS_ON(LEDS_MASK);
while(true)
{
    // Do nothing.
}

}

/** @brief Function for testing UART loop back.

  • @details Transmitts one character at a time to check if the data received from the loopback is same as the transmitted data.

  • @note @ref TX_PIN_NUMBER must be connected to @ref RX_PIN_NUMBER) */ /*static void uart_loopback_test() { uint8_t * tx_data = (uint8_t *)("\n\rLOOPBACK_TEST\n\r"); uint8_t rx_data;

    // Start sending one byte and see if you get the same for (uint32_t i = 0; i < MAX_TEST_DATA_BYTES; i++) { uint32_t err_code; while(app_uart_put(tx_data[i]) != NRF_SUCCESS);

    nrf_delay_ms(10);
    err_code = app_uart_get(&rx_data);
    
    if ((rx_data != tx_data[i]) || (err_code != NRF_SUCCESS))
    {
        show_error();
    }
    

    } return; }

#endif*/

/**

  • @brief Function for main application entry. */ int main(void) { LEDS_CONFIGURE(LEDS_MASK); LEDS_OFF(LEDS_MASK); uint32_t err_code; const app_uart_comm_params_t comm_params = { RX_PIN_NUMBER, TX_PIN_NUMBER, RTS_PIN_NUMBER, CTS_PIN_NUMBER, APP_UART_FLOW_CONTROL_ENABLED, false, UART_BAUDRATE_BAUDRATE_Baud38400 };

    APP_UART_FIFO_INIT(&comm_params, UART_RX_BUF_SIZE, UART_TX_BUF_SIZE, uart_error_handle, APP_IRQ_PRIORITY_LOW, err_code);

    APP_ERROR_CHECK(err_code);

//#ifndef ENABLE_LOOPBACK_TEST printf("\n\rStart: \n\r");

while (true)
{
    uint8_t cr;
    while(app_uart_get(&cr) != NRF_SUCCESS);
    while(app_uart_put(cr) != NRF_SUCCESS);

    if (cr == 'q' || cr == 'Q')
    {
        printf(" \n\rExit!\n\r");

        while (true)
        {
            // Do nothing.
        }
    }
}

//#else

// This part of the example is just for testing the loopback .

// while (true) // { // uart_loopback_test(); // } //#endif*/ }

/** @} */ great help appriciated!!

  • Look into the ble uart bridge example (~\nRF5_SDK_11.0.0_89a8197\examples\ble_peripheral\ble_app_uart\main.c) and look for the function uart_event_handle() for the uart receive example. As for sending it depends on how you structure your application to invoke the app_uart_put(). In my application I have the nRF52832 talking to the Telit GE866 module to send and receive data from a cloud server through the GSM network; I also have a QuecTel EVK but have not got the chance to try it yet!

    I strongly suggest you code some kind of fsm to handle the AT command sequence to the QuecTel module as that involves registration, setting up the connection and requesting a socket, all of which can fail and thus the same AT commands must be resent again, in which case a fsm definitely helps to make the code/logic clear.

  • Hey,Thanks matt can you please elaborate what is fsm and how to write a fsm to send AT commands using nrf 51822?if you share the fsm algorithm code for uart it will be great help.can you please tell me the working of a sending and receiving functions in uart example?how can i write a function to send AT commands?how to check receiving data?

  • Finite State Machine (fsm). You really need to look at the example code like ~\nRF5_SDK_11.0.0_89a8197\examples\ble_peripheral\ble_app_uart\main.c to see how to send and receive data with the UART. I don't have a "simple" example to show you right away :(

  • hey matt Thanks,i have gone through the ble_uart example and it worked well.but i have problem while sending AT commands to quectel ec25 .you had mentioned about finite state machine.i dont have any clue about writing fsm algorithm for sending AT commands to EC25 Board.could you send me the fsm code for sendinfg at command .i want just sample fsm code for sending data over uart.

  • But can you manually send AT commands to the ec25 right now? I mean using something like a putty program to manually send AT commands and get the response from the ec25. If that works, then connect the nrf51822 to the ec25, set up the uart and use the app_uart_put() api call.

Related