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 int count = 0;
57 
58 /****************************************************/
59 /*
60 * \brief Initializes the ESOS system tick.
61 *
62 * \pre None assumed
63 *
64 * \post Sets up any hardware required to generate the 1.0ms tick
65 * required by ESOS.
66 *
67 * The (platform-independent) ESOS initialization code will
68 * call this function to setup and init the hardware (HWXXX
69 * MCU, in this case) to create the required timers,IRQs,etc
70 * to generate the 1.0ms ESOS system tick.
71 *
72 * \note We can either generate an IRQ every 1.0ms or longer period,
73 * we just need to make sure that ISR that increments the tick
74 * count is consistent.
75 ********************************************************/
76 
77 void __esos_hw_InitSystemTick(void) {
78 
79  // Config the SysTick
80  systick_set_clocksource(STK_CSR_CLKSOURCE_AHB);
81 
82  systick_set_reload(80e3);
83  systick_counter_enable();
84  systick_interrupt_enable();
85 
86  rcc_osc_on(RCC_HSI16);
87 
88  flash_prefetch_enable();
89  flash_set_ws(4);
90  flash_dcache_enable();
91  flash_icache_enable();
92  /* 16MHz / 4 = > 4 * 40 = 160MHz VCO => 80MHz main pll */
93  rcc_set_main_pll(RCC_PLLCFGR_PLLSRC_HSI16, 4, 40,
94  0, 0, RCC_PLLCFGR_PLLR_DIV2);
95  rcc_osc_on(RCC_PLL);
96  /* either rcc_wait_for_osc_ready() or do other things */
97 
98  /* Enable clocks for the ports we need */
99  rcc_periph_clock_enable(RCC_GPIOA);
100  rcc_periph_clock_enable(RCC_GPIOB);
101  rcc_periph_clock_enable(RCC_GPIOC);
102  rcc_periph_clock_enable(RCC_GPIOD);
103  rcc_periph_clock_enable(RCC_GPIOE);
104 
105  /* Enable clocks for peripherals we need */
106  //rcc_periph_clock_enable(RCC_USART2);
107  rcc_periph_clock_enable(RCC_SYSCFG);
108 
109  rcc_set_sysclk_source(RCC_CFGR_SW_PLL); /* careful with the param here! */
110  rcc_wait_for_sysclk_status(RCC_PLL);
111  /* FIXME - eventually handled internally */
112  rcc_ahb_frequency = 80e6;
113  rcc_apb1_frequency = 80e6;
114  rcc_apb2_frequency = 80e6;
115 
116  esos_tick_count = 0;
117 
118 } // end __esos_hw_InitSystemTick()
119 
120 /****************************************************/
121 
122 /* *****************************************************
123 * \brief Returns the ESOS system tick count.
124 *
125 * \pre ESOS system tick is running/working.
126 *
127 * \return A 32-bit value of the number of ESOS system ticks
128 * since the system has booted.
129 *
130 ********************************************************/
131 
132 uint32_t __esos_hw_GetSystemTickCount(void) {
133  // if a hardware timer register contains the free-running tick
134  // value, be sure to save the register value to esos_tick_count
135  // before returning. This is done to ensure that any rogue app
136  // that tries to read esos_tick_count directly without calling
137  // the ESOS_GET_TICKS() macro (which ultimately maps to this
138  // function) will get a tick value that is
139  // reasonable..... maybe.... sort of..... not really...
140  //
141 
142  return esos_tick_count;
143 } // end __esos_hw_GetSystemTickCount()
144 
145 void sys_tick_handler(void)
146 {
147  // ISR for the systick, named by LibOpenCM3
148  // Increment the esos tick counter
149  esos_tick_count++;
150 
151  // The timer services callback function for ESOS
152  // Must be called every tick
153  __esos_tmrSvcsExecute();
154 }
155 
156 // temporary home for the Default handler
157 void Default_Handler()
158 {
159 
160 }
161 
166 void Error_Handler(void) {
167  /* USER CODE BEGIN Error_Handler_Debug */
168  /* User can add his own implementation to report the HAL error return state */
169 
170  /* USER CODE END Error_Handler_Debug */
171 }
__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