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_USART2_UART_Init(void);
79 static void MX_RTC_Init(void);
80 static void MX_RNG_Init(void);
81 
82 /* USER CODE BEGIN PFP */
83 /* Private function prototypes -----------------------------------------------*/
84 
85 /* USER CODE END PFP */
86 
87 /* USER CODE BEGIN 0 */
88 
89 /* USER CODE END 0 */
90 
91 /**
92   * @brief  The application entry point.
93   *
94   * @retval None
95   */
main(void)96 int main(void)
97 {
98   /* USER CODE BEGIN 1 */
99 
100   /* USER CODE END 1 */
101 
102   /* MCU Configuration----------------------------------------------------------*/
103 
104   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
105   HAL_Init();
106 
107   /* USER CODE BEGIN Init */
108 
109   /* USER CODE END Init */
110 
111   /* Configure the system clock */
112   SystemClock_Config();
113 
114   /* USER CODE BEGIN SysInit */
115 
116   /* USER CODE END SysInit */
117 
118   /* Initialize all configured peripherals */
119   MX_GPIO_Init();
120   MX_USART2_UART_Init();
121   MX_RTC_Init();
122   MX_USB_DEVICE_Init();
123   MX_RNG_Init();
124   /* USER CODE BEGIN 2 */
125   InitializeITM();
126   fprintf(stderr, "\r\n\r\n=========================\r\n"
127                           "= Nucleo-L476RG TPM 2.0 =\r\n"
128                           "=========================\r\n");
129   printf("Nucleo-L476RG TPM 2.0\r\n");
130 
131   if(!TpmInitializeDevice())
132   {
133       _Error_Handler(__FILE__, __LINE__);
134   }
135 
136   /* USER CODE END 2 */
137 
138   /* Infinite loop */
139   /* USER CODE BEGIN WHILE */
140   while (1)
141   {
142 
143   /* USER CODE END WHILE */
144 
145   /* USER CODE BEGIN 3 */
146       if(!TpmOperationsLoop())
147       {
148           _Error_Handler(__FILE__, __LINE__);
149       }
150 
151   }
152   /* USER CODE END 3 */
153 
154 }
155 
156 /**
157   * @brief System Clock Configuration
158   * @retval None
159   */
SystemClock_Config(void)160 void SystemClock_Config(void)
161 {
162 
163   RCC_OscInitTypeDef RCC_OscInitStruct;
164   RCC_ClkInitTypeDef RCC_ClkInitStruct;
165   RCC_PeriphCLKInitTypeDef PeriphClkInit;
166 
167     /**Configure LSE Drive Capability
168     */
169   HAL_PWR_EnableBkUpAccess();
170 
171   __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
172 
173     /**Initializes the CPU, AHB and APB busses clocks
174     */
175   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSI
176                               |RCC_OSCILLATORTYPE_LSE;
177   RCC_OscInitStruct.LSEState = RCC_LSE_ON;
178   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
179   RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
180   RCC_OscInitStruct.HSICalibrationValue = 64;
181   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
182   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
183   RCC_OscInitStruct.PLL.PLLM = 1;  // <-- This one gets dropped by V1.11.0 add me manually back in when CubeMX ran
184   RCC_OscInitStruct.PLL.PLLN = 10;
185   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
186   RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
187   RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
188   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
189   {
190     _Error_Handler(__FILE__, __LINE__);
191   }
192 
193     /**Initializes the CPU, AHB and APB busses clocks
194     */
195   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
196                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
197   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
198   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
199   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
200   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
201 
202   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
203   {
204     _Error_Handler(__FILE__, __LINE__);
205   }
206 
207   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_USART2
208                               |RCC_PERIPHCLK_USB|RCC_PERIPHCLK_RNG;
209   PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
210   PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
211   PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_HSI48;
212   PeriphClkInit.RngClockSelection = RCC_RNGCLKSOURCE_HSI48;
213   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
214   {
215     _Error_Handler(__FILE__, __LINE__);
216   }
217 
218     /**Configure the main internal regulator output voltage
219     */
220   if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
221   {
222     _Error_Handler(__FILE__, __LINE__);
223   }
224 
225     /**Configure the Systick interrupt time
226     */
227   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
228 
229     /**Configure the Systick
230     */
231   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
232 
233   /* SysTick_IRQn interrupt configuration */
234   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
235 }
236 
237 /* RNG init function */
MX_RNG_Init(void)238 static void MX_RNG_Init(void)
239 {
240 
241   hrng.Instance = RNG;
242   if (HAL_RNG_Init(&hrng) != HAL_OK)
243   {
244     _Error_Handler(__FILE__, __LINE__);
245   }
246 
247 }
248 
249 /* RTC init function */
MX_RTC_Init(void)250 static void MX_RTC_Init(void)
251 {
252 
253   RTC_TimeTypeDef sTime;
254   RTC_DateTypeDef sDate;
255 
256     /**Initialize RTC Only
257     */
258   hrtc.Instance = RTC;
259 if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0x32F2){
260   hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
261   hrtc.Init.AsynchPrediv = 127;
262   hrtc.Init.SynchPrediv = 255;
263   hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
264   hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
265   hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
266   hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
267   if (HAL_RTC_Init(&hrtc) != HAL_OK)
268   {
269     _Error_Handler(__FILE__, __LINE__);
270   }
271 
272     /**Initialize RTC and set the Time and Date
273     */
274   sTime.Hours = 0;
275   sTime.Minutes = 0;
276   sTime.Seconds = 0;
277   sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
278   sTime.StoreOperation = RTC_STOREOPERATION_RESET;
279   if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
280   {
281     _Error_Handler(__FILE__, __LINE__);
282   }
283 
284   sDate.WeekDay = RTC_WEEKDAY_MONDAY;
285   sDate.Month = RTC_MONTH_JANUARY;
286   sDate.Date = 1;
287   sDate.Year = 0;
288 
289   if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
290   {
291     _Error_Handler(__FILE__, __LINE__);
292   }
293 
294     HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0x32F2);
295   }
296 
297 }
298 
299 /* USART2 init function */
MX_USART2_UART_Init(void)300 static void MX_USART2_UART_Init(void)
301 {
302 
303   huart2.Instance = USART2;
304   huart2.Init.BaudRate = 115200;
305   huart2.Init.WordLength = UART_WORDLENGTH_8B;
306   huart2.Init.StopBits = UART_STOPBITS_1;
307   huart2.Init.Parity = UART_PARITY_NONE;
308   huart2.Init.Mode = UART_MODE_TX;
309   huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
310   huart2.Init.OverSampling = UART_OVERSAMPLING_16;
311   huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
312   huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
313   if (HAL_UART_Init(&huart2) != HAL_OK)
314   {
315     _Error_Handler(__FILE__, __LINE__);
316   }
317 
318 }
319 
320 /** Configure pins as
321         * Analog
322         * Input
323         * Output
324         * EVENT_OUT
325         * EXTI
326 */
MX_GPIO_Init(void)327 static void MX_GPIO_Init(void)
328 {
329 
330   GPIO_InitTypeDef GPIO_InitStruct;
331 
332   /* GPIO Ports Clock Enable */
333   __HAL_RCC_GPIOC_CLK_ENABLE();
334   __HAL_RCC_GPIOA_CLK_ENABLE();
335   __HAL_RCC_GPIOB_CLK_ENABLE();
336 
337   /*Configure GPIO pin Output Level */
338   HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
339 
340   /*Configure GPIO pin : B1_Pin */
341   GPIO_InitStruct.Pin = B1_Pin;
342   GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
343   GPIO_InitStruct.Pull = GPIO_NOPULL;
344   HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
345 
346   /*Configure GPIO pin : LD2_Pin */
347   GPIO_InitStruct.Pin = LD2_Pin;
348   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
349   GPIO_InitStruct.Pull = GPIO_NOPULL;
350   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
351   HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
352 
353 }
354 
355 /* USER CODE BEGIN 4 */
356 
357 /* USER CODE END 4 */
358 
359 /**
360   * @brief  This function is executed in case of error occurrence.
361   * @param  file: The file name as string.
362   * @param  line: The line in file as a number.
363   * @retval None
364   */
_Error_Handler(char * file,int line)365 void _Error_Handler(char *file, int line)
366 {
367   /* USER CODE BEGIN Error_Handler_Debug */
368     dbgPrint("PANIC: EXECUTION HALTED %s@%d\r\n", file, line);
369   /* User can add his own implementation to report the HAL error return state */
370   while(1)
371   {
372   }
373   /* USER CODE END Error_Handler_Debug */
374 }
375 
376 #ifdef  USE_FULL_ASSERT
377 /**
378   * @brief  Reports the name of the source file and the source line number
379   *         where the assert_param error has occurred.
380   * @param  file: pointer to the source file name
381   * @param  line: assert_param error line source number
382   * @retval None
383   */
assert_failed(uint8_t * file,uint32_t line)384 void assert_failed(uint8_t* file, uint32_t line)
385 {
386   /* USER CODE BEGIN 6 */
387   /* User can add his own implementation to report the file name and line number,
388      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
389   /* USER CODE END 6 */
390 }
391 #endif /* USE_FULL_ASSERT */
392 
393 /**
394   * @}
395   */
396 
397 /**
398   * @}
399   */
400 
401 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
402