added prescaler for 16 bit pwm in LPC1347 target

Fork of mbed-dev by mbed official

Committer:
JojoS
Date:
Sat Sep 10 15:32:04 2016 +0000
Revision:
147:ba84b7dc41a7
Parent:
144:ef7eb2e8f9f7
added prescaler for 16 bit timers (solution as in LPC11xx), default prescaler 31 for max 28 ms period time

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 74:9322579e4309 1 /**
mbed_official 74:9322579e4309 2 ******************************************************************************
mbed_official 74:9322579e4309 3 * @file hal_tick.c
mbed_official 74:9322579e4309 4 * @author MCD Application Team
mbed_official 74:9322579e4309 5 * @brief Initialization of HAL tick
mbed_official 74:9322579e4309 6 ******************************************************************************
mbed_official 74:9322579e4309 7 * @attention
mbed_official 74:9322579e4309 8 *
mbed_official 74:9322579e4309 9 * <h2><center>&copy; COPYRIGHT 2016 STMicroelectronics</center></h2>
mbed_official 74:9322579e4309 10 *
mbed_official 74:9322579e4309 11 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 74:9322579e4309 12 * are permitted provided that the following conditions are met:
mbed_official 74:9322579e4309 13 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 74:9322579e4309 14 * this list of conditions and the following disclaimer.
mbed_official 74:9322579e4309 15 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 74:9322579e4309 16 * this list of conditions and the following disclaimer in the documentation
mbed_official 74:9322579e4309 17 * and/or other materials provided with the distribution.
mbed_official 74:9322579e4309 18 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 74:9322579e4309 19 * may be used to endorse or promote products derived from this software
mbed_official 74:9322579e4309 20 * without specific prior written permission.
mbed_official 74:9322579e4309 21 *
mbed_official 74:9322579e4309 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 74:9322579e4309 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 74:9322579e4309 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 74:9322579e4309 25 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 74:9322579e4309 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 74:9322579e4309 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 74:9322579e4309 28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 74:9322579e4309 29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 74:9322579e4309 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 74:9322579e4309 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 74:9322579e4309 32 *
mbed_official 74:9322579e4309 33 ******************************************************************************
mbed_official 74:9322579e4309 34 */
mbed_official 74:9322579e4309 35 #include "hal_tick.h"
mbed_official 74:9322579e4309 36
mbed_official 74:9322579e4309 37 // 0=NO, 1=PG6 toggles at each tick
mbed_official 74:9322579e4309 38 #define DEBUG_TICK 0
mbed_official 74:9322579e4309 39
mbed_official 74:9322579e4309 40 TIM_HandleTypeDef TimMasterHandle;
mbed_official 74:9322579e4309 41 uint32_t PreviousVal = 0;
mbed_official 74:9322579e4309 42
mbed_official 74:9322579e4309 43 void HAL_IncTick(void);
mbed_official 74:9322579e4309 44 void us_ticker_irq_handler(void);
mbed_official 74:9322579e4309 45
mbed_official 74:9322579e4309 46 void timer_irq_handler(void) {
mbed_official 74:9322579e4309 47 // Channel 1 for mbed timeout
<> 144:ef7eb2e8f9f7 48 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) {
<> 144:ef7eb2e8f9f7 49 if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC1) == SET) {
<> 144:ef7eb2e8f9f7 50 __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1);
<> 144:ef7eb2e8f9f7 51 us_ticker_irq_handler();
<> 144:ef7eb2e8f9f7 52 }
mbed_official 74:9322579e4309 53 }
mbed_official 74:9322579e4309 54
mbed_official 74:9322579e4309 55 // Channel 2 for HAL tick
<> 144:ef7eb2e8f9f7 56 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC2) == SET) {
<> 144:ef7eb2e8f9f7 57 if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC2) == SET) {
<> 144:ef7eb2e8f9f7 58 __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC2);
<> 144:ef7eb2e8f9f7 59 uint32_t val = __HAL_TIM_GetCounter(&TimMasterHandle);
<> 144:ef7eb2e8f9f7 60 if ((val - PreviousVal) >= HAL_TICK_DELAY) {
<> 144:ef7eb2e8f9f7 61 // Increment HAL variable
<> 144:ef7eb2e8f9f7 62 HAL_IncTick();
<> 144:ef7eb2e8f9f7 63 // Prepare next interrupt
<> 144:ef7eb2e8f9f7 64 __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, val + HAL_TICK_DELAY);
<> 144:ef7eb2e8f9f7 65 PreviousVal = val;
mbed_official 74:9322579e4309 66 #if DEBUG_TICK > 0
<> 144:ef7eb2e8f9f7 67 HAL_GPIO_TogglePin(GPIOG, GPIO_PIN_6);
mbed_official 74:9322579e4309 68 #endif
<> 144:ef7eb2e8f9f7 69 }
mbed_official 74:9322579e4309 70 }
mbed_official 74:9322579e4309 71 }
mbed_official 74:9322579e4309 72 }
mbed_official 74:9322579e4309 73
mbed_official 74:9322579e4309 74 // Reconfigure the HAL tick using a standard timer instead of systick.
mbed_official 74:9322579e4309 75 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) {
mbed_official 74:9322579e4309 76 RCC_ClkInitTypeDef RCC_ClkInitStruct;
mbed_official 74:9322579e4309 77 uint32_t PclkFreq;
mbed_official 74:9322579e4309 78
mbed_official 74:9322579e4309 79 // Get clock configuration
mbed_official 74:9322579e4309 80 // Note: PclkFreq contains here the Latency (not used after)
mbed_official 74:9322579e4309 81 HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq);
mbed_official 74:9322579e4309 82
mbed_official 74:9322579e4309 83 // Get TIM5 clock value
mbed_official 74:9322579e4309 84 PclkFreq = HAL_RCC_GetPCLK1Freq();
mbed_official 74:9322579e4309 85
mbed_official 74:9322579e4309 86 // Enable timer clock
mbed_official 74:9322579e4309 87 TIM_MST_RCC;
mbed_official 74:9322579e4309 88
mbed_official 74:9322579e4309 89 // Reset timer
mbed_official 74:9322579e4309 90 TIM_MST_RESET_ON;
mbed_official 74:9322579e4309 91 TIM_MST_RESET_OFF;
mbed_official 74:9322579e4309 92
mbed_official 74:9322579e4309 93 // Configure time base
mbed_official 74:9322579e4309 94 TimMasterHandle.Instance = TIM_MST;
mbed_official 74:9322579e4309 95 TimMasterHandle.Init.Period = 0xFFFFFFFF;
mbed_official 74:9322579e4309 96
mbed_official 74:9322579e4309 97 // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx
mbed_official 74:9322579e4309 98 if (RCC_ClkInitStruct.APB1CLKDivider == RCC_HCLK_DIV1)
mbed_official 74:9322579e4309 99 TimMasterHandle.Init.Prescaler = (uint16_t)((PclkFreq) / 1000000) - 1; // 1 us tick
mbed_official 74:9322579e4309 100 else
mbed_official 74:9322579e4309 101 TimMasterHandle.Init.Prescaler = (uint16_t)((PclkFreq * 2) / 1000000) - 1; // 1 us tick
mbed_official 74:9322579e4309 102
mbed_official 74:9322579e4309 103 TimMasterHandle.Init.ClockDivision = 0;
mbed_official 74:9322579e4309 104 TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
mbed_official 74:9322579e4309 105 TimMasterHandle.Init.RepetitionCounter = 0;
mbed_official 74:9322579e4309 106 HAL_TIM_OC_Init(&TimMasterHandle);
mbed_official 74:9322579e4309 107
mbed_official 74:9322579e4309 108 NVIC_SetVector(TIM_MST_IRQ, (uint32_t)timer_irq_handler);
mbed_official 74:9322579e4309 109 NVIC_EnableIRQ(TIM_MST_IRQ);
mbed_official 74:9322579e4309 110
mbed_official 74:9322579e4309 111 // Channel 1 for mbed timeout
mbed_official 74:9322579e4309 112 HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1);
mbed_official 74:9322579e4309 113
mbed_official 74:9322579e4309 114 // Channel 2 for HAL tick
mbed_official 74:9322579e4309 115 HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_2);
mbed_official 74:9322579e4309 116 PreviousVal = __HAL_TIM_GetCounter(&TimMasterHandle);
mbed_official 74:9322579e4309 117 __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, PreviousVal + HAL_TICK_DELAY);
mbed_official 74:9322579e4309 118 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2);
mbed_official 74:9322579e4309 119
mbed_official 74:9322579e4309 120 #if DEBUG_TICK > 0
mbed_official 74:9322579e4309 121 __GPIOG_CLK_ENABLE();
mbed_official 74:9322579e4309 122 GPIO_InitTypeDef GPIO_InitStruct;
mbed_official 74:9322579e4309 123 GPIO_InitStruct.Pin = GPIO_PIN_6;
mbed_official 74:9322579e4309 124 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
mbed_official 74:9322579e4309 125 GPIO_InitStruct.Pull = GPIO_PULLUP;
mbed_official 74:9322579e4309 126 GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
mbed_official 74:9322579e4309 127 HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
mbed_official 74:9322579e4309 128 #endif
mbed_official 74:9322579e4309 129
mbed_official 74:9322579e4309 130 return HAL_OK;
mbed_official 74:9322579e4309 131 }
mbed_official 74:9322579e4309 132
<> 144:ef7eb2e8f9f7 133 __INLINE void HAL_SuspendTick(void)
<> 144:ef7eb2e8f9f7 134 {
<> 144:ef7eb2e8f9f7 135 TimMasterHandle.Instance = TIM_MST;
<> 144:ef7eb2e8f9f7 136
<> 144:ef7eb2e8f9f7 137 // Disable HAL tick and us_ticker update interrupts (used for 32 bit counter)
<> 144:ef7eb2e8f9f7 138 __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC2);
<> 144:ef7eb2e8f9f7 139 }
<> 144:ef7eb2e8f9f7 140
<> 144:ef7eb2e8f9f7 141 __INLINE void HAL_ResumeTick(void)
<> 144:ef7eb2e8f9f7 142 {
<> 144:ef7eb2e8f9f7 143 TimMasterHandle.Instance = TIM_MST;
<> 144:ef7eb2e8f9f7 144
<> 144:ef7eb2e8f9f7 145 // Enable HAL tick and us_ticker update interrupts (used for 32 bit counter)
<> 144:ef7eb2e8f9f7 146 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2);
<> 144:ef7eb2e8f9f7 147 }
mbed_official 74:9322579e4309 148 /**
mbed_official 74:9322579e4309 149 * @}
mbed_official 74:9322579e4309 150 */
mbed_official 74:9322579e4309 151
mbed_official 74:9322579e4309 152 /**
mbed_official 74:9322579e4309 153 * @}
mbed_official 74:9322579e4309 154 */
mbed_official 74:9322579e4309 155
mbed_official 74:9322579e4309 156 /**
mbed_official 74:9322579e4309 157 * @}
mbed_official 74:9322579e4309 158 */
mbed_official 74:9322579e4309 159 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/