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