2
0

main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* main.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. /* Example for main.c with STM32Cube/wolfssl_example.c */
  22. #if 0 /* EXAMPLE main.c */
  23. /* Includes ------------------------------------------------------------------*/
  24. #include "wolfssl_example.h"
  25. #include "wolfssl/wolfcrypt/settings.h"
  26. /* Private variables ---------------------------------------------------------*/
  27. CRYP_HandleTypeDef hcryp;
  28. __ALIGN_BEGIN static const uint32_t pKeyCRYP[6] __ALIGN_END = {
  29. 0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000};
  30. HASH_HandleTypeDef hhash;
  31. RNG_HandleTypeDef hrng;
  32. RTC_HandleTypeDef hrtc;
  33. UART_HandleTypeDef huart4;
  34. /* Definitions for defaultTask */
  35. #ifndef SINGLE_THREADED
  36. #ifdef CMSIS_OS2_H_
  37. osThreadId_t defaultTaskHandle;
  38. const osThreadAttr_t wolfCryptDemo_attributes = {
  39. .name = "wolfCryptDemo",
  40. .priority = (osPriority_t) osPriorityNormal,
  41. .stack_size = WOLF_EXAMPLES_STACK
  42. };
  43. #else
  44. osThreadId defaultTaskHandle;
  45. #endif
  46. #endif
  47. /* Private function prototypes -----------------------------------------------*/
  48. void SystemClock_Config(void);
  49. static void MX_GPIO_Init(void);
  50. static void MX_CRYP_Init(void);
  51. static void MX_HASH_Init(void);
  52. static void MX_RNG_Init(void);
  53. static void MX_UART4_Init(void);
  54. static void MX_RTC_Init(void);
  55. /* Retargets the C library printf function to the USART. */
  56. #include <stdio.h>
  57. #ifdef __GNUC__
  58. int __io_putchar(int ch)
  59. #else
  60. int fputc(int ch, FILE *f)
  61. #endif
  62. {
  63. HAL_UART_Transmit(&HAL_CONSOLE_UART, (uint8_t *)&ch, 1, 0xFFFF);
  64. return ch;
  65. }
  66. #ifdef __GNUC__
  67. int _write(int file,char *ptr, int len)
  68. {
  69. int DataIdx;
  70. for (DataIdx= 0; DataIdx< len; DataIdx++) {
  71. __io_putchar(*ptr++);
  72. }
  73. return len;
  74. }
  75. #endif
  76. int main(void)
  77. {
  78. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  79. HAL_Init();
  80. /* Turn off buffers, so I/O occurs immediately */
  81. setvbuf(stdin, NULL, _IONBF, 0);
  82. setvbuf(stdout, NULL, _IONBF, 0);
  83. setvbuf(stderr, NULL, _IONBF, 0);
  84. /* Configure the system clock */
  85. SystemClock_Config();
  86. /* Initialize all configured peripherals */
  87. MX_GPIO_Init();
  88. MX_CRYP_Init();
  89. MX_HASH_Init();
  90. MX_RNG_Init();
  91. MX_UART4_Init();
  92. MX_RTC_Init();
  93. MX_SPI1_Init();
  94. MX_UART4_Init();
  95. #ifdef SINGLE_THREADED
  96. wolfCryptDemo(NULL);
  97. #else
  98. /* Init scheduler */
  99. osKernelInitialize();
  100. /* Create the thread(s) */
  101. /* definition and creation of defaultTask */
  102. #ifdef CMSIS_OS2_H_
  103. defaultTaskHandle = osThreadNew(wolfCryptDemo, NULL, &wolfCryptDemo_attributes);
  104. #else
  105. osThreadDef(defaultTask, wolfCryptDemo, osPriorityNormal, 0, WOLF_EXAMPLES_STACK);
  106. defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
  107. #endif
  108. /* Start scheduler */
  109. osKernelStart();
  110. /* We should never get here as control is now taken by the scheduler */
  111. /* Infinite loop */
  112. while (1) {}
  113. #endif /* SINGLE_THREADED */
  114. }
  115. /** System Clock Configuration
  116. */
  117. static void SystemClock_Config(void)
  118. {
  119. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  120. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  121. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  122. /** Configure the main internal regulator output voltage
  123. */
  124. __HAL_RCC_PWR_CLK_ENABLE();
  125. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  126. /** Initializes the CPU, AHB and APB busses clocks
  127. */
  128. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
  129. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  130. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  131. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  132. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  133. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  134. RCC_OscInitStruct.PLL.PLLM = 8;
  135. RCC_OscInitStruct.PLL.PLLN = 160;
  136. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  137. RCC_OscInitStruct.PLL.PLLQ = 7;
  138. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
  139. Error_Handler();
  140. }
  141. /** Initializes the CPU, AHB and APB busses clocks
  142. */
  143. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  144. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  145. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  146. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  147. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  148. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) {
  149. Error_Handler();
  150. }
  151. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  152. PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  153. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
  154. Error_Handler();
  155. }
  156. }
  157. /**
  158. * @brief CRYP Initialization Function
  159. * @param None
  160. * @retval None
  161. */
  162. static void MX_CRYP_Init(void)
  163. {
  164. hcryp.Instance = CRYP;
  165. hcryp.Init.DataType = CRYP_DATATYPE_32B;
  166. hcryp.Init.pKey = (uint32_t *)pKeyCRYP;
  167. hcryp.Init.Algorithm = CRYP_TDES_ECB;
  168. hcryp.Init.DataWidthUnit = CRYP_DATAWIDTHUNIT_WORD;
  169. if (HAL_CRYP_Init(&hcryp) != HAL_OK) {
  170. Error_Handler();
  171. }
  172. }
  173. /**
  174. * @brief HASH Initialization Function
  175. * @param None
  176. * @retval None
  177. */
  178. static void MX_HASH_Init(void)
  179. {
  180. hhash.Init.DataType = HASH_DATATYPE_32B;
  181. if (HAL_HASH_Init(&hhash) != HAL_OK) {
  182. Error_Handler();
  183. }
  184. }
  185. /**
  186. * @brief RNG Initialization Function
  187. * @param None
  188. * @retval None
  189. */
  190. static void MX_RNG_Init(void)
  191. {
  192. hrng.Instance = RNG;
  193. if (HAL_RNG_Init(&hrng) != HAL_OK) {
  194. Error_Handler();
  195. }
  196. }
  197. /**
  198. * @brief RTC Initialization Function
  199. * @param None
  200. * @retval None
  201. */
  202. static void MX_RTC_Init(void)
  203. {
  204. RTC_TimeTypeDef sTime = {0};
  205. RTC_DateTypeDef sDate = {0};
  206. /* Initialize RTC Only */
  207. hrtc.Instance = RTC;
  208. hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  209. hrtc.Init.AsynchPrediv = 127;
  210. hrtc.Init.SynchPrediv = 255;
  211. hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  212. hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  213. hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  214. if (HAL_RTC_Init(&hrtc) != HAL_OK) {
  215. Error_Handler();
  216. }
  217. /* Initialize RTC and set the Time and Date */
  218. sTime.Hours = 0x0;
  219. sTime.Minutes = 0x0;
  220. sTime.Seconds = 0x0;
  221. sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  222. sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  223. if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK) {
  224. Error_Handler();
  225. }
  226. sDate.WeekDay = RTC_WEEKDAY_MONDAY;
  227. sDate.Month = RTC_MONTH_JANUARY;
  228. sDate.Date = 0x1;
  229. sDate.Year = 0x0;
  230. if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK) {
  231. Error_Handler();
  232. }
  233. }
  234. /**
  235. * @brief UART4 Initialization Function
  236. * @param None
  237. * @retval None
  238. */
  239. static void MX_UART4_Init(void)
  240. {
  241. huart4.Instance = UART4;
  242. huart4.Init.BaudRate = 115200;
  243. huart4.Init.WordLength = UART_WORDLENGTH_8B;
  244. huart4.Init.StopBits = UART_STOPBITS_1;
  245. huart4.Init.Parity = UART_PARITY_NONE;
  246. huart4.Init.Mode = UART_MODE_TX_RX;
  247. huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  248. huart4.Init.OverSampling = UART_OVERSAMPLING_16;
  249. if (HAL_UART_Init(&huart4) != HAL_OK) {
  250. Error_Handler();
  251. }
  252. }
  253. /**
  254. * @brief GPIO Initialization Function
  255. * @param None
  256. * @retval None
  257. */
  258. static void MX_GPIO_Init(void)
  259. {
  260. /* GPIO Ports Clock Enable */
  261. __HAL_RCC_GPIOC_CLK_ENABLE();
  262. }
  263. /**
  264. * @brief Period elapsed callback in non blocking mode
  265. * @note This function is called when TIM1 interrupt took place, inside
  266. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  267. * a global variable "uwTick" used as application time base.
  268. * @param htim : TIM handle
  269. * @retval None
  270. */
  271. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  272. {
  273. if (htim->Instance == TIM1) {
  274. HAL_IncTick();
  275. }
  276. }
  277. /**
  278. * @brief This function is executed in case of error occurrence.
  279. * @retval None
  280. */
  281. void Error_Handler(void)
  282. {
  283. /* User can add his own implementation to report the HAL error return state */
  284. while(1)
  285. {
  286. }
  287. }
  288. #ifdef USE_FULL_ASSERT
  289. /**
  290. * @brief Reports the name of the source file and the source line number
  291. * where the assert_param error has occurred.
  292. * @param file: pointer to the source file name
  293. * @param line: assert_param error line source number
  294. * @retval None
  295. */
  296. void assert_failed(uint8_t *file, uint32_t line)
  297. {
  298. /* User can add his own implementation to report the file name and line number,
  299. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  300. }
  301. #endif /* USE_FULL_ASSERT */
  302. #if 0
  303. /* Working _sbrk example for .ld based libC malloc/free */
  304. /* Replace this with one in Core/Src/sysmem.c */
  305. /* Symbols defined in the linker script */
  306. extern uint8_t _end;
  307. extern uint8_t _estack;
  308. extern uint32_t _Min_Stack_Size;
  309. void* _sbrk(ptrdiff_t incr)
  310. {
  311. static uint8_t* __sbrk_heap_end = NULL;
  312. const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size;
  313. const uint8_t* max_heap = (uint8_t *)stack_limit;
  314. uint8_t* prev_heap_end;
  315. /* Initialize heap end at first call */
  316. if (__sbrk_heap_end == NULL) {
  317. __sbrk_heap_end = &_end;
  318. }
  319. /* Protect heap from growing into the reserved MSP stack */
  320. if (__sbrk_heap_end + incr > max_heap) {
  321. errno = ENOMEM;
  322. return (void *)-1;
  323. }
  324. prev_heap_end = __sbrk_heap_end;
  325. __sbrk_heap_end += incr;
  326. return (void*)prev_heap_end;
  327. }
  328. #endif
  329. #endif /* EXAMPLE main.c */