mbed lib with startup delay fixed for Nucleo401RE

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Fri Jul 31 14:00:09 2015 +0100
Revision:
599:efb83a170500
Parent:
441:d2c15dda23c1
Synchronized with git revision 5b1dc60c03fa1b3a5d082ac137d15d6584f7fe1a

Full URL: https://github.com/mbedmicro/mbed/commit/5b1dc60c03fa1b3a5d082ac137d15d6584f7fe1a/

MAXWSNENV, MAX32600MBED - Fixing pwm array search.

Who changed what in which revision?

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