Skip to content

Commit 3e76f8c

Browse files
committed
Update us_ticker.c
Fixed hardcoded MRT_Clock_MHz so that the code will also work at a SystemCoreClock different from 30MHz. Optimized time calculations for us_ticker_read. Same modifications as done previously for LPC812.
1 parent d1d900d commit 3e76f8c

File tree

1 file changed

+44
-30
lines changed
  • libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC82X

1 file changed

+44
-30
lines changed

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC82X/us_ticker.c

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,77 +13,91 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
#include <stddef.h>
17-
#include "us_ticker_api.h"
18-
#include "PeripheralNames.h"
1916

2017
static int us_ticker_inited = 0;
21-
static int ticker_expired = 0;
18+
int MRT_Clock_MHz;
19+
unsigned int ticker_fullcount_us;
20+
unsigned long int ticker_expired_count_us = 0;
2221

2322
#define US_TICKER_TIMER_IRQn MRT_IRQn
24-
#define MRT_CLOCK_MHZ 30
2523

26-
void us_ticker_init(void)
27-
{
24+
void us_ticker_init(void) {
25+
2826
if (us_ticker_inited)
2927
return;
3028

3129
us_ticker_inited = 1;
3230

31+
// Calculate MRT clock value (MRT has no prescaler)
32+
MRT_Clock_MHz = (SystemCoreClock / 1000000);
33+
// Calculate fullcounter value in us (MRT has 31 bits and clock is 30MHz)
34+
ticker_fullcount_us = 0x80000000UL/MRT_Clock_MHz;
35+
3336
// Enable the MRT clock
3437
LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 10);
3538

3639
// Clear peripheral reset the MRT
3740
LPC_SYSCON->PRESETCTRL |= (1 << 7);
3841

39-
// Force load interval value
42+
// Force load interval value (Bit 0-30 is interval value, Bit 31 is Force Load bit)
4043
LPC_MRT->INTVAL0 = 0xFFFFFFFFUL;
41-
// Enable ch0 interrupt
42-
LPC_MRT->CTRL0 = 1;
44+
// Enable Ch0 interrupt, Mode 0 is Repeat Interrupt
45+
LPC_MRT->CTRL0 = (0x0 << 1) | (0x1 << 0);
4346

44-
// Force load interval value
47+
// Force load interval value (Bit 0-30 is interval value, Bit 31 is Force Load bit)
4548
LPC_MRT->INTVAL1 = 0x80000000UL;
46-
// Disable ch1 interrupt
47-
LPC_MRT->CTRL1 = 0;
48-
49+
// Disable ch1 interrupt, Mode 0 is Repeat Interrupt
50+
LPC_MRT->CTRL1 = (0x0 << 1) | (0x0 << 0);
51+
4952
// Set MRT interrupt vector
5053
NVIC_SetVector(US_TICKER_TIMER_IRQn, (uint32_t)us_ticker_irq_handler);
5154
NVIC_EnableIRQ(US_TICKER_TIMER_IRQn);
5255
}
5356

54-
uint32_t us_ticker_read()
55-
{
57+
//TIMER0 is used for us ticker and timers (Timer, wait(), wait_us() etc)
58+
uint32_t us_ticker_read() {
59+
5660
if (!us_ticker_inited)
5761
us_ticker_init();
5862

5963
// Generate ticker value
60-
// MRT source clock is SystemCoreClock (30MHz) and 31-bit down count timer
61-
// Calculate expected value using number of expired times
62-
return (0x7FFFFFFFUL - LPC_MRT->TIMER0)/MRT_CLOCK_MHZ + (ticker_expired * (0x80000000UL/MRT_CLOCK_MHZ));
64+
// MRT source clock is SystemCoreClock (30MHz) and MRT is a 31-bit countdown timer
65+
// Calculate expected value using number of expired times to mimic a 32bit timer @ 1 MHz
66+
return (0x7FFFFFFFUL - LPC_MRT->TIMER0)/MRT_Clock_MHz + ticker_expired_count_us;
6367
}
6468

65-
66-
void us_ticker_set_interrupt(timestamp_t timestamp)
67-
{
68-
// Force load interval value
69-
LPC_MRT->INTVAL1 = (((timestamp - us_ticker_read()) * MRT_CLOCK_MHZ) | 0x80000000UL);
70-
69+
//TIMER1 is used for Timestamped interrupts (Ticker(), Timeout())
70+
void us_ticker_set_interrupt(timestamp_t timestamp) {
71+
72+
// MRT source clock is SystemCoreClock (30MHz) and MRT is a 31-bit countdown timer
73+
// Force load interval value (Bit 0-30 is interval value, Bit 31 is Force Load bit)
74+
// Note: The MRT has less counter headroom available than the typical mbed 32bit timer @ 1 MHz.
75+
// The calculated counter interval until the next timestamp will be truncated and an
76+
// 'early' interrupt will be generated in case the max required count interval exceeds
77+
// the available 31 bits space. However, the mbed us_ticker interrupt handler will
78+
// check current time against the next scheduled timestamp and simply re-issue the
79+
// same interrupt again when needed. The calculated counter interval will now be smaller.
80+
LPC_MRT->INTVAL1 = (((timestamp - us_ticker_read()) * MRT_Clock_MHz) | 0x80000000UL);
81+
7182
// Enable interrupt
7283
LPC_MRT->CTRL1 |= 1;
7384
}
7485

75-
void us_ticker_disable_interrupt()
76-
{
86+
//Disable Timestamped interrupts triggered by TIMER1
87+
void us_ticker_disable_interrupt() {
88+
//Timer1 for Timestamped interrupts (31 bits downcounter @ SystemCoreClock)
7789
LPC_MRT->CTRL1 &= ~1;
7890
}
7991

80-
void us_ticker_clear_interrupt()
81-
{
92+
void us_ticker_clear_interrupt() {
93+
94+
//Timer1 for Timestamped interrupts (31 bits downcounter @ SystemCoreClock)
8295
if (LPC_MRT->STAT1 & 1)
8396
LPC_MRT->STAT1 = 1;
8497

98+
//Timer0 for us counter (31 bits downcounter @ SystemCoreClock)
8599
if (LPC_MRT->STAT0 & 1) {
86100
LPC_MRT->STAT0 = 1;
87-
ticker_expired++;
101+
ticker_expired_count_us += ticker_fullcount_us;
88102
}
89103
}

0 commit comments

Comments
 (0)