ADC

Last Updated on : 2023-01-29 07:32:16

File description

API files

The ADC API is located in ty_adc.h. The driver code for different chip platforms is located in ty_adc_xxxx.c.

tuya_ble_sdk_demo
└── board
     ├── include
     |    └── ty_adc.h
     ├── xxxx                      /* xxxx represents the chip platform, such as TLSR825x */
     |    └── ty_board_xxxx        /* xxxx represents the chip platform, such as ty_board_tlsr825x */
     |         └── ty_adc_xxxx.c   /* xxxx represents the chip platform, such as ty_adc_tlsr825x.c */
     └── board.h

Demo files

File structure of tuya_board_api_demo:

tuya_ble_sdk_demo
├── app  /* API example */
|    ├── include
|    |    └── ty_board_demo
|    |    |    ├── demo_config.h        /* Demo configuration file */
|    |    |    └── ty_adc_demo.h        /* ADC example */
|    |    ├── tuya_ble_board_api_demo.h /* Entry point to Board API example */
|    |    └── tuya_ble_sdk_demo.h       /* Entry point to tuya_ble_sdk application */
|    └── src
|         ├── ty_board_demo
|         |    └── ty_adc_demo.c        /* ADC example */
|         ├── tuya_ble_board_api_demo.c /* Entry point to Board API example */
|         └── tuya_ble_sdk_demo.c       /* Entry point to tuya_ble_sdk application */
└── board /* Example of modifying API code */

Modify demo_config.h to toggle to the ADC example.

#define BOARD_API_DEMO          BOARD_API_ADC

API list

Function name Description
ty_adc_init Initialize the ADC.
ty_adc_start Start ADC capture.
ty_adc_stop Stop ADC capture.
ty_adc_control Control the ADC.
ty_adc_uninit Turn off the ADC.

API description

ty_adc_init

Function name ty_adc_init
Function prototype uint32_t ty_adc_init(ty_adc_t* p_adc);
Description Initialize the ADC.
Parameters p_adc [in]: The ADC parameter, defined by ty_adc_t
Return value 0: Success.
Others: Failure.
Notes You can add or modify functions, parameters, and return values as needed.

ty_adc_start

Function name ty_adc_start
Function prototype uint32_t ty_adc_start(ty_adc_t* p_adc);
Description Start ADC capture.
Parameters p_adc [inout]: The ADC parameter, defined by ty_adc_t
Return value 0: Success.
Others: Failure.
Notes You can add or modify functions, parameters, and return values as needed.

ty_adc_stop

Function name ty_adc_stop
Function prototype uint32_t ty_adc_stop(ty_adc_t* p_adc);
Description Stop ADC capture.
Parameters p_adc [in]: The ADC parameter, defined by ty_adc_t
Return value 0: Success.
Others: Failure.
Notes You can add or modify functions, parameters, and return values as needed.

ty_adc_control

Function name ty_adc_control
Function prototype uint32_t ty_adc_control(ty_adc_t* p_adc, uint8_t cmd, void* arg);
Description Control the ADC.
Parameters p_adc [in]: The ADC parameter, defined by ty_adc_t
cmd [in]: The control command.
arg [in]: The control parameter.
Return value 0: Success.
Others: Failure.
Notes You can add or modify functions, parameters, and return values as needed.

ty_adc_uninit

Function name ty_adc_uninit
Function prototype uint32_t ty_adc_uninit(ty_adc_t* p_adc);
Description Turn off the ADC.
Parameters p_adc [in]: The ADC parameter, defined by ty_adc_t
Return value 0: Success.
Others: Failure.
Notes You can add or modify functions, parameters, and return values as needed.

Data type

ty_adc_t

/* TLSR825x platform */
typedef struct {
    GPIO_PinTypeDef pin;        /* Pin number */
    ADC_InputPchTypeDef channel;/* ADC channel */
    uint32_t value;             /* Captured data by ADC */
} ty_adc_t;

/* nRF52832 platform */
typedef struct {
    uint8_t channel;            /* ADC channel */
    int16_t value;              /* Captured data by ADC */
} ty_adc_t;

API example

Use TLSR825x as an example. You can add ADC API function or call APIs provided by the chip vendor in the application code.

ty_adc.h

/* ! The channel member in this example can be deleted */
typedef struct {
    GPIO_PinTypeDef pin;        /* Pin number */
    uint32_t value;             /* Captured data by ADC */
} ty_adc_t;

ty_adc_tlsr825x.c

uint32_t ty_adc_init(ty_adc_t* p_adc)
{
    adc_init();
    adc_base_init(p_adc->pin);
    adc_power_on_sar_adc(1);
}

uint32_t ty_adc_start(ty_adc_t* p_adc)
{
    p_adc.value = adc_sample_and_get_result();
    return 0;
}

Sample application

Description

Read the voltage at the ADC pin every one second.

Sample code

The sample code uses the TLSR825x platform:

#include "ty_adc_demo.h"
#include "ty_adc.h"
#include "tuya_ble_log.h"
#include "tuya_ble_port.h"

/***********************************************************
************************micro define************************
***********************************************************/
#define ADC_PIN             GPIO_PB6
#define ADC_SAMPLE_TIME_MS  1000

/***********************************************************
***********************variable define**********************
***********************************************************/
static ty_adc_t sg_adc = {
    .pin = ADC_PIN,
    .value = 0
};

static tuya_ble_timer_t sg_adc_timer;

/***********************************************************
***********************function define**********************
***********************************************************/
/**
 * @brief adc timer callback
 * @param none
 * @return none
 */
void __adc_timer_cb(void)
{
    uint32_t res = ty_adc_start(&sg_adc);
    if (res) {
        TUYA_APP_LOG_ERROR("ty_adc_start failed, error code: %d", res);
    } else {
        TUYA_APP_LOG_INFO("adc_value is: %d mV", sg_adc.value);
    }
}

/**
 * @brief ty_adc api demo init
 * @param none
 * @return none
 */
void ty_adc_demo_init(void)
{
    uint32_t res = ty_adc_init(&sg_adc);
    if (res) {
        TUYA_APP_LOG_ERROR("ty_adc_init failed, error code: %d", res);
        return;
    }
    tuya_ble_timer_create(&sg_adc_timer, ADC_SAMPLE_TIME_MS, TUYA_BLE_TIMER_REPEATED, (tuya_ble_timer_handler_t)__adc_timer_cb);
    tuya_ble_timer_start(sg_adc_timer);
}