|
| 1 | +/****************************************************************************** |
| 2 | +
|
| 3 | + @file IntrOS: osport.c |
| 4 | + @author Rajmund Szymanski |
| 5 | + @date 14.10.2022 |
| 6 | + @brief IntrOS port file for STM32G0 uC. |
| 7 | +
|
| 8 | + ****************************************************************************** |
| 9 | +
|
| 10 | + Copyright (c) 2018-2022 Rajmund Szymanski. All rights reserved. |
| 11 | +
|
| 12 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 13 | + of this software and associated documentation files (the "Software"), to |
| 14 | + deal in the Software without restriction, including without limitation the |
| 15 | + rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 16 | + sell copies of the Software, and to permit persons to whom the Software is |
| 17 | + furnished to do so, subject to the following conditions: |
| 18 | +
|
| 19 | + The above copyright notice and this permission notice shall be included |
| 20 | + in all copies or substantial portions of the Software. |
| 21 | +
|
| 22 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 23 | + OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 24 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 25 | + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 26 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 27 | + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 28 | + IN THE SOFTWARE. |
| 29 | +
|
| 30 | + ******************************************************************************/ |
| 31 | + |
| 32 | +#include "oskernel.h" |
| 33 | + |
| 34 | +/* -------------------------------------------------------------------------- */ |
| 35 | + |
| 36 | +void port_sys_init( void ) |
| 37 | +{ |
| 38 | +#if HW_TIMER_SIZE == 0 |
| 39 | + |
| 40 | +/****************************************************************************** |
| 41 | + Non-tick-less mode: configuration of system timer |
| 42 | + It must generate interrupts with frequency OS_FREQUENCY |
| 43 | +*******************************************************************************/ |
| 44 | + |
| 45 | + #if (CPU_FREQUENCY)/(OS_FREQUENCY)-1 <= SysTick_LOAD_RELOAD_Msk |
| 46 | + |
| 47 | + SysTick_Config((CPU_FREQUENCY)/(OS_FREQUENCY)); |
| 48 | + |
| 49 | + #elif defined(ST_FREQUENCY) && \ |
| 50 | + (ST_FREQUENCY)/(OS_FREQUENCY)-1 <= SysTick_LOAD_RELOAD_Msk |
| 51 | + |
| 52 | + NVIC_SetPriority(SysTick_IRQn, 0xFF); |
| 53 | + |
| 54 | + SysTick->LOAD = (ST_FREQUENCY)/(OS_FREQUENCY)-1; |
| 55 | + SysTick->VAL = 0U; |
| 56 | + SysTick->CTRL = SysTick_CTRL_ENABLE_Msk|SysTick_CTRL_TICKINT_Msk; |
| 57 | + |
| 58 | + #else |
| 59 | + #error Incorrect SysTick frequency! |
| 60 | + #endif |
| 61 | + |
| 62 | +/****************************************************************************** |
| 63 | + End of configuration |
| 64 | +*******************************************************************************/ |
| 65 | + |
| 66 | +#else //HW_TIMER_SIZE |
| 67 | + |
| 68 | +/****************************************************************************** |
| 69 | + Tick-less mode: configuration of system timer |
| 70 | + It must be rescaled to frequency OS_FREQUENCY |
| 71 | +*******************************************************************************/ |
| 72 | + |
| 73 | + #if (CPU_FREQUENCY)/(OS_FREQUENCY)-1 > UINT16_MAX |
| 74 | + #error Incorrect Timer frequency! |
| 75 | + #endif |
| 76 | + |
| 77 | + RCC->APBENR1 |= RCC_APBENR1_TIM7EN; |
| 78 | + #if HW_TIMER_SIZE < OS_TIMER_SIZE |
| 79 | + NVIC_SetPriority(TIM7_IRQn, 0xFF); |
| 80 | + NVIC_EnableIRQ(TIM7_IRQn); |
| 81 | + #else |
| 82 | + __ISB(); |
| 83 | + #endif |
| 84 | + #if HW_TIMER_SIZE > OS_TIMER_SIZE |
| 85 | + TIM7->ARR = CNT_MAX; |
| 86 | + #endif |
| 87 | + TIM7->PSC = (CPU_FREQUENCY)/(OS_FREQUENCY)-1; |
| 88 | + TIM7->EGR = TIM_EGR_UG; |
| 89 | + TIM7->CR1 = TIM_CR1_CEN; |
| 90 | + #if HW_TIMER_SIZE < OS_TIMER_SIZE |
| 91 | + TIM7->DIER = TIM_DIER_UIE; |
| 92 | + #endif |
| 93 | + |
| 94 | +/****************************************************************************** |
| 95 | + End of configuration |
| 96 | +*******************************************************************************/ |
| 97 | + |
| 98 | +#endif//HW_TIMER_SIZE |
| 99 | +} |
| 100 | + |
| 101 | +/* -------------------------------------------------------------------------- */ |
| 102 | + |
| 103 | +#if HW_TIMER_SIZE == 0 |
| 104 | + |
| 105 | +/****************************************************************************** |
| 106 | + Non-tick-less mode: interrupt handler of system timer |
| 107 | +*******************************************************************************/ |
| 108 | + |
| 109 | +void SysTick_Handler( void ) |
| 110 | +{ |
| 111 | + SysTick->CTRL; |
| 112 | + core_sys_tick(); |
| 113 | +} |
| 114 | + |
| 115 | +/****************************************************************************** |
| 116 | + End of the handler |
| 117 | +*******************************************************************************/ |
| 118 | + |
| 119 | +#else //HW_TIMER_SIZE |
| 120 | + |
| 121 | +/****************************************************************************** |
| 122 | + Tick-less mode: interrupt handler of system timer |
| 123 | +*******************************************************************************/ |
| 124 | + |
| 125 | +#if HW_TIMER_SIZE < OS_TIMER_SIZE |
| 126 | + |
| 127 | +void TIM7_IRQHandler( void ) |
| 128 | +{ |
| 129 | +// if (TIM7->SR & TIM_SR_UIF) |
| 130 | + { |
| 131 | + TIM7->SR = ~TIM_SR_UIF; |
| 132 | + core_sys_tick(); |
| 133 | + __ISB(); // delay because of GNUCC |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +#endif |
| 138 | + |
| 139 | +/****************************************************************************** |
| 140 | + End of the handler |
| 141 | +*******************************************************************************/ |
| 142 | + |
| 143 | +/****************************************************************************** |
| 144 | + Tick-less mode: return current system time |
| 145 | +*******************************************************************************/ |
| 146 | + |
| 147 | +#if HW_TIMER_SIZE < OS_TIMER_SIZE |
| 148 | + |
| 149 | +cnt_t port_sys_time( void ) |
| 150 | +{ |
| 151 | + cnt_t cnt; |
| 152 | + uint32_t tck; |
| 153 | + |
| 154 | + cnt = System.cnt; |
| 155 | + tck = TIM7->CNT; |
| 156 | + |
| 157 | + if (TIM7->SR & TIM_SR_UIF) |
| 158 | + { |
| 159 | + tck = TIM7->CNT; |
| 160 | + cnt += (cnt_t)(1) << (HW_TIMER_SIZE); |
| 161 | + } |
| 162 | + |
| 163 | + return cnt + tck; |
| 164 | +} |
| 165 | + |
| 166 | +#endif |
| 167 | + |
| 168 | +/****************************************************************************** |
| 169 | + End of the function |
| 170 | +*******************************************************************************/ |
| 171 | + |
| 172 | +#endif//HW_TIMER_SIZE |
| 173 | + |
| 174 | +/* -------------------------------------------------------------------------- */ |
0 commit comments