stm32f4_hal.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include "stm32f4_hal.h"
  2. #include "stm32f4_hal_lowlevel.h"
  3. #include "stm32f4xx_hal_rcc.h"
  4. #include "stm32f4xx_hal_gpio.h"
  5. #include "stm32f4xx_hal_dma.h"
  6. #include "stm32f4xx_hal_uart.h"
  7. #include "stm32f4xx_hal_cryp.h"
  8. #include "stm32f4xx_hal_rng.h"
  9. RNG_HandleTypeDef RngHandle;
  10. UART_HandleTypeDef UartHandle;
  11. uint8_t hw_key[16];
  12. static CRYP_HandleTypeDef cryp;
  13. void platform_init(void)
  14. {
  15. //HAL_Init();
  16. #ifdef STM32F4FPU
  17. /* set CP10 and CP11 Full Access */
  18. SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); // SCB->CPACR |= 0x00f00000;
  19. #endif
  20. #ifdef USE_INTERNAL_CLK
  21. RCC_OscInitTypeDef RCC_OscInitStruct;
  22. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  23. RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
  24. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  25. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; // we need PLL to use RNG
  26. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  27. RCC_OscInitStruct.PLL.PLLM = 16; // Internal clock is 16MHz.
  28. RCC_OscInitStruct.PLL.PLLN = 336;
  29. RCC_OscInitStruct.PLL.PLLP = 2;
  30. RCC_OscInitStruct.PLL.PLLQ = 7; // divisor for RNG, USB and SDIO
  31. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  32. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  33. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  34. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  35. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  36. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  37. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  38. uint32_t flash_latency = 0;
  39. HAL_RCC_ClockConfig(&RCC_ClkInitStruct, flash_latency);
  40. #elif USE_PLL
  41. RCC_OscInitTypeDef RCC_OscInitStruct;
  42. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI;
  43. RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  44. RCC_OscInitStruct.HSIState = RCC_HSI_ON; // HSI is needed for the RNG
  45. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; // we need PLL to use RNG
  46. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  47. RCC_OscInitStruct.PLL.PLLM = 12; // Internal clock is 16MHz
  48. RCC_OscInitStruct.PLL.PLLN = 196;
  49. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  50. RCC_OscInitStruct.PLL.PLLQ = 7; // divisor for RNG, USB and SDIO
  51. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
  52. for(;;);
  53. }
  54. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  55. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  56. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  57. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  58. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  59. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  60. HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_ACR_LATENCY_5WS);
  61. FLASH->ACR |= 0b111 << 8; //enable ART acceleration
  62. #else
  63. RCC_OscInitTypeDef RCC_OscInitStruct;
  64. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI;
  65. RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  66. RCC_OscInitStruct.HSIState = RCC_HSI_ON; // HSI is needed for the RNG
  67. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; // we need PLL to use RNG
  68. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  69. RCC_OscInitStruct.PLL.PLLM = 12; // Internal clock is 16MHz
  70. RCC_OscInitStruct.PLL.PLLN = 196;
  71. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  72. RCC_OscInitStruct.PLL.PLLQ = 7; // divisor for RNG, USB and SDIO
  73. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
  74. for(;;);
  75. }
  76. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  77. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  78. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
  79. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  80. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  81. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  82. HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_ACR_LATENCY_0WS); //wait states not needed for HSE
  83. #endif
  84. // Configure and starts the RNG
  85. __HAL_RCC_RNG_CLK_ENABLE();
  86. RngHandle.Instance = RNG;
  87. RngHandle.State = HAL_RNG_STATE_RESET;
  88. HAL_RNG_Init(&RngHandle);
  89. }
  90. void init_uart(void)
  91. {
  92. GPIO_InitTypeDef GpioInit;
  93. GpioInit.Pin = GPIO_PIN_9 | GPIO_PIN_10;
  94. GpioInit.Mode = GPIO_MODE_AF_PP;
  95. GpioInit.Pull = GPIO_PULLUP;
  96. GpioInit.Speed = GPIO_SPEED_FREQ_HIGH;
  97. GpioInit.Alternate = GPIO_AF7_USART1;
  98. __GPIOA_CLK_ENABLE();
  99. HAL_GPIO_Init(GPIOA, &GpioInit);
  100. UartHandle.Instance = USART1;
  101. #if SS_VER==SS_VER_2_0
  102. UartHandle.Init.BaudRate = 230400;
  103. #else
  104. UartHandle.Init.BaudRate = 38400;
  105. #endif
  106. UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  107. UartHandle.Init.StopBits = UART_STOPBITS_1;
  108. UartHandle.Init.Parity = UART_PARITY_NONE;
  109. UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  110. UartHandle.Init.Mode = UART_MODE_TX_RX;
  111. __USART1_CLK_ENABLE();
  112. HAL_UART_Init(&UartHandle);
  113. }
  114. //#define STM32F4_WLCSP
  115. void trigger_setup(void)
  116. {
  117. __GPIOA_CLK_ENABLE();
  118. #ifdef STM32F4_WLCSP
  119. GPIO_InitTypeDef GpioInit;
  120. GpioInit.Pin = GPIO_PIN_4;
  121. GpioInit.Mode = GPIO_MODE_OUTPUT_PP;
  122. GpioInit.Pull = GPIO_NOPULL;
  123. GpioInit.Speed = GPIO_SPEED_FREQ_HIGH;
  124. __GPIOD_CLK_ENABLE();
  125. HAL_GPIO_Init(GPIOD, &GpioInit);
  126. #else
  127. GPIO_InitTypeDef GpioInit;
  128. GpioInit.Pin = GPIO_PIN_12;
  129. GpioInit.Mode = GPIO_MODE_OUTPUT_PP;
  130. GpioInit.Pull = GPIO_NOPULL;
  131. GpioInit.Speed = GPIO_SPEED_FREQ_HIGH;
  132. HAL_GPIO_Init(GPIOA, &GpioInit);
  133. #endif
  134. }
  135. void trigger_high(void)
  136. {
  137. #ifdef STM32F4_WLCSP
  138. HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, SET);
  139. #else
  140. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, SET);
  141. #endif
  142. }
  143. void trigger_low(void)
  144. {
  145. #ifdef STM32F4_WLCSP
  146. HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, RESET);
  147. #else
  148. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, RESET);
  149. #endif
  150. }
  151. char getch(void)
  152. {
  153. uint8_t d;
  154. while (HAL_UART_Receive(&UartHandle, &d, 1, 5000) != HAL_OK);
  155. return d;
  156. }
  157. void putch(char c)
  158. {
  159. uint8_t d = c;
  160. HAL_UART_Transmit(&UartHandle, &d, 1, 5000);
  161. }
  162. uint32_t get_rand(void)
  163. {
  164. uint32_t prev_rand = RngHandle.RandomNumber;
  165. uint32_t next_rand;
  166. HAL_StatusTypeDef error;
  167. do {
  168. error = HAL_RNG_GenerateRandomNumber(&RngHandle, &next_rand);
  169. } while (error != HAL_OK && prev_rand == next_rand);
  170. return next_rand;
  171. }
  172. void HW_AES128_Init(void)
  173. {
  174. cryp.Instance = CRYP;
  175. cryp.Init.DataType = CRYP_DATATYPE_8B;
  176. cryp.Init.KeySize = CRYP_KEYSIZE_128B;
  177. cryp.Init.pKey = hw_key;
  178. HW_AES128_LoadKey(hw_key);
  179. __HAL_RCC_CRYP_CLK_ENABLE();
  180. HAL_CRYP_Init(&cryp);
  181. }
  182. void HW_AES128_LoadKey(uint8_t* key)
  183. {
  184. for(int i = 0; i < 16; i++)
  185. {
  186. cryp.Init.pKey[i] = key[i];
  187. }
  188. }
  189. void HW_AES128_Enc_pretrigger(uint8_t* pt)
  190. {
  191. HAL_CRYP_Init(&cryp);
  192. }
  193. void HW_AES128_Enc(uint8_t* pt)
  194. {
  195. HAL_CRYP_AESECB_Encrypt(&cryp, pt, 16, pt, 1000);
  196. }
  197. void HW_AES128_Enc_posttrigger(uint8_t* pt)
  198. {
  199. ;
  200. }
  201. void HW_AES128_Dec(uint8_t *pt)
  202. {
  203. HAL_CRYP_Init(&cryp);
  204. HAL_CRYP_AESECB_Decrypt(&cryp, pt, 16, pt, 1000);
  205. }