1
0

time-STM32F2xx.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* time-STM32F2xx.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include "time.h"
  25. #define PERIPH_BASE ((uint32_t)0x40000000)
  26. /*-----------------------------------------------------------------------------
  27. * initialize RTC
  28. *----------------------------------------------------------------------------*/
  29. #include "stm32f2xx.h"
  30. #define assert_param(a) /* null expansion */
  31. #define RTC_RSF_MASK ((uint32_t)0xFFFFFF5F)
  32. #define SYNCHRO_TIMEOUT ((uint32_t) 0x00008000)
  33. #define Bcd2ToByte(v) \
  34. ((((uint8_t)(v & (uint8_t)0xF0) >> (uint8_t)0x4) * 10) + (v & (uint8_t)0x0F))
  35. #define RTC_TR_RESERVED_MASK ((uint32_t)0x007F7F7F)
  36. #define RTC_TR_MNT ((uint32_t)0x00007000)
  37. #define RTC_TR_MNU ((uint32_t)0x00000F00)
  38. #define PWR_OFFSET (PWR_BASE - PERIPH_BASE)
  39. #define CR_OFFSET (PWR_OFFSET + 0x00)
  40. #define DBP_BitNumber 0x08
  41. #define CR_DBP_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (DBP_BitNumber * 4))
  42. #define RTC_INIT_MASK ((uint32_t)0xFFFFFFFF)
  43. #define INITMODE_TIMEOUT ((uint32_t) 0x00010000)
  44. static void init_RTC()
  45. {
  46. __IO uint32_t initcounter = 0x00 ;
  47. uint32_t initstatus = 0x00; /* Enable the PWR clock : RCC_APB1Periph_PWR */
  48. ((uint32_t *)RCC)[0x10] |= ((uint32_t)0x10000000) ;
  49. /* Allow access to RTC */
  50. *(__IO uint32_t *) CR_DBP_BB = ENABLE ;
  51. /* RCC_LSEConfig(RCC_LSE_ON) */
  52. *(__IO uint8_t *) (RCC_BASE + 0x70) = ((uint8_t)0x00);
  53. /* Reset LSEBYP bit */
  54. *(__IO uint8_t *) (RCC_BASE + 0x70) = ((uint8_t)0x00);
  55. *(__IO uint8_t *) (RCC_BASE + 0x70) = ((uint8_t)0x01);
  56. /* Wait till LSE is ready */
  57. while((RCC->BDCR << 0x2) == 0x0) { }
  58. /* Select the RTC clock source: RCC_RTCCLKSource_LSE */
  59. ((RCC_TypeDef *)RCC)->BDCR |= (uint32_t)0x00000100;
  60. /* Enable the RTC Clock */
  61. *(__IO uint32_t *) (PERIPH_BB_BASE + (((RCC_BASE - PERIPH_BASE)+ 0x70) * 32) + (0x0F* 4)) = (uint32_t)ENABLE;
  62. *(__IO uint32_t *) CR_DBP_BB = (uint32_t)ENABLE;
  63. RTC->ISR = (uint32_t) RTC_INIT_MASK;
  64. do {
  65. initstatus = RTC->ISR & RTC_ISR_INITF;
  66. initcounter++;
  67. } while((initcounter != INITMODE_TIMEOUT) && (initstatus == 0x00));
  68. /* Disable the write protection for RTC registers */
  69. RTC->WPR = 0xCA;
  70. RTC->WPR = 0x53;
  71. RTC->CR &= ((uint32_t)~(RTC_CR_FMT)); /* Clear RTC CR FMT Bit */
  72. /* Set RTC_CR register */
  73. RTC->CR |= ((uint32_t)0x00000000) ; /* RTC_HourFormat_24 */
  74. /* Configure the RTC PRER */
  75. RTC->PRER = 0x7f ;
  76. RTC->PRER |= (uint32_t)(0xff << 16);
  77. /* Exit Initialization mode */
  78. RTC->ISR &= (uint32_t)~RTC_ISR_INIT;
  79. /* Enable the write protection for RTC registers */
  80. RTC->WPR = 0xFF;
  81. }
  82. /*-----------------------------------------------------------------------------
  83. * initialize TIM
  84. *----------------------------------------------------------------------------*/
  85. #define RCC_APB1Periph_TIM2 ((uint32_t)0x00000001)
  86. static void init_TIM()
  87. {
  88. uint16_t tmpcr1 = 0;
  89. ((uint32_t *)RCC)[0x10] |= RCC_APB1Periph_TIM2 ;
  90. tmpcr1 = TIM2->CR1 ;
  91. tmpcr1 &= (uint16_t) (~(((uint16_t)0x0010) | ((uint16_t)0x0060) ));
  92. /* CR1 &= ~(TIM_CR1_DIR | TIM_CR1_CMS) */
  93. tmpcr1 |= (uint16_t)0x0000 ; /* CR1 |= TIM_CounterMode_Up */
  94. TIM2->CR1= tmpcr1 ;
  95. TIM2->ARR = 0xffffffff ; /* ARR= TIM_Period */
  96. TIM2->PSC = 60 ; /* PSC = TIM_Prescaler */
  97. TIM2->EGR = ((uint16_t)0x0001) ; /* EGR = TIM_PSCReloadMode_Immediate */
  98. *(uint16_t *)(PERIPH_BASE+0x0) |=((uint16_t)0x0001) ;
  99. /* TIM_Cmd(TIM2, ENABLE) ; */
  100. }
  101. void init_time(void) {
  102. init_RTC() ;
  103. init_TIM() ;
  104. }
  105. static void GetTime(uint8_t *h, uint8_t *m, uint8_t *s)
  106. {
  107. uint32_t tmpreg = 0;
  108. tmpreg = (uint32_t)(RTC->TR & RTC_TR_RESERVED_MASK);
  109. *h = (uint8_t)Bcd2ToByte((uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16));
  110. *m = (uint8_t)Bcd2ToByte((uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >>8));
  111. *s = (uint8_t)Bcd2ToByte((tmpreg & (RTC_TR_ST | RTC_TR_SU)));
  112. }
  113. static uint32_t ByteToBcd2(uint8_t Value)
  114. {
  115. uint8_t bcdhigh = 0;
  116. while (Value >= 10) {
  117. bcdhigh++;
  118. Value -= 10;
  119. }
  120. return ((uint8_t)(bcdhigh << 4) | Value);
  121. }
  122. static void SetTime(uint8_t h, uint8_t m, uint8_t s)
  123. {
  124. __IO uint32_t synchrocounter = 0;
  125. uint32_t synchrostatus = 0x00;
  126. __IO uint32_t initcounter = 0;
  127. uint32_t initstatus = 0x00;
  128. uint32_t tmpreg ;
  129. tmpreg = ((ByteToBcd2(h) << 16) | (ByteToBcd2(m) << 8) | ByteToBcd2(s)) ;
  130. /* Disable the write protection for RTC registers */
  131. RTC->WPR = 0xCA;
  132. RTC->WPR = 0x53;
  133. RTC->ISR &= (uint32_t)~RTC_ISR_INIT;
  134. RTC->ISR = (uint32_t)RTC_INIT_MASK;
  135. /* Wait till RTC is in INIT state and if Time out is reached exit */
  136. do {
  137. initstatus = RTC->ISR & RTC_ISR_INITF;
  138. initcounter++;
  139. } while((initcounter != INITMODE_TIMEOUT) && (initstatus == 0x00));
  140. RTC->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
  141. RTC->ISR &= (uint32_t)RTC_RSF_MASK;
  142. /* Wait the registers to be synchronised */
  143. do {
  144. synchrostatus = RTC->ISR & RTC_ISR_RSF;
  145. synchrocounter++;
  146. } while((synchrocounter != SYNCHRO_TIMEOUT) && (synchrostatus == 0x00));
  147. RTC->WPR = 0xFF;
  148. }
  149. static void GetDate(uint8_t *y, uint8_t *m, uint8_t *d)
  150. {
  151. uint32_t tmpreg = 0;
  152. tmpreg = (uint32_t)(RTC->DR & RTC_TR_RESERVED_MASK);
  153. *y = (uint8_t)Bcd2ToByte((uint8_t)((tmpreg & (RTC_DR_YT|RTC_DR_YU)) >>16));
  154. *m = (uint8_t)Bcd2ToByte((uint8_t)((tmpreg & (RTC_DR_MT|RTC_DR_MU)) >> 8));
  155. *d = (uint8_t)Bcd2ToByte((uint8_t)(tmpreg & (RTC_DR_DT |RTC_DR_DU)));
  156. }
  157. static void SetDate(uint8_t y, uint8_t m, uint8_t d)
  158. {
  159. __IO uint32_t synchrocounter = 0;
  160. uint32_t synchrostatus = 0x00;
  161. __IO uint32_t initcounter = 0;
  162. uint32_t initstatus = 0x00;
  163. uint32_t tmpreg = 0 ;
  164. tmpreg = ((ByteToBcd2(y) << 16) | (ByteToBcd2(m) << 8) | ByteToBcd2(d)) ;
  165. /* Disable the write protection for RTC registers */
  166. RTC->WPR = 0xCA;
  167. RTC->WPR = 0x53;
  168. RTC->ISR &= (uint32_t)~RTC_ISR_INIT;
  169. RTC->ISR = (uint32_t)RTC_INIT_MASK;
  170. /* Wait till RTC is in INIT state and if Time out is reached exit */
  171. do {
  172. initstatus = RTC->ISR & RTC_ISR_INITF;
  173. initcounter++;
  174. } while((initcounter != INITMODE_TIMEOUT) && (initstatus == 0x00));
  175. RTC->DR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
  176. RTC->ISR &= (uint32_t)RTC_RSF_MASK;
  177. /* Wait the registers to be synchronised */
  178. do {
  179. synchrostatus = RTC->ISR & RTC_ISR_RSF;
  180. synchrocounter++;
  181. } while((synchrocounter != SYNCHRO_TIMEOUT) && (synchrostatus == 0x00));
  182. RTC->WPR = 0xFF;
  183. }
  184. #include <stdio.h>
  185. void CYASSL_MSG(const char *msg) ;
  186. struct tm *Cyassl_MDK_gmtime(const time_t *c)
  187. {
  188. uint8_t h, m, s ;
  189. uint8_t y, mo, d ;
  190. static struct tm date ;
  191. GetTime(&h, &m, &s) ;
  192. GetDate(&y, &mo, &d) ;
  193. date.tm_year = y + 100 ;
  194. date.tm_mon = mo - 1 ;
  195. date.tm_mday = d ;
  196. date.tm_hour = h ;
  197. date.tm_min = m ;
  198. date.tm_sec = s ;
  199. #if defined(DEBUG_CYASSL)
  200. {
  201. char msg[100] ;
  202. sprintf(msg,
  203. "Debug::Cyassl_KEIL_gmtime(DATE=/%2d/%02d/%04d TIME=%02d:%02d:%02d)\n",
  204. d, mo, y+2000, h, m, s) ;
  205. CYASSL_MSG(msg) ;
  206. }
  207. #endif
  208. return(&date) ;
  209. }
  210. double current_time()
  211. {
  212. return ((double)TIM2->CNT/1000000.0) ;
  213. }
  214. typedef struct func_args {
  215. int argc;
  216. char** argv;
  217. int return_code;
  218. } func_args;
  219. void time_main(void *args)
  220. {
  221. char * datetime ;
  222. uint8_t h, m, s ;
  223. uint8_t y, mo, d ;
  224. if( args == NULL || ((func_args *)args)->argc == 1) {
  225. GetTime(&h, &m, &s) ;
  226. GetDate(&y, &mo, &d) ;
  227. printf("Date: %d/%d/%d, Time: %02d:%02d:%02d\n",
  228. mo, d, y+2000, h, m, s) ;
  229. } else if(((func_args *)args)->argc == 3 &&
  230. ((func_args *)args)->argv[1][0] == '-' &&
  231. ((func_args *)args)->argv[1][1] == 'd' ) {
  232. datetime = ((func_args *)args)->argv[2];
  233. sscanf(datetime, "%d/%d/%d", (int *)&mo, (int *)&d, (int *) &y) ;
  234. SetDate(y-2000, mo, d) ;
  235. } else if(((func_args *)args)->argc == 3 &&
  236. ((func_args *)args)->argv[1][0] == '-' &&
  237. ((func_args *)args)->argv[1][1] == 't' ) {
  238. datetime = ((func_args *)args)->argv[2];
  239. sscanf(datetime, "%d:%d:%d",
  240. (int *)&h, (int *)&m, (int *)&s) ;
  241. SetTime(h, m, s) ;
  242. } else printf("Invalid argument\n") ;
  243. }
  244. /*******************************************************************
  245. time()
  246. ********************************************************************/
  247. time_t time(time_t * t) { return 0 ; }