PWM

Last Updated on : 2022-11-24 09:20:21download

API list

Function name Description
tuya_pwm_init Initialize the PWM.
tuya_pwm_start Start the PWM.
tuya_pwm_stop Stop the PWM.
tuya_pwm_set Set the PWM frequency and duty cycle.
tuya_pwm_frequency_set Set the PWM frequency.
tuya_pwm_duty_set Set the PWM duty cycle.
tuya_pwm_polarity_set Set the initial polarity of PWM channels.
tuya_pwm_deinit Disable the PWM.

APIs

tuya_pwm_init

Function prototype

  int tuya_pwm_init(tuya_pwm_t *pwm);

Parameters

Parameter name Parameter type Description
pwm tuya_pwm_t The PWM handle. Initialize the PWM parameters.

Return value

Return value Description
OPRT_OK Initialization succeeded.
Error codes Initialization failed.

tuya_pwm_start

Function prototype

  int tuya_pwm_start(tuya_pwm_t *pwm);

Parameters

Parameter name Parameter type Description
pwm tuya_pwm_t The PWM handle. Initialize the PWM parameters.

Return value

Return value Description
OPRT_OK Initialization succeeded.
Error codes Initialization failed.

tuya_pwm_stop

Function prototype

  int tuya_pwm_stop(tuya_pwm_t *pwm);

Parameters

Parameter name Parameter type Description
pwm tuya_pwm_t The PWM handle. Initialize the PWM parameters.

Return value

Return value Description
OPRT_OK Initialization succeeded.
Error codes Initialization failed.

tuya_pwm_set

Function prototype

  int tuya_pwm_set(tuya_pwm_t *pwm, float frequency, float percent);

Parameters

Parameter name Parameter type Description
pwm tuya_pwm_t The PWM handle. Initialize the PWM parameters.
pwm float The PWM frequency.
pwm float The PWM duty cycle, ranging from 0 to 100%.

Return value

Return value Description
OPRT_OK Initialization succeeded.
Error codes Initialization failed.

tuya_pwm_frequency_set

Function prototype

  int tuya_pwm_frequency_set(tuya_pwm_t *pwm, float frequency);

Parameters

Parameter name Parameter type Description
pwm tuya_pwm_t The PWM handle. Initialize the PWM parameters.
pwm float The PWM frequency.

Return value

Return value Description
OPRT_OK Initialization succeeded.
Error codes Initialization failed.

tuya_pwm_duty_set

Function prototype

  int tuya_pwm_duty_set(tuya_pwm_t *pwm, float percent);

Parameters

Parameter name Parameter type Description
pwm tuya_pwm_t The PWM handle. Initialize the PWM parameters.
pwm float The PWM duty cycle, ranging from 0 to 100%.

Return value

Return value Description
OPRT_OK Initialization succeeded.
Error codes Initialization failed.

tuya_pwm_polarity_set

Function prototype

  int tuya_pwm_polarity_set(tuya_pwm_t *pwm, tuya_pwm_polarity_t polarity);

Parameters

Parameter name Parameter type Description
pwm tuya_pwm_t The PWM handle. Initialize the PWM parameters.
pwm tuya_pwm_polarity_t The initial polarity of PWM channels. The enum values in the @tuya_pwm_polarity_t.

Return value

Return value Description
OPRT_OK Initialization succeeded.
Error codes Initialization failed.

tuya_pwm_deinit

Function prototype

  INT_T tuya_pwm_deinit(TY_PWM_DEV_S *pwm);

Parameters

Parameter name Parameter type Description
pwm tuya_pwm_t The PWM handle. Initialize the PWM parameters.

Return value

Return value Description
OPRT_OK Initialization succeeded.
Error codes Initialization failed.

Structs

tuya_pwm_port_t

Struct prototype

typedef enum {
    TUYA_PWM0 = 0x00,
    TUYA_PWM1,
    TUYA_PWM2,
    TUYA_PWM3,
    TUYA_PWM4,
    TUYA_PWM5,
    TUYA_PWM6,
    TUYA_PWM7,
    TUYA_PWM8,
} tuya_pwm_port_t;

tuya_pwm_cfg_t

Struct prototype

typedef struct {
    uint8_t         pin;             //!  The PWM output pin.
    uint8_t         polarity;        //!  The PWM output polarity.
    uint32_t        period_ns;       //!  The PWM output period.
    uint32_t        pulse_ns;        //!  The PWM output pulse width.
    float           percent;         //!  The PWM output duty cycle.
} tuya_pwm_cfg_t;

tuya_pwm_polarity_t

Struct prototype

