为了达到节能状态,采用RTCalarm唤醒睡眠。
static rt_err_t _rtc_init(void)
{
#ifdef BSP_RTC_USING_LSE
Cy_RTC_SelectClockSource(CY_RTC_CLK_SELECT_WCO);
#else
Cy_RTC_SelectClockSource(CY_RTC_CLK_SELECT_ILO);
#endif
if (cyhal_rtc_init(&rtc_obj) != RT_EOK)
{
LOG_E("rtc init failed.");
return -RT_ERROR;
}
return RT_EOK;
}
static struct rt_alarm * p_alarm_hour = RT_NULL;
/* 闹钟的回调函数,多个闹钟,可以有多个回调函数,用于创建闹钟时注册。闹钟时间到了,会执行此函数 */
static void alarm_hour_cb(rt_alarm_t alarm, time_t timestamp)
{
LOG_D("alarm_hour_cb ok!
");
}
static void rtc_alarm_hour_create(void)
{
static time_t now;
struct tm *p_tm;
struct rt_alarm_setup alarm_setup_test;
if (p_alarm_hour != RT_NULL)
return;
now = time(NULL);
p_tm = localtime(&now);
alarm_setup_test.flag = RT_ALARM_SECOND;
alarm_setup_test.wktime.tm_year = p_tm- >tm_year;
alarm_setup_test.wktime.tm_mon = p_tm- >tm_mon;
alarm_setup_test.wktime.tm_mday = p_tm- >tm_mday;
alarm_setup_test.wktime.tm_wday = p_tm- >tm_wday;
alarm_setup_test.wktime.tm_hour = p_tm- >tm_hour + 1;
alarm_setup_test.wktime.tm_min = p_tm- >tm_min;
alarm_setup_test.wktime.tm_sec = p_tm- >tm_sec;
p_alarm_hour = rt_alarm_create(alarm_hour_cb, &alarm_setup_test);
}
static void rtc_alarm_hour_start(void)
{
if (p_alarm_hour != RT_NULL)
rt_alarm_start(p_alarm_hour);
}
static void rtc_alarm_hour_stop(void)
{
if (p_alarm_hour != RT_NULL)
rt_alarm_stop(p_alarm_hour);
}
static void rtc_alarm_hour_delete(void)
{
if (p_alarm_hour != RT_NULL)
{
if (rt_alarm_delete(p_alarm_hour) == RT_EOK)
p_alarm_hour = RT_NULL;
}
}
static int rt_hw_rtc_init(void)
{
rt_err_t result = RT_EOK;
ifx32_rtc_dev.ops = &_rtc_ops;
if (rt_hw_rtc_register(&ifx32_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL) != RT_EOK)
{
LOG_E("rtc init failed");
result = -RT_ERROR;
}
else
{
LOG_D("rtc init success");
}
/* 设置ALARM*/
rtc_alarm_hour_create();
rtc_alarm_hour_start();
return result;
}
#include
#include
#include "drv_gpio.h"
#define LED_PIN GET_PIN(0, 0)
#define USE_SECONDS_FOR_ALARM 10
#define LONG_GLITCH_DELAY_MS 100u /* in ms */
#define RTC_INTERRUPT_PRIORITY 3u
void set_rtc_alarm_date_time(void);
cyhal_rtc_t rtc_obj;
int main(void)
{
cy_rslt_t result;
__enable_irq();
rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT);
rt_thread_mdelay(500);
/* Initialize RTC */
cyhal_rtc_enable_event(&rtc_obj, CYHAL_RTC_ALARM, RTC_INTERRUPT_PRIORITY, true);
for (;;)
{
rt_kprintf("Go to DeepSleep mode
");
/* Set the RTC generate alarm after 10 seconds */
set_rtc_alarm_date_time();
cyhal_system_delay_ms(LONG_GLITCH_DELAY_MS);
/* Go to deep sleep */
cyhal_syspm_deepsleep();
rt_kprintf("Wakeup from DeepSleep mode
");
}
}
/*******************************************************************************/
void set_rtc_alarm_date_time(void)
{
cy_rslt_t result;
/* Print the RTC alarm time by UART */
//LOG_D("RTC alarm will be generated after 10 seconds
");
/* Set the RTC alarm for the specified number of seconds in the future by editing the macro(USE_SECONDS_FOR_ALARM) */
result = cyhal_rtc_set_alarm_by_seconds(&rtc_obj, USE_SECONDS_FOR_ALARM);
if (result != CY_RSLT_SUCCESS)
{
rt_kprintf("RTC alarm err
");
}
}
下载到开发板后,打开串口终端,显示系统进入睡眠,然后在10秒钟后唤醒
:
全部0条评论
快来发表一下你的评论吧 !