Continuous Mode

This example measures voltage in continuous sampling mode using the ADC.

The example detects the input voltages of P2_5 and P2_7 as an interrupt. After ADC sampling is completed, ADC_INT_FIFO_THD interrupt is triggered, and raw data is read and voltage conversion calculations are performed within the interrupt function.

In this example, the voltage sampling range of the ADC can be selected by configuring the ADC_MODE_DIVIDE_OR_BYPASS.

Requirements

The sample supports the following development kits:

Development Kits

Hardware Platforms

Board Name

RTL8752H HDK

RTL8752H EVB

For more requirements, please refer to Quick Start.

Wiring

Connect P2_5 and P2_7 to the external voltage input.

Configurations

The macros that can be configured in this example are as follows:

  1. ADC_MODE_DIVIDE_OR_BYPASS : Configures the ADC voltage sampling range, with the following selectable values.

    • ADC_DIVIDE_MODE : In Divide Mode, the ADC samples voltage values ranging from 0 to 3.3V.

    • ADC_BYPASS_MODE : In Bypass Mode, the ADC samples voltage values ranging from 0 to 0.9V.

Building and Downloading

This sample can be found in the SDK folder:

Project file: board\evb\io_sample\ADC\ContinuousMode\mdk

Project file: board\evb\io_sample\ADC\ContinuousMode\gcc

Please follow these steps to build and run the example:

  1. Open sample project file.

  2. To build the target, follow the steps listed on the Generating App Image in Quick Start.

  3. After a successful compilation, the app bin app_MP_xxx.bin will be generated in the directory mdk\bin or gcc\bin.

  4. To download app bin into EVB board, follow the steps listed on the MP Tool Download in Quick Start.

  5. Press reset button on EVB board and it will start running.

Experimental Verification

  1. ADC Configurations:

    1. If the ADC is configured as ADC_DIVIDE_MODE, print the following log.

      [ADC]ADC sample mode is divide mode !
      
    2. If the ADC is configured as ADC_BYPASS_MODE, print the following log.

      [ADC]ADC sample mode is bypass mode !
      
  2. After the ADC sampling is finished, the raw data acquired and the converted voltage values are printed within the Debug Analyzer.

    [io_adc] io_adc_voltage_calculate: ADC rawdata_0 = xxx, voltage_0 = xxxmV
    [io_adc] io_adc_voltage_calculate: ADC rawdata_1 = xxx, voltage_1 = xxxmV
    ...
    

Code Overview

This chapter will be introduced according to the following several parts:

  1. Source Code Directory.

  2. Peripheral initialization will be introduced in chapter Initialization.

  3. Functional implementation after initialization will be introduced in chapter Function Implementation.

Source Code Directory

  • Project directory: sdk\board\evb\io_sample\ADC\ContinuousMode

  • Source code directory: sdk\src\sample\io_sample\ADC\ContinuousMode

Source files are currently categorized into several groups as below.

└── Project: adc_continuous
    └── secure_only_app
        └── include
            ├── app_define.h
            └── rom_uuid.h
        ├── cmsis                    includes CMSIS header files and startup files
            ├── overlay_mgr.c
            ├── system_rtl876x.c
            └── startup_rtl876x.s
        ├── lib                      includes all binary symbol files that user application is built on
            ├── rtl8752h_sdk.lib
            ├── gap_utils.lib
            ├── ROM.lib
            └── adc.lib
        ├── peripheral               includes all peripheral drivers and module code used by the application
            ├── rtl876x_rcc.c
            ├── rtl876x_pinmux.c
            ├── rtl876x_nvic.c
            └── rtl876x_adc.c
        ├── profile
        └── app                      includes the ble_peripheral user application implementation
            ├── main.c
            ├── ancs.c
            ├── app.c
            ├── app_task.c
            └── io_adc.c

Initialization

When the EVB reset is initiated, the main() function is called, and the following process will be executed:

int main(void)
{
    extern uint32_t random_seed_value;
    srand(random_seed_value);
    global_data_init();
    board_init();
    le_gap_init(APP_MAX_LINKS);
    gap_lib_init();
    app_le_gap_init();
    app_le_profile_init();
    pwr_mgr_init();
    task_init();
    os_sched_start();

    return 0;
}

Note

le_gap_init(), gap_lib_init(), app_le_gap_init, and app_le_profile_init are related to the initialization of the privacy management module. Refer to the initialization process description in LE Peripheral Privacy.

