Skip to content

Commit 1b2a621

Browse files
committed
Merge pull request #905 from Willem23/master
LPC81x - Update us_ticker.c, using MRT
2 parents 7ae7daf + 9241e5e commit 1b2a621

File tree

2 files changed

+116
-65
lines changed
  • libraries/mbed/targets

2 files changed

+116
-65
lines changed

libraries/mbed/targets/cmsis/TARGET_NXP/TARGET_LPC81X/LPC8xx.h

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -368,23 +368,46 @@ typedef struct { /*!< (@ 0x40028000) WKT Structure
368368
} LPC_WKT_TypeDef;
369369
/*@}*/ /* end of group LPC8xx_WKT */
370370

371-
372371
/*------------- Multi-Rate Timer(MRT) --------------------------------------------------*/
373-
typedef struct {
374-
__IO uint32_t INTVAL;
375-
__IO uint32_t TIMER;
376-
__IO uint32_t CTRL;
377-
__IO uint32_t STAT;
378-
} MRT_Channel_cfg_Type;
379-
380-
typedef struct {
381-
MRT_Channel_cfg_Type Channel[4];
382-
uint32_t Reserved0[1];
383-
__IO uint32_t IDLE_CH;
384-
__IO uint32_t IRQ_FLAG;
372+
//New, Copied from lpc824
373+
/**
374+
* @brief Multi-Rate Timer (MRT) (MRT)
375+
*/
376+
typedef struct { /*!< (@ 0x40004000) MRT Structure */
377+
__IO uint32_t INTVAL0; /*!< (@ 0x40004000) MRT0 Time interval value register. This value
378+
is loaded into the TIMER0 register. */
379+
__I uint32_t TIMER0; /*!< (@ 0x40004004) MRT0 Timer register. This register reads the
380+
value of the down-counter. */
381+
__IO uint32_t CTRL0; /*!< (@ 0x40004008) MRT0 Control register. This register controls
382+
the MRT0 modes. */
383+
__IO uint32_t STAT0; /*!< (@ 0x4000400C) MRT0 Status register. */
384+
__IO uint32_t INTVAL1; /*!< (@ 0x40004010) MRT0 Time interval value register. This value
385+
is loaded into the TIMER0 register. */
386+
__I uint32_t TIMER1; /*!< (@ 0x40004014) MRT0 Timer register. This register reads the
387+
value of the down-counter. */
388+
__IO uint32_t CTRL1; /*!< (@ 0x40004018) MRT0 Control register. This register controls
389+
the MRT0 modes. */
390+
__IO uint32_t STAT1; /*!< (@ 0x4000401C) MRT0 Status register. */
391+
__IO uint32_t INTVAL2; /*!< (@ 0x40004020) MRT0 Time interval value register. This value
392+
is loaded into the TIMER0 register. */
393+
__I uint32_t TIMER2; /*!< (@ 0x40004024) MRT0 Timer register. This register reads the
394+
value of the down-counter. */
395+
__IO uint32_t CTRL2; /*!< (@ 0x40004028) MRT0 Control register. This register controls
396+
the MRT0 modes. */
397+
__IO uint32_t STAT2; /*!< (@ 0x4000402C) MRT0 Status register. */
398+
__IO uint32_t INTVAL3; /*!< (@ 0x40004030) MRT0 Time interval value register. This value
399+
is loaded into the TIMER0 register. */
400+
__I uint32_t TIMER3; /*!< (@ 0x40004034) MRT0 Timer register. This register reads the
401+
value of the down-counter. */
402+
__IO uint32_t CTRL3; /*!< (@ 0x40004038) MRT0 Control register. This register controls
403+
the MRT0 modes. */
404+
__IO uint32_t STAT3; /*!< (@ 0x4000403C) MRT0 Status register. */
405+
__I uint32_t RESERVED0[45];
406+
__I uint32_t IDLE_CH; /*!< (@ 0x400040F4) Idle channel register. This register returns
407+
the number of the first idle channel. */
408+
__IO uint32_t IRQ_FLAG; /*!< (@ 0x400040F8) Global interrupt flag register */
385409
} LPC_MRT_TypeDef;
386410

387-
388411
/*------------- Universal Asynchronous Receiver Transmitter (USART) -----------*/
389412
/** @addtogroup LPC8xx_UART LPC8xx Universal Asynchronous Receiver/Transmitter
390413
@{

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

Lines changed: 79 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,77 +17,105 @@
1717
#include "us_ticker_api.h"
1818
#include "PeripheralNames.h"
1919

20-
#define US_TICKER_TIMER_IRQn SCT_IRQn
20+
//New, using MRT instead of SCT, needed to free up SCT for PWM
21+
//Ported from LPC824 libs
22+
static int us_ticker_inited = 0;
23+
unsigned int ticker_fullcount_us;
24+
unsigned long int ticker_expired_count_us = 0;
25+
int MRT_Clock_MHz;
2126

22-
int us_ticker_inited = 0;
27+
#define US_TICKER_TIMER_IRQn MRT_IRQn
2328

2429
void us_ticker_init(void) {
25-
if (us_ticker_inited) return;
26-
us_ticker_inited = 1;
27-
28-
// Enable the SCT clock
29-
LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 8);
30-
31-
// Clear peripheral reset the SCT:
32-
LPC_SYSCON->PRESETCTRL |= (1 << 8);
33-
34-
// Unified counter (32 bits)
35-
LPC_SCT->CONFIG |= 1;
36-
37-
// halt and clear the counter
38-
LPC_SCT->CTRL_L |= (1 << 2) | (1 << 3);
3930

40-
// System Clock (12)MHz -> us_ticker (1)MHz
41-
LPC_SCT->CTRL_L |= ((SystemCoreClock/1000000 - 1) << 5);
31+
if (us_ticker_inited)
32+
return;
33+
34+
us_ticker_inited = 1;
4235

43-
// unhalt the counter:
44-
// - clearing bit 2 of the CTRL register
45-
LPC_SCT->CTRL_L &= ~(1 << 2);
36+
// Calculate MRT clock value (MRT has no prescaler)
37+
MRT_Clock_MHz = (SystemCoreClock / 1000000);
38+
// Calculate fullcounter value in us (MRT has 31 bits and clock is 30 MHz)
39+
ticker_fullcount_us = 0x80000000UL/MRT_Clock_MHz;
40+
41+
// Enable the MRT clock
42+
LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 10);
43+
44+
// Clear peripheral reset the MRT
45+
LPC_SYSCON->PRESETCTRL |= (1 << 7);
46+
47+
// Force load interval value (Bit 0-30 is interval value, Bit 31 is Force Load bit)
48+
LPC_MRT->INTVAL0 = 0xFFFFFFFFUL;
49+
// Enable Ch0 interrupt, Mode 0 is Repeat Interrupt
50+
LPC_MRT->CTRL0 = (0x0 << 1) | (0x1 << 0);
51+
52+
// Force load interval value (Bit 0-30 is interval value, Bit 31 is Force Load bit)
53+
LPC_MRT->INTVAL1 = 0x80000000UL;
54+
// Disable ch1 interrupt, Mode 0 is Repeat Interrupt
55+
LPC_MRT->CTRL1 = (0x0 << 1) | (0x0 << 0);
4656

57+
// Set MRT interrupt vector
4758
NVIC_SetVector(US_TICKER_TIMER_IRQn, (uint32_t)us_ticker_irq_handler);
4859
NVIC_EnableIRQ(US_TICKER_TIMER_IRQn);
4960
}
5061

62+
//TIMER0 is used for us ticker and timers (Timer, wait(), wait_us() etc)
5163
uint32_t us_ticker_read() {
64+
5265
if (!us_ticker_inited)
5366
us_ticker_init();
54-
55-
return LPC_SCT->COUNT_U;
67+
68+
// Generate ticker value
69+
// MRT source clock is SystemCoreClock (30MHz) and MRT is a 31-bit countdown timer
70+
// Calculate expected value using current count and number of expired times to mimic a 32bit timer @ 1 MHz
71+
//
72+
// ticker_expired_count_us
73+
// The variable ticker_expired_count_us keeps track of the number of 31bits overflows (counted by TIMER0) and
74+
// corrects that back to us counts.
75+
//
76+
// (0x7FFFFFFFUL - LPC_MRT->TIMER0)/MRT_Clock_MHz
77+
// The counter is a 31bit downcounter from 7FFFFFFF so correct to actual count-up value and correct
78+
// for 30 counts per us.
79+
//
80+
// Added up these 2 parts result in current us time returned as 32 bits.
81+
return (0x7FFFFFFFUL - LPC_MRT->TIMER0)/MRT_Clock_MHz + ticker_expired_count_us;
5682
}
5783

84+
//TIMER1 is used for Timestamped interrupts (Ticker(), Timeout())
5885
void us_ticker_set_interrupt(timestamp_t timestamp) {
59-
// halt the counter:
60-
// - setting bit 2 of the CTRL register
61-
LPC_SCT->CTRL_L |= (1 << 2);
62-
63-
// set timestamp in compare register
64-
LPC_SCT->MATCH[0].U = (uint32_t)timestamp;
6586

66-
// unhalt the counter:
67-
// - clearing bit 2 of the CTRL register
68-
LPC_SCT->CTRL_L &= ~(1 << 2);
87+
// MRT source clock is SystemCoreClock (30MHz) and MRT is a 31-bit countdown timer
88+
// Force load interval value (Bit 0-30 is interval value, Bit 31 is Force Load bit)
89+
// Note: The MRT has less counter headroom available than the typical mbed 32bit timer @ 1 MHz.
90+
// The calculated counter interval until the next timestamp will be truncated and an
91+
// 'early' interrupt will be generated in case the max required count interval exceeds
92+
// the available 31 bits space. However, the mbed us_ticker interrupt handler will
93+
// check current time against the next scheduled timestamp and simply re-issue the
94+
// same interrupt again when needed. The calculated counter interval will now be smaller.
95+
LPC_MRT->INTVAL1 = (((timestamp - us_ticker_read()) * MRT_Clock_MHz) | 0x80000000UL);
6996

70-
// if events are not enabled, enable them
71-
if (!(LPC_SCT->EVEN & 0x01)) {
72-
73-
// comb mode = match only
74-
LPC_SCT->EVENT[0].CTRL = (1 << 12);
75-
76-
// ref manual:
77-
// In simple applications that do not
78-
// use states, write 0x01 to this
79-
// register to enable an event
80-
LPC_SCT->EVENT[0].STATE |= 0x1;
81-
82-
// enable events
83-
LPC_SCT->EVEN |= 0x1;
84-
}
97+
// Enable interrupt
98+
LPC_MRT->CTRL1 |= 1;
8599
}
86100

87-
void us_ticker_disable_interrupt(void) {
88-
LPC_SCT->EVEN &= ~1;
101+
//Disable Timestamped interrupts triggered by TIMER1
102+
void us_ticker_disable_interrupt() {
103+
//Timer1 for Timestamped interrupts (31 bits downcounter @ SystemCoreClock)
104+
LPC_MRT->CTRL1 &= ~1;
89105
}
90106

91-
void us_ticker_clear_interrupt(void) {
92-
LPC_SCT->EVFLAG = 1;
107+
void us_ticker_clear_interrupt() {
108+
109+
//Timer1 for Timestamped interrupts (31 bits downcounter @ SystemCoreClock)
110+
if (LPC_MRT->STAT1 & 1)
111+
LPC_MRT->STAT1 = 1;
112+
113+
//Timer0 for us counter (31 bits downcounter @ SystemCoreClock)
114+
if (LPC_MRT->STAT0 & 1) {
115+
LPC_MRT->STAT0 = 1;
116+
// ticker_expired_count_us = (ticker_expired * 0x80000000UL) / MRT_Clock_MHz
117+
// The variable ticker_expired_count_us keeps track of the number of 31bits overflows (counted by TIMER0) and
118+
// the multiplication/division corrects that back to us counts.
119+
ticker_expired_count_us += ticker_fullcount_us;
120+
}
93121
}

0 commit comments

Comments
 (0)