2013-08-24 06:49:16 +00:00
|
|
|
/* mbed Microcontroller Library
|
|
|
|
* Copyright (c) 2006-2013 ARM Limited
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
#include <stddef.h>
|
|
|
|
#include "us_ticker_api.h"
|
|
|
|
#include "PeripheralNames.h"
|
|
|
|
|
2015-02-11 18:35:44 +00:00
|
|
|
//New, using MRT instead of SCT, needed to free up SCT for PWM
|
|
|
|
//Ported from LPC824 libs
|
|
|
|
static int us_ticker_inited = 0;
|
2015-08-17 01:02:00 +00:00
|
|
|
static int us_ticker_interrupt_inited = 0;
|
2015-02-19 14:09:18 +00:00
|
|
|
unsigned int ticker_fullcount_us;
|
|
|
|
unsigned long int ticker_expired_count_us = 0;
|
2015-02-11 18:35:44 +00:00
|
|
|
int MRT_Clock_MHz;
|
2013-08-24 06:49:16 +00:00
|
|
|
|
2015-02-11 18:35:44 +00:00
|
|
|
#define US_TICKER_TIMER_IRQn MRT_IRQn
|
2013-08-24 06:49:16 +00:00
|
|
|
|
|
|
|
void us_ticker_init(void) {
|
|
|
|
|
2015-02-11 18:35:44 +00:00
|
|
|
if (us_ticker_inited)
|
|
|
|
return;
|
|
|
|
|
|
|
|
us_ticker_inited = 1;
|
2015-02-19 14:09:18 +00:00
|
|
|
|
2015-02-11 18:35:44 +00:00
|
|
|
// Calculate MRT clock value (MRT has no prescaler)
|
|
|
|
MRT_Clock_MHz = (SystemCoreClock / 1000000);
|
2015-02-19 14:09:18 +00:00
|
|
|
// Calculate fullcounter value in us (MRT has 31 bits and clock is 30 MHz)
|
|
|
|
ticker_fullcount_us = 0x80000000UL/MRT_Clock_MHz;
|
2015-02-11 18:35:44 +00:00
|
|
|
|
|
|
|
// Enable the MRT clock
|
|
|
|
LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 10);
|
|
|
|
|
|
|
|
// Clear peripheral reset the MRT
|
|
|
|
LPC_SYSCON->PRESETCTRL |= (1 << 7);
|
|
|
|
|
|
|
|
// Force load interval value (Bit 0-30 is interval value, Bit 31 is Force Load bit)
|
|
|
|
LPC_MRT->INTVAL0 = 0xFFFFFFFFUL;
|
|
|
|
// Enable Ch0 interrupt, Mode 0 is Repeat Interrupt
|
|
|
|
LPC_MRT->CTRL0 = (0x0 << 1) | (0x1 << 0);
|
|
|
|
|
|
|
|
// Force load interval value (Bit 0-30 is interval value, Bit 31 is Force Load bit)
|
|
|
|
LPC_MRT->INTVAL1 = 0x80000000UL;
|
|
|
|
// Disable ch1 interrupt, Mode 0 is Repeat Interrupt
|
|
|
|
LPC_MRT->CTRL1 = (0x0 << 1) | (0x0 << 0);
|
2015-08-17 01:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void us_ticker_interrupt_init(void) {
|
|
|
|
|
|
|
|
if (us_ticker_interrupt_inited)
|
|
|
|
return;
|
|
|
|
|
|
|
|
us_ticker_interrupt_inited = 1;
|
|
|
|
|
2015-02-11 18:35:44 +00:00
|
|
|
// Set MRT interrupt vector
|
2013-08-24 06:49:16 +00:00
|
|
|
NVIC_SetVector(US_TICKER_TIMER_IRQn, (uint32_t)us_ticker_irq_handler);
|
|
|
|
NVIC_EnableIRQ(US_TICKER_TIMER_IRQn);
|
|
|
|
}
|
|
|
|
|
2015-02-11 18:35:44 +00:00
|
|
|
//TIMER0 is used for us ticker and timers (Timer, wait(), wait_us() etc)
|
2013-08-24 06:49:16 +00:00
|
|
|
uint32_t us_ticker_read() {
|
2015-02-11 18:35:44 +00:00
|
|
|
|
2013-08-24 06:49:16 +00:00
|
|
|
if (!us_ticker_inited)
|
|
|
|
us_ticker_init();
|
2015-02-11 18:35:44 +00:00
|
|
|
|
|
|
|
// Generate ticker value
|
|
|
|
// MRT source clock is SystemCoreClock (30MHz) and MRT is a 31-bit countdown timer
|
2015-02-19 14:09:18 +00:00
|
|
|
// Calculate expected value using current count and number of expired times to mimic a 32bit timer @ 1 MHz
|
|
|
|
//
|
|
|
|
// ticker_expired_count_us
|
|
|
|
// The variable ticker_expired_count_us keeps track of the number of 31bits overflows (counted by TIMER0) and
|
|
|
|
// corrects that back to us counts.
|
|
|
|
//
|
|
|
|
// (0x7FFFFFFFUL - LPC_MRT->TIMER0)/MRT_Clock_MHz
|
|
|
|
// The counter is a 31bit downcounter from 7FFFFFFF so correct to actual count-up value and correct
|
|
|
|
// for 30 counts per us.
|
|
|
|
//
|
|
|
|
// Added up these 2 parts result in current us time returned as 32 bits.
|
|
|
|
return (0x7FFFFFFFUL - LPC_MRT->TIMER0)/MRT_Clock_MHz + ticker_expired_count_us;
|
2013-08-24 06:49:16 +00:00
|
|
|
}
|
|
|
|
|
2015-02-11 18:35:44 +00:00
|
|
|
//TIMER1 is used for Timestamped interrupts (Ticker(), Timeout())
|
2014-08-21 10:48:02 +00:00
|
|
|
void us_ticker_set_interrupt(timestamp_t timestamp) {
|
2013-08-24 06:49:16 +00:00
|
|
|
|
2015-08-17 01:02:00 +00:00
|
|
|
if (!us_ticker_interrupt_inited)
|
|
|
|
us_ticker_interrupt_init();
|
|
|
|
|
2015-02-11 18:35:44 +00:00
|
|
|
// MRT source clock is SystemCoreClock (30MHz) and MRT is a 31-bit countdown timer
|
|
|
|
// Force load interval value (Bit 0-30 is interval value, Bit 31 is Force Load bit)
|
|
|
|
// Note: The MRT has less counter headroom available than the typical mbed 32bit timer @ 1 MHz.
|
|
|
|
// The calculated counter interval until the next timestamp will be truncated and an
|
|
|
|
// 'early' interrupt will be generated in case the max required count interval exceeds
|
|
|
|
// the available 31 bits space. However, the mbed us_ticker interrupt handler will
|
|
|
|
// check current time against the next scheduled timestamp and simply re-issue the
|
|
|
|
// same interrupt again when needed. The calculated counter interval will now be smaller.
|
|
|
|
LPC_MRT->INTVAL1 = (((timestamp - us_ticker_read()) * MRT_Clock_MHz) | 0x80000000UL);
|
2013-08-24 06:49:16 +00:00
|
|
|
|
2015-02-11 18:35:44 +00:00
|
|
|
// Enable interrupt
|
|
|
|
LPC_MRT->CTRL1 |= 1;
|
2013-08-24 06:49:16 +00:00
|
|
|
}
|
|
|
|
|
Ticker: add fire interrupt now function
fire_interrupt function should be used for events in the past. As we have now
64bit timestamp, we can figure out what is in the past, and ask a target to invoke
an interrupt immediately. The previous attemps in the target HAL tickers were not ideal, as it can wrap around easily (16 or 32 bit counters). This new
functionality should solve this problem.
set_interrupt for tickers in HAL code should not handle anything but the next match interrupt. If it was in the past is handled by the upper layer.
It is possible that we are setting next event to the close future, so once it is set it is already in the past. Therefore we add a check after set interrupt to verify it is in future.
If it is not, we fire interrupt immediately. This results in
two events - first one immediate, correct one. The second one might be scheduled in far future (almost entire ticker range),
that should be discarded.
The specification for the fire_interrupts are:
- should set pending bit for the ticker interrupt (as soon as possible),
the event we are scheduling is already in the past, and we do not want to skip
any events
- no arguments are provided, neither return value, not needed
- ticker should be initialized prior calling this function (no need to check if it is already initialized)
All our targets provide this new functionality, removing old misleading if (timestamp is in the past) checks.
2017-06-27 11:18:59 +00:00
|
|
|
void us_ticker_fire_interrupt(void)
|
|
|
|
{
|
|
|
|
NVIC_SetPendingIRQ(US_TICKER_TIMER_IRQn);
|
|
|
|
}
|
|
|
|
|
2015-02-11 18:35:44 +00:00
|
|
|
//Disable Timestamped interrupts triggered by TIMER1
|
|
|
|
void us_ticker_disable_interrupt() {
|
|
|
|
//Timer1 for Timestamped interrupts (31 bits downcounter @ SystemCoreClock)
|
|
|
|
LPC_MRT->CTRL1 &= ~1;
|
2013-08-24 06:49:16 +00:00
|
|
|
}
|
|
|
|
|
2015-02-11 18:35:44 +00:00
|
|
|
void us_ticker_clear_interrupt() {
|
|
|
|
|
|
|
|
//Timer1 for Timestamped interrupts (31 bits downcounter @ SystemCoreClock)
|
|
|
|
if (LPC_MRT->STAT1 & 1)
|
|
|
|
LPC_MRT->STAT1 = 1;
|
|
|
|
|
|
|
|
//Timer0 for us counter (31 bits downcounter @ SystemCoreClock)
|
|
|
|
if (LPC_MRT->STAT0 & 1) {
|
|
|
|
LPC_MRT->STAT0 = 1;
|
2015-02-19 14:09:18 +00:00
|
|
|
// ticker_expired_count_us = (ticker_expired * 0x80000000UL) / MRT_Clock_MHz
|
|
|
|
// The variable ticker_expired_count_us keeps track of the number of 31bits overflows (counted by TIMER0) and
|
|
|
|
// the multiplication/division corrects that back to us counts.
|
|
|
|
ticker_expired_count_us += ticker_fullcount_us;
|
2015-02-11 18:35:44 +00:00
|
|
|
}
|
2013-08-24 06:49:16 +00:00
|
|
|
}
|
2018-07-25 06:40:30 +00:00
|
|
|
|
|
|
|
void us_ticker_free(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|