mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Thu Feb 13 18:15:05 2014 +0000
Revision:
91:0a39e62a0464
Parent:
87:085cde657901
Child:
92:05f19f05c134
Synchronized with git revision 725053636acc025b458aa32fe3f76a700dcc0eed

Full URL: https://github.com/mbedmicro/mbed/commit/725053636acc025b458aa32fe3f76a700dcc0eed/

[NUCLEO_xxx] Fix issue with attach_us function

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 87:085cde657901 1 /* mbed Microcontroller Library
mbed_official 87:085cde657901 2 * Copyright (c) 2014, STMicroelectronics
mbed_official 87:085cde657901 3 * All rights reserved.
mbed_official 87:085cde657901 4 *
mbed_official 87:085cde657901 5 * Redistribution and use in source and binary forms, with or without
mbed_official 87:085cde657901 6 * modification, are permitted provided that the following conditions are met:
mbed_official 87:085cde657901 7 *
mbed_official 87:085cde657901 8 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 87:085cde657901 9 * this list of conditions and the following disclaimer.
mbed_official 87:085cde657901 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 87:085cde657901 11 * this list of conditions and the following disclaimer in the documentation
mbed_official 87:085cde657901 12 * and/or other materials provided with the distribution.
mbed_official 87:085cde657901 13 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 87:085cde657901 14 * may be used to endorse or promote products derived from this software
mbed_official 87:085cde657901 15 * without specific prior written permission.
mbed_official 87:085cde657901 16 *
mbed_official 87:085cde657901 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 87:085cde657901 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 87:085cde657901 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 87:085cde657901 20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 87:085cde657901 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 87:085cde657901 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 87:085cde657901 23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 87:085cde657901 24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 87:085cde657901 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 87:085cde657901 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 87:085cde657901 27 */
mbed_official 87:085cde657901 28 #include <stddef.h>
mbed_official 87:085cde657901 29 #include "us_ticker_api.h"
mbed_official 87:085cde657901 30 #include "PeripheralNames.h"
mbed_official 87:085cde657901 31 #include "stm32f4xx_hal.h"
mbed_official 87:085cde657901 32
mbed_official 87:085cde657901 33 // Timer selection:
mbed_official 87:085cde657901 34 #define TIM_MST TIM1
mbed_official 87:085cde657901 35 #define TIM_MST_UP_IRQ TIM1_UP_TIM10_IRQn
mbed_official 87:085cde657901 36 #define TIM_MST_OC_IRQ TIM1_CC_IRQn
mbed_official 87:085cde657901 37 #define TIM_MST_RCC __TIM1_CLK_ENABLE()
mbed_official 87:085cde657901 38
mbed_official 87:085cde657901 39 static TIM_HandleTypeDef TimMasterHandle;
mbed_official 87:085cde657901 40
mbed_official 87:085cde657901 41 static int us_ticker_inited = 0;
mbed_official 87:085cde657901 42 static uint32_t SlaveCounter = 0;
mbed_official 91:0a39e62a0464 43 static uint32_t oc_int_part = 0;
mbed_official 91:0a39e62a0464 44 static uint16_t oc_rem_part = 0;
mbed_official 91:0a39e62a0464 45
mbed_official 91:0a39e62a0464 46 void set_compare(uint16_t count) {
mbed_official 91:0a39e62a0464 47 // Set new output compare value
mbed_official 91:0a39e62a0464 48 __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, count);
mbed_official 91:0a39e62a0464 49 // Enable IT
mbed_official 91:0a39e62a0464 50 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1);
mbed_official 91:0a39e62a0464 51 }
mbed_official 87:085cde657901 52
mbed_official 87:085cde657901 53 // Used to increment the slave counter
mbed_official 87:085cde657901 54 static void tim_update_irq_handler(void) {
mbed_official 87:085cde657901 55 SlaveCounter++;
mbed_official 87:085cde657901 56 if (__HAL_TIM_GET_ITSTATUS(&TimMasterHandle, TIM_IT_UPDATE) == SET) {
mbed_official 87:085cde657901 57 __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_UPDATE);
mbed_official 87:085cde657901 58 __HAL_TIM_SetCounter(&TimMasterHandle, 0); // Reset counter !!!
mbed_official 87:085cde657901 59 }
mbed_official 87:085cde657901 60 }
mbed_official 87:085cde657901 61
mbed_official 87:085cde657901 62 // Used by interrupt system
mbed_official 87:085cde657901 63 static void tim_oc_irq_handler(void) {
mbed_official 91:0a39e62a0464 64 uint16_t cval = TIM_MST->CNT;
mbed_official 91:0a39e62a0464 65
mbed_official 87:085cde657901 66 // Clear interrupt flag
mbed_official 87:085cde657901 67 if (__HAL_TIM_GET_ITSTATUS(&TimMasterHandle, TIM_IT_CC1) == SET) {
mbed_official 87:085cde657901 68 __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1);
mbed_official 87:085cde657901 69 }
mbed_official 91:0a39e62a0464 70
mbed_official 91:0a39e62a0464 71 if (oc_rem_part > 0) {
mbed_official 91:0a39e62a0464 72 set_compare(oc_rem_part); // Finish the remaining time left
mbed_official 91:0a39e62a0464 73 oc_rem_part = 0;
mbed_official 91:0a39e62a0464 74 }
mbed_official 91:0a39e62a0464 75 else {
mbed_official 91:0a39e62a0464 76 if (oc_int_part > 0) {
mbed_official 91:0a39e62a0464 77 set_compare(0xFFFF);
mbed_official 91:0a39e62a0464 78 oc_rem_part = cval; // To finish the counter loop the next time
mbed_official 91:0a39e62a0464 79 oc_int_part--;
mbed_official 91:0a39e62a0464 80 }
mbed_official 91:0a39e62a0464 81 else {
mbed_official 87:085cde657901 82 us_ticker_irq_handler();
mbed_official 87:085cde657901 83 }
mbed_official 91:0a39e62a0464 84 }
mbed_official 87:085cde657901 85 }
mbed_official 87:085cde657901 86
mbed_official 87:085cde657901 87 void us_ticker_init(void) {
mbed_official 87:085cde657901 88 if (us_ticker_inited) return;
mbed_official 87:085cde657901 89 us_ticker_inited = 1;
mbed_official 87:085cde657901 90
mbed_official 87:085cde657901 91 // Enable Timer clock
mbed_official 87:085cde657901 92 TIM_MST_RCC;
mbed_official 87:085cde657901 93
mbed_official 87:085cde657901 94 // Configure time base
mbed_official 87:085cde657901 95 TimMasterHandle.Instance = TIM_MST;
mbed_official 87:085cde657901 96 TimMasterHandle.Init.Period = 0xFFFF;
mbed_official 87:085cde657901 97 TimMasterHandle.Init.Prescaler = (uint32_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick
mbed_official 87:085cde657901 98 TimMasterHandle.Init.ClockDivision = 0;
mbed_official 87:085cde657901 99 TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
mbed_official 87:085cde657901 100 TimMasterHandle.Init.RepetitionCounter = 0;
mbed_official 87:085cde657901 101 HAL_TIM_OC_Init(&TimMasterHandle);
mbed_official 91:0a39e62a0464 102
mbed_official 87:085cde657901 103 // Configure interrupts
mbed_official 87:085cde657901 104 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_UPDATE);
mbed_official 87:085cde657901 105
mbed_official 91:0a39e62a0464 106 // Update interrupt used for 32-bit counter
mbed_official 87:085cde657901 107 NVIC_SetVector(TIM_MST_UP_IRQ, (uint32_t)tim_update_irq_handler);
mbed_official 87:085cde657901 108 NVIC_EnableIRQ(TIM_MST_UP_IRQ);
mbed_official 87:085cde657901 109
mbed_official 91:0a39e62a0464 110 // Output compare interrupt used for timeout feature
mbed_official 87:085cde657901 111 NVIC_SetVector(TIM_MST_OC_IRQ, (uint32_t)tim_oc_irq_handler);
mbed_official 87:085cde657901 112 NVIC_EnableIRQ(TIM_MST_OC_IRQ);
mbed_official 87:085cde657901 113
mbed_official 87:085cde657901 114 // Enable timer
mbed_official 87:085cde657901 115 HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1);
mbed_official 87:085cde657901 116 }
mbed_official 87:085cde657901 117
mbed_official 87:085cde657901 118 uint32_t us_ticker_read() {
mbed_official 87:085cde657901 119 uint32_t counter, counter2;
mbed_official 87:085cde657901 120 if (!us_ticker_inited) us_ticker_init();
mbed_official 87:085cde657901 121 // A situation might appear when Master overflows right after Slave is read and before the
mbed_official 87:085cde657901 122 // new (overflowed) value of Master is read. Which would make the code below consider the
mbed_official 87:085cde657901 123 // previous (incorrect) value of Slave and the new value of Master, which would return a
mbed_official 87:085cde657901 124 // value in the past. Avoid this by computing consecutive values of the timer until they
mbed_official 87:085cde657901 125 // are properly ordered.
mbed_official 87:085cde657901 126 counter = (uint32_t)(SlaveCounter << 16);
mbed_official 87:085cde657901 127 counter += TIM_MST->CNT;
mbed_official 87:085cde657901 128 while (1) {
mbed_official 87:085cde657901 129 counter2 = (uint32_t)(SlaveCounter << 16);
mbed_official 87:085cde657901 130 counter2 += TIM_MST->CNT;
mbed_official 87:085cde657901 131 if (counter2 > counter) {
mbed_official 87:085cde657901 132 break;
mbed_official 87:085cde657901 133 }
mbed_official 87:085cde657901 134 counter = counter2;
mbed_official 87:085cde657901 135 }
mbed_official 87:085cde657901 136 return counter2;
mbed_official 87:085cde657901 137 }
mbed_official 87:085cde657901 138
mbed_official 87:085cde657901 139 void us_ticker_set_interrupt(unsigned int timestamp) {
mbed_official 87:085cde657901 140 int delta = (int)(timestamp - us_ticker_read());
mbed_official 91:0a39e62a0464 141 uint16_t cval = TIM_MST->CNT;
mbed_official 87:085cde657901 142
mbed_official 87:085cde657901 143 if (delta <= 0) { // This event was in the past
mbed_official 87:085cde657901 144 us_ticker_irq_handler();
mbed_official 87:085cde657901 145 }
mbed_official 87:085cde657901 146 else {
mbed_official 91:0a39e62a0464 147 oc_int_part = (uint32_t)(delta >> 16);
mbed_official 91:0a39e62a0464 148 oc_rem_part = (uint16_t)(delta & 0xFFFF);
mbed_official 91:0a39e62a0464 149 if (oc_rem_part <= (0xFFFF - cval)) {
mbed_official 91:0a39e62a0464 150 set_compare(cval + oc_rem_part);
mbed_official 91:0a39e62a0464 151 oc_rem_part = 0;
mbed_official 87:085cde657901 152 } else {
mbed_official 91:0a39e62a0464 153 set_compare(0xFFFF);
mbed_official 91:0a39e62a0464 154 oc_rem_part = oc_rem_part - (0xFFFF - cval);
mbed_official 87:085cde657901 155 }
mbed_official 87:085cde657901 156 }
mbed_official 87:085cde657901 157 }
mbed_official 87:085cde657901 158
mbed_official 87:085cde657901 159 void us_ticker_disable_interrupt(void) {
mbed_official 87:085cde657901 160 __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
mbed_official 87:085cde657901 161 }
mbed_official 87:085cde657901 162
mbed_official 87:085cde657901 163 void us_ticker_clear_interrupt(void) {
mbed_official 87:085cde657901 164 if (__HAL_TIM_GET_ITSTATUS(&TimMasterHandle, TIM_IT_CC1) == SET) {
mbed_official 87:085cde657901 165 __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1);
mbed_official 87:085cde657901 166 }
mbed_official 87:085cde657901 167 }