mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Mon Jul 06 09:15:09 2015 +0100
Revision:
583:967d0d8b7aed
Synchronized with git revision 0a7df4e114501712e80882fb66c8a1e1ffca2dcb

Full URL: https://github.com/mbedmicro/mbed/commit/0a7df4e114501712e80882fb66c8a1e1ffca2dcb/

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 583:967d0d8b7aed 1 /**
mbed_official 583:967d0d8b7aed 2 ******************************************************************************
mbed_official 583:967d0d8b7aed 3 * @file hal_tick.c
mbed_official 583:967d0d8b7aed 4 * @author MCD Application Team
mbed_official 583:967d0d8b7aed 5 * @brief Initialization of HAL tick
mbed_official 583:967d0d8b7aed 6 ******************************************************************************
mbed_official 583:967d0d8b7aed 7 * @attention
mbed_official 583:967d0d8b7aed 8 *
mbed_official 583:967d0d8b7aed 9 * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
mbed_official 583:967d0d8b7aed 10 *
mbed_official 583:967d0d8b7aed 11 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 583:967d0d8b7aed 12 * are permitted provided that the following conditions are met:
mbed_official 583:967d0d8b7aed 13 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 583:967d0d8b7aed 14 * this list of conditions and the following disclaimer.
mbed_official 583:967d0d8b7aed 15 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 583:967d0d8b7aed 16 * this list of conditions and the following disclaimer in the documentation
mbed_official 583:967d0d8b7aed 17 * and/or other materials provided with the distribution.
mbed_official 583:967d0d8b7aed 18 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 583:967d0d8b7aed 19 * may be used to endorse or promote products derived from this software
mbed_official 583:967d0d8b7aed 20 * without specific prior written permission.
mbed_official 583:967d0d8b7aed 21 *
mbed_official 583:967d0d8b7aed 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 583:967d0d8b7aed 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 583:967d0d8b7aed 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 583:967d0d8b7aed 25 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 583:967d0d8b7aed 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 583:967d0d8b7aed 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 583:967d0d8b7aed 28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 583:967d0d8b7aed 29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 583:967d0d8b7aed 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 583:967d0d8b7aed 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 583:967d0d8b7aed 32 *
mbed_official 583:967d0d8b7aed 33 ******************************************************************************
mbed_official 583:967d0d8b7aed 34 */
mbed_official 583:967d0d8b7aed 35 #include "hal_tick.h"
mbed_official 583:967d0d8b7aed 36
mbed_official 583:967d0d8b7aed 37 TIM_HandleTypeDef TimMasterHandle;
mbed_official 583:967d0d8b7aed 38 uint32_t PreviousVal = 0;
mbed_official 583:967d0d8b7aed 39
mbed_official 583:967d0d8b7aed 40 void us_ticker_irq_handler(void);
mbed_official 583:967d0d8b7aed 41 void set_compare(uint16_t count);
mbed_official 583:967d0d8b7aed 42
mbed_official 583:967d0d8b7aed 43 extern volatile uint32_t SlaveCounter;
mbed_official 583:967d0d8b7aed 44 extern volatile uint32_t oc_int_part;
mbed_official 583:967d0d8b7aed 45 extern volatile uint16_t oc_rem_part;
mbed_official 583:967d0d8b7aed 46
mbed_official 583:967d0d8b7aed 47 // Used to increment the slave counter
mbed_official 583:967d0d8b7aed 48 void timer_update_irq_handler(void)
mbed_official 583:967d0d8b7aed 49 {
mbed_official 583:967d0d8b7aed 50 TimMasterHandle.Instance = TIM_MST;
mbed_official 583:967d0d8b7aed 51
mbed_official 583:967d0d8b7aed 52 // Clear Update interrupt flag
mbed_official 583:967d0d8b7aed 53 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_UPDATE) == SET) {
mbed_official 583:967d0d8b7aed 54 __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_UPDATE);
mbed_official 583:967d0d8b7aed 55 SlaveCounter++;
mbed_official 583:967d0d8b7aed 56 }
mbed_official 583:967d0d8b7aed 57 }
mbed_official 583:967d0d8b7aed 58
mbed_official 583:967d0d8b7aed 59 // Used for mbed timeout (channel 1) and HAL tick (channel 2)
mbed_official 583:967d0d8b7aed 60 void timer_oc_irq_handler(void)
mbed_official 583:967d0d8b7aed 61 {
mbed_official 583:967d0d8b7aed 62 uint16_t cval = TIM_MST->CNT;
mbed_official 583:967d0d8b7aed 63 TimMasterHandle.Instance = TIM_MST;
mbed_official 583:967d0d8b7aed 64
mbed_official 583:967d0d8b7aed 65 // Channel 1 for mbed timeout
mbed_official 583:967d0d8b7aed 66 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) {
mbed_official 583:967d0d8b7aed 67 __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1);
mbed_official 583:967d0d8b7aed 68 if (oc_rem_part > 0) {
mbed_official 583:967d0d8b7aed 69 set_compare(oc_rem_part); // Finish the remaining time left
mbed_official 583:967d0d8b7aed 70 oc_rem_part = 0;
mbed_official 583:967d0d8b7aed 71 } else {
mbed_official 583:967d0d8b7aed 72 if (oc_int_part > 0) {
mbed_official 583:967d0d8b7aed 73 set_compare(0xFFFF);
mbed_official 583:967d0d8b7aed 74 oc_rem_part = cval; // To finish the counter loop the next time
mbed_official 583:967d0d8b7aed 75 oc_int_part--;
mbed_official 583:967d0d8b7aed 76 } else {
mbed_official 583:967d0d8b7aed 77 us_ticker_irq_handler();
mbed_official 583:967d0d8b7aed 78 }
mbed_official 583:967d0d8b7aed 79 }
mbed_official 583:967d0d8b7aed 80 }
mbed_official 583:967d0d8b7aed 81
mbed_official 583:967d0d8b7aed 82 // Channel 2 for HAL tick
mbed_official 583:967d0d8b7aed 83 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC2) == SET) {
mbed_official 583:967d0d8b7aed 84 __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC2);
mbed_official 583:967d0d8b7aed 85 uint32_t val = __HAL_TIM_GetCounter(&TimMasterHandle);
mbed_official 583:967d0d8b7aed 86 if ((val - PreviousVal) >= HAL_TICK_DELAY) {
mbed_official 583:967d0d8b7aed 87 // Increment HAL variable
mbed_official 583:967d0d8b7aed 88 HAL_IncTick();
mbed_official 583:967d0d8b7aed 89 // Prepare next interrupt
mbed_official 583:967d0d8b7aed 90 __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, val + HAL_TICK_DELAY);
mbed_official 583:967d0d8b7aed 91 PreviousVal = val;
mbed_official 583:967d0d8b7aed 92 }
mbed_official 583:967d0d8b7aed 93 }
mbed_official 583:967d0d8b7aed 94 }
mbed_official 583:967d0d8b7aed 95
mbed_official 583:967d0d8b7aed 96 // Reconfigure the HAL tick using a standard timer instead of systick.
mbed_official 583:967d0d8b7aed 97 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) {
mbed_official 583:967d0d8b7aed 98
mbed_official 583:967d0d8b7aed 99 static uint32_t ticker_inited=0;
mbed_official 583:967d0d8b7aed 100 if(ticker_inited)return HAL_OK;
mbed_official 583:967d0d8b7aed 101 ticker_inited=1;
mbed_official 583:967d0d8b7aed 102
mbed_official 583:967d0d8b7aed 103 // Enable timer clock
mbed_official 583:967d0d8b7aed 104 TIM_MST_RCC;
mbed_official 583:967d0d8b7aed 105
mbed_official 583:967d0d8b7aed 106 // Reset timer
mbed_official 583:967d0d8b7aed 107 TIM_MST_RESET_ON;
mbed_official 583:967d0d8b7aed 108 TIM_MST_RESET_OFF;
mbed_official 583:967d0d8b7aed 109
mbed_official 583:967d0d8b7aed 110 // Update the SystemCoreClock variable
mbed_official 583:967d0d8b7aed 111 SystemCoreClockUpdate();
mbed_official 583:967d0d8b7aed 112
mbed_official 583:967d0d8b7aed 113 // Configure time base
mbed_official 583:967d0d8b7aed 114 TimMasterHandle.Instance = TIM_MST;
mbed_official 583:967d0d8b7aed 115 TimMasterHandle.Init.Period = 0xFFFF;
mbed_official 583:967d0d8b7aed 116 TimMasterHandle.Init.Prescaler = (uint32_t)(SystemCoreClock / 1000000) - 1; // 1 us tick
mbed_official 583:967d0d8b7aed 117 TimMasterHandle.Init.ClockDivision = 0;
mbed_official 583:967d0d8b7aed 118 TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
mbed_official 583:967d0d8b7aed 119 HAL_TIM_Base_Init(&TimMasterHandle);
mbed_official 583:967d0d8b7aed 120
mbed_official 583:967d0d8b7aed 121 // Configure output compare channel 1 for mbed timeout (enabled later when used)
mbed_official 583:967d0d8b7aed 122 HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1);
mbed_official 583:967d0d8b7aed 123
mbed_official 583:967d0d8b7aed 124 // Configure output compare channel 2 for HAL tick
mbed_official 583:967d0d8b7aed 125 HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_2);
mbed_official 583:967d0d8b7aed 126 PreviousVal = __HAL_TIM_GetCounter(&TimMasterHandle);
mbed_official 583:967d0d8b7aed 127 __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, PreviousVal + HAL_TICK_DELAY);
mbed_official 583:967d0d8b7aed 128
mbed_official 583:967d0d8b7aed 129 // Configure interrupts
mbed_official 583:967d0d8b7aed 130 // Update interrupt used for 32-bit counter
mbed_official 583:967d0d8b7aed 131 // Output compare channel 1 interrupt for mbed timeout
mbed_official 583:967d0d8b7aed 132 // Output compare channel 2 interrupt for HAL tick
mbed_official 583:967d0d8b7aed 133 NVIC_SetVector(TIM_MST_UP_IRQ, (uint32_t)timer_update_irq_handler);
mbed_official 583:967d0d8b7aed 134 NVIC_EnableIRQ(TIM_MST_UP_IRQ);
mbed_official 583:967d0d8b7aed 135 NVIC_SetVector(TIM_MST_OC_IRQ, (uint32_t)timer_oc_irq_handler);
mbed_official 583:967d0d8b7aed 136 NVIC_EnableIRQ(TIM_MST_OC_IRQ);
mbed_official 583:967d0d8b7aed 137
mbed_official 583:967d0d8b7aed 138 // Enable interrupts
mbed_official 583:967d0d8b7aed 139 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_UPDATE); // For 32-bit counter
mbed_official 583:967d0d8b7aed 140 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2); // For HAL tick
mbed_official 583:967d0d8b7aed 141
mbed_official 583:967d0d8b7aed 142 // Enable timer
mbed_official 583:967d0d8b7aed 143 HAL_TIM_Base_Start(&TimMasterHandle);
mbed_official 583:967d0d8b7aed 144
mbed_official 583:967d0d8b7aed 145 return HAL_OK;
mbed_official 583:967d0d8b7aed 146 }
mbed_official 583:967d0d8b7aed 147
mbed_official 583:967d0d8b7aed 148 /**
mbed_official 583:967d0d8b7aed 149 * @}
mbed_official 583:967d0d8b7aed 150 */
mbed_official 583:967d0d8b7aed 151
mbed_official 583:967d0d8b7aed 152 /**
mbed_official 583:967d0d8b7aed 153 * @}
mbed_official 583:967d0d8b7aed 154 */
mbed_official 583:967d0d8b7aed 155
mbed_official 583:967d0d8b7aed 156 /**
mbed_official 583:967d0d8b7aed 157 * @}
mbed_official 583:967d0d8b7aed 158 */
mbed_official 583:967d0d8b7aed 159 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/