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:
Thu Sep 18 14:00:17 2014 +0100
Revision:
324:406fd2029f23
Child:
329:e2a9f2c7ad2b
Synchronized with git revision a73f28e6fbca9559fbed2726410eeb4c0534a4a5

Full URL: https://github.com/mbedmicro/mbed/commit/a73f28e6fbca9559fbed2726410eeb4c0534a4a5/

Extended #476, which does not break ethernet for K64F

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 324:406fd2029f23 1 /* mbed Microcontroller Library
mbed_official 324:406fd2029f23 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 324:406fd2029f23 3 *
mbed_official 324:406fd2029f23 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 324:406fd2029f23 5 * you may not use this file except in compliance with the License.
mbed_official 324:406fd2029f23 6 * You may obtain a copy of the License at
mbed_official 324:406fd2029f23 7 *
mbed_official 324:406fd2029f23 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 324:406fd2029f23 9 *
mbed_official 324:406fd2029f23 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 324:406fd2029f23 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 324:406fd2029f23 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 324:406fd2029f23 13 * See the License for the specific language governing permissions and
mbed_official 324:406fd2029f23 14 * limitations under the License.
mbed_official 324:406fd2029f23 15 */
mbed_official 324:406fd2029f23 16 #include <stddef.h>
mbed_official 324:406fd2029f23 17 #include "us_ticker_api.h"
mbed_official 324:406fd2029f23 18 #include "PeripheralNames.h"
mbed_official 324:406fd2029f23 19 #include "fsl_pit_hal.h"
mbed_official 324:406fd2029f23 20 #include "fsl_sim_hal.h"
mbed_official 324:406fd2029f23 21 #include "fsl_clock_manager.h"
mbed_official 324:406fd2029f23 22
mbed_official 324:406fd2029f23 23 static void pit_init(void);
mbed_official 324:406fd2029f23 24 static void lptmr_init(void);
mbed_official 324:406fd2029f23 25
mbed_official 324:406fd2029f23 26 static int us_ticker_inited = 0;
mbed_official 324:406fd2029f23 27
mbed_official 324:406fd2029f23 28 void us_ticker_init(void) {
mbed_official 324:406fd2029f23 29 if (us_ticker_inited) {
mbed_official 324:406fd2029f23 30 return;
mbed_official 324:406fd2029f23 31 }
mbed_official 324:406fd2029f23 32 us_ticker_inited = 1;
mbed_official 324:406fd2029f23 33
mbed_official 324:406fd2029f23 34 pit_init();
mbed_official 324:406fd2029f23 35 lptmr_init();
mbed_official 324:406fd2029f23 36 }
mbed_official 324:406fd2029f23 37
mbed_official 324:406fd2029f23 38
mbed_official 324:406fd2029f23 39 uint32_t us_ticker_read() {
mbed_official 324:406fd2029f23 40 if (!us_ticker_inited) {
mbed_official 324:406fd2029f23 41 us_ticker_init();
mbed_official 324:406fd2029f23 42 }
mbed_official 324:406fd2029f23 43
mbed_official 324:406fd2029f23 44 return ~(PIT_HAL_ReadTimerCount(PIT_BASE, 1));
mbed_official 324:406fd2029f23 45 }
mbed_official 324:406fd2029f23 46 /******************************************************************************
mbed_official 324:406fd2029f23 47 * Timer for us timing.
mbed_official 324:406fd2029f23 48 ******************************************************************************/
mbed_official 324:406fd2029f23 49 static void pit_init(void) {
mbed_official 324:406fd2029f23 50 uint32_t busClock;
mbed_official 324:406fd2029f23 51
mbed_official 324:406fd2029f23 52 CLOCK_SYS_EnablePitClock(0);
mbed_official 324:406fd2029f23 53 PIT_HAL_Enable(PIT_BASE);
mbed_official 324:406fd2029f23 54 CLOCK_SYS_GetFreq(kBusClock, &busClock);
mbed_official 324:406fd2029f23 55 PIT_HAL_SetTimerPeriodByCount(PIT_BASE, 0, busClock / 1000000 - 1);
mbed_official 324:406fd2029f23 56 PIT_HAL_SetTimerPeriodByCount(PIT_BASE, 1, 0xFFFFFFFF);
mbed_official 324:406fd2029f23 57 PIT_HAL_SetTimerChainCmd(PIT_BASE, 1, true);
mbed_official 324:406fd2029f23 58
mbed_official 324:406fd2029f23 59 PIT_HAL_StartTimer(PIT_BASE, 0);
mbed_official 324:406fd2029f23 60 PIT_HAL_StartTimer(PIT_BASE, 1);
mbed_official 324:406fd2029f23 61 }
mbed_official 324:406fd2029f23 62
mbed_official 324:406fd2029f23 63 /******************************************************************************
mbed_official 324:406fd2029f23 64 * Timer Event
mbed_official 324:406fd2029f23 65 *
mbed_official 324:406fd2029f23 66 * It schedules interrupts at given (32bit)us interval of time.
mbed_official 324:406fd2029f23 67 * It is implemented used the 16bit Low Power Timer that remains powered in all
mbed_official 324:406fd2029f23 68 * power modes.
mbed_official 324:406fd2029f23 69 ******************************************************************************/
mbed_official 324:406fd2029f23 70 static void lptmr_isr(void);
mbed_official 324:406fd2029f23 71
mbed_official 324:406fd2029f23 72 static void lptmr_init(void) {
mbed_official 324:406fd2029f23 73 CLOCK_SYS_EnableLptimerClock(0);
mbed_official 324:406fd2029f23 74
mbed_official 324:406fd2029f23 75 /* Set interrupt handler */
mbed_official 324:406fd2029f23 76 NVIC_SetVector(LPTimer_IRQn, (uint32_t)lptmr_isr);
mbed_official 324:406fd2029f23 77 NVIC_EnableIRQ(LPTimer_IRQn);
mbed_official 324:406fd2029f23 78
mbed_official 324:406fd2029f23 79 /* TODO: check clock manager, due to nonstandard 50 MHz */
mbed_official 324:406fd2029f23 80 //No suitable external oscillator clock -> Use fast internal oscillator (4MHz / divider)
mbed_official 324:406fd2029f23 81 MCG->C1 |= MCG_C1_IRCLKEN_MASK;
mbed_official 324:406fd2029f23 82 MCG->C2 |= MCG_C2_IRCS_MASK;
mbed_official 324:406fd2029f23 83 LPTMR0->PSR = LPTMR_PSR_PCS(0);
mbed_official 324:406fd2029f23 84 switch (MCG->SC & MCG_SC_FCRDIV_MASK) {
mbed_official 324:406fd2029f23 85 case MCG_SC_FCRDIV(0): //4MHz
mbed_official 324:406fd2029f23 86 LPTMR0->PSR |= LPTMR_PSR_PRESCALE(1);
mbed_official 324:406fd2029f23 87 break;
mbed_official 324:406fd2029f23 88 case MCG_SC_FCRDIV(1): //2MHz
mbed_official 324:406fd2029f23 89 LPTMR0->PSR |= LPTMR_PSR_PRESCALE(0);
mbed_official 324:406fd2029f23 90 break;
mbed_official 324:406fd2029f23 91 default: //1MHz or anything else, in which case we put it on 1MHz
mbed_official 324:406fd2029f23 92 MCG->SC &= ~MCG_SC_FCRDIV_MASK;
mbed_official 324:406fd2029f23 93 MCG->SC |= MCG_SC_FCRDIV(2);
mbed_official 324:406fd2029f23 94 LPTMR0->PSR |= LPTMR_PSR_PBYP_MASK;
mbed_official 324:406fd2029f23 95 }
mbed_official 324:406fd2029f23 96 }
mbed_official 324:406fd2029f23 97
mbed_official 324:406fd2029f23 98 void us_ticker_disable_interrupt(void) {
mbed_official 324:406fd2029f23 99 BW_LPTMR_CSR_TIE(LPTMR0_BASE, 0);
mbed_official 324:406fd2029f23 100 }
mbed_official 324:406fd2029f23 101
mbed_official 324:406fd2029f23 102 void us_ticker_clear_interrupt(void) {
mbed_official 324:406fd2029f23 103 // we already clear interrupt in lptmr_isr
mbed_official 324:406fd2029f23 104 }
mbed_official 324:406fd2029f23 105
mbed_official 324:406fd2029f23 106 static uint32_t us_ticker_int_counter = 0;
mbed_official 324:406fd2029f23 107 static uint16_t us_ticker_int_remainder = 0;
mbed_official 324:406fd2029f23 108
mbed_official 324:406fd2029f23 109 static void lptmr_set(unsigned short count) {
mbed_official 324:406fd2029f23 110 HW_LPTMR_CSR_WR(LPTMR0_BASE, 0);
mbed_official 324:406fd2029f23 111 BW_LPTMR_CMR_COMPARE(LPTMR0_BASE, count);
mbed_official 324:406fd2029f23 112 BW_LPTMR_CSR_TIE(LPTMR0_BASE, 1);
mbed_official 324:406fd2029f23 113 BW_LPTMR_CSR_TEN(LPTMR0_BASE, 1);
mbed_official 324:406fd2029f23 114 }
mbed_official 324:406fd2029f23 115
mbed_official 324:406fd2029f23 116 static void lptmr_isr(void) {
mbed_official 324:406fd2029f23 117 // write 1 to TCF to clear the LPT timer compare flag
mbed_official 324:406fd2029f23 118 BW_LPTMR_CSR_TCF(LPTMR0_BASE, 1);
mbed_official 324:406fd2029f23 119
mbed_official 324:406fd2029f23 120 if (us_ticker_int_counter > 0) {
mbed_official 324:406fd2029f23 121 lptmr_set(0xFFFF);
mbed_official 324:406fd2029f23 122 us_ticker_int_counter--;
mbed_official 324:406fd2029f23 123 } else {
mbed_official 324:406fd2029f23 124 if (us_ticker_int_remainder > 0) {
mbed_official 324:406fd2029f23 125 lptmr_set(us_ticker_int_remainder);
mbed_official 324:406fd2029f23 126 us_ticker_int_remainder = 0;
mbed_official 324:406fd2029f23 127 } else {
mbed_official 324:406fd2029f23 128 // This function is going to disable the interrupts if there are
mbed_official 324:406fd2029f23 129 // no other events in the queue
mbed_official 324:406fd2029f23 130 us_ticker_irq_handler();
mbed_official 324:406fd2029f23 131 }
mbed_official 324:406fd2029f23 132 }
mbed_official 324:406fd2029f23 133 }
mbed_official 324:406fd2029f23 134
mbed_official 324:406fd2029f23 135 void us_ticker_set_interrupt(timestamp_t timestamp) {
mbed_official 324:406fd2029f23 136 int delta = (int)(timestamp - us_ticker_read());
mbed_official 324:406fd2029f23 137 if (delta <= 0) {
mbed_official 324:406fd2029f23 138 // This event was in the past:
mbed_official 324:406fd2029f23 139 us_ticker_irq_handler();
mbed_official 324:406fd2029f23 140 return;
mbed_official 324:406fd2029f23 141 }
mbed_official 324:406fd2029f23 142
mbed_official 324:406fd2029f23 143 us_ticker_int_counter = (uint32_t)(delta >> 16);
mbed_official 324:406fd2029f23 144 us_ticker_int_remainder = (uint16_t)(0xFFFF & delta);
mbed_official 324:406fd2029f23 145 if (us_ticker_int_counter > 0) {
mbed_official 324:406fd2029f23 146 lptmr_set(0xFFFF);
mbed_official 324:406fd2029f23 147 us_ticker_int_counter--;
mbed_official 324:406fd2029f23 148 } else {
mbed_official 324:406fd2029f23 149 lptmr_set(us_ticker_int_remainder);
mbed_official 324:406fd2029f23 150 us_ticker_int_remainder = 0;
mbed_official 324:406fd2029f23 151 }
mbed_official 324:406fd2029f23 152 }