imxrt1062_hal.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. This file is part of the ChipWhisperer Example Targets
  3. Copyright (C) 2019-2020 NewAE Technology Inc.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "imxrt1062_hal.h"
  16. #include "board.h"
  17. #include "fsl_dcp.h"
  18. #include "fsl_lpuart.h"
  19. #include "fsl_snvs_lp.h"
  20. #include "pin_mux.h"
  21. #include "system_MIMXRT1062.h"
  22. #include "clock_config.h"
  23. /* This function is defined in some other functions too */
  24. __attribute__ ((weak)) void uart_puts(char * s){
  25. while(*s){
  26. putch(*(s++));
  27. }
  28. }
  29. void init_uart(void)
  30. {
  31. lpuart_config_t lpuartConfig;
  32. LPUART_GetDefaultConfig(&lpuartConfig);
  33. lpuartConfig.baudRate_Bps = 38400U;
  34. lpuartConfig.enableTx = 1;
  35. lpuartConfig.enableRx = 1;
  36. LPUART_Init(LPUART1, &lpuartConfig, 80000000U);
  37. }
  38. void putch(char c)
  39. {
  40. while (0U == (LPUART1->STAT & LPUART_STAT_TDRE_MASK)){;}
  41. LPUART1->DATA = c;
  42. /* Ensure all the data in the transmit buffer are sent out to bus. */
  43. /*
  44. while (0U == (LPUART1->STAT & LPUART_STAT_TC_MASK))
  45. {
  46. }
  47. */
  48. }
  49. char getch(void)
  50. {
  51. while (0U == (LPUART1->STAT & LPUART_STAT_RDRF_MASK)){;}
  52. return LPUART1->DATA;
  53. }
  54. void trigger_setup(void)
  55. {
  56. //Setup is done in pin_mux file
  57. ;
  58. }
  59. void trigger_high(void)
  60. {
  61. GPIO_PinWrite(GPIO1, (14U), 1U);
  62. }
  63. void trigger_low(void)
  64. {
  65. GPIO_PinWrite(GPIO1, (14U), 0U);
  66. }
  67. /*******************************************************************************
  68. * Definitions
  69. ******************************************************************************/
  70. #define EXAMPLE_LED_GPIO BOARD_USER_LED_GPIO
  71. #define EXAMPLE_LED_GPIO_PIN BOARD_USER_LED_PIN
  72. /*******************************************************************************
  73. * Prototypes
  74. ******************************************************************************/
  75. /*******************************************************************************
  76. * Variables
  77. ******************************************************************************/
  78. volatile uint32_t g_systickCounter;
  79. /* The PIN status */
  80. volatile bool g_pinSet = false;
  81. /*******************************************************************************
  82. * Code
  83. ******************************************************************************/
  84. void SysTick_Handler(void)
  85. {
  86. if (g_systickCounter != 0U)
  87. {
  88. g_systickCounter--;
  89. }
  90. }
  91. void SysTick_DelayTicks(uint32_t n)
  92. {
  93. g_systickCounter = n;
  94. while (g_systickCounter != 0U)
  95. {
  96. }
  97. }
  98. static dcp_handle_t _dcp_handle;
  99. void HW_AES128_Init(void)
  100. {
  101. dcp_config_t config;
  102. DCP_GetDefaultConfig(&config);
  103. DCP_Init(DCP, &config);
  104. _dcp_handle.channel = kDCP_Channel0;
  105. _dcp_handle.keySlot = 0;
  106. }
  107. void HW_AES128_LoadKey(uint8_t * key)
  108. {
  109. DCP_AES_SetKey(DCP, &_dcp_handle, key, 16);
  110. }
  111. void HW_AES128_Enc_pretrigger(uint8_t * pt)
  112. {
  113. ;
  114. }
  115. void HW_AES128_Enc(uint8_t * pt)
  116. {
  117. DCP_AES_EncryptEcb(DCP, &_dcp_handle, pt, pt, 16);
  118. }
  119. void HW_AES128_Enc_posttrigger(uint8_t * pt)
  120. {
  121. ;
  122. }
  123. void platform_init(void)
  124. {
  125. /* Board pin init */
  126. BOARD_InitPins();
  127. BOARD_InitBootClocks();
  128. /* Update the core clock */
  129. SystemCoreClockUpdate();
  130. init_uart();
  131. /* Check if from previous boot we had a power glitch */
  132. if(hal_glitch_detected()) {
  133. uart_puts("BOOT-GLITCH\n");
  134. }
  135. /* Power glitch detector enabled */
  136. SNVS_LP_Init(SNVS);
  137. /*
  138. if (SysTick_Config(SystemCoreClock / 1000U))
  139. {
  140. while (1)
  141. {
  142. }
  143. }
  144. while (1)
  145. {
  146. SysTick_DelayTicks(1000U);
  147. if (g_pinSet)
  148. {
  149. GPIO_PinWrite(EXAMPLE_LED_GPIO, EXAMPLE_LED_GPIO_PIN, 0U);
  150. g_pinSet = false;
  151. }
  152. else
  153. {
  154. GPIO_PinWrite(EXAMPLE_LED_GPIO, EXAMPLE_LED_GPIO_PIN, 1U);
  155. g_pinSet = true;
  156. }
  157. }
  158. */
  159. }
  160. /* Power glitch detection - appears to only be detected if caused reset */
  161. int hal_glitch_detected(void)
  162. {
  163. return SNVS->LPSR & SNVS_LPSR_PGD_MASK;
  164. }
  165. /* Reset power glitch flag */
  166. void hal_glitch_detect_reset(void)
  167. {
  168. SNVS->LPSR = SNVS_LPSR_PGD_MASK;
  169. }