ESOS32
ESOSOn32-bitProcessors
esos_stm32l4_tick.c
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2019 J. W. Bruce ("AUTHOR(S)")"
3  * All rights reserved.
4  * (J. W. Bruce, jwbruce_AT_tntech.edu, Tennessee Tech University)
5  *
6  * Permission to use, copy, modify, and distribute this software and its
7  * documentation for any purpose, without fee, and without written agreement is
8  * hereby granted, provided that the above copyright notice, the following
9  * two paragraphs and the authors appear in all copies of this software.
10  *
11  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
12  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
13  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
14  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15  *
16  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
17  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
19  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
20  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
21  *
22  * Please maintain this header in its entirety when copying/modifying
23  * these files.
24  *
25  *
26  */
27 
34 // Documentation for this file. If the \file tag isn't present,
35 // this file won't be documented.
43 // Include any HW-specific header files to pick up the HW register
44 // definitions, macros, etc.
45 #include "esos_stm32l4.h"
46 
47 // local prototypes
48 void Error_Handler(void);
49 
50 // create a "global" variable for the esos_tick count
51 // It either contains the actual tick (in the case of a using the
52 // software variable) or the system tick of the last time the tick
53 // was accessed (in the case of a hardware timer register
54 // containing the real clock tick)
55 volatile uint32_t esos_tick_count;
56 
57 /****************************************************/
58 /*
59 * \brief Initializes the ESOS system tick.
60 *
61 * \pre None assumed
62 *
63 * \post Sets up any hardware required to generate the 1.0ms tick
64 * required by ESOS.
65 *
66 * The (platform-independent) ESOS initialization code will
67 * call this function to setup and init the hardware (HWXXX
68 * MCU, in this case) to create the required timers,IRQs,etc
69 * to generate the 1.0ms ESOS system tick.
70 *
71 * \note We can either generate an IRQ every 1.0ms or longer period,
72 * we just need to make sure that ISR that increments the tick
73 * count is consistent.
74 ********************************************************/
75 void __esos_hw_InitSystemTick(void) {
76  // This implementation relies on the HAL for the TICK and
77  // many other things. So before, we get the tick going, we
78  // need to initialize the HAL
79 
80  /* Reset of all peripherals, Initializes the Flash interface and the SysTick. */
81  HAL_Init();
82 
83  // FOR NOW, we will rely on the HAL initialization code to setup
84  // its tick timer correctly. Copied from Cube-created code
85  // function SystemClock_Config(void)
86 
87  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
88  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
89  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
90 
93  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
94  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
95  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
96  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
97  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
98  RCC_OscInitStruct.PLL.PLLM = 1;
99  RCC_OscInitStruct.PLL.PLLN = 10;
100  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
101  RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
102  RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
103  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
104  Error_Handler();
105  }
108  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
109  |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
110  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
111  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
112  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
113  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
114 
115  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK) {
116  Error_Handler();
117  }
118  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2|RCC_PERIPHCLK_RNG;
119  PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
120  PeriphClkInit.RngClockSelection = RCC_RNGCLKSOURCE_PLL;
121 
122  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
123  Error_Handler();
124  }
127  if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK) {
128  Error_Handler();
129  }
130 
131  // call STM32 HAL routine the sets up our clock and hardware tick timer
132  // SystemClock_Config();
133 
134  // Finish initializing the hardware by resetting tick to
135  // the beginning of time.... zero.
136  esos_tick_count = 0;
137 
138 } // end __esos_hw_InitSystemTick()
139 
140 /****************************************************/
141 
142 /* *****************************************************
143 * \brief Returns the ESOS system tick count.
144 *
145 * \pre ESOS system tick is running/working.
146 *
147 * \return A 32-bit value of the number of ESOS system ticks
148 * since the system has booted.
149 *
150 ********************************************************/
151 uint32_t __esos_hw_GetSystemTickCount(void) {
152  // if a hardware timer register contains the free-running tick
153  // value, be sure to save the register value to esos_tick_count
154  // before returning. This is done to ensure that any rogue app
155  // that tries to read esos_tick_count directly without calling
156  // the ESOS_GET_TICKS() macro (which ultimately maps to this
157  // function) will get a tick value that is
158  // reasonable..... maybe.... sort of..... not really...
159  //
160  esos_tick_count = HAL_GetTick();
161 
162  return esos_tick_count;
163 } // end __esos_hw_GetSystemTickCount()
164 
165 
170 void Error_Handler(void) {
171  /* USER CODE BEGIN Error_Handler_Debug */
172  /* User can add his own implementation to report the HAL error return state */
173 
174  /* USER CODE END Error_Handler_Debug */
175 }
__esos_hw_InitSystemTick
void __esos_hw_InitSystemTick(void)
Definition: esos_hwxxx_tick.c:102
Error_Handler
void Error_Handler(void)
This function is executed in case of error occurrence.
Definition: esos_stm32l4_tick.c:170