lpc_18xx_port.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* lpc_18xx_port.c
  2. *
  3. * Copyright (C) 2006-2022 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. #include "board.h"
  22. #include "otp_18xx_43xx.h" /* For RNG */
  23. #include "timer_18xx_43xx.h"
  24. static uint32_t mTimeInit = 0;
  25. #define TIMER_SCALER 1000000
  26. static void init_time(void)
  27. {
  28. if(mTimeInit == 0) {
  29. uint32_t timerFreq;
  30. /* Set current time for RTC 2:00:00PM, 2012-10-05 */
  31. RTC_TIME_T FullTime;
  32. Chip_RTC_Init(LPC_RTC);
  33. FullTime.time[RTC_TIMETYPE_SECOND] = 0;
  34. FullTime.time[RTC_TIMETYPE_MINUTE] = 0;
  35. FullTime.time[RTC_TIMETYPE_HOUR] = 14;
  36. FullTime.time[RTC_TIMETYPE_DAYOFMONTH] = 5;
  37. FullTime.time[RTC_TIMETYPE_DAYOFWEEK] = 5;
  38. FullTime.time[RTC_TIMETYPE_DAYOFYEAR] = 279;
  39. FullTime.time[RTC_TIMETYPE_MONTH] = 10;
  40. FullTime.time[RTC_TIMETYPE_YEAR] = 2012;
  41. Chip_RTC_SetFullTime(LPC_RTC, &FullTime);
  42. /* Enable RTC (starts increase the tick counter and second counter register) */
  43. Chip_RTC_Enable(LPC_RTC, ENABLE);
  44. /* Enable timer 1 clock and reset it */
  45. Chip_TIMER_Init(LPC_TIMER2);
  46. Chip_RGU_TriggerReset(RGU_TIMER2_RST);
  47. while (Chip_RGU_InReset(RGU_TIMER2_RST)) {}
  48. /* Get timer peripheral clock rate */
  49. timerFreq = Chip_Clock_GetRate(CLK_MX_TIMER2);
  50. /* Timer setup */
  51. Chip_TIMER_Reset(LPC_TIMER2);
  52. Chip_TIMER_PrescaleSet(LPC_TIMER2, timerFreq/TIMER_SCALER);
  53. Chip_TIMER_Enable(LPC_TIMER2);
  54. mTimeInit = 1;
  55. }
  56. }
  57. double current_time()
  58. {
  59. //RTC_TIME_T FullTime;
  60. uint32_t timerMs;
  61. init_time();
  62. timerMs = Chip_TIMER_ReadCount(LPC_TIMER2);
  63. //Chip_RTC_GetFullTime(LPC_RTC, &FullTime);
  64. //(double)FullTime.time[RTC_TIMETYPE_SECOND]
  65. return (double)timerMs/TIMER_SCALER;
  66. }
  67. /* Memory location of the generated random numbers (for total of 128 bits) */
  68. static volatile uint32_t* mRandData = (uint32_t*)0x40045050;
  69. static uint32_t mRandInit = 0;
  70. static uint32_t mRandIndex = 0;
  71. uint32_t rand_gen(void)
  72. {
  73. uint32_t rand = 0;
  74. uint32_t status = LPC_OK;
  75. if(mRandIndex == 0) {
  76. if(mRandInit == 0) {
  77. Chip_OTP_Init();
  78. mRandInit = 1;
  79. }
  80. status = Chip_OTP_GenRand();
  81. }
  82. if(status == LPC_OK) {
  83. rand = mRandData[mRandIndex];
  84. }
  85. else {
  86. printf("GenRand Failed 0x%x\n", status);
  87. }
  88. if(++mRandIndex > 4) {
  89. mRandIndex = 0;
  90. }
  91. return rand;
  92. }