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

reading analog through UART

Hello

I just started learning nrf52832 DK and run few of the examples. I wanted to start developing my own application based on the examples. Right now, I want to read analog from A0 and print the reading in UART. I tried to combine the peripheral examples UART and SAADC, but with no success. I made some changes to the sdk_config.h file of the saadc example to enable APP_FIFO_ENABLED 1, RETARGET_ENABLED 1, APP_UART_ENABLED 1, APP_UART_DRIVER_INSTANCE 0. Building my project fails with the following three errors:

  • .\_build\nrf52832_xxaa.axf: Error: L6218E: Undefined symbol app_uart_get (referred from main.o).
  • .\_build\nrf52832_xxaa.axf: Error: L6218E: Undefined symbol app_uart_init (referred from main.o).
  • .\_build\nrf52832_xxaa.axf: Error: L6218E: Undefined symbol app_uart_put (referred from main.o).

Can somebody explain me what might cause the error. I would also appreciate if anyone indicate me an existing example with the minimal line of codes. Btw, I am using sdk15.

  • I just started learning nrf52832 DK

    Do you have any prior 'C' programming experience?

    Can somebody explain me what might cause the error

    As the messages say you have referred to those symbols, but have not defined them (the mentioned symbols are all function names - so "referring" to them probably means that you have called them).

    This is usually caused by including a header file (.h) - but not adding the corresponding .c file(s) to your project.

    This is a standard 'C' thing - nothing specifically to do with Nordic: http://c-faq.com/decl/decldef.html 

    .

    See also:

    https://devzone.nordicsemi.com/f/nordic-q-a/39627/error-l6218e/153564#153564

    https://devzone.nordicsemi.com/f/nordic-q-a/13720/undefined-symbol

    and many more.

  • Hi owneil

    Thanks for the reply. I have limited experience with C. I did not know if those errors mean that there are some missing .c files.

  • As noted, that's a standard 'C' thing - not specific to Nordic.

    It's generally easier if you learn 'C' on a PC or similar before moving on to the added complications of embedded microcontrollers and wireless comms.

    Here are some 'C' learning & reference materials - including a free online textbook:

    http://blog.antronics.co.uk/2011/08/08/so-youre-thinking-of-starting-with-c/

  • Have you figured out this issue or do you still need some help?

  • Yes, I finally figured it out, except that the result from the ADC does not correspond to what I was expecting. I was trying to read voltage from a DC power source but the results do not much. If I am allowed , I would like to share my code here so that somebody can point me out what I am missing there.

    ** @file
     * @defgroup uart_example_main main.c
     * @{
     * @ingroup uart_example
     * @brief UART Example Application main file.
     *
     * This file contains the source code for a sample application using UART.
     *
     */
    
    #include <stdbool.h>
    #include <stdint.h>
    #include <stdio.h>
    #include "app_uart.h"
    #include "nrf_drv_saadc.h"
    #include "app_error.h"
    #include "nrf_delay.h"
    #include "nrf.h"
    #include "bsp.h"
    #include "nrf_log.h"
    #if defined (UART_PRESENT)
    #include "nrf_uart.h"
    #endif
    #if defined (UARTE_PRESENT)
    #include "nrf_uarte.h"
    #endif
    
    
    //#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 256                         /**< UART RX buffer size. */
    #define UART_HWFC APP_UART_FLOW_CONTROL_DISABLED
    #define NRF_LOG_ERROR(...)                     NRF_LOG_INTERNAL_ERROR(__VA_ARGS__)
    #define NRF_LOG_WARNING(...)                   NRF_LOG_INTERNAL_WARNING( __VA_ARGS__)
    #define NRF_LOG_INFO(...)                      NRF_LOG_INTERNAL_INFO( __VA_ARGS__)
    #define NRF_LOG_DEBUG(...)                     NRF_LOG_INTERNAL_DEBUG( __VA_ARGS__)
    #define SAMPLES_IN_BUFFER 5
    static uint32_t              m_adc_evt_counter;
    
    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);
        }
    }
    
    
    
    
    /**
     * @brief Function for main application entry.
     */
    void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
    {
        if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
        {
            ret_code_t err_code;
    
            err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER);
            APP_ERROR_CHECK(err_code);
    
            int i;
            NRF_LOG_INFO("ADC event number: %d", (int)m_adc_evt_counter);
    
            for (i = 0; i < SAMPLES_IN_BUFFER; i++)
            {
                NRF_LOG_INFO("%d", p_event->data.done.p_buffer[i]);
            }
            m_adc_evt_counter++;
        }
    }
    
    void saadc_init(void)
    {
        ret_code_t err_code;
        nrf_drv_saadc_config_t saadc_config;
        saadc_config.low_power_mode = true;
        saadc_config.resolution = NRF_SAADC_RESOLUTION_12BIT;
        saadc_config.oversample = NRF_SAADC_OVERSAMPLE_DISABLED;
        saadc_config.interrupt_priority = APP_IRQ_PRIORITY_LOW;
        nrf_saadc_channel_config_t channel_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(SAADC_CH_PSELP_PSELP_VDD);
        err_code = nrf_drv_saadc_init(&saadc_config, saadc_callback);
        APP_ERROR_CHECK(err_code);
        
        channel_config.pin_p  = NRF_SAADC_INPUT_AIN5;
      err_code = nrf_drv_saadc_channel_init(0, &channel_config);
        APP_ERROR_CHECK(err_code);
        
    }
    
    
    void uart_config_init(void)
    {
        ret_code_t err_code;
        const app_uart_comm_params_t comm_params =
          {
              RX_PIN_NUMBER,
              TX_PIN_NUMBER,
              RTS_PIN_NUMBER,
              CTS_PIN_NUMBER,
              UART_HWFC,
              false,
              NRF_UART_BAUDRATE_115200
          };
    
        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);
    }
    int main(void)
    {
        
        saadc_init();
        nrf_delay_ms(2);
        bsp_board_leds_init();
      uart_config_init();
        
        nrf_saadc_value_t AdcVal[1];
        
        //nrf_drv_saadc_sample_convert(1, &AdcVal[1]);
    
      printf("\r\nStart: \r\n");
        
        while(true)
        {
            nrf_drv_saadc_sample_convert(0, &AdcVal[0]);
            //int16_t voltage = 5*AdcVal[0]/4096;
            //printf("Analog reading: %u\n\r", voltage);
            printf("Analog reading: %u\n\r", AdcVal[0]);
            nrf_delay_ms(500);
        }
    
       /* 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.
                }
            }
        }*/
    
    }
    
    
    /** @} */
    
    

    When I set the power supply voltage to 5V, the analog reading I see in the UART is about 2000. This is equivalent to 2000*5/4096 = ~2.44Volts. Any idea?

Related