The specific initialization process related to peripherals is as follows:

  1. In global_data_init, execute global_data_adc_init. This function is for global initialization and includes the following process:

    1. Execute the ADC_CalibrationInit() function for ADC calibration. If the return value is false, ADC calibration failed, possibly because the IC has not undergone FT, thus unable to accurately obtain voltage values.

    2. Initialize the global variable ADC_Global_Data.

    void global_data_adc_init(void)
    {
        /* Initialize adc k value! */
        APP_PRINT_INFO0("[io_adc] global_data_adc_init");
        bool adc_k_status = false;
        adc_k_status = ADC_CalibrationInit();
        if (false == adc_k_status)
        {
            APP_PRINT_ERROR0("[io_adc] global_data_adc_init: ADC_CalibrationInit fail!");
        }
        memset(&ADC_Global_Data, 0, sizeof(ADC_Global_Data));
    }
    
  2. In board_init, execute board_adc_init, which is responsible for PAD/PINMUX settings and includes the following process:

    1. Config PAD: Set pin as SW mode, PowerOn, internal Pull-None, disable output.

  3. After executing os_sched_start() to start task scheduling, in the app_main_task main task, execute driver_init to initialize and configure the peripheral drivers. Execute driver_adc_init, which is the initialization function for the ADC peripheral, including the following process:

    1. Enable RCC clock.

    2. Configure ADC sampling channels, configure channel 0 to P2_5 single-ended mode, configure channel 1 to P2_7 single-ended mode, set Bitmap to 0x03.

    3. Set ADC sampling time and FIFO threshold.

    4. If configured as bypass mode, execute ADC_BypassCmd() to enable the high resistance mode of the corresponding channel.

    5. Configure ADC FIFO threshold interrupt ADC_INT_FIFO_THD.

    6. Execute ADC_Cmd() to enable ADC sampling.

    void driver_adc_init(void)
    {
        RCC_PeriphClockCmd(APBPeriph_ADC, APBPeriph_ADC_CLOCK, ENABLE);
    
        ADC_InitTypeDef ADC_InitStruct;
        ADC_StructInit(&ADC_InitStruct);
        ADC_InitStruct.ADC_SchIndex[0]  = EXT_SINGLE_ENDED(ADC_SAMPLE_CHANNEL_5);
        ADC_InitStruct.ADC_SchIndex[1]  = EXT_SINGLE_ENDED(ADC_SAMPLE_CHANNEL_7);
        ADC_InitStruct.ADC_Bitmap       = 0x03;
        ADC_InitStruct.ADC_SampleTime   = ADC_CONTINUOUS_SAMPLE_PERIOD;
        /* Configure the interrupt of ADC_INT_FIFO_TH threshold value. */
        ADC_InitStruct.ADC_FifoThdLevel = 16;
        ADC_Init(ADC, &ADC_InitStruct);
    
    #if (ADC_MODE_DIVIDE_OR_BYPASS == ADC_BYPASS_MODE)
        /* High bypass resistance mode config, please notice that the input voltage of
        adc channel using high bypass mode should not be over 0.9V */
        ADC_BypassCmd(ADC_SAMPLE_CHANNEL_5, ENABLE);
        ADC_BypassCmd(ADC_SAMPLE_CHANNEL_7, ENABLE);
        APP_PRINT_INFO0("[ADC] ADC sample mode is bypass mode !");
    #else
        ADC_BypassCmd(ADC_SAMPLE_CHANNEL_5, DISABLE);
        ADC_BypassCmd(ADC_SAMPLE_CHANNEL_7, DISABLE);
        APP_PRINT_INFO0("[ADC] ADC sample mode is divide mode !");
    #endif
    
        ADC_INTConfig(ADC, ADC_INT_FIFO_THD, ENABLE);
    
        NVIC_InitTypeDef NVIC_InitStruct;
        NVIC_InitStruct.NVIC_IRQChannel = ADC_IRQn;
        NVIC_InitStruct.NVIC_IRQChannelPriority = 3;
        NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStruct);
        /* When ADC is enabled, sampling will be done quickly and interruption will occur.
        After initialization, ADC can be enabled when sampling is needed.*/
        ADC_Cmd(ADC, ADC_CONTINUOUS_MODE, ENABLE);
    }
    

Note

In Continuous mode of ADC, data is by default stored in the ADC FIFO.

Functional Implementation

  1. When the number of ADC FIFO exceeds the set threshold, it triggers ADC_INT_FIFO_THD interrupt and enters the interrupt handler function ADC_Handler.

    1. Determine whether the interrupt status of ADC_INT_FIFO_THD is SET.

    2. Disable ADC continuous mode sampling.

    3. Execute ADC_GetFIFODataLen() to get the amount of data in ADC FIFO, and execute ADC_ReadFIFOData() to get the data in ADC FIFO.

    4. Execute ADC_ClearFIFO() to clear the data in ADC FIFO and clear the interrupt flag.

    5. Define message type IO_MSG_TYPE_ADC, save the collected data to a global variable, and execute app_send_msg_to_apptask to send the message to the task.

