Emil Johnsen / mbed-src-STM32F030K6

Fork of mbed-src by Ermanno Brusadin

Committer:
emilj
Date:
Sun Oct 23 17:23:00 2016 +0000
Revision:
5:a95fd30f2195
Parent:
0:0a673c671a56
n/a

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ebrus 0:0a673c671a56 1 /**
ebrus 0:0a673c671a56 2 ******************************************************************************
ebrus 0:0a673c671a56 3 * @file hal_tick.c
ebrus 0:0a673c671a56 4 * @author MCD Application Team
ebrus 0:0a673c671a56 5 * @brief Initialization of HAL tick
ebrus 0:0a673c671a56 6 ******************************************************************************
ebrus 0:0a673c671a56 7 * @attention
ebrus 0:0a673c671a56 8 *
ebrus 0:0a673c671a56 9 * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
ebrus 0:0a673c671a56 10 *
ebrus 0:0a673c671a56 11 * Redistribution and use in source and binary forms, with or without modification,
ebrus 0:0a673c671a56 12 * are permitted provided that the following conditions are met:
ebrus 0:0a673c671a56 13 * 1. Redistributions of source code must retain the above copyright notice,
ebrus 0:0a673c671a56 14 * this list of conditions and the following disclaimer.
ebrus 0:0a673c671a56 15 * 2. Redistributions in binary form must reproduce the above copyright notice,
ebrus 0:0a673c671a56 16 * this list of conditions and the following disclaimer in the documentation
ebrus 0:0a673c671a56 17 * and/or other materials provided with the distribution.
ebrus 0:0a673c671a56 18 * 3. Neither the name of STMicroelectronics nor the names of its contributors
ebrus 0:0a673c671a56 19 * may be used to endorse or promote products derived from this software
ebrus 0:0a673c671a56 20 * without specific prior written permission.
ebrus 0:0a673c671a56 21 *
ebrus 0:0a673c671a56 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
ebrus 0:0a673c671a56 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
ebrus 0:0a673c671a56 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
ebrus 0:0a673c671a56 25 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
ebrus 0:0a673c671a56 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ebrus 0:0a673c671a56 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
ebrus 0:0a673c671a56 28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
ebrus 0:0a673c671a56 29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
ebrus 0:0a673c671a56 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
ebrus 0:0a673c671a56 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ebrus 0:0a673c671a56 32 *
ebrus 0:0a673c671a56 33 ******************************************************************************
ebrus 0:0a673c671a56 34 */
ebrus 0:0a673c671a56 35 #include "hal_tick.h"
ebrus 0:0a673c671a56 36
ebrus 0:0a673c671a56 37 TIM_HandleTypeDef TimMasterHandle;
ebrus 0:0a673c671a56 38 uint32_t PreviousVal = 0;
ebrus 0:0a673c671a56 39
ebrus 0:0a673c671a56 40 void us_ticker_irq_handler(void);
ebrus 0:0a673c671a56 41
ebrus 0:0a673c671a56 42 void timer_irq_handler(void) {
ebrus 0:0a673c671a56 43 // Channel 1 for mbed timeout
ebrus 0:0a673c671a56 44 if (__HAL_TIM_GET_ITSTATUS(&TimMasterHandle, TIM_IT_CC1) == SET) {
ebrus 0:0a673c671a56 45 __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1);
ebrus 0:0a673c671a56 46 us_ticker_irq_handler();
ebrus 0:0a673c671a56 47 }
ebrus 0:0a673c671a56 48
ebrus 0:0a673c671a56 49 // Channel 2 for HAL tick
ebrus 0:0a673c671a56 50 if (__HAL_TIM_GET_ITSTATUS(&TimMasterHandle, TIM_IT_CC2) == SET) {
ebrus 0:0a673c671a56 51 __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC2);
ebrus 0:0a673c671a56 52 uint32_t val = __HAL_TIM_GetCounter(&TimMasterHandle);
ebrus 0:0a673c671a56 53 if ((val - PreviousVal) >= HAL_TICK_DELAY) {
ebrus 0:0a673c671a56 54 // Increment HAL variable
ebrus 0:0a673c671a56 55 HAL_IncTick();
ebrus 0:0a673c671a56 56 // Prepare next interrupt
ebrus 0:0a673c671a56 57 __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, val + HAL_TICK_DELAY);
ebrus 0:0a673c671a56 58 PreviousVal = val;
ebrus 0:0a673c671a56 59 #if 0 // For DEBUG only
ebrus 0:0a673c671a56 60 HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_6);
ebrus 0:0a673c671a56 61 #endif
ebrus 0:0a673c671a56 62 }
ebrus 0:0a673c671a56 63 }
ebrus 0:0a673c671a56 64 }
ebrus 0:0a673c671a56 65
ebrus 0:0a673c671a56 66 // Reconfigure the HAL tick using a standard timer instead of systick.
ebrus 0:0a673c671a56 67 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) {
ebrus 0:0a673c671a56 68 // Enable timer clock
ebrus 0:0a673c671a56 69 TIM_MST_RCC;
ebrus 0:0a673c671a56 70
ebrus 0:0a673c671a56 71 // Reset timer
ebrus 0:0a673c671a56 72 TIM_MST_RESET_ON;
ebrus 0:0a673c671a56 73 TIM_MST_RESET_OFF;
ebrus 0:0a673c671a56 74
ebrus 0:0a673c671a56 75 // Configure time base
ebrus 0:0a673c671a56 76 TimMasterHandle.Instance = TIM_MST;
ebrus 0:0a673c671a56 77 TimMasterHandle.Init.Period = 0xFFFFFFFF;
ebrus 0:0a673c671a56 78 if ( SystemCoreClock == 16000000 ) {
ebrus 0:0a673c671a56 79 TimMasterHandle.Init.Prescaler = (uint32_t)( SystemCoreClock / 1000000) - 1; // 1 µs tick
ebrus 0:0a673c671a56 80 } else {
ebrus 0:0a673c671a56 81 TimMasterHandle.Init.Prescaler = (uint32_t)( SystemCoreClock / 2 / 1000000) - 1; // 1 µs tick
ebrus 0:0a673c671a56 82 }
ebrus 0:0a673c671a56 83 TimMasterHandle.Init.ClockDivision = 0;
ebrus 0:0a673c671a56 84 TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
ebrus 0:0a673c671a56 85 TimMasterHandle.Init.RepetitionCounter = 0;
ebrus 0:0a673c671a56 86 HAL_TIM_OC_Init(&TimMasterHandle);
ebrus 0:0a673c671a56 87
ebrus 0:0a673c671a56 88 NVIC_SetVector(TIM_MST_IRQ, (uint32_t)timer_irq_handler);
ebrus 0:0a673c671a56 89 NVIC_EnableIRQ(TIM_MST_IRQ);
ebrus 0:0a673c671a56 90
ebrus 0:0a673c671a56 91 // Channel 1 for mbed timeout
ebrus 0:0a673c671a56 92 HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1);
ebrus 0:0a673c671a56 93
ebrus 0:0a673c671a56 94 // Channel 2 for HAL tick
ebrus 0:0a673c671a56 95 HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_2);
ebrus 0:0a673c671a56 96 PreviousVal = __HAL_TIM_GetCounter(&TimMasterHandle);
ebrus 0:0a673c671a56 97 __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, PreviousVal + HAL_TICK_DELAY);
ebrus 0:0a673c671a56 98 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2);
ebrus 0:0a673c671a56 99
ebrus 0:0a673c671a56 100 #if 0 // For DEBUG only
ebrus 0:0a673c671a56 101 __GPIOB_CLK_ENABLE();
ebrus 0:0a673c671a56 102 GPIO_InitTypeDef GPIO_InitStruct;
ebrus 0:0a673c671a56 103 GPIO_InitStruct.Pin = GPIO_PIN_6;
ebrus 0:0a673c671a56 104 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
ebrus 0:0a673c671a56 105 GPIO_InitStruct.Pull = GPIO_PULLUP;
ebrus 0:0a673c671a56 106 GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
ebrus 0:0a673c671a56 107 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
ebrus 0:0a673c671a56 108 #endif
ebrus 0:0a673c671a56 109
ebrus 0:0a673c671a56 110 return HAL_OK;
ebrus 0:0a673c671a56 111 }
ebrus 0:0a673c671a56 112
ebrus 0:0a673c671a56 113 /**
ebrus 0:0a673c671a56 114 * @}
ebrus 0:0a673c671a56 115 */
ebrus 0:0a673c671a56 116
ebrus 0:0a673c671a56 117 /**
ebrus 0:0a673c671a56 118 * @}
ebrus 0:0a673c671a56 119 */
ebrus 0:0a673c671a56 120
ebrus 0:0a673c671a56 121 /**
ebrus 0:0a673c671a56 122 * @}
ebrus 0:0a673c671a56 123 */
ebrus 0:0a673c671a56 124 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/