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

nrf52832 UART send multiple bytes

Hi all,

    I am trying to send a command to the sensor so that the sensor will response with sending something back for acknowledgement, via UART.

    Let say, I want to send a command "SEND" to sensor, and the sensor will send its data back. However, I don't know how to modify the example code in 

    nRF5_SDK_14.2.0_17b948a\examples\peripheral\uart

    so that the code are fetching the FIFO when the '\n' is met, instead of response in every character. Right now the example is doing this for single character.

    I am not sure how to change from the example to achieve, it seems rx/tx are having independent fifo buffer, how they operate and connected?

    m_rx_fifo, m_tx_fifo

    tx_buffer[], rx_buffer[]

    nrf_drv_uart_rx(), nrf_drv_uart_tx()

    app_uart_get(), app_uart_put()

Please share your thoughts if you have any idea. Thanks Slight smile

Parents
  • Hi, 

    I would use app_uart_get() whenever a APP_UART_DATA_READY event arrives and check if it's a '\n' character. When that's the case, just loop app_uart_put with all your response bytes.

    If I understand correctly, you want to do something like this:

    #include "string.h"  ///< strcmp()
    ...
    uint8_t cr;
    char cmd[MAX_CMD_LEN];
    uint8_t cmdIndex = 0;
    ...
    void sendAction()
    {
        uint8_t i;
        for(i = 0; i < SENSOR_VALUE_LEN; i++)
        {
            while (app_uart_put(sensorValue[i]) != NRF_SUCCESS);
        }
    }
    ...
    void uart_handle(app_uart_evt_t * p_event)
    {
        ...
        else if (p_event->evt_type == APP_UART_DATA_READY)
        {
            
            while (app_uart_get(&cr) != NRF_SUCCESS);
            cmd[cmdIndex++] =  cr;
            cmd[cmdIndex] = 0;
     
            if(cr == '\n')
            {
                printf("RX end! Command is : %s\r\n", cmd);
                if(strcmp(cmd, "SEND\n") == 0)
                {
                    sendAction();
                }
                else if(strcmp(cmd, "STATUS\n") == 0)
                {
                    
                }
                cmdIndex = 0;
            }
        }
    }
    ...
    

    Hope this helps.

  • Thanks for the reply. I want to clarify about the usage of app_uart_put/app_uart_get. From the app_uart.h, the app_uart_put() is described to put a byte on the UART on TX buffer for transmission, I guess it means send data/command from nrf52 to sensor in my case.

    May I ask what is the difference between the uart handler from app_uart_evt_t

    void uart_handle(app_uart_evt_t * p_event)
    
    {
    ...
    else if (p_event->evt_type == APP_UART_DATA_READY)
    {...

    and nrf_drv_uart_event_t from app_uart_fifo.c

    static void uart_event_handler(nrf_drv_uart_event_t * p_event, void* p_context)
    {
        app_uart_evt_t app_uart_event;
        uint32_t err_code;
    
        switch (p_event->type)
        {
            case NRF_DRV_UART_EVT_RX_DONE:
            ...

  • Hello,

    The uart_event_handler in app_uart_fifo.c basically passes the event to your application handler you provided in the APP_UART_FIFO_INIT. 

    Can you be more specific with the data you supposed to send/receive to/from the sensor?

  • To make it simple, let say I just want to send command "SEND" to sensor, no matter from PC PuTTY or hard code in nrf MCU. When sensor receive any signal "SEND", it will response back with its temperature value which is stored in 2 bytes.

    I think I shouldn't worry about anything in app_uart_fifo.c, only modify the application handler provided in APP_UART_FIFO_INIT. But I am not sure if I need to change the FIFO buffer size for tx/rx in my case.

  • No you shouldn't worry about it.

    You can modify my first exemple to transmit "SEND" in sendAction(), and modify the handler to process the 2 bytes response with a counter or somthing like that. I think you'll be fine with the default fifo buffer size.

    const char * send_cmd = "SEND";
    char rsp[2];
    uint8_t cmdIndex = 0;
    ...
    void sendAction()
    {
        uint8_t i;
        for(i = 0; i < sizeof(send_cmd); i++)
        {
            while (app_uart_put(send_cmd[i]) != NRF_SUCCESS);
        }
    }
    void uart_handle(app_uart_evt_t * p_event)
    {
        ...
        else if (p_event->evt_type == APP_UART_DATA_READY)
        {
            
            while (app_uart_get(&cr) != NRF_SUCCESS);
            rsp[cmdIndex++] =  cr;
     
            if(cmdIndex == 2)
            {
                printf("Sensor Value : 0x%02X%02x\r\n", rsp[0],rsp[1]);
                
                cmdIndex = 0;
            }
        }
    }

  • May I ask why I can get correct response from sensor when I input command "SEND" on PuTTY but no response by using sendAction()?

    If I connect UART to nothing, the command is correctly shown on PuTTY. It means the sendAction() has no problem.

    int main(void) {
        ...
        while (true)
        {
        	sendAction();
    		nrf_delay_ms(500);
    		app_uart_flush();
    	}
    }

    But when I connect the uart serial communication betweem nrf and sensor, there is no response unless I input command by keyboard, and then the console shows temperature value.

  • What sensor are you using? Have you tried with 

    const char * send_cmd = "SEND\n";

    Also check you have the right UART configuration (baudrate, flow control, parity). The best would be to use a logic analyser to see what happens.

Reply Children
No Data
Related