modify set_date() in rtc.c

修改月份差一个月的BUG

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@250 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
coldfish.zhu 2009-12-28 12:06:00 +00:00
parent e8a5648cfe
commit fb44bdb9ba

View File

@ -1,231 +1,231 @@
/* /*
* File : rtc.c * File : rtc.c
* This file is part of RT-Thread RTOS * This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2009, RT-Thread Development Team * COPYRIGHT (C) 2009, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE * http://www.rt-thread.org/license/LICENSE
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
* 2009-01-05 Bernard the first version * 2009-01-05 Bernard the first version
*/ */
#include <rtthread.h> #include <rtthread.h>
#include <stm32f10x.h> #include <stm32f10x.h>
static struct rt_device rtc; static struct rt_device rtc;
static rt_err_t rt_rtc_open(rt_device_t dev, rt_uint16_t oflag) static rt_err_t rt_rtc_open(rt_device_t dev, rt_uint16_t oflag)
{ {
if (dev->rx_indicate != RT_NULL) if (dev->rx_indicate != RT_NULL)
{ {
/* Open Interrupt */ /* Open Interrupt */
} }
return RT_EOK; return RT_EOK;
} }
static rt_size_t rt_rtc_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size) static rt_size_t rt_rtc_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
{ {
return 0; return 0;
} }
static rt_err_t rt_rtc_control(rt_device_t dev, rt_uint8_t cmd, void *args) static rt_err_t rt_rtc_control(rt_device_t dev, rt_uint8_t cmd, void *args)
{ {
rt_time_t *time; rt_time_t *time;
RT_ASSERT(dev != RT_NULL); RT_ASSERT(dev != RT_NULL);
switch (cmd) switch (cmd)
{ {
case RT_DEVICE_CTRL_RTC_GET_TIME: case RT_DEVICE_CTRL_RTC_GET_TIME:
time = (rt_time_t *)args; time = (rt_time_t *)args;
/* read device */ /* read device */
*time = RTC_GetCounter(); *time = RTC_GetCounter();
break; break;
case RT_DEVICE_CTRL_RTC_SET_TIME: case RT_DEVICE_CTRL_RTC_SET_TIME:
{ {
time = (rt_time_t *)args; time = (rt_time_t *)args;
/* Enable PWR and BKP clocks */ /* Enable PWR and BKP clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
/* Allow access to BKP Domain */ /* Allow access to BKP Domain */
PWR_BackupAccessCmd(ENABLE); PWR_BackupAccessCmd(ENABLE);
/* Wait until last write operation on RTC registers has finished */ /* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask(); RTC_WaitForLastTask();
/* Change the current time */ /* Change the current time */
RTC_SetCounter(*time); RTC_SetCounter(*time);
/* Wait until last write operation on RTC registers has finished */ /* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask(); RTC_WaitForLastTask();
BKP_WriteBackupRegister(BKP_DR1, 0xA5A5); BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
} }
break; break;
} }
return RT_EOK; return RT_EOK;
} }
/******************************************************************************* /*******************************************************************************
* Function Name : RTC_Configuration * Function Name : RTC_Configuration
* Description : Configures the RTC. * Description : Configures the RTC.
* Input : None * Input : None
* Output : None * Output : None
* Return : None * Return : None
*******************************************************************************/ *******************************************************************************/
void RTC_Configuration(void) void RTC_Configuration(void)
{ {
/* Enable PWR and BKP clocks */ /* Enable PWR and BKP clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
/* Allow access to BKP Domain */ /* Allow access to BKP Domain */
PWR_BackupAccessCmd(ENABLE); PWR_BackupAccessCmd(ENABLE);
/* Reset Backup Domain */ /* Reset Backup Domain */
BKP_DeInit(); BKP_DeInit();
/* Enable LSE */ /* Enable LSE */
RCC_LSEConfig(RCC_LSE_ON); RCC_LSEConfig(RCC_LSE_ON);
/* Wait till LSE is ready */ /* Wait till LSE is ready */
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
{ {
} }
/* Select LSE as RTC Clock Source */ /* Select LSE as RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
/* Enable RTC Clock */ /* Enable RTC Clock */
RCC_RTCCLKCmd(ENABLE); RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC registers synchronization */ /* Wait for RTC registers synchronization */
RTC_WaitForSynchro(); RTC_WaitForSynchro();
/* Wait until last write operation on RTC registers has finished */ /* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask(); RTC_WaitForLastTask();
/* Set RTC prescaler: set RTC period to 1sec */ /* Set RTC prescaler: set RTC period to 1sec */
RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */ RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
/* Wait until last write operation on RTC registers has finished */ /* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask(); RTC_WaitForLastTask();
} }
void rt_hw_rtc_init(void) void rt_hw_rtc_init(void)
{ {
rtc.type = RT_Device_Class_RTC; rtc.type = RT_Device_Class_RTC;
if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5) if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
{ {
rt_kprintf("rtc is not configured\n"); rt_kprintf("rtc is not configured\n");
rt_kprintf("please configure with set_date and set_time\n"); rt_kprintf("please configure with set_date and set_time\n");
RTC_Configuration(); RTC_Configuration();
} }
else else
{ {
/* Wait for RTC registers synchronization */ /* Wait for RTC registers synchronization */
RTC_WaitForSynchro(); RTC_WaitForSynchro();
} }
/* register rtc device */ /* register rtc device */
rtc.init = RT_NULL; rtc.init = RT_NULL;
rtc.open = rt_rtc_open; rtc.open = rt_rtc_open;
rtc.close = RT_NULL; rtc.close = RT_NULL;
rtc.read = rt_rtc_read; rtc.read = rt_rtc_read;
rtc.write = RT_NULL; rtc.write = RT_NULL;
rtc.control = rt_rtc_control; rtc.control = rt_rtc_control;
/* no private */ /* no private */
rtc.private = RT_NULL; rtc.private = RT_NULL;
rt_device_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR); rt_device_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR);
return; return;
} }
#include <time.h> #include <time.h>
time_t time(time_t* t) time_t time(time_t* t)
{ {
rt_device_t device; rt_device_t device;
time_t time; time_t time;
device = rt_device_find("rtc"); device = rt_device_find("rtc");
if (device != RT_NULL) if (device != RT_NULL)
{ {
rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time); rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
if (t != RT_NULL) *t = time; if (t != RT_NULL) *t = time;
} }
return time; return time;
} }
#ifdef RT_USING_FINSH #ifdef RT_USING_FINSH
#include <finsh.h> #include <finsh.h>
void set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day) void set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day)
{ {
time_t now; time_t now;
struct tm* ti; struct tm* ti;
rt_device_t device; rt_device_t device;
ti = RT_NULL; ti = RT_NULL;
/* get current time */ /* get current time */
time(&now); time(&now);
ti = localtime(&now); ti = localtime(&now);
if (ti != RT_NULL) if (ti != RT_NULL)
{ {
ti->tm_year = year - 1900; ti->tm_year = year - 1900;
ti->tm_mon = month; ti->tm_mon = month - 1; /* 2009.12.27 modify by coldfish.zhu@gmail.com */
ti->tm_mday = day; ti->tm_mday = day;
} }
now = mktime(ti); now = mktime(ti);
device = rt_device_find("rtc"); device = rt_device_find("rtc");
if (device != RT_NULL) if (device != RT_NULL)
{ {
rt_rtc_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now); rt_rtc_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
} }
} }
FINSH_FUNCTION_EXPORT(set_date, set date) FINSH_FUNCTION_EXPORT(set_date, set date)
void set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second) void set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second)
{ {
time_t now; time_t now;
struct tm* ti; struct tm* ti;
rt_device_t device; rt_device_t device;
ti = RT_NULL; ti = RT_NULL;
/* get current time */ /* get current time */
time(&now); time(&now);
ti = localtime(&now); ti = localtime(&now);
if (ti != RT_NULL) if (ti != RT_NULL)
{ {
ti->tm_hour = hour; ti->tm_hour = hour;
ti->tm_min = minute; ti->tm_min = minute;
ti->tm_sec = second; ti->tm_sec = second;
} }
now = mktime(ti); now = mktime(ti);
device = rt_device_find("rtc"); device = rt_device_find("rtc");
if (device != RT_NULL) if (device != RT_NULL)
{ {
rt_rtc_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now); rt_rtc_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
} }
} }
FINSH_FUNCTION_EXPORT(set_time, set second) FINSH_FUNCTION_EXPORT(set_time, set second)
void list_date() void list_date()
{ {
time_t now; time_t now;
time(&now); time(&now);
rt_kprintf("%s\n", ctime(&now)); rt_kprintf("%s\n", ctime(&now));
} }
FINSH_FUNCTION_EXPORT(list_date, set date) FINSH_FUNCTION_EXPORT(list_date, set date)
#endif #endif