GadgetSeed  0.9.6
rtc.c
[詳解]
1 /** @file
2  @brief STM32 RTC
3 
4  @date 2015.08.12
5  @author Takashi SHUDO
6 */
7 
8 #include "device.h"
9 #include "device/rtc_ioctl.h"
10 #include "datetime.h"
11 #include "tkprintf.h"
12 #include "system.h"
13 
14 #include "stm32f7xx_hal.h"
15 
16 //#define DEBUGKBITS 0x03
17 #include "dkprintf.h"
18 
19 
20 RTC_HandleTypeDef hrtc;
21 
22 /* RTC init function */
23 void MX_RTC_Init(void)
24 {
25 #if 0
26  RTC_TimeTypeDef sTime;
27  RTC_DateTypeDef sDate;
28  RTC_AlarmTypeDef sAlarm;
29 #endif
30  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
31 
32  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
33  PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
34  HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
35 
36  /**Initialize RTC and set the Time and Date
37  */
38  hrtc.Instance = RTC;
39  hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
40  hrtc.Init.AsynchPrediv = 127;
41  hrtc.Init.SynchPrediv = 255;
42  hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
43  hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
44  hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
45  HAL_RTC_Init(&hrtc);
46 
47 #if 0
48  sTime.Hours = 0;
49  sTime.Minutes = 0;
50  sTime.Seconds = 0;
51  sTime.SubSeconds = 0;
52  sTime.TimeFormat = RTC_HOURFORMAT12_AM;
53  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
54  sTime.StoreOperation = RTC_STOREOPERATION_RESET;
55  HAL_RTC_SetTime(&hrtc, &sTime, FORMAT_BCD);
56 
57  sDate.WeekDay = RTC_WEEKDAY_MONDAY;
58  sDate.Month = RTC_MONTH_JANUARY;
59  sDate.Date = 1;
60  sDate.Year = 0;
61  HAL_RTC_SetDate(&hrtc, &sDate, FORMAT_BCD);
62 
63  /**Enable the Alarm A
64  */
65  sAlarm.AlarmTime.Hours = 0;
66  sAlarm.AlarmTime.Minutes = 0;
67  sAlarm.AlarmTime.Seconds = 0;
68  sAlarm.AlarmTime.SubSeconds = 0;
69  sAlarm.AlarmTime.TimeFormat = RTC_HOURFORMAT12_AM;
70  sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
71  sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
72  sAlarm.AlarmMask = RTC_ALARMMASK_NONE;
73  sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
74  sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
75  sAlarm.AlarmDateWeekDay = 1;
76  sAlarm.Alarm = RTC_ALARM_A;
77  HAL_RTC_SetAlarm(&hrtc, &sAlarm, FORMAT_BCD);
78 
79  /**Enable the Alarm B
80  */
81  sAlarm.Alarm = RTC_ALARM_B;
82  HAL_RTC_SetAlarm(&hrtc, &sAlarm, FORMAT_BCD);
83 
84  /**Enable the WakeUp
85  */
86  HAL_RTCEx_SetWakeUpTimer(&hrtc, 0, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
87 #endif
88 }
89 
90 void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
91 {
92  if(hrtc->Instance==RTC) {
93  __HAL_RCC_RTC_ENABLE();
94  }
95 }
96 
97 void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
98 {
99  /* NOTE : This function Should not be modified, when the callback is needed,
100  the HAL_RTC_AlarmAEventCallback could be implemented in the user file
101  */
102 }
103 
104 void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
105 {
106  /* NOTE : This function Should not be modified, when the callback is needed,
107  the HAL_RTCEx_AlarmBEventCallback could be implemented in the user file
108  */
109 }
110 
111 static int rtc_register(struct st_device *dev, char *param)
112 {
113  MX_RTC_Init();
114 
115  return 0;
116 }
117 
118 
119 static int rtc_ioctl(struct st_device *dev, unsigned int com, unsigned int arg, void *param)
120 {
121  struct st_datetime *tp = (struct st_datetime *)param;
122 
123  switch(com) {
124  case IOCMD_RTC_SET:
125  {
126  RTC_DateTypeDef sDate;
127  RTC_TimeTypeDef sTime;
128 
129  sDate.Year = tp->year - 2000;
130  sDate.Month = tp->month;
131  sDate.Date = tp->day;
132  sDate.WeekDay = tp->dayofweek;
133 
134  sTime.TimeFormat = RTC_HOURFORMAT12_AM;
135  sTime.Hours = tp->hour;
136  sTime.Minutes = tp->min;
137  sTime.Seconds = tp->sec;
138  sTime.SubSeconds = 256 - ((tp->msec * 256) / 1000);
139  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
140  sTime.StoreOperation = RTC_STOREOPERATION_RESET;
141 
142  HAL_RTC_SetDate(&hrtc, &sDate, FORMAT_BIN);
143  HAL_RTC_SetTime(&hrtc, &sTime, FORMAT_BIN);
144 
145  return 0;
146  }
147  break;
148 
149  case IOCMD_RTC_GET:
150  {
151  RTC_TimeTypeDef gTime;
152  RTC_DateTypeDef gDate;
153 
154  HAL_RTC_GetTime(&hrtc, &gTime, FORMAT_BIN);
155  HAL_RTC_GetDate(&hrtc, &gDate, FORMAT_BIN);
156 
157  DKPRINTF(0x01, "%04d-%02d-%02d ",
158  2000 + gDate.Year, gDate.Month, gDate.Date);
159  DKPRINTF(0x01, "%02d:%02d:%02d\n",
160  gTime.Hours, gTime.Minutes, gTime.Seconds);
161 
162  tp->year = gDate.Year + 2000;
163  tp->month = gDate.Month;
164  tp->day = gDate.Date;
165  tp->dayofweek = gDate.WeekDay;
166 
167  if(gTime.TimeFormat == RTC_HOURFORMAT12_AM) {
168  tp->hour = gTime.Hours;
169  } else {
170  tp->hour = gTime.Hours + 12;
171  }
172  tp->min = gTime.Minutes;
173  tp->sec = gTime.Seconds;
174  tp->msec = ((256 - gTime.SubSeconds) * 1000) / 256;
175 
176  return 0;
177  }
178  break;
179 
180  default:
181  SYSERR_PRINT("Unknown ioctl(%08lX)\n", com);
182  break;
183  }
184 
185  return -1;
186 }
187 
188 const struct st_device rtc_device = {
190  .explan = "STM32F7 RTC",
191  .register_dev = rtc_register,
192  .ioctl = rtc_ioctl,
193 };
時刻構造体
Definition: datetime.h:29
RTCドライバ ioctl 用マクロ定義
short msec
ミリ秒
Definition: datetime.h:37
char hour
Definition: datetime.h:34
void MX_RTC_Init(void)
Definition: rtc.c:23
char min
Definition: datetime.h:35
#define IOCMD_RTC_GET
時刻を取得する
Definition: rtc_ioctl.h:18
char month
Definition: datetime.h:31
char sec
Definition: datetime.h:36
日付時刻
カーネル用機能限定printf
#define IOCMD_RTC_SET
時刻を設定する
Definition: rtc_ioctl.h:17
char day
Definition: datetime.h:32
char dayofweek
曜日 0:日曜日〜6:土曜日
Definition: datetime.h:33
#define DEF_DEV_NAME_RTC
標準リアルタイムクロックデバイス名
Definition: rtc_ioctl.h:15
システム固有初期化関連
デバイスドライバAPI
デバイスドライバ構造体
Definition: device.h:25
カーネル、ドライバ(非タスク)デバッグ用マクロ
char name[MAX_DEVNAMELRN]
デバイス名文字列
Definition: device.h:26
short year
Definition: datetime.h:30