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

Receiving data from UART via a peripheral device

Hi

I am working with a Nordic nrf52840dk using segge and sdk 16.

I am using the nordic UART example in examples/peripheral/uart.

The issue I am having is that my data being sent to this device can not be read from the nordic board.

Below is an example of the code going into my Putty terminal from my UART peripheral device.

1,1183,1,1169
1,1183,1,1169
1,1183,1,1169
1,1183,1,1169
1,1183,1,1169
1,1183,1,1169

This device has 4 wire connections RX,TX,CTS and RTS. My connections are listed below:

Note this is the default.

#define RX_PIN_NUMBER  8
#define TX_PIN_NUMBER  6
#define CTS_PIN_NUMBER 7
#define RTS_PIN_NUMBER 5

The first thing i tried was running the code with no change to see if app_uart_get(&cr) would pull this data. Unfortunately it did not. It does however, pull data that i type into the terminal.

What I want to do is to take this data from the UART(Sensor data) and store it as a variable for handling later on.

I which to know how I can pull this data from the serial connection.

Side note when the device (UART) is plugged in printing from putty does not run. If I unplug the device then printing works fine.  Below is my main code (from example).

int main(void)
{
    uint32_t err_code;

    bsp_board_init(BSP_INIT_LEDS);

    const app_uart_comm_params_t comm_params =
      {
          RX_PIN_NUMBER,
          TX_PIN_NUMBER,
          RTS_PIN_NUMBER,
          CTS_PIN_NUMBER,
          UART_HWFC,
          false,
#if defined (UARTE_PRESENT)
          NRF_UARTE_BAUDRATE_115200
#else
          NRF_UARTE_BAUDRATE_115200
#endif
/*
#if defined (UART_PRESENT)
          NRF_UART_BAUDRATE_115200
#else
          NRF_UARTE_BAUDRATE_115200
#endif
*/
      };

    APP_UART_FIFO_INIT(&comm_params,
                         UART_RX_BUF_SIZE,
                         UART_TX_BUF_SIZE,
                         uart_error_handle,
                         APP_IRQ_PRIORITY_HIGH,
                         //APP_IRQ_PRIORITY_LOWEST,
                         err_code);

    APP_ERROR_CHECK(err_code);

#ifndef ENABLE_LOOPBACK_TEST
    printf("\r\nUART example started.\r\n");

    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(" \r\nExit!\r\n");

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

    // This part of the example is just for testing the loopback .
    while (true)
    {
        uart_loopback_test();
    }
#endif
}