void ADC_Handler(void)
{
    uint8_t data_len = 0;
    uint16_t sample_data[ADC_FIFO_MAX] = {0};

    if (ADC_GetINTStatus(ADC, ADC_INT_FIFO_THD) == SET)
    {
        ADC_Cmd(ADC, ADC_CONTINUOUS_MODE, DISABLE);

        data_len = ADC_GetFIFODataLen(ADC);
        /* In ADC continuous sampling mode, read data from FIFO.
        In multi-channel sampling,
        the data order in FIFO corresponds to the channel order set by schedule index.
        */
        ADC_ReadFIFOData(ADC, sample_data, data_len);

        ADC_ClearFIFO(ADC);
        ADC_ClearINTPendingBit(ADC, ADC_INT_FIFO_THD);

        T_IO_MSG int_adc_msg;

        int_adc_msg.type = IO_MSG_TYPE_ADC;
        int_adc_msg.subtype = 0;
        ADC_Global_Data.RawData[0] = data_len;
        for (uint8_t i = 0; i < data_len; i++)
        {
            ADC_Global_Data.RawData[i + 1] = sample_data[i];
        }
        int_adc_msg.u.buf = (void *)(ADC_Global_Data.RawData);
        if (false == app_send_msg_to_apptask(&int_adc_msg))
        {
            APP_PRINT_ERROR0("[io_adc] ADC_Handler: Send int_adc_msg failed!");
            // Add user code here!
            ADC_ClearFIFO(ADC);
            ADC_ClearINTPendingBit(ADC, ADC_INT_FIFO_THD);
            return;
        }
    }
}
  1. In app_main_task, loop to check the message queue. When a msg is detected, execute the app_handle_io_msg function to process the msg.

  2. In the app_handle_io_msg function, if the message type is determined to be IO_MSG_TYPE_ADC, execute the io_handle_adc_msg function and execute io_adc_voltage_calculate.

    1. Extract sampling data from the msg.

    2. Execute ADC_GetVoltage() to calculate the sampling voltage value based on the sampling mode.

    3. Print the sampling data and the sampling voltage.

    4. Re-enable the ADC continuous mode.

static void io_adc_voltage_calculate(T_IO_MSG *io_adc_msg)
{
    uint8_t sample_data_len = 0;
    uint16_t sample_data[ADC_FIFO_MAX] = {0};
    float sample_voltage[ADC_FIFO_MAX] = {0};
    ADC_ErrorStatus error_status = NO_ERROR;

    uint16_t *p_buf = io_adc_msg->u.buf;

    sample_data_len = p_buf[0];
    for (uint8_t i = 0; i < sample_data_len; i++)
    {
        sample_data[i] = p_buf[i + 1];
    }
    for (uint8_t i = 0; i < sample_data_len; i++)
    {
#if (ADC_MODE_DIVIDE_OR_BYPASS == ADC_BYPASS_MODE)
        sample_voltage[i] = ADC_GetVoltage(BYPASS_SINGLE_MODE, (int32_t)sample_data[i], &error_status);
#else
        sample_voltage[i] = ADC_GetVoltage(DIVIDE_SINGLE_MODE, (int32_t)sample_data[i], &error_status);
#endif
        if (error_status < 0)
        {
            APP_PRINT_INFO2("[io_adc] io_adc_voltage_calculate: ADC parameter or efuse data error! i = %d, error_status = %d",
                            i, error_status);
        }
        else
        {
            APP_PRINT_INFO4("[io_adc] io_adc_voltage_calculate: ADC rawdata_%-4d = %d, voltage_%-4d = %dmV ", i,
                            sample_data[i], i, (uint32_t)sample_voltage[i]);

        }
    }
    memset(&ADC_Global_Data, 0, sizeof(ADC_Global_Data));
    for (uint32_t i = 0; i < 1000000; i++);
    ADC_Cmd(ADC, ADC_CONTINUOUS_MODE, ENABLE);
}

Troubleshooting

  1. If the IC obtained has not been verified by FT, the ADC will not be able to convert to the correct voltage value. The following message will be printed within the log tool.

    [ADC]ADC_CalibrationInit fail!
    
  2. If the ADC sample value is incorrect, print the error status.

    [ADC]adc_sample_demo: ADC parameter or efuse data error! i = xxx, error_status = xxx