rt-thread-official/examples/test/hwtimer_test.c

103 lines
2.5 KiB
C
Raw Normal View History

2015-09-02 22:03:16 +08:00
#include <rtthread.h>
#include <rtdevice.h>
#include <finsh.h>
#ifdef RT_USING_HWTIMER
#define TIMER "timer0"
static rt_err_t timer_timeout_cb(rt_device_t dev, rt_size_t size)
{
2018-12-12 16:59:02 +08:00
rt_kprintf("enter hardware timer isr\n");
2015-09-02 22:03:16 +08:00
return 0;
}
2015-09-06 15:05:47 +08:00
int hwtimer(void)
2015-09-02 22:03:16 +08:00
{
rt_err_t err;
rt_hwtimerval_t val;
rt_device_t dev = RT_NULL;
rt_tick_t tick;
rt_hwtimer_mode_t mode;
2015-09-06 15:05:47 +08:00
int freq = 10000;
int t = 5;
2015-09-02 22:03:16 +08:00
if ((dev = rt_device_find(TIMER)) == RT_NULL)
{
rt_kprintf("No Device: %s\n", TIMER);
return -1;
}
if (rt_device_open(dev, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
{
rt_kprintf("Open %s Fail\n", TIMER);
2015-09-06 15:05:47 +08:00
return -1;
2015-09-02 22:03:16 +08:00
}
2018-12-12 16:59:02 +08:00
/* ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
2015-09-02 22:03:16 +08:00
/* <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><><C4AC>1Mhz<68><7A>֧<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5>) */
err = rt_device_control(dev, HWTIMER_CTRL_FREQ_SET, &freq);
if (err != RT_EOK)
{
rt_kprintf("Set Freq=%dhz Fail\n", freq);
goto EXIT;
}
2015-09-06 15:05:47 +08:00
/* <20><><EFBFBD><EFBFBD>ģʽ */
mode = HWTIMER_MODE_PERIOD;
err = rt_device_control(dev, HWTIMER_CTRL_MODE_SET, &mode);
tick = rt_tick_get();
rt_kprintf("Start Timer> Tick: %d\n", tick);
/* <20><><EFBFBD>ö<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʱֵ<CAB1><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1> */
2015-09-02 22:03:16 +08:00
val.sec = t;
val.usec = 0;
rt_kprintf("SetTime: Sec %d, Usec %d\n", val.sec, val.usec);
if (rt_device_write(dev, 0, &val, sizeof(val)) != sizeof(val))
{
rt_kprintf("SetTime Fail\n");
goto EXIT;
}
rt_kprintf("Sleep %d sec\n", t);
rt_thread_delay(t*RT_TICK_PER_SECOND);
/* ֹͣ<CDA3><D6B9>ʱ<EFBFBD><CAB1> */
err = rt_device_control(dev, HWTIMER_CTRL_STOP, RT_NULL);
rt_kprintf("Timer Stoped\n");
/* <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD> */
rt_device_read(dev, 0, &val, sizeof(val));
rt_kprintf("Read: Sec = %d, Usec = %d\n", val.sec, val.usec);
2018-12-12 16:59:02 +08:00
/* <20><>ʱִ<CAB1>лص<D0BB><D8B5><EFBFBD><EFBFBD><EFBFBD> -- <20><><EFBFBD><EFBFBD>ģʽ */
/* <20><><EFBFBD>ó<EFBFBD>ʱ<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD> */
rt_device_set_rx_indicate(dev, timer_timeout_cb);
/* <20><><EFBFBD><EFBFBD>ģʽ */
mode = HWTIMER_MODE_PERIOD;
err = rt_device_control(dev, HWTIMER_CTRL_MODE_SET, &mode);
/* <20><><EFBFBD>ö<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʱֵ<CAB1><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1> */
val.sec = t;
val.usec = 0;
rt_kprintf("SetTime: Sec %d, Usec %d\n", val.sec, val.usec);
if (rt_device_write(dev, 0, &val, sizeof(val)) != sizeof(val))
{
rt_kprintf("SetTime Fail\n");
goto EXIT;
}
/* <20>ȴ<EFBFBD><C8B4>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><D6B4> */
rt_thread_delay((t + 1)*RT_TICK_PER_SECOND);
2015-09-02 22:03:16 +08:00
EXIT:
err = rt_device_close(dev);
rt_kprintf("Close %s\n", TIMER);
return err;
}
2018-12-12 16:59:02 +08:00
#ifdef FINSH_USING_MSH
MSH_CMD_EXPORT(hwtimer, "Test hardware timer");
2015-09-02 22:03:16 +08:00
#endif
2018-12-12 16:59:02 +08:00
#endif /* RT_USING_HWTIMER */