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:
Fri Nov 07 12:00:11 2014 +0000
Revision:
393:17d2d502961d
Parent:
304:89b9c3a9a045
Child:
402:09075a3b15e3
Synchronized with git revision 78a3d4f2301aad7367e924e8231b4411d11038ea

Full URL: https://github.com/mbedmicro/mbed/commit/78a3d4f2301aad7367e924e8231b4411d11038ea/

Toolchain: NUCLEO_F0 - Add missing files for IAR exporter

Who changed what in which revision?

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