1
2 /**
3 ******************************************************************************
4 * @file : main.c
5 * @brief : Main program body
6 ******************************************************************************
7 * This notice applies to any and all portions of this file
8 * that are not between comment pairs USER CODE BEGIN and
9 * USER CODE END. Other portions of this file, whether
10 * inserted by the user or by software development tools
11 * are owned by their respective copyright owners.
12 *
13 * Copyright (c) 2018 STMicroelectronics International N.V.
14 * All rights reserved.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted, provided that the following conditions are met:
18 *
19 * 1. Redistribution of source code must retain the above copyright notice,
20 * this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright notice,
22 * this list of conditions and the following disclaimer in the documentation
23 * and/or other materials provided with the distribution.
24 * 3. Neither the name of STMicroelectronics nor the names of other
25 * contributors to this software may be used to endorse or promote products
26 * derived from this software without specific written permission.
27 * 4. This software, including modifications and/or derivative works of this
28 * software, must execute solely and exclusively on microcontroller or
29 * microprocessor devices manufactured by or for STMicroelectronics.
30 * 5. Redistribution and use of this software other than as permitted under
31 * this license is void and will automatically terminate your rights under
32 * this license.
33 *
34 * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
35 * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
37 * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
38 * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
39 * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
40 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
42 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
43 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
44 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
45 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 *
47 ******************************************************************************
48 */
49 /* Includes ------------------------------------------------------------------*/
50 #include "main.h"
51 #include "stm32l4xx_hal.h"
52 #include "usb_device.h"
53
54 /* USER CODE BEGIN Includes */
55 #include <stdint.h>
56 #include <stdbool.h>
57 #include <time.h>
58 #include "TpmDevice.h"
59 #include "StmUtil.h"
60
61 /* USER CODE END Includes */
62
63 /* Private variables ---------------------------------------------------------*/
64 RNG_HandleTypeDef hrng;
65
66 RTC_HandleTypeDef hrtc;
67
68 UART_HandleTypeDef huart2;
69
70 /* USER CODE BEGIN PV */
71 /* Private variables ---------------------------------------------------------*/
72
73 /* USER CODE END PV */
74
75 /* Private function prototypes -----------------------------------------------*/
76 void SystemClock_Config(void);
77 static void MX_GPIO_Init(void);
78 static void MX_RNG_Init(void);
79 static void MX_RTC_Init(void);
80 static void MX_USART2_UART_Init(void);
81
82 /* USER CODE BEGIN PFP */
83 /* Private function prototypes -----------------------------------------------*/
84 #define CPU_CORE_FREQUENCY_HZ 800000000 /* CPU core frequency in Hz */
85 void SWO_Init(uint32_t portBits, uint32_t cpuCoreFreqHz);
86 /* USER CODE END PFP */
87
88 /* USER CODE BEGIN 0 */
89
90 /* USER CODE END 0 */
91
92 /**
93 * @brief The application entry point.
94 *
95 * @retval None
96 */
main(void)97 int main(void)
98 {
99 /* USER CODE BEGIN 1 */
100
101 /* USER CODE END 1 */
102
103 /* MCU Configuration----------------------------------------------------------*/
104
105 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
106 HAL_Init();
107
108 /* USER CODE BEGIN Init */
109
110 /* USER CODE END Init */
111
112 /* Configure the system clock */
113 SystemClock_Config();
114
115 /* USER CODE BEGIN SysInit */
116
117 /* USER CODE END SysInit */
118
119 /* Initialize all configured peripherals */
120 MX_GPIO_Init();
121 MX_RNG_Init();
122 MX_RTC_Init();
123 MX_USART2_UART_Init();
124 MX_USB_DEVICE_Init();
125 /* USER CODE BEGIN 2 */
126 InitializeITM();
127 fprintf(stderr, "\r\n\r\n=========================\r\n"
128 "= Nucleo-L476RG TPM 2.0 =\r\n"
129 "=========================\r\n");
130 printf("Nucleo-L476RG TPM 2.0\r\n");
131
132 if(!TpmInitializeDevice())
133 {
134 _Error_Handler(__FILE__, __LINE__);
135 }
136
137 /* USER CODE END 2 */
138
139 /* Infinite loop */
140 /* USER CODE BEGIN WHILE */
141 while (1)
142 {
143
144 /* USER CODE END WHILE */
145
146 /* USER CODE BEGIN 3 */
147 if(!TpmOperationsLoop())
148 {
149 _Error_Handler(__FILE__, __LINE__);
150 }
151
152 }
153 /* USER CODE END 3 */
154
155 }
156
157 /**
158 * @brief System Clock Configuration
159 * @retval None
160 */
SystemClock_Config(void)161 void SystemClock_Config(void)
162 {
163
164 RCC_OscInitTypeDef RCC_OscInitStruct;
165 RCC_ClkInitTypeDef RCC_ClkInitStruct;
166 RCC_PeriphCLKInitTypeDef PeriphClkInit;
167
168 /**Configure LSE Drive Capability
169 */
170 HAL_PWR_EnableBkUpAccess();
171
172 __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
173
174 /**Initializes the CPU, AHB and APB busses clocks
175 */
176 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSE
177 |RCC_OSCILLATORTYPE_MSI;
178 RCC_OscInitStruct.LSEState = RCC_LSE_ON;
179 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
180 RCC_OscInitStruct.HSICalibrationValue = 16;
181 RCC_OscInitStruct.MSIState = RCC_MSI_ON;
182 RCC_OscInitStruct.MSICalibrationValue = 0;
183 RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_11;
184 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
185 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
186 RCC_OscInitStruct.PLL.PLLM = 1; // <-- This one gets dropped by V1.11.0 add me manually back in when CubeMX ran
187 RCC_OscInitStruct.PLL.PLLN = 10;
188 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
189 RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
190 RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
191 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
192 {
193 _Error_Handler(__FILE__, __LINE__);
194 }
195
196 /**Initializes the CPU, AHB and APB busses clocks
197 */
198 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
199 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
200 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
201 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
202 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
203 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
204
205 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
206 {
207 _Error_Handler(__FILE__, __LINE__);
208 }
209
210 PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_USART2
211 |RCC_PERIPHCLK_USB|RCC_PERIPHCLK_RNG;
212 PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
213 PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
214 PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_MSI;
215 PeriphClkInit.RngClockSelection = RCC_RNGCLKSOURCE_MSI;
216 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
217 {
218 _Error_Handler(__FILE__, __LINE__);
219 }
220
221 /**Configure the main internal regulator output voltage
222 */
223 if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
224 {
225 _Error_Handler(__FILE__, __LINE__);
226 }
227
228 /**Configure the Systick interrupt time
229 */
230 HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
231
232 /**Configure the Systick
233 */
234 HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
235
236 /**Enable MSI Auto calibration
237 */
238 HAL_RCCEx_EnableMSIPLLMode();
239
240 /* SysTick_IRQn interrupt configuration */
241 HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
242 }
243
244 /* RNG init function */
MX_RNG_Init(void)245 static void MX_RNG_Init(void)
246 {
247
248 hrng.Instance = RNG;
249 if (HAL_RNG_Init(&hrng) != HAL_OK)
250 {
251 _Error_Handler(__FILE__, __LINE__);
252 }
253
254 }
255
256 /* RTC init function */
MX_RTC_Init(void)257 static void MX_RTC_Init(void)
258 {
259
260 RTC_TimeTypeDef sTime;
261 RTC_DateTypeDef sDate;
262
263 /**Initialize RTC Only
264 */
265 hrtc.Instance = RTC;
266 if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0x32F2){
267 hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
268 hrtc.Init.AsynchPrediv = 127;
269 hrtc.Init.SynchPrediv = 255;
270 hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
271 hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
272 hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
273 hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
274 if (HAL_RTC_Init(&hrtc) != HAL_OK)
275 {
276 _Error_Handler(__FILE__, __LINE__);
277 }
278
279 /**Initialize RTC and set the Time and Date
280 */
281 sTime.Hours = 0;
282 sTime.Minutes = 0;
283 sTime.Seconds = 0;
284 sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
285 sTime.StoreOperation = RTC_STOREOPERATION_RESET;
286 if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
287 {
288 _Error_Handler(__FILE__, __LINE__);
289 }
290
291 sDate.WeekDay = RTC_WEEKDAY_MONDAY;
292 sDate.Month = RTC_MONTH_JANUARY;
293 sDate.Date = 1;
294 sDate.Year = 0;
295
296 if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
297 {
298 _Error_Handler(__FILE__, __LINE__);
299 }
300
301 HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0x32F2);
302 }
303
304 }
305
306 /* USART2 init function */
MX_USART2_UART_Init(void)307 static void MX_USART2_UART_Init(void)
308 {
309
310 huart2.Instance = USART2;
311 huart2.Init.BaudRate = 115200;
312 huart2.Init.WordLength = UART_WORDLENGTH_8B;
313 huart2.Init.StopBits = UART_STOPBITS_1;
314 huart2.Init.Parity = UART_PARITY_NONE;
315 huart2.Init.Mode = UART_MODE_TX;
316 huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
317 huart2.Init.OverSampling = UART_OVERSAMPLING_16;
318 huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
319 huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
320 if (HAL_UART_Init(&huart2) != HAL_OK)
321 {
322 _Error_Handler(__FILE__, __LINE__);
323 }
324
325 }
326
327 /** Configure pins as
328 * Analog
329 * Input
330 * Output
331 * EVENT_OUT
332 * EXTI
333 */
MX_GPIO_Init(void)334 static void MX_GPIO_Init(void)
335 {
336
337 GPIO_InitTypeDef GPIO_InitStruct;
338
339 /* GPIO Ports Clock Enable */
340 __HAL_RCC_GPIOC_CLK_ENABLE();
341 __HAL_RCC_GPIOH_CLK_ENABLE();
342 __HAL_RCC_GPIOA_CLK_ENABLE();
343 __HAL_RCC_GPIOB_CLK_ENABLE();
344
345 /*Configure GPIO pin Output Level */
346 HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
347
348 /*Configure GPIO pin : B1_Pin */
349 GPIO_InitStruct.Pin = B1_Pin;
350 GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
351 GPIO_InitStruct.Pull = GPIO_NOPULL;
352 HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
353
354 /*Configure GPIO pin : LD2_Pin */
355 GPIO_InitStruct.Pin = LD2_Pin;
356 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
357 GPIO_InitStruct.Pull = GPIO_NOPULL;
358 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
359 HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
360
361 }
362
363 /* USER CODE BEGIN 4 */
364
365 /* USER CODE END 4 */
366
367 /**
368 * @brief This function is executed in case of error occurrence.
369 * @param file: The file name as string.
370 * @param line: The line in file as a number.
371 * @retval None
372 */
_Error_Handler(char * file,int line)373 void _Error_Handler(char *file, int line)
374 {
375 /* USER CODE BEGIN Error_Handler_Debug */
376 dbgPrint("PANIC: EXECUTION HALTED %s@%d\r\n", file, line);
377 /* User can add his own implementation to report the HAL error return state */
378 while(1)
379 {
380 }
381 /* USER CODE END Error_Handler_Debug */
382 }
383
384 #ifdef USE_FULL_ASSERT
385 /**
386 * @brief Reports the name of the source file and the source line number
387 * where the assert_param error has occurred.
388 * @param file: pointer to the source file name
389 * @param line: assert_param error line source number
390 * @retval None
391 */
assert_failed(uint8_t * file,uint32_t line)392 void assert_failed(uint8_t* file, uint32_t line)
393 {
394 /* USER CODE BEGIN 6 */
395 /* User can add his own implementation to report the file name and line number,
396 tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
397 /* USER CODE END 6 */
398 }
399 #endif /* USE_FULL_ASSERT */
400
401 /**
402 * @}
403 */
404
405 /**
406 * @}
407 */
408
409 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
410