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:
Sat Feb 08 19:45:06 2014 +0000
Revision:
87:085cde657901
Child:
91:0a39e62a0464
Synchronized with git revision 9272cdeb45ec7e6077641536509413da8fd2ebc2

Full URL: https://github.com/mbedmicro/mbed/commit/9272cdeb45ec7e6077641536509413da8fd2ebc2/

Add NUCLEO_F401RE, improvements

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
mbed_official 87:085cde657901 35 #define TIM_MST TIM1
mbed_official 87:085cde657901 36 #define TIM_MST_UP_IRQ TIM1_UP_TIM10_IRQn
mbed_official 87:085cde657901 37 #define TIM_MST_OC_IRQ TIM1_CC_IRQn
mbed_official 87:085cde657901 38 #define TIM_MST_RCC __TIM1_CLK_ENABLE()
mbed_official 87:085cde657901 39
mbed_official 87:085cde657901 40 static TIM_HandleTypeDef TimMasterHandle;
mbed_official 87:085cde657901 41
mbed_official 87:085cde657901 42 static int us_ticker_inited = 0;
mbed_official 87:085cde657901 43 static uint32_t SlaveCounter = 0;
mbed_official 87:085cde657901 44 static uint32_t us_ticker_int_counter = 0;
mbed_official 87:085cde657901 45 static uint16_t us_ticker_int_remainder = 0;
mbed_official 87:085cde657901 46
mbed_official 87:085cde657901 47 // Used to increment the slave counter
mbed_official 87:085cde657901 48 static void tim_update_irq_handler(void) {
mbed_official 87:085cde657901 49 SlaveCounter++;
mbed_official 87:085cde657901 50 if (__HAL_TIM_GET_ITSTATUS(&TimMasterHandle, TIM_IT_UPDATE) == SET) {
mbed_official 87:085cde657901 51 __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_UPDATE);
mbed_official 87:085cde657901 52 __HAL_TIM_SetCounter(&TimMasterHandle, 0); // Reset counter !!!
mbed_official 87:085cde657901 53 }
mbed_official 87:085cde657901 54 }
mbed_official 87:085cde657901 55
mbed_official 87:085cde657901 56 // Used by interrupt system
mbed_official 87:085cde657901 57 static void tim_oc_irq_handler(void) {
mbed_official 87:085cde657901 58 // Clear interrupt flag
mbed_official 87:085cde657901 59 if (__HAL_TIM_GET_ITSTATUS(&TimMasterHandle, TIM_IT_CC1) == SET) {
mbed_official 87:085cde657901 60 __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1);
mbed_official 87:085cde657901 61 }
mbed_official 87:085cde657901 62
mbed_official 87:085cde657901 63 if (us_ticker_int_counter > 0) {
mbed_official 87:085cde657901 64 __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, 0xFFFF);
mbed_official 87:085cde657901 65 us_ticker_int_counter--;
mbed_official 87:085cde657901 66 } else {
mbed_official 87:085cde657901 67 if (us_ticker_int_remainder > 0) {
mbed_official 87:085cde657901 68 __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, us_ticker_int_remainder);
mbed_official 87:085cde657901 69 us_ticker_int_remainder = 0;
mbed_official 87:085cde657901 70 } else {
mbed_official 87:085cde657901 71 // This function is going to disable the interrupts if there are
mbed_official 87:085cde657901 72 // no other events in the queue
mbed_official 87:085cde657901 73 us_ticker_irq_handler();
mbed_official 87:085cde657901 74 }
mbed_official 87:085cde657901 75 }
mbed_official 87:085cde657901 76 }
mbed_official 87:085cde657901 77
mbed_official 87:085cde657901 78 void us_ticker_init(void) {
mbed_official 87:085cde657901 79 if (us_ticker_inited) return;
mbed_official 87:085cde657901 80 us_ticker_inited = 1;
mbed_official 87:085cde657901 81
mbed_official 87:085cde657901 82 // Enable Timer clock
mbed_official 87:085cde657901 83 TIM_MST_RCC;
mbed_official 87:085cde657901 84
mbed_official 87:085cde657901 85 // Configure time base
mbed_official 87:085cde657901 86 TimMasterHandle.Instance = TIM_MST;
mbed_official 87:085cde657901 87 TimMasterHandle.Init.Period = 0xFFFF;
mbed_official 87:085cde657901 88 TimMasterHandle.Init.Prescaler = (uint32_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick
mbed_official 87:085cde657901 89 TimMasterHandle.Init.ClockDivision = 0;
mbed_official 87:085cde657901 90 TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
mbed_official 87:085cde657901 91 TimMasterHandle.Init.RepetitionCounter = 0;
mbed_official 87:085cde657901 92 //HAL_TIM_Base_Init(&TimMasterHandle);
mbed_official 87:085cde657901 93 HAL_TIM_OC_Init(&TimMasterHandle);
mbed_official 87:085cde657901 94
mbed_official 87:085cde657901 95 /*
mbed_official 87:085cde657901 96 TIM_OC_InitTypeDef sConfig;
mbed_official 87:085cde657901 97 sConfig.OCMode = TIM_OCMODE_INACTIVE;
mbed_official 87:085cde657901 98 sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
mbed_official 87:085cde657901 99 sConfig.Pulse = 0;
mbed_official 87:085cde657901 100 HAL_TIM_OC_ConfigChannel(&TimMasterHandle, &sConfig, TIM_CHANNEL_1);
mbed_official 87:085cde657901 101 */
mbed_official 87:085cde657901 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 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1);
mbed_official 87:085cde657901 106
mbed_official 87:085cde657901 107 // For 32-bit counter
mbed_official 87:085cde657901 108 NVIC_SetVector(TIM_MST_UP_IRQ, (uint32_t)tim_update_irq_handler);
mbed_official 87:085cde657901 109 NVIC_EnableIRQ(TIM_MST_UP_IRQ);
mbed_official 87:085cde657901 110
mbed_official 87:085cde657901 111 // For ouput compare
mbed_official 87:085cde657901 112 NVIC_SetVector(TIM_MST_OC_IRQ, (uint32_t)tim_oc_irq_handler);
mbed_official 87:085cde657901 113 NVIC_EnableIRQ(TIM_MST_OC_IRQ);
mbed_official 87:085cde657901 114
mbed_official 87:085cde657901 115 // Enable timer
mbed_official 87:085cde657901 116 //HAL_TIM_Base_Start(&TimMasterHandle);
mbed_official 87:085cde657901 117 HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1);
mbed_official 87:085cde657901 118 }
mbed_official 87:085cde657901 119
mbed_official 87:085cde657901 120 uint32_t us_ticker_read() {
mbed_official 87:085cde657901 121 uint32_t counter, counter2;
mbed_official 87:085cde657901 122 if (!us_ticker_inited) us_ticker_init();
mbed_official 87:085cde657901 123 // A situation might appear when Master overflows right after Slave is read and before the
mbed_official 87:085cde657901 124 // new (overflowed) value of Master is read. Which would make the code below consider the
mbed_official 87:085cde657901 125 // previous (incorrect) value of Slave and the new value of Master, which would return a
mbed_official 87:085cde657901 126 // value in the past. Avoid this by computing consecutive values of the timer until they
mbed_official 87:085cde657901 127 // are properly ordered.
mbed_official 87:085cde657901 128 counter = (uint32_t)(SlaveCounter << 16);
mbed_official 87:085cde657901 129 counter += TIM_MST->CNT;
mbed_official 87:085cde657901 130 while (1) {
mbed_official 87:085cde657901 131 counter2 = (uint32_t)(SlaveCounter << 16);
mbed_official 87:085cde657901 132 counter2 += TIM_MST->CNT;
mbed_official 87:085cde657901 133 if (counter2 > counter) {
mbed_official 87:085cde657901 134 break;
mbed_official 87:085cde657901 135 }
mbed_official 87:085cde657901 136 counter = counter2;
mbed_official 87:085cde657901 137 }
mbed_official 87:085cde657901 138 return counter2;
mbed_official 87:085cde657901 139 }
mbed_official 87:085cde657901 140
mbed_official 87:085cde657901 141 void us_ticker_set_interrupt(unsigned int timestamp) {
mbed_official 87:085cde657901 142 int delta = (int)(timestamp - us_ticker_read());
mbed_official 87:085cde657901 143
mbed_official 87:085cde657901 144 if (delta <= 0) { // This event was in the past
mbed_official 87:085cde657901 145 us_ticker_irq_handler();
mbed_official 87:085cde657901 146 return;
mbed_official 87:085cde657901 147 }
mbed_official 87:085cde657901 148 else {
mbed_official 87:085cde657901 149 us_ticker_int_counter = (uint32_t)(delta >> 16);
mbed_official 87:085cde657901 150 us_ticker_int_remainder = (uint16_t)(delta & 0xFFFF);
mbed_official 87:085cde657901 151 if (us_ticker_int_counter > 0) { // means delta > 0xFFFF
mbed_official 87:085cde657901 152 __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, 0xFFFF);
mbed_official 87:085cde657901 153 us_ticker_int_counter--;
mbed_official 87:085cde657901 154 } else {
mbed_official 87:085cde657901 155 __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, us_ticker_int_remainder);
mbed_official 87:085cde657901 156 us_ticker_int_remainder = 0;
mbed_official 87:085cde657901 157 }
mbed_official 87:085cde657901 158 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1);
mbed_official 87:085cde657901 159 }
mbed_official 87:085cde657901 160 }
mbed_official 87:085cde657901 161
mbed_official 87:085cde657901 162 void us_ticker_disable_interrupt(void) {
mbed_official 87:085cde657901 163 __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
mbed_official 87:085cde657901 164 }
mbed_official 87:085cde657901 165
mbed_official 87:085cde657901 166 void us_ticker_clear_interrupt(void) {
mbed_official 87:085cde657901 167 if (__HAL_TIM_GET_ITSTATUS(&TimMasterHandle, TIM_IT_CC1) == SET) {
mbed_official 87:085cde657901 168 __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1);
mbed_official 87:085cde657901 169 }
mbed_official 87:085cde657901 170 }