You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.9 KiB
80 lines
2.9 KiB
/*
|
|
* Copyright (c) Hisilicon Technologies Co., Ltd.. 2021-2021. All rights reserved.
|
|
* Description: pwm sample
|
|
* Author: Hisilicon
|
|
* Create: 2021-12-10
|
|
*/
|
|
|
|
/********************************************************************************************/
|
|
/* Includes */
|
|
/********************************************************************************************/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "uapi_pwm.h"
|
|
#include "sample_pwm.h"
|
|
|
|
/*********************************************************************************************
|
|
Function Implementation
|
|
*********************************************************************************************/
|
|
static td_void printf_help(td_void)
|
|
{
|
|
printf("\nUsage: sample_pwm pwm_num | freq | duty_ratio\n");
|
|
printf(" pwm_num - The pwm number computing formula is [group*8+bit]. For example, The pwm number of\n");
|
|
printf(" GPIO24_3 is 195.\n");
|
|
printf(" The common pwm number is from 0 to 247 on type_v1.\n");
|
|
printf(" The common pwm number is from 0 to 3 and the led pwm number is 4 on type_v2.\n");
|
|
printf(" The common pwm number is from 0 to 167 and the led pwm number is 168 type_v3.\n");
|
|
printf(" freq - The frequency in Hz\n");
|
|
printf(" duty_ratio - The value of duty ratio is from 0 to 1000.\n");
|
|
printf("Example: sample_pwm 195 36000 500\n");
|
|
printf("Note: When gpio is used for pwm function, the pin direction must be output.\n");
|
|
}
|
|
|
|
static td_void wait_quit(td_void)
|
|
{
|
|
td_s32 i;
|
|
|
|
while (1) {
|
|
printf("enter q to quit\n");
|
|
i = getchar();
|
|
if (i == 'q') {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
td_s32 main(td_s32 argc, td_char *argv[])
|
|
{
|
|
td_s32 ret, ret1;
|
|
td_u32 pwm_no, freq, duty_ratio;
|
|
uapi_pwm_attr attr = {};
|
|
|
|
sample_pwm_check_param(ret, TD_FAILURE, argc != 0x4, goto help);
|
|
|
|
pwm_no = (td_u32)strtoul(argv[0x1], TD_NULL, 0);
|
|
freq = (td_u32)strtoul(argv[0x2], TD_NULL, 0);
|
|
duty_ratio = (td_u32)strtoul(argv[0x3], TD_NULL, 0);
|
|
|
|
sample_pwm_check_param(ret, TD_FAILURE, duty_ratio > 1000, goto help); /* 1000: max duty ratio */
|
|
|
|
sample_pwm_func_call(ret, uapi_pwm_init(), goto out);
|
|
attr.pwm_frequency = freq;
|
|
attr.pwm_duty_ratio = duty_ratio;
|
|
sample_pwm_func_call(ret, uapi_pwm_set_attr(pwm_no, &attr), goto deinit);
|
|
sample_pwm_func_call(ret, uapi_pwm_get_attr(pwm_no, &attr), goto deinit);
|
|
sample_pwm_func_call(ret, uapi_pwm_send_signal(pwm_no, 10000, 10000), goto deinit); /* 10000 : wait 10ms */
|
|
sample_pwm_func_call(ret, uapi_pwm_set_enable(pwm_no, TD_TRUE), goto deinit);
|
|
|
|
wait_quit();
|
|
|
|
sample_pwm_func_call(ret, uapi_pwm_set_enable(pwm_no, TD_FALSE), goto deinit);
|
|
|
|
deinit:
|
|
sample_pwm_func_call(ret1, uapi_pwm_deinit(), goto out);
|
|
goto out;
|
|
help:
|
|
printf_help();
|
|
out:
|
|
return ret;
|
|
}
|