rtc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdio.h>
  27. #include "py/runtime.h"
  28. #include "rtc.h"
  29. #include "irq.h"
  30. /// \moduleref pyb
  31. /// \class RTC - real time clock
  32. ///
  33. /// The RTC is and independent clock that keeps track of the date
  34. /// and time.
  35. ///
  36. /// Example usage:
  37. ///
  38. /// rtc = pyb.RTC()
  39. /// rtc.datetime((2014, 5, 1, 4, 13, 0, 0, 0))
  40. /// print(rtc.datetime())
  41. RTC_HandleTypeDef RTCHandle;
  42. // rtc_info indicates various things about RTC startup
  43. // it's a bit of a hack at the moment
  44. static mp_uint_t rtc_info;
  45. // Note: LSI is around (32KHz), these dividers should work either way
  46. // ck_spre(1Hz) = RTCCLK(LSE) /(uwAsynchPrediv + 1)*(uwSynchPrediv + 1)
  47. // modify RTC_ASYNCH_PREDIV & RTC_SYNCH_PREDIV in board/<BN>/mpconfigport.h to change sub-second ticks
  48. // default is 3906.25 µs, min is ~30.52 µs (will increas Ivbat by ~500nA)
  49. #ifndef RTC_ASYNCH_PREDIV
  50. #define RTC_ASYNCH_PREDIV (0x7f)
  51. #endif
  52. #ifndef RTC_SYNCH_PREDIV
  53. #define RTC_SYNCH_PREDIV (0x00ff)
  54. #endif
  55. STATIC HAL_StatusTypeDef PYB_RTC_Init(RTC_HandleTypeDef *hrtc);
  56. STATIC void PYB_RTC_MspInit_Kick(RTC_HandleTypeDef *hrtc, bool rtc_use_lse);
  57. STATIC HAL_StatusTypeDef PYB_RTC_MspInit_Finalise(RTC_HandleTypeDef *hrtc);
  58. STATIC void RTC_CalendarConfig(void);
  59. #if defined(MICROPY_HW_RTC_USE_LSE) && MICROPY_HW_RTC_USE_LSE
  60. STATIC bool rtc_use_lse = true;
  61. #else
  62. STATIC bool rtc_use_lse = false;
  63. #endif
  64. STATIC uint32_t rtc_startup_tick;
  65. STATIC bool rtc_need_init_finalise = false;
  66. // check if LSE exists
  67. // not well tested, should probably be removed
  68. STATIC bool lse_magic(void) {
  69. #if 0
  70. uint32_t mode_in = GPIOC->MODER & 0x3fffffff;
  71. uint32_t mode_out = mode_in | 0x40000000;
  72. GPIOC->MODER = mode_out;
  73. GPIOC->OTYPER &= 0x7fff;
  74. GPIOC->BSRRH = 0x8000;
  75. GPIOC->OSPEEDR &= 0x3fffffff;
  76. GPIOC->PUPDR &= 0x3fffffff;
  77. int i = 0xff0;
  78. __IO int d = 0;
  79. uint32_t tc = 0;
  80. __IO uint32_t j;
  81. while (i) {
  82. GPIOC->MODER = mode_out;
  83. GPIOC->MODER = mode_in;
  84. for (j = 0; j < d; j++) ;
  85. i--;
  86. if ((GPIOC->IDR & 0x8000) == 0) {
  87. tc++;
  88. }
  89. }
  90. return (tc < 0xff0)?true:false;
  91. #else
  92. return false;
  93. #endif
  94. }
  95. void rtc_init_start(bool force_init) {
  96. RTCHandle.Instance = RTC;
  97. /* Configure RTC prescaler and RTC data registers */
  98. /* RTC configured as follow:
  99. - Hour Format = Format 24
  100. - Asynch Prediv = Value according to source clock
  101. - Synch Prediv = Value according to source clock
  102. - OutPut = Output Disable
  103. - OutPutPolarity = High Polarity
  104. - OutPutType = Open Drain */
  105. RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24;
  106. RTCHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
  107. RTCHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
  108. RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
  109. RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  110. RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  111. rtc_need_init_finalise = false;
  112. if (!force_init) {
  113. if ((RCC->BDCR & (RCC_BDCR_LSEON | RCC_BDCR_LSERDY)) == (RCC_BDCR_LSEON | RCC_BDCR_LSERDY)) {
  114. // LSE is enabled & ready --> no need to (re-)init RTC
  115. // remove Backup Domain write protection
  116. HAL_PWR_EnableBkUpAccess();
  117. // Clear source Reset Flag
  118. __HAL_RCC_CLEAR_RESET_FLAGS();
  119. // provide some status information
  120. rtc_info |= 0x40000 | (RCC->BDCR & 7) | (RCC->CSR & 3) << 8;
  121. return;
  122. } else if ((RCC->BDCR & RCC_BDCR_RTCSEL) == RCC_BDCR_RTCSEL_1) {
  123. // LSI configured as the RTC clock source --> no need to (re-)init RTC
  124. // remove Backup Domain write protection
  125. HAL_PWR_EnableBkUpAccess();
  126. // Clear source Reset Flag
  127. __HAL_RCC_CLEAR_RESET_FLAGS();
  128. // Turn the LSI on (it may need this even if the RTC is running)
  129. RCC->CSR |= RCC_CSR_LSION;
  130. // provide some status information
  131. rtc_info |= 0x80000 | (RCC->BDCR & 7) | (RCC->CSR & 3) << 8;
  132. return;
  133. }
  134. }
  135. rtc_startup_tick = HAL_GetTick();
  136. rtc_info = 0x3f000000 | (rtc_startup_tick & 0xffffff);
  137. if (rtc_use_lse) {
  138. if (lse_magic()) {
  139. // don't even try LSE
  140. rtc_use_lse = false;
  141. rtc_info &= ~0x01000000;
  142. }
  143. }
  144. PYB_RTC_MspInit_Kick(&RTCHandle, rtc_use_lse);
  145. }
  146. void rtc_init_finalise() {
  147. if (!rtc_need_init_finalise) {
  148. return;
  149. }
  150. rtc_info = 0x20000000;
  151. if (PYB_RTC_Init(&RTCHandle) != HAL_OK) {
  152. if (rtc_use_lse) {
  153. // fall back to LSI...
  154. rtc_use_lse = false;
  155. rtc_startup_tick = HAL_GetTick();
  156. PYB_RTC_MspInit_Kick(&RTCHandle, rtc_use_lse);
  157. HAL_PWR_EnableBkUpAccess();
  158. RTCHandle.State = HAL_RTC_STATE_RESET;
  159. if (PYB_RTC_Init(&RTCHandle) != HAL_OK) {
  160. rtc_info = 0x0100ffff; // indicate error
  161. return;
  162. }
  163. } else {
  164. // init error
  165. rtc_info = 0xffff; // indicate error
  166. return;
  167. }
  168. }
  169. // record if LSE or LSI is used
  170. rtc_info |= (rtc_use_lse << 28);
  171. // record how long it took for the RTC to start up
  172. rtc_info |= (HAL_GetTick() - rtc_startup_tick) & 0xffff;
  173. // fresh reset; configure RTC Calendar
  174. RTC_CalendarConfig();
  175. #if defined(STM32L4)
  176. if(__HAL_RCC_GET_FLAG(RCC_FLAG_BORRST) != RESET) {
  177. #else
  178. if(__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) != RESET) {
  179. #endif
  180. // power on reset occurred
  181. rtc_info |= 0x10000;
  182. }
  183. if(__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) != RESET) {
  184. // external reset occurred
  185. rtc_info |= 0x20000;
  186. }
  187. // Clear source Reset Flag
  188. __HAL_RCC_CLEAR_RESET_FLAGS();
  189. rtc_need_init_finalise = false;
  190. }
  191. STATIC HAL_StatusTypeDef PYB_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct) {
  192. /*------------------------------ LSI Configuration -------------------------*/
  193. if ((RCC_OscInitStruct->OscillatorType & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI) {
  194. // Check the LSI State
  195. if (RCC_OscInitStruct->LSIState != RCC_LSI_OFF) {
  196. // Enable the Internal Low Speed oscillator (LSI).
  197. __HAL_RCC_LSI_ENABLE();
  198. } else {
  199. // Disable the Internal Low Speed oscillator (LSI).
  200. __HAL_RCC_LSI_DISABLE();
  201. }
  202. }
  203. /*------------------------------ LSE Configuration -------------------------*/
  204. if ((RCC_OscInitStruct->OscillatorType & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE) {
  205. #if !defined(STM32H7)
  206. // Enable Power Clock
  207. __HAL_RCC_PWR_CLK_ENABLE();
  208. #endif
  209. // Enable access to the backup domain
  210. HAL_PWR_EnableBkUpAccess();
  211. uint32_t tickstart = HAL_GetTick();
  212. #if defined(STM32F7) || defined(STM32L4) || defined(STM32H7)
  213. //__HAL_RCC_PWR_CLK_ENABLE();
  214. // Enable write access to Backup domain
  215. //PWR->CR1 |= PWR_CR1_DBP;
  216. // Wait for Backup domain Write protection disable
  217. while ((PWR->CR1 & PWR_CR1_DBP) == RESET) {
  218. if (HAL_GetTick() - tickstart > RCC_DBP_TIMEOUT_VALUE) {
  219. return HAL_TIMEOUT;
  220. }
  221. }
  222. #else
  223. // Enable write access to Backup domain
  224. //PWR->CR |= PWR_CR_DBP;
  225. // Wait for Backup domain Write protection disable
  226. while ((PWR->CR & PWR_CR_DBP) == RESET) {
  227. if (HAL_GetTick() - tickstart > RCC_DBP_TIMEOUT_VALUE) {
  228. return HAL_TIMEOUT;
  229. }
  230. }
  231. #endif
  232. // Set the new LSE configuration
  233. __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
  234. }
  235. return HAL_OK;
  236. }
  237. STATIC HAL_StatusTypeDef PYB_RTC_Init(RTC_HandleTypeDef *hrtc) {
  238. // Check the RTC peripheral state
  239. if (hrtc == NULL) {
  240. return HAL_ERROR;
  241. }
  242. if (hrtc->State == HAL_RTC_STATE_RESET) {
  243. // Allocate lock resource and initialize it
  244. hrtc->Lock = HAL_UNLOCKED;
  245. // Initialize RTC MSP
  246. if (PYB_RTC_MspInit_Finalise(hrtc) != HAL_OK) {
  247. return HAL_ERROR;
  248. }
  249. }
  250. // Set RTC state
  251. hrtc->State = HAL_RTC_STATE_BUSY;
  252. // Disable the write protection for RTC registers
  253. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  254. // Set Initialization mode
  255. if (RTC_EnterInitMode(hrtc) != HAL_OK) {
  256. // Enable the write protection for RTC registers
  257. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  258. // Set RTC state
  259. hrtc->State = HAL_RTC_STATE_ERROR;
  260. return HAL_ERROR;
  261. } else {
  262. // Clear RTC_CR FMT, OSEL and POL Bits
  263. hrtc->Instance->CR &= ((uint32_t)~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL));
  264. // Set RTC_CR register
  265. hrtc->Instance->CR |= (uint32_t)(hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity);
  266. // Configure the RTC PRER
  267. hrtc->Instance->PRER = (uint32_t)(hrtc->Init.SynchPrediv);
  268. hrtc->Instance->PRER |= (uint32_t)(hrtc->Init.AsynchPrediv << 16);
  269. // Exit Initialization mode
  270. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  271. #if defined(STM32L4) || defined(STM32H7)
  272. hrtc->Instance->OR &= (uint32_t)~RTC_OR_ALARMOUTTYPE;
  273. hrtc->Instance->OR |= (uint32_t)(hrtc->Init.OutPutType);
  274. #elif defined(STM32F7)
  275. hrtc->Instance->OR &= (uint32_t)~RTC_OR_ALARMTYPE;
  276. hrtc->Instance->OR |= (uint32_t)(hrtc->Init.OutPutType);
  277. #else
  278. hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_ALARMOUTTYPE;
  279. hrtc->Instance->TAFCR |= (uint32_t)(hrtc->Init.OutPutType);
  280. #endif
  281. // Enable the write protection for RTC registers
  282. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  283. // Set RTC state
  284. hrtc->State = HAL_RTC_STATE_READY;
  285. return HAL_OK;
  286. }
  287. }
  288. STATIC void PYB_RTC_MspInit_Kick(RTC_HandleTypeDef *hrtc, bool rtc_use_lse) {
  289. /* To change the source clock of the RTC feature (LSE, LSI), You have to:
  290. - Enable the power clock using __PWR_CLK_ENABLE()
  291. - Enable write access using HAL_PWR_EnableBkUpAccess() function before to
  292. configure the RTC clock source (to be done once after reset).
  293. - Reset the Back up Domain using __HAL_RCC_BACKUPRESET_FORCE() and
  294. __HAL_RCC_BACKUPRESET_RELEASE().
  295. - Configure the needed RTc clock source */
  296. // RTC clock source uses LSE (external crystal) only if relevant
  297. // configuration variable is set. Otherwise it uses LSI (internal osc).
  298. RCC_OscInitTypeDef RCC_OscInitStruct;
  299. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
  300. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  301. if (rtc_use_lse) {
  302. #if MICROPY_HW_RTC_USE_BYPASS
  303. RCC_OscInitStruct.LSEState = RCC_LSE_BYPASS;
  304. #else
  305. RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  306. #endif
  307. RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
  308. } else {
  309. RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
  310. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  311. }
  312. PYB_RCC_OscConfig(&RCC_OscInitStruct);
  313. // now ramp up osc. in background and flag calendear init needed
  314. rtc_need_init_finalise = true;
  315. }
  316. #define PYB_LSE_TIMEOUT_VALUE 1000 // ST docs spec 2000 ms LSE startup, seems to be too pessimistic
  317. #define PYB_LSI_TIMEOUT_VALUE 500 // this is way too pessimistic, typ. < 1ms
  318. STATIC HAL_StatusTypeDef PYB_RTC_MspInit_Finalise(RTC_HandleTypeDef *hrtc) {
  319. // we already had a kick so now wait for the corresponding ready state...
  320. if (rtc_use_lse) {
  321. // we now have to wait for LSE ready or timeout
  322. uint32_t tickstart = rtc_startup_tick;
  323. while (__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET) {
  324. if ((HAL_GetTick() - tickstart ) > PYB_LSE_TIMEOUT_VALUE) {
  325. return HAL_TIMEOUT;
  326. }
  327. }
  328. } else {
  329. // we now have to wait for LSI ready or timeout
  330. uint32_t tickstart = rtc_startup_tick;
  331. while (__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET) {
  332. if ((HAL_GetTick() - tickstart ) > PYB_LSI_TIMEOUT_VALUE) {
  333. return HAL_TIMEOUT;
  334. }
  335. }
  336. }
  337. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
  338. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  339. if (rtc_use_lse) {
  340. PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  341. } else {
  342. PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  343. }
  344. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
  345. //Error_Handler();
  346. return HAL_ERROR;
  347. }
  348. // enable RTC peripheral clock
  349. __HAL_RCC_RTC_ENABLE();
  350. return HAL_OK;
  351. }
  352. STATIC void RTC_CalendarConfig(void) {
  353. // set the date to 1st Jan 2015
  354. RTC_DateTypeDef date;
  355. date.Year = 15;
  356. date.Month = 1;
  357. date.Date = 1;
  358. date.WeekDay = RTC_WEEKDAY_THURSDAY;
  359. if(HAL_RTC_SetDate(&RTCHandle, &date, RTC_FORMAT_BIN) != HAL_OK) {
  360. // init error
  361. return;
  362. }
  363. // set the time to 00:00:00
  364. RTC_TimeTypeDef time;
  365. time.Hours = 0;
  366. time.Minutes = 0;
  367. time.Seconds = 0;
  368. time.TimeFormat = RTC_HOURFORMAT12_AM;
  369. time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  370. time.StoreOperation = RTC_STOREOPERATION_RESET;
  371. if (HAL_RTC_SetTime(&RTCHandle, &time, RTC_FORMAT_BIN) != HAL_OK) {
  372. // init error
  373. return;
  374. }
  375. }
  376. /******************************************************************************/
  377. // MicroPython bindings
  378. typedef struct _pyb_rtc_obj_t {
  379. mp_obj_base_t base;
  380. } pyb_rtc_obj_t;
  381. STATIC const pyb_rtc_obj_t pyb_rtc_obj = {{&pyb_rtc_type}};
  382. /// \classmethod \constructor()
  383. /// Create an RTC object.
  384. STATIC mp_obj_t pyb_rtc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  385. // check arguments
  386. mp_arg_check_num(n_args, n_kw, 0, 0, false);
  387. // return constant object
  388. return MP_OBJ_FROM_PTR(&pyb_rtc_obj);
  389. }
  390. // force rtc to re-initialise
  391. mp_obj_t pyb_rtc_init(mp_obj_t self_in) {
  392. rtc_init_start(true);
  393. rtc_init_finalise();
  394. return mp_const_none;
  395. }
  396. MP_DEFINE_CONST_FUN_OBJ_1(pyb_rtc_init_obj, pyb_rtc_init);
  397. /// \method info()
  398. /// Get information about the startup time and reset source.
  399. ///
  400. /// - The lower 0xffff are the number of milliseconds the RTC took to
  401. /// start up.
  402. /// - Bit 0x10000 is set if a power-on reset occurred.
  403. /// - Bit 0x20000 is set if an external reset occurred
  404. mp_obj_t pyb_rtc_info(mp_obj_t self_in) {
  405. return mp_obj_new_int(rtc_info);
  406. }
  407. MP_DEFINE_CONST_FUN_OBJ_1(pyb_rtc_info_obj, pyb_rtc_info);
  408. /// \method datetime([datetimetuple])
  409. /// Get or set the date and time of the RTC.
  410. ///
  411. /// With no arguments, this method returns an 8-tuple with the current
  412. /// date and time. With 1 argument (being an 8-tuple) it sets the date
  413. /// and time.
  414. ///
  415. /// The 8-tuple has the following format:
  416. ///
  417. /// (year, month, day, weekday, hours, minutes, seconds, subseconds)
  418. ///
  419. /// `weekday` is 1-7 for Monday through Sunday.
  420. ///
  421. /// `subseconds` counts down from 255 to 0
  422. #define MEG_DIV_64 (1000000 / 64)
  423. #define MEG_DIV_SCALE ((RTC_SYNCH_PREDIV + 1) / 64)
  424. #if defined(MICROPY_HW_RTC_USE_US) && MICROPY_HW_RTC_USE_US
  425. uint32_t rtc_subsec_to_us(uint32_t ss) {
  426. return ((RTC_SYNCH_PREDIV - ss) * MEG_DIV_64) / MEG_DIV_SCALE;
  427. }
  428. uint32_t rtc_us_to_subsec(uint32_t us) {
  429. return RTC_SYNCH_PREDIV - (us * MEG_DIV_SCALE / MEG_DIV_64);
  430. }
  431. #else
  432. #define rtc_us_to_subsec
  433. #define rtc_subsec_to_us
  434. #endif
  435. mp_obj_t pyb_rtc_datetime(size_t n_args, const mp_obj_t *args) {
  436. rtc_init_finalise();
  437. if (n_args == 1) {
  438. // get date and time
  439. // note: need to call get time then get date to correctly access the registers
  440. RTC_DateTypeDef date;
  441. RTC_TimeTypeDef time;
  442. HAL_RTC_GetTime(&RTCHandle, &time, RTC_FORMAT_BIN);
  443. HAL_RTC_GetDate(&RTCHandle, &date, RTC_FORMAT_BIN);
  444. mp_obj_t tuple[8] = {
  445. mp_obj_new_int(2000 + date.Year),
  446. mp_obj_new_int(date.Month),
  447. mp_obj_new_int(date.Date),
  448. mp_obj_new_int(date.WeekDay),
  449. mp_obj_new_int(time.Hours),
  450. mp_obj_new_int(time.Minutes),
  451. mp_obj_new_int(time.Seconds),
  452. mp_obj_new_int(rtc_subsec_to_us(time.SubSeconds)),
  453. };
  454. return mp_obj_new_tuple(8, tuple);
  455. } else {
  456. // set date and time
  457. mp_obj_t *items;
  458. mp_obj_get_array_fixed_n(args[1], 8, &items);
  459. RTC_DateTypeDef date;
  460. date.Year = mp_obj_get_int(items[0]) - 2000;
  461. date.Month = mp_obj_get_int(items[1]);
  462. date.Date = mp_obj_get_int(items[2]);
  463. date.WeekDay = mp_obj_get_int(items[3]);
  464. HAL_RTC_SetDate(&RTCHandle, &date, RTC_FORMAT_BIN);
  465. RTC_TimeTypeDef time;
  466. time.Hours = mp_obj_get_int(items[4]);
  467. time.Minutes = mp_obj_get_int(items[5]);
  468. time.Seconds = mp_obj_get_int(items[6]);
  469. time.TimeFormat = RTC_HOURFORMAT12_AM;
  470. time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  471. time.StoreOperation = RTC_STOREOPERATION_SET;
  472. HAL_RTC_SetTime(&RTCHandle, &time, RTC_FORMAT_BIN);
  473. return mp_const_none;
  474. }
  475. }
  476. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_rtc_datetime_obj, 1, 2, pyb_rtc_datetime);
  477. #if defined(STM32F0)
  478. #define RTC_WKUP_IRQn RTC_IRQn
  479. #endif
  480. // wakeup(None)
  481. // wakeup(ms, callback=None)
  482. // wakeup(wucksel, wut, callback)
  483. mp_obj_t pyb_rtc_wakeup(size_t n_args, const mp_obj_t *args) {
  484. // wut is wakeup counter start value, wucksel is clock source
  485. // counter is decremented at wucksel rate, and wakes the MCU when it gets to 0
  486. // wucksel=0b000 is RTC/16 (RTC runs at 32768Hz)
  487. // wucksel=0b001 is RTC/8
  488. // wucksel=0b010 is RTC/4
  489. // wucksel=0b011 is RTC/2
  490. // wucksel=0b100 is 1Hz clock
  491. // wucksel=0b110 is 1Hz clock with 0x10000 added to wut
  492. // so a 1 second wakeup could be wut=2047, wucksel=0b000, or wut=4095, wucksel=0b001, etc
  493. rtc_init_finalise();
  494. // disable wakeup IRQ while we configure it
  495. HAL_NVIC_DisableIRQ(RTC_WKUP_IRQn);
  496. bool enable = false;
  497. mp_int_t wucksel;
  498. mp_int_t wut;
  499. mp_obj_t callback = mp_const_none;
  500. if (n_args <= 3) {
  501. if (args[1] == mp_const_none) {
  502. // disable wakeup
  503. } else {
  504. // time given in ms
  505. mp_int_t ms = mp_obj_get_int(args[1]);
  506. mp_int_t div = 2;
  507. wucksel = 3;
  508. while (div <= 16 && ms > 2000 * div) {
  509. div *= 2;
  510. wucksel -= 1;
  511. }
  512. if (div <= 16) {
  513. wut = 32768 / div * ms / 1000;
  514. } else {
  515. // use 1Hz clock
  516. wucksel = 4;
  517. wut = ms / 1000;
  518. if (wut > 0x10000) {
  519. // wut too large for 16-bit register, try to offset by 0x10000
  520. wucksel = 6;
  521. wut -= 0x10000;
  522. if (wut > 0x10000) {
  523. // wut still too large
  524. mp_raise_ValueError("wakeup value too large");
  525. }
  526. }
  527. }
  528. // wut register should be 1 less than desired value, but guard against wut=0
  529. if (wut > 0) {
  530. wut -= 1;
  531. }
  532. enable = true;
  533. }
  534. if (n_args == 3) {
  535. callback = args[2];
  536. }
  537. } else {
  538. // config values given directly
  539. wucksel = mp_obj_get_int(args[1]);
  540. wut = mp_obj_get_int(args[2]);
  541. callback = args[3];
  542. enable = true;
  543. }
  544. // set the callback
  545. MP_STATE_PORT(pyb_extint_callback)[22] = callback;
  546. // disable register write protection
  547. RTC->WPR = 0xca;
  548. RTC->WPR = 0x53;
  549. // clear WUTE
  550. RTC->CR &= ~(1 << 10);
  551. // wait until WUTWF is set
  552. while (!(RTC->ISR & (1 << 2))) {
  553. }
  554. if (enable) {
  555. // program WUT
  556. RTC->WUTR = wut;
  557. // set WUTIE to enable wakeup interrupts
  558. // set WUTE to enable wakeup
  559. // program WUCKSEL
  560. RTC->CR = (RTC->CR & ~7) | (1 << 14) | (1 << 10) | (wucksel & 7);
  561. // enable register write protection
  562. RTC->WPR = 0xff;
  563. // enable external interrupts on line 22
  564. #if defined(STM32L4)
  565. EXTI->IMR1 |= 1 << 22;
  566. EXTI->RTSR1 |= 1 << 22;
  567. #elif defined(STM32H7)
  568. EXTI_D1->IMR1 |= 1 << 22;
  569. EXTI->RTSR1 |= 1 << 22;
  570. #else
  571. EXTI->IMR |= 1 << 22;
  572. EXTI->RTSR |= 1 << 22;
  573. #endif
  574. // clear interrupt flags
  575. RTC->ISR &= ~(1 << 10);
  576. #if defined(STM32L4)
  577. EXTI->PR1 = 1 << 22;
  578. #elif defined(STM32H7)
  579. EXTI_D1->PR1 = 1 << 22;
  580. #else
  581. EXTI->PR = 1 << 22;
  582. #endif
  583. NVIC_SetPriority(RTC_WKUP_IRQn, IRQ_PRI_RTC_WKUP);
  584. HAL_NVIC_EnableIRQ(RTC_WKUP_IRQn);
  585. //printf("wut=%d wucksel=%d\n", wut, wucksel);
  586. } else {
  587. // clear WUTIE to disable interrupts
  588. RTC->CR &= ~(1 << 14);
  589. // enable register write protection
  590. RTC->WPR = 0xff;
  591. // disable external interrupts on line 22
  592. #if defined(STM32L4)
  593. EXTI->IMR1 &= ~(1 << 22);
  594. #elif defined(STM32H7)
  595. EXTI_D1->IMR1 |= 1 << 22;
  596. #else
  597. EXTI->IMR &= ~(1 << 22);
  598. #endif
  599. }
  600. return mp_const_none;
  601. }
  602. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_rtc_wakeup_obj, 2, 4, pyb_rtc_wakeup);
  603. // calibration(None)
  604. // calibration(cal)
  605. // When an integer argument is provided, check that it falls in the range [-511 to 512]
  606. // and set the calibration value; otherwise return calibration value
  607. mp_obj_t pyb_rtc_calibration(size_t n_args, const mp_obj_t *args) {
  608. rtc_init_finalise();
  609. mp_int_t cal;
  610. if (n_args == 2) {
  611. cal = mp_obj_get_int(args[1]);
  612. mp_uint_t cal_p, cal_m;
  613. if (cal < -511 || cal > 512) {
  614. #if defined(MICROPY_HW_RTC_USE_CALOUT) && MICROPY_HW_RTC_USE_CALOUT
  615. if ((cal & 0xfffe) == 0x0ffe) {
  616. // turn on/off X18 (PC13) 512Hz output
  617. // Note:
  618. // Output will stay active even in VBAT mode (and inrease current)
  619. if (cal & 1) {
  620. HAL_RTCEx_SetCalibrationOutPut(&RTCHandle, RTC_CALIBOUTPUT_512HZ);
  621. } else {
  622. HAL_RTCEx_DeactivateCalibrationOutPut(&RTCHandle);
  623. }
  624. return mp_obj_new_int(cal & 1);
  625. } else {
  626. mp_raise_ValueError("calibration value out of range");
  627. }
  628. #else
  629. mp_raise_ValueError("calibration value out of range");
  630. #endif
  631. }
  632. if (cal > 0) {
  633. cal_p = RTC_SMOOTHCALIB_PLUSPULSES_SET;
  634. cal_m = 512 - cal;
  635. } else {
  636. cal_p = RTC_SMOOTHCALIB_PLUSPULSES_RESET;
  637. cal_m = -cal;
  638. }
  639. HAL_RTCEx_SetSmoothCalib(&RTCHandle, RTC_SMOOTHCALIB_PERIOD_32SEC, cal_p, cal_m);
  640. return mp_const_none;
  641. } else {
  642. // printf("CALR = 0x%x\n", (mp_uint_t) RTCHandle.Instance->CALR); // DEBUG
  643. // Test if CALP bit is set in CALR:
  644. if (RTCHandle.Instance->CALR & 0x8000) {
  645. cal = 512 - (RTCHandle.Instance->CALR & 0x1ff);
  646. } else {
  647. cal = -(RTCHandle.Instance->CALR & 0x1ff);
  648. }
  649. return mp_obj_new_int(cal);
  650. }
  651. }
  652. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_rtc_calibration_obj, 1, 2, pyb_rtc_calibration);
  653. STATIC const mp_rom_map_elem_t pyb_rtc_locals_dict_table[] = {
  654. { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&pyb_rtc_init_obj) },
  655. { MP_ROM_QSTR(MP_QSTR_info), MP_ROM_PTR(&pyb_rtc_info_obj) },
  656. { MP_ROM_QSTR(MP_QSTR_datetime), MP_ROM_PTR(&pyb_rtc_datetime_obj) },
  657. { MP_ROM_QSTR(MP_QSTR_wakeup), MP_ROM_PTR(&pyb_rtc_wakeup_obj) },
  658. { MP_ROM_QSTR(MP_QSTR_calibration), MP_ROM_PTR(&pyb_rtc_calibration_obj) },
  659. };
  660. STATIC MP_DEFINE_CONST_DICT(pyb_rtc_locals_dict, pyb_rtc_locals_dict_table);
  661. const mp_obj_type_t pyb_rtc_type = {
  662. { &mp_type_type },
  663. .name = MP_QSTR_RTC,
  664. .make_new = pyb_rtc_make_new,
  665. .locals_dict = (mp_obj_dict_t*)&pyb_rtc_locals_dict,
  666. };