Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
rt-thread
提交
5ed2d0ea
R
rt-thread
项目概览
BaiXuePrincess
/
rt-thread
与 Fork 源项目一致
Fork自
RT-Thread / rt-thread
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rt-thread
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
5ed2d0ea
编写于
9月 29, 2015
作者:
F
FlyM
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[bsp]add stm32f4xx bsp rtc driver
上级
cfc72ec7
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
270 addition
and
0 deletion
+270
-0
bsp/stm32f40x/drivers/stm32f4_rtc.c
bsp/stm32f40x/drivers/stm32f4_rtc.c
+247
-0
bsp/stm32f40x/drivers/stm32f4_rtc.h
bsp/stm32f40x/drivers/stm32f4_rtc.h
+23
-0
未找到文件。
bsp/stm32f40x/drivers/stm32f4_rtc.c
0 → 100644
浏览文件 @
5ed2d0ea
/*
* File : stm32f4_rtc.c
* This file is stm32f4 rtc driver part of RT-Thread RTOS
* COPYRIGHT (C) 2009, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2015-09-29 FlyM the first version.
*/
#include <time.h>
#include <rtthread.h>
#include <stm32f4xx.h>
#include "stm32f4_rtc.h"
static
int
get_week
(
int
year
,
int
month
,
int
day
)
{
if
(
month
==
1
||
month
==
2
)
{
year
-=
1
;
month
+=
12
;
}
return
(
day
+
1
+
2
*
month
+
3
*
(
month
+
1
)
/
5
+
year
+
(
year
/
4
)
-
year
/
100
+
year
/
400
)
%
7
;
}
static
struct
rt_device
rtc
;
static
rt_err_t
rt_rtc_open
(
rt_device_t
dev
,
rt_uint16_t
oflag
)
{
if
(
dev
->
rx_indicate
!=
RT_NULL
)
{
/* Open Interrupt */
}
return
RT_EOK
;
}
static
rt_size_t
rt_rtc_read
(
rt_device_t
dev
,
rt_off_t
pos
,
void
*
buffer
,
rt_size_t
size
)
{
return
0
;
}
static
rt_err_t
rt_rtc_control
(
rt_device_t
dev
,
rt_uint8_t
cmd
,
void
*
args
)
{
rt_time_t
*
time
;
RT_ASSERT
(
dev
!=
RT_NULL
);
switch
(
cmd
)
{
case
RT_DEVICE_CTRL_RTC_GET_TIME
:
{
struct
tm
ct
;
RTC_TimeTypeDef
t
;
RTC_DateTypeDef
d
;
rt_memset
(
&
ct
,
0
,
sizeof
(
struct
tm
));
time
=
(
rt_time_t
*
)
args
;
/* read device */
RTC_GetDate
(
RTC_Format_BIN
,
&
d
);
RTC_GetTime
(
RTC_Format_BIN
,
&
t
);
ct
.
tm_year
=
d
.
RTC_Year
+
100
;
ct
.
tm_mon
=
d
.
RTC_Month
-
1
;
ct
.
tm_mday
=
d
.
RTC_Date
;
ct
.
tm_wday
=
d
.
RTC_WeekDay
;
ct
.
tm_hour
=
t
.
RTC_Hours
;
ct
.
tm_min
=
t
.
RTC_Minutes
;
ct
.
tm_sec
=
t
.
RTC_Seconds
;
*
time
=
mktime
(
&
ct
);
}
break
;
case
RT_DEVICE_CTRL_RTC_SET_TIME
:
{
struct
tm
*
ct
;
struct
tm
tm_new
;
time_t
*
time
;
RTC_TimeTypeDef
RTC_TimeStructure
;
RTC_InitTypeDef
RTC_InitStructure
;
RTC_DateTypeDef
RTC_DateStructure
;
time
=
(
time_t
*
)
args
;
/* lock scheduler. */
rt_enter_critical
();
/* converts calendar time time into local time. */
ct
=
localtime
(
time
);
/* copy the statically located variable */
rt_memcpy
(
&
tm_new
,
ct
,
sizeof
(
struct
tm
));
/* unlock scheduler. */
rt_exit_critical
();
/* Enable the PWR clock */
RCC_APB1PeriphClockCmd
(
RCC_APB1Periph_PWR
,
ENABLE
);
/* Allow access to RTC */
PWR_BackupAccessCmd
(
ENABLE
);
/* ck_spre(1Hz) = RTCCLK(LSI) /(uwAsynchPrediv + 1)*(uwSynchPrediv + 1)*/
//uwSynchPrediv = 0xFF;
//uwAsynchPrediv = 0x7F;
/* Configure the RTC data register and RTC prescaler */
RTC_InitStructure
.
RTC_AsynchPrediv
=
0x7F
;
RTC_InitStructure
.
RTC_SynchPrediv
=
0xFF
;
RTC_InitStructure
.
RTC_HourFormat
=
RTC_HourFormat_24
;
RTC_Init
(
&
RTC_InitStructure
);
/* Set the date */
RTC_DateStructure
.
RTC_Year
=
tm_new
.
tm_year
-
100
;
RTC_DateStructure
.
RTC_Month
=
tm_new
.
tm_mon
+
1
;
RTC_DateStructure
.
RTC_Date
=
tm_new
.
tm_mday
;
RTC_DateStructure
.
RTC_WeekDay
=
get_week
(
tm_new
.
tm_year
+
1900
,
tm_new
.
tm_mon
+
1
,
tm_new
.
tm_mday
);
RTC_SetDate
(
RTC_Format_BIN
,
&
RTC_DateStructure
);
/* Set the time*/
if
(
tm_new
.
tm_hour
>
11
)
{
RTC_TimeStructure
.
RTC_H12
=
RTC_H12_PM
;
}
else
{
RTC_TimeStructure
.
RTC_H12
=
RTC_H12_AM
;
}
RTC_TimeStructure
.
RTC_Hours
=
tm_new
.
tm_hour
;
RTC_TimeStructure
.
RTC_Minutes
=
tm_new
.
tm_min
;
RTC_TimeStructure
.
RTC_Seconds
=
tm_new
.
tm_sec
;
RTC_SetTime
(
RTC_Format_BIN
,
&
RTC_TimeStructure
);
/* Indicator for the RTC configuration */
RTC_WriteBackupRegister
(
RTC_BKP_DR0
,
0x32F2
);
}
break
;
}
return
RT_EOK
;
}
/*******************************************************************************
* Function Name : RTC_Configuration
* Description : Configures the RTC.
* Input : None
* Output : None
* Return : 0 reday,-1 error.
*******************************************************************************/
int
RTC_Configuration
(
void
)
{
/* Enable the PWR clock */
RCC_APB1PeriphClockCmd
(
RCC_APB1Periph_PWR
,
ENABLE
);
/* Allow access to RTC */
PWR_BackupAccessCmd
(
ENABLE
);
#if defined (RTC_CLOCK_SOURCE_LSI)
/* LSI used as RTC source clock*/
/* The RTC Clock may varies due to LSI frequency dispersion */
/* Enable the LSI OSC */
RCC_LSICmd
(
ENABLE
);
/* Wait till LSI is ready */
while
(
RCC_GetFlagStatus
(
RCC_FLAG_LSIRDY
)
==
RESET
)
{
}
/* Select the RTC Clock Source */
RCC_RTCCLKConfig
(
RCC_RTCCLKSource_LSI
);
/* ck_spre(1Hz) = RTCCLK(LSI) /(uwAsynchPrediv + 1)*(uwSynchPrediv + 1)*/
//uwSynchPrediv = 0xFF;
//uwAsynchPrediv = 0x7F;
#elif defined (RTC_CLOCK_SOURCE_LSE)
/* LSE used as RTC source clock */
/* Enable the LSE OSC */
RCC_LSEConfig
(
RCC_LSE_ON
);
/* Wait till LSE is ready */
while
(
RCC_GetFlagStatus
(
RCC_FLAG_LSERDY
)
==
RESET
)
{
}
/* Select the RTC Clock Source */
RCC_RTCCLKConfig
(
RCC_RTCCLKSource_LSE
);
#else
#error Please select the RTC Clock source inside the main.c file
#endif
/* RTC_CLOCK_SOURCE_LSI */
/* Enable the RTC Clock */
RCC_RTCCLKCmd
(
ENABLE
);
/* Wait for RTC APB registers synchronisation */
RTC_WaitForSynchro
();
return
0
;
}
void
rt_hw_rtc_init
(
void
)
{
rtc
.
type
=
RT_Device_Class_RTC
;
if
(
RTC_ReadBackupRegister
(
RTC_BKP_DR0
)
!=
0x32F2
)
{
rt_kprintf
(
"rtc is not configured
\n
"
);
rt_kprintf
(
"please configure with set_date and set_time
\n
"
);
if
(
RTC_Configuration
()
!=
0
)
{
rt_kprintf
(
"rtc configure fail...
\r\n
"
);
return
;
}
}
else
{
/* Wait for RTC registers synchronization */
RTC_WaitForSynchro
();
}
/* register rtc device */
rtc
.
init
=
RT_NULL
;
rtc
.
open
=
rt_rtc_open
;
rtc
.
close
=
RT_NULL
;
rtc
.
read
=
rt_rtc_read
;
rtc
.
write
=
RT_NULL
;
rtc
.
control
=
rt_rtc_control
;
/* no private */
rtc
.
user_data
=
RT_NULL
;
rt_device_register
(
&
rtc
,
"rtc"
,
RT_DEVICE_FLAG_RDWR
);
return
;
}
#ifdef RT_USING_FINSH
#include "finsh.h"
void
show_time
(
void
)
{
RTC_TimeTypeDef
t
;
RTC_DateTypeDef
d
;
/* Get the current Date */
RTC_GetDate
(
RTC_Format_BIN
,
&
d
);
/* Get the current Time */
RTC_GetTime
(
RTC_Format_BIN
,
&
t
);
/* Display time Format : hh:mm:ss */
rt_kprintf
(
"Now Time = 20%02d %02d %02d[%02d]-%0.2d:%0.2d:%0.2d
\r\n
"
,
\
d
.
RTC_Year
,
d
.
RTC_Month
,
d
.
RTC_Date
,
d
.
RTC_WeekDay
,
t
.
RTC_Hours
,
t
.
RTC_Minutes
,
t
.
RTC_Seconds
);
}
FINSH_FUNCTION_EXPORT
(
show_time
,
show
current
time
.)
#endif
bsp/stm32f40x/drivers/stm32f4_rtc.h
0 → 100644
浏览文件 @
5ed2d0ea
/*
* File : stm32f4_rtc.h
* This file is stm32f4 rtc driver part of RT-Thread RTOS
* COPYRIGHT (C) 2009, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2015-09-29 FlyM the first version
*/
#ifndef __STM32F4_RTC_H__
#define __STM32F4_RTC_H__
#define RTC_CLOCK_SOURCE_LSE
void
rt_hw_rtc_init
(
void
);
#endif
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录