typedef enum {		
    TUYA_PWM_POSITIVE = 0,  		 //!  The PWM signal is initially high.
    TUYA_PWM_NEGATIVE,  		//!  The PWM signal is initially low.
} tuya_pwm_polarity_t;

tuya_pwm_t

Struct prototype

struct tuya_pwm {
    tuya_drv_node_t     node;  	//!  PWM nodes.
    tuya_pwm_cfg_t      cfg;  	//!  PWM configuration. For more information, see `tuya_pwm_cfg_t`.
    tuya_pwm_ops_t     *ops;  	//!  Leave it as is.
};
typedef struct tuya_pwm tuya_pwm_t;

Simplified configuration of PWM macros

//!  __PWM:   		The handle.
//!  __PIN:   		The pin number.
//!  __FREQUENCY:    The frequency.
//!  __PERCENT:     The duty cycle.
#define TUYA_PWM_CFG(__PWM, __PIN, __FREQUENCY, __PERCENT)                      \
    (__PWM)->cfg.pin       = __PIN;                                             \
    (__PWM)->cfg.period_ns = (uint32_t)1000000000 / (__FREQUENCY);              \
    (__PWM)->cfg.percent   = __PERCENT;                                         \
    (__PWM)->cfg.pulse_ns  = (uint32_t)((__PWM)->cfg.period_ns * (__PERCENT));  \
    (__PWM)->cfg.polarity  = TUYA_PWM_POSITIVE

Usage example

#include "tuya_pin.h"
#include "tuya_pwm.h"
#include "tuya_hal_system.h"

static tuya_pwm_t *pwm[5];

void tuya_pwm_test(void)
{
    UINT8_T pwm_pin[] = {
        TUYA_PA14, TUYA_PA15, TUYA_PA0, TUYA_PA5, TUYA_PA12
    };

#if 1  //!  Loop initialization.
    int i;
    for (i = 0; i < 5; i++) {
        pwm[i] = (tuya_pwm_t *)tuya_driver_find(TUYA_DRV_PWM, (tuya_pwm_port_t)i);
        TUYA_PWM_CFG(pwm[i], pwm_pin[i], 10 * 1000, 0.1);
        tuya_pwm_init(pwm[i]);  
        tuya_pwm_start(pwm[i]);
    }
#else  //!  One-by-one initialization.
    pwm[0] = tuya_driver_find(TUYA_DRV_PWM, TUYA_PWM0);
    TUYA_PWM_CFG(pwm[0], TUYA_PA14, 10 * 1000, 0.1);
    tuya_pwm_init(pwm[0]);
    tuya_pwm_start(pwm[0]);

    pwm[1] = tuya_driver_find(TUYA_DRV_PWM, TUYA_PWM1);
    TUYA_PWM_CFG(pwm[1], TUYA_PA15, 10 * 1000, 0.1);
    tuya_pwm_init(pwm[1]);
    tuya_pwm_start(pwm[1]);

    pwm[2] = tuya_driver_find(TUYA_DRV_PWM, TUYA_PWM2);
    TUYA_PWM_CFG(pwm[2], TUYA_PA0, 10 * 1000, 0.1);
    tuya_pwm_init(pwm[2]);
    tuya_pwm_start(pwm[2]);

    pwm[3] = tuya_driver_find(TUYA_DRV_PWM, TUYA_PWM3);
    TUYA_PWM_CFG(pwm[3], TUYA_PA5, 10 * 1000, 0.1);
    tuya_pwm_init(pwm[3]);
    tuya_pwm_start(pwm[3]);

    pwm[4] = tuya_driver_find(TUYA_DRV_PWM, TUYA_PWM4);
    TUYA_PWM_CFG(pwm[4], TUYA_PA12, 10 * 1000, 0.1);
    tuya_pwm_init(pwm[4]);
    tuya_pwm_start(pwm[4]);
#endif

    PR_DEBUG("ns %d us  %d", pwm[0]->cfg.period_ns,  pwm[0]->cfg.period_ns / 1000);

    uint8_t  flag  = 1;
    float    percent = TUYA_PWM_PERCENT(pwm[0]);

	 //!  PWM breathing light effect.
    while (1) {
        if (flag) {
            if (percent < 1.0) {
                percent += 0.005; 
            } else {
                flag = 0;
                continue;
            }
        } else {
            if (percent > 0.005) {
                percent -= 0.005;
            } else {
                flag = 1;
                continue;
            }
        }
        PR_NOTICE("percent = %d", (int)(percent * 1000));
        for (UINT8_T i = 0; i < 5; i ++) {
            tuya_pwm_duty_set(pwm[i], percent);
        }
        tuya_hal_system_sleep(3);
    }
}