Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-dev by
targets/cmsis/TARGET_STM/TARGET_STM32F4/TARGET_NUCLEO_F446RE/hal_tick.c@79:9f34958201cc, 2016-03-14 (annotated)
- Committer:
- jcassette
- Date:
- Mon Mar 14 14:20:17 2016 +0000
- Revision:
- 79:9f34958201cc
- Parent:
- 0:9b334a45a8ff
FRDM-K64F AnalogIn: add support for ADC channels with no mapped pins
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 0:9b334a45a8ff | 1 | /** |
bogdanm | 0:9b334a45a8ff | 2 | ****************************************************************************** |
bogdanm | 0:9b334a45a8ff | 3 | * @file hal_tick.c |
bogdanm | 0:9b334a45a8ff | 4 | * @author MCD Application Team |
bogdanm | 0:9b334a45a8ff | 5 | * @brief Initialization of HAL tick |
bogdanm | 0:9b334a45a8ff | 6 | ****************************************************************************** |
bogdanm | 0:9b334a45a8ff | 7 | * @attention |
bogdanm | 0:9b334a45a8ff | 8 | * |
bogdanm | 0:9b334a45a8ff | 9 | * <h2><center>© COPYRIGHT 2015 STMicroelectronics</center></h2> |
bogdanm | 0:9b334a45a8ff | 10 | * |
bogdanm | 0:9b334a45a8ff | 11 | * Redistribution and use in source and binary forms, with or without modification, |
bogdanm | 0:9b334a45a8ff | 12 | * are permitted provided that the following conditions are met: |
bogdanm | 0:9b334a45a8ff | 13 | * 1. Redistributions of source code must retain the above copyright notice, |
bogdanm | 0:9b334a45a8ff | 14 | * this list of conditions and the following disclaimer. |
bogdanm | 0:9b334a45a8ff | 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
bogdanm | 0:9b334a45a8ff | 16 | * this list of conditions and the following disclaimer in the documentation |
bogdanm | 0:9b334a45a8ff | 17 | * and/or other materials provided with the distribution. |
bogdanm | 0:9b334a45a8ff | 18 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
bogdanm | 0:9b334a45a8ff | 19 | * may be used to endorse or promote products derived from this software |
bogdanm | 0:9b334a45a8ff | 20 | * without specific prior written permission. |
bogdanm | 0:9b334a45a8ff | 21 | * |
bogdanm | 0:9b334a45a8ff | 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
bogdanm | 0:9b334a45a8ff | 23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
bogdanm | 0:9b334a45a8ff | 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
bogdanm | 0:9b334a45a8ff | 25 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
bogdanm | 0:9b334a45a8ff | 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
bogdanm | 0:9b334a45a8ff | 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
bogdanm | 0:9b334a45a8ff | 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
bogdanm | 0:9b334a45a8ff | 29 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
bogdanm | 0:9b334a45a8ff | 30 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
bogdanm | 0:9b334a45a8ff | 31 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
bogdanm | 0:9b334a45a8ff | 32 | * |
bogdanm | 0:9b334a45a8ff | 33 | ****************************************************************************** |
bogdanm | 0:9b334a45a8ff | 34 | */ |
bogdanm | 0:9b334a45a8ff | 35 | #include "hal_tick.h" |
bogdanm | 0:9b334a45a8ff | 36 | |
bogdanm | 0:9b334a45a8ff | 37 | // 0=NO, 1=PB6 toggles at each tick |
bogdanm | 0:9b334a45a8ff | 38 | #define DEBUG_TICK 0 |
bogdanm | 0:9b334a45a8ff | 39 | |
bogdanm | 0:9b334a45a8ff | 40 | TIM_HandleTypeDef TimMasterHandle; |
bogdanm | 0:9b334a45a8ff | 41 | uint32_t PreviousVal = 0; |
bogdanm | 0:9b334a45a8ff | 42 | |
bogdanm | 0:9b334a45a8ff | 43 | void us_ticker_irq_handler(void); |
bogdanm | 0:9b334a45a8ff | 44 | |
bogdanm | 0:9b334a45a8ff | 45 | void timer_irq_handler(void) { |
bogdanm | 0:9b334a45a8ff | 46 | // Channel 1 for mbed timeout |
bogdanm | 0:9b334a45a8ff | 47 | if (__HAL_TIM_GET_ITSTATUS(&TimMasterHandle, TIM_IT_CC1) == SET) { |
bogdanm | 0:9b334a45a8ff | 48 | us_ticker_irq_handler(); |
bogdanm | 0:9b334a45a8ff | 49 | } |
bogdanm | 0:9b334a45a8ff | 50 | |
bogdanm | 0:9b334a45a8ff | 51 | // Channel 2 for HAL tick |
bogdanm | 0:9b334a45a8ff | 52 | if (__HAL_TIM_GET_ITSTATUS(&TimMasterHandle, TIM_IT_CC2) == SET) { |
bogdanm | 0:9b334a45a8ff | 53 | __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC2); |
bogdanm | 0:9b334a45a8ff | 54 | uint32_t val = __HAL_TIM_GetCounter(&TimMasterHandle); |
bogdanm | 0:9b334a45a8ff | 55 | if ((val - PreviousVal) >= HAL_TICK_DELAY) { |
bogdanm | 0:9b334a45a8ff | 56 | // Increment HAL variable |
bogdanm | 0:9b334a45a8ff | 57 | HAL_IncTick(); |
bogdanm | 0:9b334a45a8ff | 58 | // Prepare next interrupt |
bogdanm | 0:9b334a45a8ff | 59 | __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, val + HAL_TICK_DELAY); |
bogdanm | 0:9b334a45a8ff | 60 | PreviousVal = val; |
bogdanm | 0:9b334a45a8ff | 61 | #if DEBUG_TICK > 0 |
bogdanm | 0:9b334a45a8ff | 62 | HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_6); |
bogdanm | 0:9b334a45a8ff | 63 | #endif |
bogdanm | 0:9b334a45a8ff | 64 | } |
bogdanm | 0:9b334a45a8ff | 65 | } |
bogdanm | 0:9b334a45a8ff | 66 | } |
bogdanm | 0:9b334a45a8ff | 67 | |
bogdanm | 0:9b334a45a8ff | 68 | // Reconfigure the HAL tick using a standard timer instead of systick. |
bogdanm | 0:9b334a45a8ff | 69 | HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) { |
bogdanm | 0:9b334a45a8ff | 70 | RCC_ClkInitTypeDef RCC_ClkInitStruct; |
bogdanm | 0:9b334a45a8ff | 71 | uint32_t PclkFreq; |
bogdanm | 0:9b334a45a8ff | 72 | |
bogdanm | 0:9b334a45a8ff | 73 | // Get clock configuration |
bogdanm | 0:9b334a45a8ff | 74 | // Note: PclkFreq contains here the Latency (not used after) |
bogdanm | 0:9b334a45a8ff | 75 | HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq); |
bogdanm | 0:9b334a45a8ff | 76 | |
bogdanm | 0:9b334a45a8ff | 77 | // Get TIM5 clock value |
bogdanm | 0:9b334a45a8ff | 78 | PclkFreq = HAL_RCC_GetPCLK1Freq(); |
bogdanm | 0:9b334a45a8ff | 79 | |
bogdanm | 0:9b334a45a8ff | 80 | // Enable timer clock |
bogdanm | 0:9b334a45a8ff | 81 | TIM_MST_RCC; |
bogdanm | 0:9b334a45a8ff | 82 | |
bogdanm | 0:9b334a45a8ff | 83 | // Reset timer |
bogdanm | 0:9b334a45a8ff | 84 | TIM_MST_RESET_ON; |
bogdanm | 0:9b334a45a8ff | 85 | TIM_MST_RESET_OFF; |
bogdanm | 0:9b334a45a8ff | 86 | |
bogdanm | 0:9b334a45a8ff | 87 | // Configure time base |
bogdanm | 0:9b334a45a8ff | 88 | TimMasterHandle.Instance = TIM_MST; |
bogdanm | 0:9b334a45a8ff | 89 | TimMasterHandle.Init.Period = 0xFFFFFFFF; |
bogdanm | 0:9b334a45a8ff | 90 | |
bogdanm | 0:9b334a45a8ff | 91 | // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx |
bogdanm | 0:9b334a45a8ff | 92 | if (RCC_ClkInitStruct.APB1CLKDivider == RCC_HCLK_DIV1) |
bogdanm | 0:9b334a45a8ff | 93 | TimMasterHandle.Init.Prescaler = (uint16_t)((PclkFreq) / 1000000) - 1; // 1 us tick |
bogdanm | 0:9b334a45a8ff | 94 | else |
bogdanm | 0:9b334a45a8ff | 95 | TimMasterHandle.Init.Prescaler = (uint16_t)((PclkFreq * 2) / 1000000) - 1; // 1 us tick |
bogdanm | 0:9b334a45a8ff | 96 | |
bogdanm | 0:9b334a45a8ff | 97 | TimMasterHandle.Init.ClockDivision = 0; |
bogdanm | 0:9b334a45a8ff | 98 | TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP; |
bogdanm | 0:9b334a45a8ff | 99 | TimMasterHandle.Init.RepetitionCounter = 0; |
bogdanm | 0:9b334a45a8ff | 100 | HAL_TIM_OC_Init(&TimMasterHandle); |
bogdanm | 0:9b334a45a8ff | 101 | |
bogdanm | 0:9b334a45a8ff | 102 | NVIC_SetVector(TIM_MST_IRQ, (uint32_t)timer_irq_handler); |
bogdanm | 0:9b334a45a8ff | 103 | NVIC_EnableIRQ(TIM_MST_IRQ); |
bogdanm | 0:9b334a45a8ff | 104 | |
bogdanm | 0:9b334a45a8ff | 105 | // Channel 1 for mbed timeout |
bogdanm | 0:9b334a45a8ff | 106 | HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1); |
bogdanm | 0:9b334a45a8ff | 107 | |
bogdanm | 0:9b334a45a8ff | 108 | // Channel 2 for HAL tick |
bogdanm | 0:9b334a45a8ff | 109 | HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_2); |
bogdanm | 0:9b334a45a8ff | 110 | PreviousVal = __HAL_TIM_GetCounter(&TimMasterHandle); |
bogdanm | 0:9b334a45a8ff | 111 | __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, PreviousVal + HAL_TICK_DELAY); |
bogdanm | 0:9b334a45a8ff | 112 | __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2); |
bogdanm | 0:9b334a45a8ff | 113 | |
bogdanm | 0:9b334a45a8ff | 114 | #if DEBUG_TICK > 0 |
bogdanm | 0:9b334a45a8ff | 115 | __GPIOB_CLK_ENABLE(); |
bogdanm | 0:9b334a45a8ff | 116 | GPIO_InitTypeDef GPIO_InitStruct; |
bogdanm | 0:9b334a45a8ff | 117 | GPIO_InitStruct.Pin = GPIO_PIN_6; |
bogdanm | 0:9b334a45a8ff | 118 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
bogdanm | 0:9b334a45a8ff | 119 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
bogdanm | 0:9b334a45a8ff | 120 | GPIO_InitStruct.Speed = GPIO_SPEED_FAST; |
bogdanm | 0:9b334a45a8ff | 121 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
bogdanm | 0:9b334a45a8ff | 122 | #endif |
bogdanm | 0:9b334a45a8ff | 123 | |
bogdanm | 0:9b334a45a8ff | 124 | return HAL_OK; |
bogdanm | 0:9b334a45a8ff | 125 | } |
bogdanm | 0:9b334a45a8ff | 126 | |
bogdanm | 0:9b334a45a8ff | 127 | /** |
bogdanm | 0:9b334a45a8ff | 128 | * @} |
bogdanm | 0:9b334a45a8ff | 129 | */ |
bogdanm | 0:9b334a45a8ff | 130 | |
bogdanm | 0:9b334a45a8ff | 131 | /** |
bogdanm | 0:9b334a45a8ff | 132 | * @} |
bogdanm | 0:9b334a45a8ff | 133 | */ |
bogdanm | 0:9b334a45a8ff | 134 | |
bogdanm | 0:9b334a45a8ff | 135 | /** |
bogdanm | 0:9b334a45a8ff | 136 | * @} |
bogdanm | 0:9b334a45a8ff | 137 | */ |
bogdanm | 0:9b334a45a8ff | 138 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |