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:
Tue Jul 29 19:00:07 2014 +0100
Revision:
268:402bcc0c870b
Parent:
126:549ba18ddd81
Child:
304:89b9c3a9a045
Synchronized with git revision 490d1a6606b3138f165c5edf2f2370ca616587c0

Full URL: https://github.com/mbedmicro/mbed/commit/490d1a6606b3138f165c5edf2f2370ca616587c0/

[LPC1114] Sleep fix + some device.h settings

Who changed what in which revision?

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