at32f421_ertc.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  1. /**
  2. **************************************************************************
  3. * @file at32f421_ertc.c
  4. * @brief contains all the functions for the ertc firmware library
  5. **************************************************************************
  6. * Copyright notice & Disclaimer
  7. *
  8. * The software Board Support Package (BSP) that is made available to
  9. * download from Artery official website is the copyrighted work of Artery.
  10. * Artery authorizes customers to use, copy, and distribute the BSP
  11. * software and its related documentation for the purpose of design and
  12. * development in conjunction with Artery microcontrollers. Use of the
  13. * software is governed by this copyright notice and the following disclaimer.
  14. *
  15. * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  16. * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  17. * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  18. * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  19. * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  21. *
  22. **************************************************************************
  23. */
  24. #include "at32f421_conf.h"
  25. /** @addtogroup AT32F421_periph_driver
  26. * @{
  27. */
  28. /** @defgroup ERTC
  29. * @brief ERTC driver modules
  30. * @{
  31. */
  32. #ifdef ERTC_MODULE_ENABLED
  33. /** @defgroup ERTC_private_functions
  34. * @{
  35. */
  36. #define ERTC_TIMEOUT ((uint32_t) 0x00360000)
  37. /**
  38. * @brief number conversion to bcd code.
  39. * @param num: number(0~99)
  40. * @retval bcd code.
  41. */
  42. uint8_t ertc_num_to_bcd(uint8_t num)
  43. {
  44. uint8_t bcd_h = 0, bcd_l = 0;
  45. bcd_h = num / 10;
  46. bcd_l = num % 10;
  47. return ((uint8_t)(bcd_h << 4) | bcd_l);
  48. }
  49. /**
  50. * @brief bcd code conversion to number.
  51. * @param bcd: bcd code(0~99).
  52. * @retval number.
  53. */
  54. uint8_t ertc_bcd_to_num(uint8_t bcd)
  55. {
  56. return ((((uint8_t)(bcd & (uint8_t)0xF0) >> 4) * 10) + (bcd & (uint8_t)0x0F));
  57. }
  58. /**
  59. * @brief enable write protection.
  60. * @param none.
  61. * @retval none
  62. */
  63. void ertc_write_protect_enable(void)
  64. {
  65. ERTC->wp = 0xFF;
  66. }
  67. /**
  68. * @brief disable write protection.
  69. * @param none.
  70. * @retval none
  71. */
  72. void ertc_write_protect_disable(void)
  73. {
  74. ERTC->wp = 0xCA;
  75. ERTC->wp = 0x53;
  76. }
  77. /**
  78. * @brief ertc wait register update finish.
  79. * @param none.
  80. * @retval error_status (ERROR or SUCCESS).
  81. */
  82. error_status ertc_wait_update(void)
  83. {
  84. uint32_t timeout = ERTC_TIMEOUT * 2;
  85. /* clear updf flag */
  86. ERTC->sts = ~(ERTC_UPDF_FLAG | 0x00000080) | (ERTC->sts_bit.imen << 7);
  87. while(ERTC->sts_bit.updf == 0)
  88. {
  89. if(timeout == 0)
  90. {
  91. return ERROR;
  92. }
  93. timeout--;
  94. }
  95. return SUCCESS;
  96. }
  97. /**
  98. * @brief ertc wait flag status.
  99. * @param flag: flag to wait.
  100. * this parameter can be one of the following values:
  101. * - ERTC_ALAWF_FLAG: alarm a register allows write flag.
  102. * - ERTC_TADJF_FLAG: time adjustment flag.
  103. * - ERTC_CALUPDF_FLAG: calibration value update completed flag.
  104. * @param status: status to wait.
  105. * this parameter can be one of the following values:
  106. * - SET.
  107. * - RESET.
  108. * @retval error_status (ERROR or SUCCESS).
  109. */
  110. error_status ertc_wait_flag(uint32_t flag, flag_status status)
  111. {
  112. uint32_t timeout = ERTC_TIMEOUT;
  113. while(ertc_flag_get(flag) == status)
  114. {
  115. if(timeout == 0)
  116. {
  117. /* enable write protection */
  118. ertc_write_protect_enable();
  119. return ERROR;
  120. }
  121. timeout--;
  122. }
  123. return SUCCESS;
  124. }
  125. /**
  126. * @brief ertc enter init mode.
  127. * @param none.
  128. * @retval error_status (ERROR or SUCCESS).
  129. */
  130. error_status ertc_init_mode_enter(void)
  131. {
  132. uint32_t timeout = ERTC_TIMEOUT * 2;
  133. if(ERTC->sts_bit.imf == 0)
  134. {
  135. /* enter init mode */
  136. ERTC->sts = 0xFFFFFFFF;
  137. while(ERTC->sts_bit.imf == 0)
  138. {
  139. if(timeout == 0)
  140. {
  141. /* enable write protection */
  142. ertc_write_protect_enable();
  143. return ERROR;
  144. }
  145. timeout--;
  146. }
  147. }
  148. return SUCCESS;
  149. }
  150. /**
  151. * @brief ertc exit init mode.
  152. * @param none.
  153. * @retval none.
  154. */
  155. void ertc_init_mode_exit(void)
  156. {
  157. ERTC->sts = 0xFFFFFF7F;
  158. }
  159. /**
  160. * @brief ertc reset all register.
  161. * @param none.
  162. * @retval error_status (ERROR or SUCCESS).
  163. */
  164. error_status ertc_reset(void)
  165. {
  166. /* disable write protection */
  167. ertc_write_protect_disable();
  168. ERTC->ctrl = (uint32_t)0x00000000;
  169. /* enter init mode */
  170. if(ertc_init_mode_enter() != SUCCESS)
  171. {
  172. return ERROR;
  173. }
  174. /* reset register */
  175. ERTC->time = (uint32_t)0x00000000;
  176. ERTC->date = (uint32_t)0x00002101;
  177. ERTC->ctrl = (uint32_t)0x00000000;
  178. ERTC->div = (uint32_t)0x007F00FF;
  179. ERTC->ala = (uint32_t)0x00000000;
  180. ERTC->tadj = (uint32_t)0x00000000;
  181. ERTC->scal = (uint32_t)0x00000000;
  182. ERTC->tamp = (uint32_t)0x00000000;
  183. ERTC->alasbs = (uint32_t)0x00000000;
  184. ERTC->sts = (uint32_t)0x00000000;
  185. /* wait calendar update */
  186. ertc_wait_update();
  187. /* enable write protection */
  188. ertc_write_protect_enable();
  189. return SUCCESS;
  190. }
  191. /**
  192. * @brief ertc division set.
  193. * @param div_a: division a (0~0x7F).
  194. * @param div_b: division b (0~0x7FFF).
  195. * @retval error_status (ERROR or SUCCESS).
  196. */
  197. error_status ertc_divider_set(uint16_t div_a, uint16_t div_b)
  198. {
  199. /* disable write protection */
  200. ertc_write_protect_disable();
  201. /* enter init mode */
  202. if(ertc_init_mode_enter() != SUCCESS)
  203. {
  204. return ERROR;
  205. }
  206. /* config the ertc divider */
  207. ERTC->div_bit.diva = div_a;
  208. ERTC->div_bit.divb = div_b;
  209. /* exit init mode */
  210. ertc_init_mode_exit();
  211. /* enable write protection */
  212. ertc_write_protect_enable();
  213. return SUCCESS;
  214. }
  215. /**
  216. * @brief ertc hour mode set.
  217. * @param mode: hour mode.
  218. * this parameter can be one of the following values:
  219. * - ERTC_HOUR_MODE_24: 24-hour format.
  220. * - ERTC_HOUR_MODE_12: 12-hour format.
  221. * @retval error_status (ERROR or SUCCESS).
  222. */
  223. error_status ertc_hour_mode_set(ertc_hour_mode_set_type mode)
  224. {
  225. /* disable write protection */
  226. ertc_write_protect_disable();
  227. /* enter init mode */
  228. if(ertc_init_mode_enter() != SUCCESS)
  229. {
  230. return ERROR;
  231. }
  232. /* write register */
  233. ERTC->ctrl_bit.hm = mode;
  234. /* exit init mode */
  235. ertc_init_mode_exit();
  236. /* enable write protection */
  237. ertc_write_protect_enable();
  238. return SUCCESS;
  239. }
  240. /**
  241. * @brief set date.
  242. * @param year: year (0~99).
  243. * @param month: month (1~12).
  244. * @param date: date (1~31).
  245. * @param week: week (1~7).
  246. * @retval error_status (ERROR or SUCCESS).
  247. */
  248. error_status ertc_date_set(uint8_t year, uint8_t month, uint8_t date, uint8_t week)
  249. {
  250. ertc_reg_date_type reg;
  251. reg.date = 0;
  252. reg.date_bit.y = ertc_num_to_bcd(year);
  253. reg.date_bit.m = ertc_num_to_bcd(month);
  254. reg.date_bit.d = ertc_num_to_bcd(date);
  255. reg.date_bit.wk = week;
  256. /* disable write protection */
  257. ertc_write_protect_disable();
  258. /* enter init mode */
  259. if(ertc_init_mode_enter() != SUCCESS)
  260. {
  261. return ERROR;
  262. }
  263. /* Set the ertc_DR register */
  264. ERTC->date = reg.date;
  265. /* exit init mode */
  266. ertc_init_mode_exit();
  267. if(ERTC->ctrl_bit.dren == 0)
  268. {
  269. ertc_wait_update();
  270. }
  271. /* enable write protection */
  272. ertc_write_protect_enable();
  273. return SUCCESS;
  274. }
  275. /**
  276. * @brief set time.
  277. * @param hour: hour (0~23).
  278. * @param min: minute (0~59).
  279. * @param sec: second (0~59).
  280. * @param ampm: hour mode.
  281. * this parameter can be one of the following values:
  282. * - ERTC_24H: 24-hour format.
  283. * - ERTC_AM: 12-hour format, ante meridiem.
  284. * - ERTC_PM: 12-hour format, post meridiem.
  285. * @retval error_status (ERROR or SUCCESS).
  286. */
  287. error_status ertc_time_set(uint8_t hour, uint8_t min, uint8_t sec, ertc_am_pm_type ampm)
  288. {
  289. ertc_reg_time_type reg;
  290. reg.time = 0;
  291. reg.time_bit.h = ertc_num_to_bcd(hour);
  292. reg.time_bit.m = ertc_num_to_bcd(min);
  293. reg.time_bit.s = ertc_num_to_bcd(sec);
  294. reg.time_bit.ampm = ampm;
  295. /* disable write protection */
  296. ertc_write_protect_disable();
  297. /* enter init mode */
  298. if(ertc_init_mode_enter() != SUCCESS)
  299. {
  300. return ERROR;
  301. }
  302. ERTC->time = reg.time;
  303. /* exit init mode */
  304. ertc_init_mode_exit();
  305. if(ERTC->ctrl_bit.dren == 0)
  306. {
  307. ertc_wait_update();
  308. }
  309. /* enable write protection */
  310. ertc_write_protect_enable();
  311. return SUCCESS;
  312. }
  313. /**
  314. * @brief get calendar
  315. * @param time: ertc time.
  316. * @retval none.
  317. */
  318. void ertc_calendar_get(ertc_time_type* time)
  319. {
  320. ertc_reg_time_type reg_tm;
  321. ertc_reg_date_type reg_dt;
  322. UNUSED(ERTC->sts);
  323. reg_tm.time = ERTC->time;
  324. reg_dt.date = ERTC->date;
  325. time->hour = ertc_bcd_to_num(reg_tm.time_bit.h);
  326. time->min = ertc_bcd_to_num(reg_tm.time_bit.m);
  327. time->sec = ertc_bcd_to_num(reg_tm.time_bit.s);
  328. time->ampm = (ertc_am_pm_type)reg_tm.time_bit.ampm;
  329. time->year = ertc_bcd_to_num(reg_dt.date_bit.y);
  330. time->month = ertc_bcd_to_num(reg_dt.date_bit.m);
  331. time->day = ertc_bcd_to_num(reg_dt.date_bit.d);
  332. time->week = reg_dt.date_bit.wk;
  333. }
  334. /**
  335. * @brief get current sub second.
  336. * @param none.
  337. * @retval sub second.
  338. */
  339. uint32_t ertc_sub_second_get(void)
  340. {
  341. uint32_t reg = 0;
  342. reg = ERTC->sbs;
  343. (void) (ERTC->date);
  344. return (reg);
  345. }
  346. /**
  347. * @brief set which bits are irrelevant to the alarm match.
  348. * @param alarm_x: select the alarm.
  349. * this parameter can be one of the following values:
  350. * - ERTC_ALA: alarm a.
  351. * @param mask: select which bits are irrelevant to the alarm match.
  352. * this parameter can be one of the following values:
  353. * - ERTC_ALARM_MASK_NONE: match all.
  354. * - ERTC_ALARM_MASK_SEC: don't match seconds.
  355. * - ERTC_ALARM_MASK_MIN: don't match minute.
  356. * - ERTC_ALARM_MASK_HOUR: don't match hour.
  357. * - ERTC_ALARM_MASK_DATE_WEEK: don't match date or week.
  358. * - ERTC_ALARM_MASK_ALL: don't match all.
  359. * @param alarm: alarm para.
  360. * @retval none.
  361. */
  362. void ertc_alarm_mask_set(ertc_alarm_type alarm_x, uint32_t mask)
  363. {
  364. uint32_t reg;
  365. /* disable write protection */
  366. ertc_write_protect_disable();
  367. if(alarm_x == ERTC_ALA)
  368. {
  369. reg = ERTC->ala;
  370. reg &= ~ERTC_ALARM_MASK_ALL;
  371. reg |= mask;
  372. ERTC->ala= reg;
  373. }
  374. /* enable write protection */
  375. ertc_write_protect_enable();
  376. }
  377. /**
  378. * @brief alarm week or date mode select.
  379. * @param alarm_x: select the alarm.
  380. * this parameter can be one of the following values:
  381. * - ERTC_ALA: alarm a.
  382. * @param wk: week or date mode select.
  383. * this parameter can be one of the following values:
  384. * - ERTC_SLECT_DATE: slect date mode.
  385. * - ERTC_SLECT_WEEK: slect week mode.
  386. * @retval none.
  387. */
  388. void ertc_alarm_week_date_select(ertc_alarm_type alarm_x, ertc_week_date_select_type wk)
  389. {
  390. /* disable write protection */
  391. ertc_write_protect_disable();
  392. if(alarm_x == ERTC_ALA)
  393. {
  394. ERTC->ala_bit.wksel = wk;
  395. }
  396. /* enable write protection */
  397. ertc_write_protect_enable();
  398. }
  399. /**
  400. * @brief set alarm.
  401. * @param alarm_x: select the alarm.
  402. * this parameter can be one of the following values:
  403. * - ERTC_ALA: alarm a.
  404. * @param week_day: week or date.
  405. * - week: 1~7.
  406. * - date: 1~31.
  407. * @param hour: hour (0~23).
  408. * @param min: minute (0~59).
  409. * @param sec: second (0~59).
  410. * @param ampm: hour mode.
  411. * this parameter can be one of the following values:
  412. * - ERTC_24H: 24-hour format.
  413. * - ERTC_AM: 12-hour format, ante meridiem.
  414. * - ERTC_PM: 12-hour format, post meridiem.
  415. * @param alarm: alarm para.
  416. * @retval none.
  417. */
  418. void ertc_alarm_set(ertc_alarm_type alarm_x, uint8_t week_date, uint8_t hour, uint8_t min, uint8_t sec, ertc_am_pm_type ampm)
  419. {
  420. ertc_reg_alarm_type reg;
  421. if(alarm_x == ERTC_ALA)
  422. {
  423. reg.ala = ERTC->ala;
  424. }
  425. reg.ala_bit.d = ertc_num_to_bcd(week_date);
  426. reg.ala_bit.h = ertc_num_to_bcd(hour);
  427. reg.ala_bit.m = ertc_num_to_bcd(min);
  428. reg.ala_bit.s = ertc_num_to_bcd(sec);
  429. reg.ala_bit.ampm = ampm;
  430. /* disable write protection */
  431. ertc_write_protect_disable();
  432. if(alarm_x == ERTC_ALA)
  433. {
  434. ERTC->ala= reg.ala;
  435. }
  436. /* enable write protection */
  437. ertc_write_protect_enable();
  438. }
  439. /**
  440. * @brief set alarm sub second.
  441. * @param alarm_x: select the alarm.
  442. * this parameter can be one of the following values:
  443. * - ERTC_ALA: alarm a.
  444. * @param value: sub second value.
  445. * @param mask: sub second mask.
  446. * this parameter can be one of the following values:
  447. * - ERTC_ALARM_SBS_MASK_ALL: do not match the sub-second.
  448. * - ERTC_ALARM_SBS_MASK_14_1: only compare bit [0].
  449. * - ERTC_ALARM_SBS_MASK_14_2: only compare bit [1:0].
  450. * - ERTC_ALARM_SBS_MASK_14_3: only compare bit [2:0].
  451. * - ERTC_ALARM_SBS_MASK_14_4: only compare bit [3:0].
  452. * - ERTC_ALARM_SBS_MASK_14_5: only compare bit [4:0].
  453. * - ERTC_ALARM_SBS_MASK_14_6: only compare bit [5:0].
  454. * - ERTC_ALARM_SBS_MASK_14_7: only compare bit [6:0].
  455. * - ERTC_ALARM_SBS_MASK_14_8: only compare bit [7:0].
  456. * - ERTC_ALARM_SBS_MASK_14_9: only compare bit [8:0].
  457. * - ERTC_ALARM_SBS_MASK_14_10: only compare bit [9:0].
  458. * - ERTC_ALARM_SBS_MASK_14_11: only compare bit [10:0].
  459. * - ERTC_ALARM_SBS_MASK_14_12: only compare bit [11:0].
  460. * - ERTC_ALARM_SBS_MASK_14_13: only compare bit [12:0].
  461. * - ERTC_ALARM_SBS_MASK_14: only compare bit [13:0].
  462. * - ERTC_ALARM_SBS_MASK_NONE: compare bit [14:0].
  463. * @retval none.
  464. */
  465. void ertc_alarm_sub_second_set(ertc_alarm_type alarm_x, uint32_t value, ertc_alarm_sbs_mask_type mask)
  466. {
  467. /* disable write protection */
  468. ertc_write_protect_disable();
  469. if(alarm_x == ERTC_ALA)
  470. {
  471. ERTC->alasbs_bit.sbsmsk = mask;
  472. ERTC->alasbs_bit.sbs = value;
  473. }
  474. /* enable write protection */
  475. ertc_write_protect_enable();
  476. }
  477. /**
  478. * @brief enable or disable alarm clock.
  479. * @param alarm_x: select the alarm.
  480. * this parameter can be one of the following values:
  481. * - ERTC_ALA: alarm a.
  482. * @param new_state (TRUE or FALSE).
  483. * @retval error_status (ERROR or SUCCESS).
  484. */
  485. error_status ertc_alarm_enable(ertc_alarm_type alarm_x, confirm_state new_state)
  486. {
  487. /* disable write protection */
  488. ertc_write_protect_disable();
  489. if(alarm_x == ERTC_ALA)
  490. {
  491. ERTC->ctrl_bit.alaen = new_state;
  492. if(new_state == FALSE)
  493. {
  494. if(ertc_wait_flag(ERTC_ALAWF_FLAG, RESET) != SUCCESS)
  495. {
  496. return ERROR;
  497. }
  498. }
  499. }
  500. /* enable write protection */
  501. ertc_write_protect_enable();
  502. return SUCCESS;
  503. }
  504. /**
  505. * @brief get alarm value.
  506. * @param alarm_x: select the alarm.
  507. * this parameter can be one of the following values:
  508. * - ERTC_ALA: alarm a.
  509. * @param alarm: alarm para.
  510. * @retval none.
  511. */
  512. void ertc_alarm_get(ertc_alarm_type alarm_x, ertc_alarm_value_type* alarm)
  513. {
  514. ertc_reg_alarm_type reg;
  515. reg.ala = 0;
  516. if(alarm_x == ERTC_ALA)
  517. {
  518. reg.ala = ERTC->ala;
  519. }
  520. alarm->day = ertc_bcd_to_num(reg.ala_bit.d);
  521. alarm->week = ertc_bcd_to_num(reg.ala_bit.d);
  522. alarm->hour = ertc_bcd_to_num(reg.ala_bit.h);
  523. alarm->min = ertc_bcd_to_num(reg.ala_bit.m);
  524. alarm->sec = ertc_bcd_to_num(reg.ala_bit.s);
  525. alarm->ampm = (ertc_am_pm_type)reg.ala_bit.ampm;
  526. alarm->week_date_sel = reg.ala_bit.wksel;
  527. alarm->mask = reg.ala & ERTC_ALARM_MASK_ALL;
  528. }
  529. /**
  530. * @brief get alarm sub second.
  531. * @param alarm_x: select the alarm.
  532. * this parameter can be one of the following values:
  533. * - ERTC_ALA: alarm a.
  534. * @retval sub second.
  535. */
  536. uint32_t ertc_alarm_sub_second_get(ertc_alarm_type alarm_x)
  537. {
  538. return (ERTC->alasbs_bit.sbs);
  539. }
  540. /**
  541. * @brief config the smooth calibration.
  542. * @param period: calibration period.
  543. * this parameter can be one of the following values:
  544. * - ERTC_SMOOTH_CAL_PERIOD_32: 32 second calibration period.
  545. * - ERTC_SMOOTH_CAL_PERIOD_16: 16 second calibration period.
  546. * - ERTC_SMOOTH_CAL_PERIOD_8: 8 second calibration period.
  547. * @param clk_add: add clock.
  548. * this parameter can be one of the following values:
  549. * - ERTC_SMOOTH_CAL_CLK_ADD_0: do not increase clock.
  550. * - ERTC_SMOOTH_CAL_CLK_ADD_512: add 512 clocks.
  551. * @param clk_dec: decrease clock(0~511).
  552. * @retval error_status (ERROR or SUCCESS).
  553. */
  554. error_status ertc_smooth_calibration_config(ertc_smooth_cal_period_type period, ertc_smooth_cal_clk_add_type clk_add, uint32_t clk_dec)
  555. {
  556. ertc_reg_scal_type reg;
  557. /* disable write protection */
  558. ertc_write_protect_disable();
  559. if(ertc_wait_flag(ERTC_CALUPDF_FLAG, SET) != SUCCESS)
  560. {
  561. return ERROR;
  562. }
  563. reg.scal = 0;
  564. switch (period)
  565. {
  566. case ERTC_SMOOTH_CAL_PERIOD_32:
  567. break;
  568. case ERTC_SMOOTH_CAL_PERIOD_16:
  569. reg.scal_bit.cal16 = 1;
  570. break;
  571. case ERTC_SMOOTH_CAL_PERIOD_8:
  572. reg.scal_bit.cal8 = 1;
  573. break;
  574. default:
  575. break;
  576. }
  577. reg.scal_bit.add = clk_add;
  578. reg.scal_bit.dec = clk_dec;
  579. ERTC->scal = reg.scal;
  580. /* enable write protection */
  581. ertc_write_protect_enable();
  582. return SUCCESS;
  583. }
  584. /**
  585. * @brief calibration output source select.
  586. * @param output: output source.
  587. * this parameter can be one of the following values:
  588. * - ERTC_CAL_OUTPUT_512HZ: output 512 hz.
  589. * - ERTC_CAL_OUTPUT_1HZ: output 1 hz.
  590. * @retval none.
  591. */
  592. void ertc_cal_output_select(ertc_cal_output_select_type output)
  593. {
  594. /* disable write protection */
  595. ertc_write_protect_disable();
  596. ERTC->ctrl_bit.calosel = output;
  597. /* enable write protection */
  598. ertc_write_protect_enable();
  599. }
  600. /**
  601. * @brief enable or disable calibration output.
  602. * @param new_state (TRUE or FALSE).
  603. * @retval none.
  604. */
  605. void ertc_cal_output_enable(confirm_state new_state)
  606. {
  607. /* disable write protection */
  608. ertc_write_protect_disable();
  609. ERTC->ctrl_bit.caloen = new_state;
  610. /* enable write protection */
  611. ertc_write_protect_enable();
  612. }
  613. /**
  614. * @brief adjust the time.
  615. * @param add1s: second operation.
  616. * this parameter can be one of the following values:
  617. * - ERTC_TIME_ADD_NONE: none operation.
  618. * - ERTC_TIME_ADD_1S: add 1 second.
  619. * @param decsbs: decrease sub second(0~0x7FFF).
  620. * @retval error_status (ERROR or SUCCESS).
  621. */
  622. error_status ertc_time_adjust(ertc_time_adjust_type add1s, uint32_t decsbs)
  623. {
  624. ertc_reg_tadj_type reg;
  625. reg.tadj = 0;
  626. /* disable write protection */
  627. ertc_write_protect_disable();
  628. if(ertc_wait_flag(ERTC_TADJF_FLAG, SET) != SUCCESS)
  629. {
  630. return ERROR;
  631. }
  632. /* check if the reference clock detection is disabled */
  633. if(ERTC->ctrl_bit.rcden == 0)
  634. {
  635. reg.tadj_bit.add1s = add1s;
  636. reg.tadj_bit.decsbs = decsbs;
  637. ERTC->tadj = reg.tadj;
  638. if(ertc_wait_update() == ERROR)
  639. {
  640. return ERROR;
  641. }
  642. }
  643. else
  644. {
  645. return ERROR;
  646. }
  647. /* enable write protection */
  648. ertc_write_protect_enable();
  649. return SUCCESS;
  650. }
  651. /**
  652. * @brief config the daylight saving time.
  653. * @param operation: time adjust.
  654. * this parameter can be one of the following values:
  655. * - ERTC_DST_ADD_1H: add 1 hour.
  656. * - ERTC_DST_DEC_1H: dec 1 hour.
  657. * @param save: operation save.
  658. * this parameter can be one of the following values:
  659. * - ERTC_DST_SAVE_0: set the bpr register value to 0.
  660. * - ERTC_DST_SAVE_1: set the bpr register value to 1.
  661. * @retval none.
  662. */
  663. void ertc_daylight_set(ertc_dst_operation_type operation, ertc_dst_save_type save)
  664. {
  665. /* disable write protection */
  666. ertc_write_protect_disable();
  667. if(operation == ERTC_DST_ADD_1H)
  668. {
  669. ERTC->ctrl_bit.add1h = 1;
  670. }
  671. else
  672. {
  673. ERTC->ctrl_bit.dec1h = 1;
  674. }
  675. ERTC->ctrl_bit.bpr = save;
  676. /* enable write protection */
  677. ertc_write_protect_enable();
  678. }
  679. /**
  680. * @brief get the bpr value.
  681. * @param none.
  682. * @retval bpr value.
  683. */
  684. uint8_t ertc_daylight_bpr_get(void)
  685. {
  686. return ERTC->ctrl_bit.bpr;
  687. }
  688. /**
  689. * @brief enable or disable refer clock detect.
  690. * @param new_state (TRUE or FALSE).
  691. * @retval error_status (ERROR or SUCCESS).
  692. */
  693. error_status ertc_refer_clock_detect_enable(confirm_state new_state)
  694. {
  695. /* disable write protection */
  696. ertc_write_protect_disable();
  697. /* enter init mode */
  698. if(ertc_init_mode_enter() != SUCCESS)
  699. {
  700. return ERROR;
  701. }
  702. /* write register */
  703. ERTC->ctrl_bit.rcden = new_state;
  704. /* exit init mode */
  705. ertc_init_mode_exit();
  706. /* enable write protection */
  707. ertc_write_protect_enable();
  708. return SUCCESS;
  709. }
  710. /**
  711. * @brief enable or disable direct read mode.
  712. * @param new_state (TRUE or FALSE).
  713. * @retval none.
  714. */
  715. void ertc_direct_read_enable(confirm_state new_state)
  716. {
  717. /* disable write protection */
  718. ertc_write_protect_disable();
  719. ERTC->ctrl_bit.dren = new_state;
  720. /* enable write protection */
  721. ertc_write_protect_enable();
  722. }
  723. /**
  724. * @brief set the output mode.
  725. * @param source: output source.
  726. * this parameter can be one of the following values:
  727. * - ERTC_OUTPUT_DISABLE: diable output.
  728. * - ERTC_OUTPUT_ALARM_A: output alarm a event.
  729. * @param polarity: output polarity.
  730. * this parameter can be one of the following values:
  731. * - ERTC_OUTPUT_POLARITY_HIGH: when the event occurs, the output is high.
  732. * - ERTC_OUTPUT_POLARITY_LOW: when the event occurs, the output is low.
  733. * @param type: output type.
  734. * this parameter can be one of the following values:
  735. * - ERTC_OUTPUT_TYPE_OPEN_DRAIN: open drain output.
  736. * - ERTC_OUTPUT_TYPE_PUSH_PULL: push pull output.
  737. * @retval none.
  738. */
  739. void ertc_output_set(ertc_output_source_type source, ertc_output_polarity_type polarity, ertc_output_type type)
  740. {
  741. /* disable write protection */
  742. ertc_write_protect_disable();
  743. ERTC->ctrl_bit.outp = polarity;
  744. ERTC->tamp_bit.outtype = type;
  745. ERTC->ctrl_bit.outsel = source;
  746. /* enable write protection */
  747. ertc_write_protect_enable();
  748. }
  749. /**
  750. * @brief set the timestamp valid edge.
  751. * @param edge: calibration period.
  752. * this parameter can be one of the following values:
  753. * - ERTC_TIMESTAMP_EDGE_RISING : rising edge trigger.
  754. * - ERTC_TIMESTAMP_EDGE_FALLING: falling edge trigger.
  755. * @retval none.
  756. */
  757. void ertc_timestamp_valid_edge_set(ertc_timestamp_valid_edge_type edge)
  758. {
  759. /* disable write protection */
  760. ertc_write_protect_disable();
  761. ERTC->ctrl_bit.tsedg = edge;
  762. /* enable write protection */
  763. ertc_write_protect_enable();
  764. }
  765. /**
  766. * @brief enable or disable timestamp.
  767. * @param new_state (TRUE or FALSE).
  768. * @retval none.
  769. */
  770. void ertc_timestamp_enable(confirm_state new_state)
  771. {
  772. /* disable write protection */
  773. ertc_write_protect_disable();
  774. ERTC->ctrl_bit.tsen = new_state;
  775. /* enable write protection */
  776. ertc_write_protect_enable();
  777. }
  778. /**
  779. * @brief get the timestamp.
  780. * @param time: time.
  781. * @param date: date.
  782. * @retval none.
  783. */
  784. void ertc_timestamp_get(ertc_time_type* time)
  785. {
  786. ertc_reg_tstm_type tmtime;
  787. ertc_reg_tsdt_type tmdate;
  788. tmtime.tstm = ERTC->tstm;
  789. tmdate.tsdt = ERTC->tsdt;
  790. time->year = 0;
  791. time->month = ertc_bcd_to_num(tmdate.tsdt_bit.m);
  792. time->day = ertc_bcd_to_num(tmdate.tsdt_bit.d);
  793. time->week = ertc_bcd_to_num(tmdate.tsdt_bit.wk);
  794. time->hour = ertc_bcd_to_num(tmtime.tstm_bit.h);
  795. time->min = ertc_bcd_to_num(tmtime.tstm_bit.m);
  796. time->sec = ertc_bcd_to_num(tmtime.tstm_bit.s);
  797. time->ampm = (ertc_am_pm_type)tmtime.tstm_bit.ampm;
  798. }
  799. /**
  800. * @brief get the timestamp sub second.
  801. * @param none.
  802. * @retval timestamp sub second.
  803. */
  804. uint32_t ertc_timestamp_sub_second_get(void)
  805. {
  806. return ERTC->tssbs_bit.sbs;
  807. }
  808. /**
  809. * @brief enable or disable tamper pin pull up.
  810. * @param new_state (TRUE or FALSE).
  811. * @retval none.
  812. */
  813. void ertc_tamper_pull_up_enable(confirm_state new_state)
  814. {
  815. /* disable write protection */
  816. ertc_write_protect_disable();
  817. ERTC->tamp_bit.tppu = !new_state;
  818. /* enable write protection */
  819. ertc_write_protect_enable();
  820. }
  821. /**
  822. * @brief set the tamper pin pre-charge time.
  823. * @param precharge: tamper detection pre-charge time
  824. * this parameter can be one of the following values:
  825. * - ERTC_TAMPER_PR_1_ERTCCLK: pre-charge time is 1 ERTC_CLK.
  826. * - ERTC_TAMPER_PR_2_ERTCCLK: pre-charge time is 2 ERTC_CLK.
  827. * - ERTC_TAMPER_PR_4_ERTCCLK: pre-charge time is 4 ERTC_CLK.
  828. * - ERTC_TAMPER_PR_8_ERTCCLK: pre-charge time is 8 ERTC_CLK.
  829. * @retval none.
  830. */
  831. void ertc_tamper_precharge_set(ertc_tamper_precharge_type precharge)
  832. {
  833. /* disable write protection */
  834. ertc_write_protect_disable();
  835. ERTC->tamp_bit.tppr = precharge;
  836. /* enable write protection */
  837. ertc_write_protect_enable();
  838. }
  839. /**
  840. * @brief set the tamper filter time.
  841. * @param filter: tamper filter.
  842. * this parameter can be one of the following values:
  843. * - ERTC_TAMPER_FILTER_DISABLE: disable filter function.
  844. * - ERTC_TAMPER_FILTER_2: 2 consecutive samples arw valid, effective tamper event.
  845. * - ERTC_TAMPER_FILTER_4: 4 consecutive samples arw valid, effective tamper event.
  846. * - ERTC_TAMPER_FILTER_8: 8 consecutive samples arw valid, effective tamper event.
  847. * @retval none.
  848. */
  849. void ertc_tamper_filter_set(ertc_tamper_filter_type filter)
  850. {
  851. /* disable write protection */
  852. ertc_write_protect_disable();
  853. ERTC->tamp_bit.tpflt = filter;
  854. /* enable write protection */
  855. ertc_write_protect_enable();
  856. }
  857. /**
  858. * @brief set the tamper detection frequency.
  859. * @param freq: tamper detection frequency.
  860. * this parameter can be one of the following values:
  861. * - ERTC_TAMPER_FREQ_DIV_32768: ERTC_CLK / 32768.
  862. * - ERTC_TAMPER_FREQ_DIV_16384: ERTC_CLK / 16384.
  863. * - ERTC_TAMPER_FREQ_DIV_8192: ERTC_CLK / 8192.
  864. * - ERTC_TAMPER_FREQ_DIV_4096: ERTC_CLK / 4096.
  865. * - ERTC_TAMPER_FREQ_DIV_2048: ERTC_CLK / 2048.
  866. * - ERTC_TAMPER_FREQ_DIV_1024: ERTC_CLK / 1024.
  867. * - ERTC_TAMPER_FREQ_DIV_512: ERTC_CLK / 512.
  868. * - ERTC_TAMPER_FREQ_DIV_256: ERTC_CLK / 256.
  869. * @retval none.
  870. */
  871. void ertc_tamper_detect_freq_set(ertc_tamper_detect_freq_type freq)
  872. {
  873. /* disable write protection */
  874. ertc_write_protect_disable();
  875. ERTC->tamp_bit.tpfreq = freq;
  876. /* enable write protection */
  877. ertc_write_protect_enable();
  878. }
  879. /**
  880. * @brief set the tamper trigger.
  881. * @param tamper_x: select the tamper.
  882. * this parameter can be one of the following values:
  883. * - ERTC_TAMPER_1: tamper 1.
  884. * @param trigger: tamper valid edge.
  885. * this parameter can be one of the following values:
  886. * - ERTC_TAMPER_EDGE_RISING: rising gedge.
  887. * - ERTC_TAMPER_EDGE_FALLING: falling gedge.
  888. * - ERTC_TAMPER_EDGE_LOW: low level.
  889. * - ERTC_TAMPER_EDGE_HIGH: high level.
  890. * @param alarm: alarm para.
  891. * @retval none.
  892. */
  893. void ertc_tamper_valid_edge_set(ertc_tamper_select_type tamper_x, ertc_tamper_valid_edge_type trigger)
  894. {
  895. /* disable write protection */
  896. ertc_write_protect_disable();
  897. if(tamper_x == ERTC_TAMPER_1)
  898. {
  899. ERTC->tamp_bit.tp1edg = trigger;
  900. }
  901. /* enable write protection */
  902. ertc_write_protect_enable();
  903. }
  904. /**
  905. * @brief enable or disable trigger timestamp when tamper event occurs.
  906. * @param new_state (TRUE or FALSE).
  907. * @retval none.
  908. */
  909. void ertc_tamper_timestamp_enable(confirm_state new_state)
  910. {
  911. /* disable write protection */
  912. ertc_write_protect_disable();
  913. ERTC->tamp_bit.tptsen = new_state;
  914. /* enable write protection */
  915. ertc_write_protect_enable();
  916. }
  917. /**
  918. * @brief enable or disable tamper.
  919. * @param tamper_x: select the tamper.
  920. * this parameter can be one of the following values:
  921. * - ERTC_TAMPER_1: tamper 1.
  922. * @param new_state (TRUE or FALSE).
  923. * @retval none.
  924. */
  925. void ertc_tamper_enable(ertc_tamper_select_type tamper_x, confirm_state new_state)
  926. {
  927. /* disable write protection */
  928. ertc_write_protect_disable();
  929. if(tamper_x == ERTC_TAMPER_1)
  930. {
  931. ERTC->tamp_bit.tp1en = new_state;
  932. }
  933. /* enable write protection */
  934. ertc_write_protect_enable();
  935. }
  936. /**
  937. * @brief enable or disable interrupt.
  938. * @param source: interrupts sources
  939. * this parameter can be any combination of the following values:
  940. * - ERTC_TP_INT: tamper interrupt.
  941. * - ERTC_ALA_INT: alarm a interrupt.
  942. * - ERTC_TS_INT: timestamp interrupt.
  943. * @param new_state (TRUE or FALSE).
  944. * @retval none.
  945. */
  946. void ertc_interrupt_enable(uint32_t source, confirm_state new_state)
  947. {
  948. /* disable write protection */
  949. ertc_write_protect_disable();
  950. if(source & ERTC_TP_INT)
  951. {
  952. if(new_state != FALSE)
  953. {
  954. ERTC->tamp |= ERTC_TP_INT;
  955. }
  956. else
  957. {
  958. ERTC->tamp &= ~ERTC_TP_INT;
  959. }
  960. source &= ~ERTC_TP_INT;
  961. }
  962. if(new_state != FALSE)
  963. {
  964. ERTC->ctrl |= source;
  965. }
  966. else
  967. {
  968. ERTC->ctrl &= ~source;
  969. }
  970. /* enable write protection */
  971. ertc_write_protect_enable();
  972. }
  973. /**
  974. * @brief get interrupt status
  975. * @param source
  976. * this parameter can be one of the following values:
  977. * - ERTC_TP_INT: tamper interrupt.
  978. * - ERTC_ALA_INT: alarm a interrupt.
  979. * - ERTC_TS_INT: timestamp interrupt.
  980. * @retval flag_status (SET or RESET)
  981. */
  982. flag_status ertc_interrupt_get(uint32_t source)
  983. {
  984. if(source & ERTC_TP_INT)
  985. {
  986. if((ERTC->tamp & ERTC_TP_INT) != RESET)
  987. {
  988. return SET;
  989. }
  990. else
  991. {
  992. return RESET;
  993. }
  994. }
  995. if((ERTC->ctrl & source) != RESET)
  996. {
  997. return SET;
  998. }
  999. else
  1000. {
  1001. return RESET;
  1002. }
  1003. }
  1004. /**
  1005. * @brief get flag status.
  1006. * @param flag: specifies the flag to check.
  1007. * this parameter can be one of the following values:
  1008. * - ERTC_ALAWF_FLAG: alarm a register allows write flag.
  1009. * - ERTC_TADJF_FLAG: time adjustment flag.
  1010. * - ERTC_INITF_FLAG: calendar initialization flag.
  1011. * - ERTC_UPDF_FLAG: calendar update flag.
  1012. * - ERTC_IMF_FLAG: enter initialization mode flag.
  1013. * - ERTC_ALAF_FLAG: alarm clock a flag.
  1014. * - ERTC_TSF_FLAG: timestamp flag.
  1015. * - ERTC_TSOF_FLAG: timestamp overflow flag.
  1016. * - ERTC_TP1F_FLAG: tamper detection 1 flag.
  1017. * - ERTC_CALUPDF_FLAG: calibration value update completed flag.
  1018. * @retval the new state of flag (SET or RESET).
  1019. */
  1020. flag_status ertc_flag_get(uint32_t flag)
  1021. {
  1022. if((ERTC->sts & flag) != (uint32_t)RESET)
  1023. {
  1024. return SET;
  1025. }
  1026. else
  1027. {
  1028. return RESET;
  1029. }
  1030. }
  1031. /**
  1032. * @brief clear flag status
  1033. * @param flag: specifies the flag to clear.
  1034. * this parameter can be any combination of the following values:
  1035. * - ERTC_ALAWF_FLAG: alarm a register allows write flag.
  1036. * - ERTC_TADJF_FLAG: time adjustment flag.
  1037. * - ERTC_INITF_FLAG: calendar initialization flag.
  1038. * - ERTC_UPDF_FLAG: calendar update flag.
  1039. * - ERTC_IMF_FLAG: enter initialization mode flag.
  1040. * - ERTC_ALAF_FLAG: alarm clock a flag.
  1041. * - ERTC_TSF_FLAG: timestamp flag.
  1042. * - ERTC_TSOF_FLAG: timestamp overflow flag.
  1043. * - ERTC_TP1F_FLAG: tamper detection 1 flag.
  1044. * - ERTC_CALUPDF_FLAG: calibration value update completed flag.
  1045. * @retval none
  1046. */
  1047. void ertc_flag_clear(uint32_t flag)
  1048. {
  1049. ERTC->sts = ~(flag | 0x00000080) | (ERTC->sts_bit.imen << 7);
  1050. }
  1051. /**
  1052. * @brief write data to the bpr register.
  1053. * @param dt: data register
  1054. * this parameter can be one of the following values:
  1055. * - ERTC_DT1
  1056. * - ERTC_DT2
  1057. * - ERTC_DT3
  1058. * - ERTC_DT4
  1059. * - ERTC_DT5
  1060. * @param data: data to be write.
  1061. * @retval none.
  1062. */
  1063. void ertc_bpr_data_write(ertc_dt_type dt, uint32_t data)
  1064. {
  1065. __IO uint32_t reg = 0;
  1066. reg = ERTC_BASE + 0x50 + (dt * 4);
  1067. /* disable write protection */
  1068. ertc_write_protect_disable();
  1069. *(__IO uint32_t *)reg = data;
  1070. /* enable write protection */
  1071. ertc_write_protect_enable();
  1072. }
  1073. /**
  1074. * @brief read data from bpr register.
  1075. * @param dt: data register
  1076. * this parameter can be one of the following values:
  1077. * - ERTC_DT1
  1078. * - ERTC_DT2
  1079. * - ERTC_DT3
  1080. * - ERTC_DT4
  1081. * - ERTC_DT5
  1082. * @retval data value.
  1083. */
  1084. uint32_t ertc_bpr_data_read(ertc_dt_type dt)
  1085. {
  1086. __IO uint32_t reg = 0;
  1087. reg = ERTC_BASE + 0x50 + (dt * 4);
  1088. return (*(__IO uint32_t *)reg);
  1089. }
  1090. /**
  1091. * @}
  1092. */
  1093. #endif
  1094. /**
  1095. * @}
  1096. */
  1097. /**
  1098. * @}
  1099. */