stm32l4_hal.c 4.9 KB

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