rtc.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /**
  2. * @author Tilen Majerle
  3. * @email tilen@majerle.eu
  4. * @website http://stm32f4-discovery.com
  5. * @link http://stm32f4-discovery.com/2014/07/library-19-use-internal-rtc-on-stm32f4xx-devices/
  6. * @version 1.7.1
  7. * @ide Keil uVision
  8. * @license GNU GPL v3
  9. * @brief Internal RTC library for STM32F4xx devices
  10. *
  11. @verbatim
  12. ----------------------------------------------------------------------
  13. Copyright (C) Tilen Majerle, 2015
  14. This program is free software: you can redistribute it and/or modify
  15. it under the terms of the GNU General Public License as published by
  16. the Free Software Foundation, either version 3 of the License, or
  17. any later version.
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22. You should have received a copy of the GNU General Public License
  23. along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. ----------------------------------------------------------------------
  25. @endverbatim
  26. */
  27. #ifndef TM_RTC_H
  28. #define TM_RTC_H 171
  29. /* C++ detection */
  30. #ifdef __cplusplus
  31. extern C {
  32. #endif
  33. /**
  34. * @addtogroup TM_STM32F4xx_Libraries
  35. * @{
  36. */
  37. /**
  38. * @defgroup TM_RTC
  39. * @brief RTC Library for STM32F4xx devices - http://stm32f4-discovery.com/2014/07/library-19-use-internal-rtc-on-stm32f4xx-devices/
  40. * @{
  41. *
  42. * \par Features
  43. *
  44. @verbatim
  45. - Support Internal or external clock source
  46. - PC14 and PC15 pins are used for external crystal oscillator
  47. - STM32F4/429 Discovery does not have RTC crystal onboard. Check board's manual on how to set it up
  48. - Support wakeup interrupt
  49. - Support to set 2 internal alarms to trigger interrupts
  50. - They can also wake up STM32F4 from any low power mode
  51. - Get seconds from 01.01.1970 00:00:00
  52. - Get readable time from seconds from 01.01.1970 00:00:00
  53. - Support to save/get data in binary or BCD format
  54. - Support for read/write data to/from RTC backup registers
  55. - Support for subsecond
  56. - Support to write data in string format
  57. - Date and time are checked before saved for valid input data
  58. - Get days in month and year
  59. @endverbatim
  60. *
  61. * \par Pinout for RTC external 32768Hz crystal
  62. *
  63. @verbatim
  64. STM32F4XX Oscillator Description
  65. PC14 OSC1 Oscillator terminal 1
  66. PC15 OSC2 Oscillator terminal 2
  67. @endverbatim
  68. *
  69. * \par Changelog
  70. *
  71. @verbatim
  72. Version 1.7.1
  73. - March 25, 2015
  74. - Fixed issue with RTC wakeup interrupts
  75. Version 1.7
  76. - March 15, 2015
  77. - Added support for read/write data to/from RTC backup registers
  78. Version 1.6
  79. - February 17, 2015
  80. - Created typedef TM_RTC_t from TM_RTC_Time_t
  81. Version 1.5
  82. - December 21, 2014
  83. - Added 2 new functions:
  84. TM_RTC_GetDaysInMonth: returns number of days in specific month and year
  85. TM_RTC_GetDaysInYear: returns number of days in specific year
  86. Version 1.4
  87. - December 21, 2014
  88. - TM_RTC_SetDateTime now checks for valid input data before save
  89. - Added function TM_RTC_SetDateTimeString which allows you to set your
  90. date and time using string format
  91. Version 1.3
  92. - December 03, 2014
  93. - Fixed bug when reading month in december was set to 0
  94. Version 1.2
  95. - October 27, 2014
  96. - Added support to read subseconds from time
  97. - This can be used for stopwatch
  98. Version 1.1
  99. - October 20, 2014
  100. - Added support for Alarm set
  101. Version 1.0.2
  102. - September 24, 2014
  103. - TM_RTC_RequestHandler function has now "__weak" attribute to prevent errors,
  104. if function is not implemented by user
  105. Version 1.0.1
  106. - September 01, 2014
  107. - Date to unix convert bug fix
  108. Version 1.0
  109. - First release
  110. @endverbatim
  111. *
  112. * \par Dependencies
  113. *
  114. @verbatim
  115. - STM32F4xx
  116. - STM32F4xx RCC
  117. - STM32F4xx RTC
  118. - STM32F4xx PWR
  119. - STM32F4xx EXTI
  120. - MISC
  121. - defines.h
  122. - attributes.h
  123. @endverbatim
  124. */
  125. #include "stm32f4xx.h"
  126. #include "attributes.h"
  127. /**
  128. * @defgroup TM_RTC_Macros
  129. * @brief Library defines
  130. *
  131. * All these settings can be overwritten in defines.h file if necessary
  132. *
  133. * @{
  134. */
  135. /* RTC clock is: f_clk = RTCCLK(LSI or LSE) / ((RTC_SYNC_PREDIV + 1) * (RTC_ASYNC_PREDIV + 1)) */
  136. /* Sync pre division for clock */
  137. #ifndef RTC_SYNC_PREDIV
  138. #define RTC_SYNC_PREDIV 0x3FF
  139. #endif
  140. /* Async pre division for clock */
  141. #ifndef RTC_ASYNC_PREDIV
  142. #define RTC_ASYNC_PREDIV 0x1F
  143. #endif
  144. /* NVIC global Priority set */
  145. #ifndef RTC_PRIORITY
  146. #define RTC_PRIORITY 0x04
  147. #endif
  148. /* Sub priority for wakeup trigger */
  149. #ifndef RTC_WAKEUP_SUBPRIORITY
  150. #define RTC_WAKEUP_SUBPRIORITY 0x00
  151. #endif
  152. /* Sub priority for alarm trigger */
  153. #ifndef RTC_ALARM_SUBPRIORITY
  154. #define RTC_ALARM_SUBPRIORITY 0x01
  155. #endif
  156. /**
  157. * @}
  158. */
  159. /**
  160. * @defgroup TM_RTC_Typedefs
  161. * @brief Library Typedefs
  162. * @{
  163. */
  164. /**
  165. * @brief RTC Struct for date/time
  166. */
  167. typedef struct {
  168. uint8_t seconds; /*!< Seconds parameter, from 00 to 59 */
  169. uint16_t subseconds; /*!< Subsecond downcounter. When it reaches zero, it's reload value is the same as
  170. @ref RTC_SYNC_PREDIV, so in our case 0x3FF = 1023, 1024 steps in one second */
  171. uint8_t minutes; /*!< Minutes parameter, from 00 to 59 */
  172. uint8_t hours; /*!< Hours parameter, 24Hour mode, 00 to 23 */
  173. uint8_t day; /*!< Day in a week, from 1 to 7 */
  174. uint8_t date; /*!< Date in a month, 1 to 31 */
  175. uint8_t month; /*!< Month in a year, 1 to 12 */
  176. uint8_t year; /*!< Year parameter, 00 to 99, 00 is 2000 and 99 is 2099 */
  177. uint32_t unix; /*!< Seconds from 01.01.1970 00:00:00 */
  178. } TM_RTC_t;
  179. /**
  180. * @brief Backward compatibility for RTC time
  181. */
  182. typedef TM_RTC_t TM_RTC_Time_t;
  183. /**
  184. * @brief RTC Result enumeration
  185. */
  186. typedef enum {
  187. TM_RTC_Result_Ok, /*!< Everything OK */
  188. TM_RTC_Result_Error /*!< An error occurred */
  189. } TM_RTC_Result_t;
  190. /**
  191. * @brief RTC date and time format
  192. */
  193. typedef enum {
  194. TM_RTC_Format_BIN = 0x00, /*!< RTC data in binary format */
  195. TM_RTC_Format_BCD /*!< RTC data in binary-coded decimal format */
  196. } TM_RTC_Format_t;
  197. /**
  198. * @brief RTC Interrupt enumeration
  199. */
  200. typedef enum {
  201. TM_RTC_Int_Disable = 0x00, /*!< Disable RTC wakeup interrupts */
  202. TM_RTC_Int_60s, /*!< RTC Wakeup interrupt every 60 seconds */
  203. TM_RTC_Int_30s, /*!< RTC Wakeup interrupt every 30 seconds */
  204. TM_RTC_Int_15s, /*!< RTC Wakeup interrupt every 15 seconds */
  205. TM_RTC_Int_10s, /*!< RTC Wakeup interrupt every 10 seconds */
  206. TM_RTC_Int_5s, /*!< RTC Wakeup interrupt every 5 seconds */
  207. TM_RTC_Int_2s, /*!< RTC Wakeup interrupt every 2 seconds */
  208. TM_RTC_Int_1s, /*!< RTC Wakeup interrupt every 1 seconds */
  209. TM_RTC_Int_500ms, /*!< RTC Wakeup interrupt every 500 milliseconds */
  210. TM_RTC_Int_250ms, /*!< RTC Wakeup interrupt every 250 milliseconds */
  211. TM_RTC_Int_125ms /*!< RTC Wakeup interrupt every 125 milliseconds */
  212. } TM_RTC_Int_t;
  213. /**
  214. * @brief Select RTC clock source
  215. * @note Internal clock is not accurate and should not be used in production
  216. */
  217. typedef enum {
  218. TM_RTC_ClockSource_Internal = 0x00, /*!< Use internal clock source for RTC (LSI oscillator) */
  219. TM_RTC_ClockSource_External /*!< Use external clock source for RTC (LSE oscillator) */
  220. } TM_RTC_ClockSource_t;
  221. /**
  222. * @brief RTC Alarm type
  223. */
  224. typedef enum {
  225. TM_RTC_AlarmType_DayInWeek, /*!< Trigger alarm every day in a week, days from 1 to 7 (Monday to Sunday) */
  226. TM_RTC_AlarmType_DayInMonth /*!< Trigger alarm every month */
  227. } TM_RTC_AlarmType_t;
  228. /**
  229. * @brief Alarm identifier you will use for Alarm functions
  230. */
  231. typedef enum {
  232. TM_RTC_Alarm_A = 0x00, /*!< Work with alarm A */
  233. TM_RTC_Alarm_B /*!< Work with alarm B */
  234. } TM_RTC_Alarm_t;
  235. /**
  236. * @brief RTC structure for alarm time
  237. */
  238. typedef struct {
  239. TM_RTC_AlarmType_t alarmtype; /*!< Alarm type setting. @ref TM_RTC_AlarmType_t for more info */
  240. uint8_t seconds; /*!< Alarm seconds value */
  241. uint8_t minutes; /*!< Alarm minutes value */
  242. uint8_t hours; /*!< Alarm hours value */
  243. uint8_t day; /*!< Alarm day value. If you select trigger for alarm every week, then this parameter has value between
  244. 1 and 7, representing days in a week, Monday to Sunday
  245. If you select trigger for alarm every month, then this parameter has value between
  246. 1 - 31, representing days in a month. */
  247. } TM_RTC_AlarmTime_t;
  248. /**
  249. * @}
  250. */
  251. /**
  252. * @defgroup TM_RTC_Functions
  253. * @brief Library Functions
  254. * @{
  255. */
  256. void TM_RTC_SetDataTimeUnix(uint32_t unixTime);
  257. /**
  258. * @brief Кореектировака времени.
  259. */
  260. void TM_RTC_Correction(float utc);
  261. /**
  262. * @brief Initializes RTC and starts counting
  263. * @param source. RTC Clock source @ref TM_RTC_ClockSource_t to be used for RTC
  264. * @note Internal clock source is not so accurate
  265. * @note If you reset your MCU and RTC still has power, it will count independent of MCU status
  266. * @retval Returns RTC status.
  267. * - 1: RTC has already been initialized and time is set
  268. * - 0: RTC was now initialized first time. Now you can set your first clock
  269. */
  270. uint32_t TM_RTC_Init(TM_RTC_ClockSource_t source);
  271. /**
  272. * @brief Get number of seconds from date and time since 01.01.1970 00:00:00
  273. * @param *data: Pointer to @ref TM_RTC_t data structure
  274. * @retval Calculated seconds from date and time since 01.01.1970 00:00:00
  275. */
  276. uint32_t TM_RTC_GetUnixTimeStamp(TM_RTC_t* data);
  277. /**
  278. * @brief Get formatted time from seconds till 01.01.1970 00:00:00
  279. * It fills struct with valid data
  280. * @note Valid if year is greater or equal (>=) than 2000
  281. * @param *data: Pointer to @ref TM_RTC_Time_t struct to store formatted data in
  282. * @param unix: Seconds from 01.01.1970 00:00:00 to calculate user friendly time
  283. * @retval None
  284. */
  285. void TM_RTC_GetDateTimeFromUnix(TM_RTC_t* data, uint32_t unix);
  286. /**
  287. * @brief Select RTC wakeup interrupts interval
  288. * @note This function can also be used to disable interrupt
  289. * @param int_value: Look for @ref TM_RTC_Int_t for valid inputs
  290. * @retval None
  291. */
  292. void TM_RTC_Interrupts(TM_RTC_Int_t int_value);
  293. /**
  294. * @brief Set date and time to internal RTC registers
  295. * @param *data: Pointer to filled @ref TM_RTC_t structure with date and time
  296. * @param format: Format of your structure. This parameter can be a value of @ref TM_RTC_Format_t enumeration
  297. * @retval RTC datetime status @ref TM_RTC_Result_t:
  298. * - @ref TM_RTC_Result_Ok: Date and Time set OK
  299. * - @ref TM_RTC_Result_Error: Date and time is wrong
  300. */
  301. TM_RTC_Result_t TM_RTC_SetDateTime(TM_RTC_t* data, TM_RTC_Format_t format);
  302. /**
  303. * @brief Set date and time using string formatted date time
  304. * @note Valid string format is: <b>dd.mm.YY.x;HH:ii:ss</b>
  305. * - <b>dd</b>: date, 2 digits, decimal
  306. * - <b>mm</b>: month, 2 digits, decimal
  307. * - <b>YY</b>: year, last 2 digits, decimal
  308. * - <b>x</b>: day in a week: 1 digit, 1 = Monday, 7 = Sunday
  309. * - <b>HH</b>: hours, 24-hour mode, 2 digits, decimal
  310. * - <b>ii</b>: minutes, 2 digits, decimal
  311. * - <b>ss</b>: seconds, 2 digits, decimal
  312. * @param *str: Pointer to string with datetime format
  313. * @retval RTC datetime status @ref TM_RTC_Result_t:
  314. * - @ref TM_RTC_Result_Ok: Date and Time set OK
  315. * - @ref TM_RTC_Result_Error: Date and time is wrong
  316. */
  317. TM_RTC_Result_t TM_RTC_SetDateTimeString(char* str);
  318. /**
  319. * @brief Get date and time from internal RTC registers
  320. * @param *data: Pointer to @ref TM_RTC_t structure to save data to
  321. * @param format: Format of your structure. This parameter can be a value of @ref TM_RTC_Format_t enumeration
  322. * @retval None
  323. */
  324. void TM_RTC_GetDateTime(TM_RTC_t* data, TM_RTC_Format_t format);
  325. /**
  326. * @brief Get number of days in month
  327. * @note This function also detects if it is leap year and returns different number for February
  328. * @param month: Month number in year, valid numbers are 1 - 12
  329. * @param year: Year number where you want to get days in month, last 2 digits
  330. * @retval Number of days in specific month and year
  331. */
  332. uint8_t TM_RTC_GetDaysInMonth(uint8_t month, uint8_t year);
  333. /**
  334. * @brief Get number of days in specific year
  335. * @note This function also detects if it is leap year
  336. * @param year: Year number where you want to get days in month, last 2 digits
  337. * @retval Number of days in year
  338. */
  339. uint16_t TM_RTC_GetDaysInYear(uint8_t year);
  340. /**
  341. * @brief Write RTC backup register value.
  342. * This method allows you to write 32bit value from backup register 0 - 18.
  343. * @note RTC has 20 backup registers where you can store data which will be available all the time RTC is running and has power.
  344. *
  345. * @note My library uses register 19 to store info about RTC settings and is not available for USER to store data there.
  346. *
  347. * @note RTC HAS to be initialized first before you can use this method.
  348. * @param location: RTC backup register location. 0 to 18 are valid
  349. * @param value: 32-bit long value to be stored in RTC backup register
  350. * @retval Value at specific RTC backup register location
  351. */
  352. void TM_RTC_WriteBackupRegister(uint8_t location, uint32_t value);
  353. /**
  354. * @brief Read RTC backup register value.
  355. * This method allows you to read 32bit value from backup register 0 - 18.
  356. * @note RTC has 20 backup registers where you can store data which will be available all the time RTC is running and has power.
  357. *
  358. * @note My library uses register 19 to store info about RTC settings and is not available for USER to store data there.
  359. *
  360. * @note RTC HAS to be initialized first before you can use this method.
  361. * @param location: RTC backup register location. 0 to 18 are valid
  362. * @retval Value at specific RTC backup register location
  363. */
  364. uint32_t TM_RTC_ReadBackupRegister(uint8_t location);
  365. /**
  366. * @brief Enables alarm A or alarm B
  367. * @param Alarm: Specify which alarm to set. This parameter can be a value of @ref TM_RTC_Alarm_t enumeration
  368. * @param *AlarmTime: Pointer to @ref TM_RTC_AlarmTime_t structure to get data from.
  369. * @param format: RTC date and time format. This parameter can be a value of @ref TM_RTC_Format_t enumeration.
  370. * @retval None
  371. */
  372. void TM_RTC_SetAlarm(TM_RTC_Alarm_t Alarm, TM_RTC_AlarmTime_t* AlarmTime, TM_RTC_Format_t format);
  373. /**
  374. * @brief Disables specific alarm
  375. * @param Alarm: Select which alarm you want to disable. This parameter can be a value of @ref TM_RTC_Alarm_t enumeration
  376. * @retval None
  377. */
  378. void TM_RTC_DisableAlarm(TM_RTC_Alarm_t Alarm);
  379. /**
  380. * @brief RTC Wakeup handler function. Called when wakeup interrupt is triggered
  381. * @note Called from my RTC library
  382. * @param None
  383. * @retval None
  384. * @note With __weak parameter to prevent link errors if not defined by user
  385. */
  386. void TM_RTC_RequestHandler(void);
  387. /**
  388. * @brief RTC Alarm A handler function. Called when interrupt is triggered for alarm A
  389. * @note Called from my RTC library
  390. * @param None
  391. * @retval None
  392. * @note With __weak parameter to prevent link errors if not defined by user
  393. */
  394. void TM_RTC_AlarmAHandler(void);
  395. /**
  396. * @brief RTC Alarm B handler function. Called when interrupt is triggered for alarm B.
  397. * @note Called from my RTC library
  398. * @param None
  399. * @retval None
  400. * @note With __weak parameter to prevent link errors if not defined by user
  401. */
  402. void TM_RTC_AlarmBHandler(void);
  403. /**
  404. * @brief У�тановливает врем� �рабатывани� профилактики �ульфатации дл� отладки
  405. * @retval
  406. */
  407. void RTC_SetDebugProfTime(void);
  408. /**
  409. * @brief Óñòàíîâëèâàåò âðåìÿ ñðàáàòûâàíèÿ ïðîôèëàêòèêè ñóëüôàòàöèè
  410. */
  411. void RTC_SetProfTime(char *date);
  412. /**
  413. * @brief
  414. * @retval
  415. */
  416. uint32_t RTC_GetUnixTime(void);
  417. /* C++ detection */
  418. #ifdef __cplusplus
  419. }
  420. #endif
  421. #endif