Yuqiang Wang e1498cabc9 SDK build (#47) hai 5 meses
..
.settings 4795f3d206 更新fsp为2.2.0 hai 6 meses
board 4795f3d206 更新fsp为2.2.0 hai 6 meses
figures 539cd344e1 增加示例的FSP配置描述 hai 1 ano
packages 4795f3d206 更新fsp为2.2.0 hai 6 meses
rzn 0e9a0dfdbd fix iar link source hai 6 meses
rzn_cfg 0e9a0dfdbd fix iar link source hai 6 meses
rzn_gen 0e9a0dfdbd fix iar link source hai 6 meses
script 4795f3d206 更新fsp为2.2.0 hai 6 meses
src f4bde77e2a 修复gpt 使用pwm 框架 以及其他示例中hal_entry 中的 主循环内容 hai 1 ano
.api_xml e7e2582a7c EtherKit 示例工程添加 hai 1 ano
.config 4795f3d206 更新fsp为2.2.0 hai 6 meses
.cproject 4795f3d206 更新fsp为2.2.0 hai 6 meses
.gitignore 43669c1c97 Fixed some known issues hai 1 ano
.project e7e2582a7c EtherKit 示例工程添加 hai 1 ano
.secure_azone e7e2582a7c EtherKit 示例工程添加 hai 1 ano
.secure_rzone 4795f3d206 更新fsp为2.2.0 hai 6 meses
.secure_xml 4795f3d206 更新fsp为2.2.0 hai 6 meses
Kconfig 4795f3d206 更新fsp为2.2.0 hai 6 meses
README.md e1498cabc9 SDK build (#47) hai 5 meses
README_zh.md e1498cabc9 SDK build (#47) hai 5 meses
SConscript 0e9a0dfdbd fix iar link source hai 6 meses
SConstruct e7e2582a7c EtherKit 示例工程添加 hai 1 ano
buildinfo.ipcf 0e9a0dfdbd fix iar link source hai 6 meses
buildinfo.json 4795f3d206 更新fsp为2.2.0 hai 6 meses
configuration.xml 4795f3d206 更新fsp为2.2.0 hai 6 meses
envsetup.sh e7e2582a7c EtherKit 示例工程添加 hai 1 ano
mklinks.bat e7e2582a7c EtherKit 示例工程添加 hai 1 ano
mklinks.sh e7e2582a7c EtherKit 示例工程添加 hai 1 ano
ozone_scons.jdebug 384e7a31df 修复scons固件无法运行的问题,添加ozone调试脚本 hai 1 ano
project.ewd 0e9a0dfdbd fix iar link source hai 6 meses
project.ewp 0e9a0dfdbd fix iar link source hai 6 meses
project.ewt 0e9a0dfdbd fix iar link source hai 6 meses
project.eww e7e2582a7c EtherKit 示例工程添加 hai 1 ano
rtconfig.h 4795f3d206 更新fsp为2.2.0 hai 6 meses
rtconfig.py 384e7a31df 修复scons固件无法运行的问题,添加ozone调试脚本 hai 1 ano
rzn_cfg.txt 4795f3d206 更新fsp为2.2.0 hai 6 meses
template.ewd 0e9a0dfdbd fix iar link source hai 6 meses
template.ewp 0e9a0dfdbd fix iar link source hai 6 meses
template.eww e7e2582a7c EtherKit 示例工程添加 hai 1 ano

README.md

GPT Driver Usage Instructions

English | 中文

Introduction

In our specific application scenarios, the use of timers is often indispensable. This example mainly introduces how to use the GPT device on the EtherKit board, including the basic timer usage and PWM usage.

FSP Configuration Instructions

The FSP is configured to enable GPT0 as a basic timer mode and GPT5 as a PWM mode:

image-20241204164917896

image-20241204164922394

Additionally, the pins for GPT0 and GPT5 are enabled:

image-20241126102147584

RT-Thread Settings Configuration

In the configuration, enable timer0 and PWM:

image-20241126102203077

image-20241126131544356

Example Project Instructions

The source code for this example is located in /projects/etherkit_driver_gpt:

int hwtimer_sample(void)
{
    rt_err_t ret = RT_EOK;
    rt_hwtimerval_t timeout_s;
    rt_device_t hw_dev = RT_NULL;
    rt_hwtimer_mode_t mode;
    rt_uint32_t freq = 400000000; /* 1 MHz */
    hw_dev = rt_device_find(HWTIMER_DEV_NAME);
    if (hw_dev == RT_NULL)
    {
        rt_kprintf("hwtimer sample run failed! can't find %s device!\n", HWTIMER_DEV_NAME);
        return -RT_ERROR;
    }
    ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
    if (ret != RT_EOK)
    {
        rt_kprintf("open %s device failed!\n", HWTIMER_DEV_NAME);
        return ret;
    }
    rt_device_set_rx_indicate(hw_dev, timeout_cb);
    rt_device_control(hw_dev, HWTIMER_CTRL_FREQ_SET, &freq);
    mode = HWTIMER_MODE_PERIOD;
    ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
    if (ret != RT_EOK)
    {
        rt_kprintf("set mode failed! ret is :%d\n", ret);
        return ret;
    }
    /* Example: Set the timeout period of the timer */
    timeout_s.sec = 1;  /* seconds */
    timeout_s.usec = 0; /* microseconds */
    if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
    {
        rt_kprintf("set timeout value failed\n");
        return -RT_ERROR;
    }
    /* Read hwtimer value */
    rt_device_read(hw_dev, 0, &timeout_s, sizeof(timeout_s));
    rt_kprintf("Read: Sec = %d, Usec = %d\n", timeout_s.sec, timeout_s.usec);
    return ret;
}
MSH_CMD_EXPORT(hwtimer_sample, hwtimer sample);

The interrupt callback function is triggered every 1 second, printing output. Below is the PWM configuration and enablement:

PWM-related macros:

The current version of the PWM driver treats each channel as a separate PWM device, with each device having only one channel (channel 0). Using the PWM5 device, note that channel 0 is selected here:

#define PWM_DEV_NAME        "pwm5"  /* PWM device name */
#define PWM_DEV_CHANNEL      0      /* PWM channel */
struct rt_device_pwm *pwm_dev;      /* PWM device handle */

Configure the PWM period and duty cycle:

static int pwm_sample(int argc, char *argv[])
{
    rt_uint32_t period, pulse, dir;
    period = 500000;    /* Period is 0.5 ms, in nanoseconds */
    dir = 1;            /* Direction of PWM pulse width increase/decrease */
    pulse = 100000;     /* PWM pulse width value, in nanoseconds */
    /* Find device */
    pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
    if (pwm_dev == RT_NULL)
    {
        rt_kprintf("pwm sample run failed! can't find %s device!\n", PWM_DEV_NAME);
        return RT_ERROR;
    }
    /* Set PWM period and default pulse width */
    rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
    /* Enable device */
    rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
}
/* Export to msh command list */
MSH_CMD_EXPORT(pwm_sample, pwm sample);

Compilation & Download

  • RT-Thread Studio: Download the EtherKit resource package in the RT-Thread Studio package manager, then create a new project and compile it.

  • IAR: First, double-click mklinks.bat to generate the links for rt-thread and libraries folders. Then use Env to generate the IAR project, and finally, double-click project.eww to open the IAR project and compile it.

After compilation, connect the development board's Jlink interface to the PC, and download the firmware to the development board.

Running Results

In the serial terminal, input pwm_sample and hwtimer_sample to see the specific results.

The callback function is triggered every 1 second, and the output is printed:

image-20241126102330524

Using a logic analyzer, the PWM output waveform is measured as follows:

image-20241126131735797