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 "stm32f4xx_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 #ifdef GSC_TARGET_SYSTEM_32F469IDISCOVERY
93  RCC_OscInitTypeDef Osc;
94 
95  /*##-1- Configue LSE as RTC clock soucre ###################################*/
96  Osc.OscillatorType = RCC_OSCILLATORTYPE_LSE;
97  Osc.LSEState = RCC_LSE_ON;
98  Osc.HSIState = RCC_HSI_OFF;
99  Osc.HSICalibrationValue = 0;
100  Osc.LSIState = RCC_LSI_OFF;
101  Osc.PLL.PLLState = RCC_PLL_NONE;
102  HAL_RCC_OscConfig(&Osc);
103 
104  __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSE);
105  __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);
106 #endif
107 
108  if(hrtc->Instance==RTC) {
109  __HAL_RCC_RTC_ENABLE();
110  }
111 }
112 
113 void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
114 {
115  /* NOTE : This function Should not be modified, when the callback is needed,
116  the HAL_RTC_AlarmAEventCallback could be implemented in the user file
117  */
118 }
119 
120 void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
121 {
122  /* NOTE : This function Should not be modified, when the callback is needed,
123  the HAL_RTCEx_AlarmBEventCallback could be implemented in the user file
124  */
125 }
126 
127 static int rtc_register(struct st_device *dev, char *param)
128 {
129  MX_RTC_Init();
130 
131  return 0;
132 }
133 
134 
135 static int rtc_ioctl(struct st_device *dev, unsigned int com, unsigned int arg, void *param)
136 {
137  struct st_datetime *tp = (struct st_datetime *)param;
138 
139  switch(com) {
140  case IOCMD_RTC_SET:
141  {
142  RTC_DateTypeDef sDate;
143  RTC_TimeTypeDef sTime;
144 
145  sDate.Year = tp->year - 2000;
146  sDate.Month = tp->month;
147  sDate.Date = tp->day;
148  sDate.WeekDay = tp->dayofweek;
149 
150  sTime.TimeFormat = RTC_HOURFORMAT12_AM;
151  sTime.Hours = tp->hour;
152  sTime.Minutes = tp->min;
153  sTime.Seconds = tp->sec;
154  sTime.SubSeconds = 256 - ((tp->msec * 256) / 1000);
155  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
156  sTime.StoreOperation = RTC_STOREOPERATION_RESET;
157 
158  HAL_RTC_SetDate(&hrtc, &sDate, FORMAT_BIN);
159  HAL_RTC_SetTime(&hrtc, &sTime, FORMAT_BIN);
160 
161  return 0;
162  }
163  break;
164 
165  case IOCMD_RTC_GET:
166  {
167  RTC_TimeTypeDef gTime;
168  RTC_DateTypeDef gDate;
169 
170  HAL_RTC_GetTime(&hrtc, &gTime, FORMAT_BIN);
171  HAL_RTC_GetDate(&hrtc, &gDate, FORMAT_BIN);
172 
173  DKPRINTF(0x01, "%04d-%02d-%02d ",
174  2000 + gDate.Year, gDate.Month, gDate.Date);
175  DKPRINTF(0x01, "%02d:%02d:%02d\n",
176  gTime.Hours, gTime.Minutes, gTime.Seconds);
177 
178  tp->year = gDate.Year + 2000;
179  tp->month = gDate.Month;
180  tp->day = gDate.Date;
181  tp->dayofweek = gDate.WeekDay;
182 
183  if(gTime.TimeFormat == RTC_HOURFORMAT12_AM) {
184  tp->hour = gTime.Hours;
185  } else {
186  tp->hour = gTime.Hours + 12;
187  }
188  tp->min = gTime.Minutes;
189  tp->sec = gTime.Seconds;
190  tp->msec = ((256 - gTime.SubSeconds) * 1000) / 256;
191 
192  return 0;
193  }
194  break;
195 
196  default:
197  SYSERR_PRINT("Unknown ioctl(%08lX)\n", com);
198  break;
199  }
200 
201  return -1;
202 }
203 
204 const struct st_device rtc_device = {
206  .explan = "STM32F4 RTC",
207  .register_dev = rtc_register,
208  .ioctl = rtc_ioctl,
209 };
時刻構造体
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