stm32l5_hal.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include "stm32l5_hal.h"
  2. #include "stm32l562xx.h"
  3. #include "stm32l5xx_hal_rcc.h"
  4. #include "stm32l5xx_hal_gpio.h"
  5. #include "stm32l5xx_hal_dma.h"
  6. #include "stm32l5xx_hal_uart.h"
  7. #include "stm32l5xx_hal_cryp.h"
  8. //Just copy these from another project - hopefully ok..
  9. uint32_t SystemCoreClock = 4000000U;
  10. const uint8_t AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U};
  11. const uint8_t APBPrescTable[8] = {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U};
  12. const uint32_t MSIRangeTable[16] = {100000U, 200000U, 400000U, 800000U, 1000000U, 2000000U, \
  13. 4000000U, 8000000U, 16000000U, 24000000U, 32000000U, 48000000U, \
  14. 0U, 0U, 0U, 0U}; /* MISRAC-2012: 0U for unexpected value */
  15. void SystemInit(void)
  16. {
  17. //Init happens higher up
  18. }
  19. void SystemCoreClockUpdate(void)
  20. {
  21. ;
  22. }
  23. void HAL_IncTick(void);
  24. void SysTick_Handler(void)
  25. {
  26. HAL_IncTick();
  27. }
  28. void _exit(int status)
  29. {
  30. while(1);
  31. }
  32. UART_HandleTypeDef UartHandle;
  33. void platform_init(void)
  34. {
  35. #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
  36. SCB->CPACR |= ((3UL << 20U)|(3UL << 22U)); /* set CP10 and CP11 Full Access */
  37. #endif
  38. #ifdef USE_INTERNAL_CLK
  39. RCC_OscInitTypeDef RCC_OscInitStruct;
  40. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  41. RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
  42. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  43. RCC_OscInitStruct.PLL.PLLSource = RCC_PLL_NONE;
  44. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  45. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  46. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  47. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  48. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  49. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  50. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  51. uint32_t flash_latency = 0;
  52. HAL_RCC_ClockConfig(&RCC_ClkInitStruct, flash_latency);
  53. #else
  54. RCC_OscInitTypeDef RCC_OscInitStruct;
  55. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI;
  56. RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  57. RCC_OscInitStruct.HSIState = RCC_HSI_OFF;
  58. RCC_OscInitStruct.PLL.PLLSource = RCC_PLL_NONE;
  59. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  60. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  61. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  62. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
  63. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  64. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  65. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  66. HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_ACR_LATENCY_5WS);
  67. for (volatile int i = 0; i < 10000; i++); //firmware doesn't work unless this is here for some reason
  68. #endif
  69. //SysTick interrupt will cause power trace noise - it's also re-enabled elsewhere,
  70. //so we just disable global interrupts. Re-enable this for more interesting work.
  71. //NVIC_DisableIRQ(SysTick_IRQn);
  72. __disable_irq();
  73. }
  74. void init_uart(void)
  75. {
  76. GPIO_InitTypeDef GpioInit;
  77. GpioInit.Pin = GPIO_PIN_9 | GPIO_PIN_10;
  78. GpioInit.Mode = GPIO_MODE_AF_PP;
  79. GpioInit.Pull = GPIO_PULLUP;
  80. GpioInit.Speed = GPIO_SPEED_FREQ_HIGH;
  81. GpioInit.Alternate = GPIO_AF7_USART1;
  82. __GPIOA_CLK_ENABLE();
  83. HAL_GPIO_Init(GPIOA, &GpioInit);
  84. UartHandle.Instance = USART1;
  85. #if SS_VER==SS_VER_2_0
  86. UartHandle.Init.BaudRate = 230400;
  87. #else
  88. UartHandle.Init.BaudRate = 38400;
  89. #endif
  90. UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  91. UartHandle.Init.StopBits = UART_STOPBITS_1;
  92. UartHandle.Init.Parity = UART_PARITY_NONE;
  93. UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  94. UartHandle.Init.Mode = UART_MODE_TX_RX;
  95. __USART1_CLK_ENABLE();
  96. HAL_UART_Init(&UartHandle);
  97. }
  98. void trigger_setup(void)
  99. {
  100. GPIO_InitTypeDef GpioInit;
  101. GpioInit.Pin = GPIO_PIN_12;
  102. GpioInit.Mode = GPIO_MODE_OUTPUT_PP;
  103. GpioInit.Pull = GPIO_NOPULL;
  104. GpioInit.Speed = GPIO_SPEED_FREQ_HIGH;
  105. HAL_GPIO_Init(GPIOA, &GpioInit);
  106. }
  107. void trigger_high(void)
  108. {
  109. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, SET);
  110. }
  111. void trigger_low(void)
  112. {
  113. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, RESET);
  114. }
  115. char getch(void)
  116. {
  117. uint8_t d;
  118. while (HAL_UART_Receive(&UartHandle, &d, 1, 50) != HAL_OK)
  119. USART1->ICR |= (1 << 3);
  120. return d;
  121. }
  122. void putch(char c)
  123. {
  124. uint8_t d = c;
  125. HAL_UART_Transmit(&UartHandle, &d, 1, 5000);
  126. }
  127. CRYP_HandleTypeDef hcryp;
  128. CRYP_ConfigTypeDef ccryp;
  129. uint8_t hw_key[16];
  130. void HW_AES128_Init(void)
  131. {
  132. hcryp.Instance = AES;
  133. hcryp.Init.DataType = CRYP_DATATYPE_8B;
  134. hcryp.Init.KeySize = CRYP_KEYSIZE_128B;
  135. hcryp.Init.pKey = (uint32_t *)hw_key;
  136. hcryp.Init.Algorithm = CRYP_AES_ECB;
  137. hcryp.Init.DataWidthUnit = CRYP_DATAWIDTHUNIT_BYTE;
  138. hcryp.Init.KeyIVConfigSkip = CRYP_KEYIVCONFIG_ALWAYS;
  139. __HAL_RCC_AES_CLK_ENABLE();
  140. HAL_CRYP_Init(&hcryp);
  141. }
  142. void HW_AES128_LoadKey(uint8_t* key)
  143. {
  144. for(int i = 0; i < 16; i++)
  145. {
  146. hw_key[i] = key[i];
  147. }
  148. hcryp.Instance = AES;
  149. hcryp.Init.DataType = CRYP_DATATYPE_8B;
  150. hcryp.Init.KeySize = CRYP_KEYSIZE_128B;
  151. hcryp.Init.pKey = (uint32_t *)hw_key;
  152. hcryp.Init.Algorithm = CRYP_AES_ECB;
  153. hcryp.Init.DataWidthUnit = CRYP_DATAWIDTHUNIT_BYTE;
  154. hcryp.Init.KeyIVConfigSkip = CRYP_KEYIVCONFIG_ALWAYS;
  155. HAL_CRYP_Init(&hcryp);
  156. }
  157. void HW_AES128_Enc_pretrigger(uint8_t* pt)
  158. {
  159. ;
  160. }
  161. void HW_AES128_Enc(uint8_t* pt)
  162. {
  163. HAL_CRYP_Encrypt(&hcryp, (uint32_t *)pt, 16, (uint32_t *)pt, 1000);
  164. }
  165. void HW_AES128_Enc_posttrigger(uint8_t* pt)
  166. {
  167. ;
  168. }
  169. void HW_AES128_Dec(uint8_t *pt)
  170. {
  171. HAL_CRYP_Decrypt(&hcryp, (uint32_t *)pt, 16, (uint32_t *)pt, 1000);
  172. }