Parents
  • Hi,

    It is not entirely clear to me, but the serial terminal on the PC is via the onboard debugger, right (USB connector on the left of the board)? If you are using that (or have the debugger enabled in general so that IF OFF is not asserted, then you should use different pins for the external UART device. In other words, connect the UART pins between the nRF and another device to some available GPIO pins, and update the pin number defines.

  • Thanks for your response, I am using the onboard usb connector (the one in the top-middle of the board). This is where my putty serial terminal connects to. Jlink CDC UART PORT(COM5)

    I have tried redefining the pinouts to:

    #define RX_PIN_NUMBER  NRF_GPIO_PIN_MAP(0,3)//8
    #define TX_PIN_NUMBER  NRF_GPIO_PIN_MAP(0,28)//6
    #define CTS_PIN_NUMBER NRF_GPIO_PIN_MAP(0,4)//7
    #define RTS_PIN_NUMBER NRF_GPIO_PIN_MAP(0,29)//5

    I have then connected my device to those pinout.

    However, I now no longer see that information (from the uart device) to putty, nor can I use a printf command and see the result.

    When I  run this through the dubugger after running the APP_UART_FIFO_INIT and checking the error code it goes to NRF_BREAKPOINT_COND;

    I then get this in the call stack area of the debugger.

    What other details do you need to help resolve this issue?

  • I've include it (added an exisitng file) via the project items list.

    But, when I build now i get app_fifo.h no such file or directory. This error runs from app_uart_fifo.c

  • I've tried including the directory which, app_fifo.h resides in.

    ../../../../../components/libraries/uart
    ../../../../../components/libraries/fifo

  • Hi,

    If it is correctly in the include path than it should be found. There are two typical issues when adding include paths:

    1. An incorrect number of "../". This is relative to the project file. But if it is the same as those includes that were already in the project, and it works, then this should be OK.
    2. Whitespace after the end of the path. E.g. if it really is "../../../../../components/libraries/uart " instead of "../../../../../components/libraries/uart". You will not see this without putting the cursor at the end of the line or highlighting it.

    Do you still have problems after doublechecking this?

  • Yeah was the number of ../

    Now i get undefined reference to app_fifo_put and app_fifo_init

    Building ‘ble_app_beacon_pca10056_s140’ from solution ‘ble_app_beacon_pca10056_s140’ in configuration ‘Release’
      Compiling ‘app_uart_fifo.c’
      Compiling ‘main.c’
      Generating linker script ‘ble_app_beacon_pca10056_s140.ld’
      Linking ble_app_beacon_pca10056_s140.elf
        Output/ble_app_beacon_pca10056_s140 Release/Obj/app_uart_fifo.o: in function `uart_event_handler':
        undefined reference to `app_fifo_put'
        D:\Segger projects\Segger Projects\DeviceDownload2\components\libraries\uart/app_uart_fifo.c:119: undefined reference to `app_fifo_get'
        Output/ble_app_beacon_pca10056_s140 Release/Obj/app_uart_fifo.o: in function `app_uart_init':
        undefined reference to `app_fifo_init'
        D:\Segger projects\Segger Projects\DeviceDownload2\components\libraries\uart/app_uart_fifo.c:156: undefined reference to `app_fifo_init'
        Output/ble_app_beacon_pca10056_s140 Release/Obj/app_uart_fifo.o: in function `app_uart_get':
        undefined reference to `app_fifo_get'
    Build failed

  • It is the same here as well, and probably will be for some more cases. For undefined reference, this means it has not been compiled. And there are typically two reasons for that:

    • Missing sdk_config.h macro (check in the source file where the missing function is implemented to see which config macro is missing. - for app_fifo_put you cna see that it is only included by the preprocessor if APP_FIFO_ENABLED is set to 1 in sdk_config.h.
    • Make sure that the file (app_fifo.c in this caes) is actually added to the project and is compiled.

    You need to do this for all missing stuff, until you don't get any more errors when building.

Reply
  • It is the same here as well, and probably will be for some more cases. For undefined reference, this means it has not been compiled. And there are typically two reasons for that:

    • Missing sdk_config.h macro (check in the source file where the missing function is implemented to see which config macro is missing. - for app_fifo_put you cna see that it is only included by the preprocessor if APP_FIFO_ENABLED is set to 1 in sdk_config.h.
    • Make sure that the file (app_fifo.c in this caes) is actually added to the project and is compiled.

    You need to do this for all missing stuff, until you don't get any more errors when building.

Children
  • Ok it built. But when I combine my code i get an error unknown function at 0X00024C92

  • This is my main function.

    If i remove the RTT code I can see an advert and the counter increase once per second.

    If I include it the RTT viewer does not have any data on it nor does the advert run.

    int main(void)
    {
        T1_data_1 = 0;T1_data_2 = 0;T2_data_1 = 0;T2_data_2 = 0;T3_data_1 = 0;
        T3_data_2 = 0;P1_data_1 = 0;P1_data_2 = 0;P2_data_1 = 0;P2_data_2 = 0;
        // Initialize.
        log_init();
        timers_init();
        leds_init();
        power_management_init();
        ble_stack_init();
        advertising_init();
        
        uint32_t err_code;
        char string[MAX_TEST_DATA_BYTES];
        char c;
        int pos;
    
        hfxo_enable();
    
        log_init();
    
    
        const app_uart_comm_params_t comm_params =
          {
              RX_PIN_NUMBER,
              TX_PIN_NUMBER,
              RTS_PIN_NUMBER,
              CTS_PIN_NUMBER,
              UART_HWFC,
              false,
    #if defined (UART_PRESENT)
              NRF_UART_BAUDRATE_115200
    #else
              NRF_UARTE_BAUDRATE_115200
    #endif
          };
    
        APP_UART_FIFO_INIT(&comm_params,
                             UART_RX_BUF_SIZE,
                             UART_TX_BUF_SIZE,
                             uart_error_handle,
                             APP_IRQ_PRIORITY_LOWEST,
                             err_code);
    
        APP_ERROR_CHECK(err_code);
    
    
        pos = 0;
    
        // Start execution.
        NRF_LOG_INFO("Beacon example started.");
        advertising_start();
        
        advertising_mod(counter,00,00,00,00,00,00,70,80,70,80);//Only have two temperature sensors
        // Enter main loop.
        for (;;)
        {
            nrf_delay_ms(1000);
            
            counter ++;
            idle_state_handle();
    
            while (app_uart_get(&c) != NRF_SUCCESS);//Comes once persond
            counter ++;
            idle_state_handle();
            data_changed = true;
           
            string[pos] = c;
    
            // Assume that all strings end with \n. If end, print and start over.
            if (string[pos] == '\n')
            {
                string[pos+1] = '\0';
                NRF_LOG_INFO("Received string: \"%s\"", string);
    
                pos = 0;
            }
            else
            {
                pos++;
            }
            data_changed = true;
            if(data_changed==true)
            {
              printf("data changed value is: %d\r\n",data_changed);
              advertising_mod(counter,T1_data_1,T1_data_2,T2_data_1,T2_data_2,00,00,70,80,70,80);//Only have two temperature sensors
              data_changed = false;
              printf("data changed value is: %d\r\n",data_changed);
            }
        }
    }

    And it goes to NRF_BREAKPOINT_COND in the code. (when debugging)

  • Would it help if i shared my entire code with you?

  • The error i think comes from hfxo_enable.

    One issue is though if I use your RTT RX code, it crashes when I debug and try stepping through. So I presume when I merge the projects I won't be able to debug it via the debugger.

  • Hi,

    Yes, I forgot to mention that you cannot enable the HFCO the way I did it in the UART example when you have the SoftDevice. Then you need to use the SoftDevice API, or the clock driver which does it for you. Then you enable the clock by a call to nrf_drv_clock_hfclk_request(). Alternatively, you can just remove that part of the code (i.e. not start the HFXO) if that is OK for the UART communication.

    Note that if current consumption is an issue, you should only enable the HFXO when there is a UART transaction. That is a bit hidden when you use app_uart_fifo, since all happens in the library. So if you stick with that approach you can implement it in the library.

Related