|
13 | 13 | * See the License for the specific language governing permissions and
|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 |
| -#include <stddef.h> |
17 |
| -#include "us_ticker_api.h" |
18 |
| -#include "PeripheralNames.h" |
19 | 16 |
|
20 | 17 | 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; |
22 | 21 |
|
23 | 22 | #define US_TICKER_TIMER_IRQn MRT_IRQn
|
24 |
| -#define MRT_CLOCK_MHZ 30 |
25 | 23 |
|
26 |
| -void us_ticker_init(void) |
27 |
| -{ |
| 24 | +void us_ticker_init(void) { |
| 25 | + |
28 | 26 | if (us_ticker_inited)
|
29 | 27 | return;
|
30 | 28 |
|
31 | 29 | us_ticker_inited = 1;
|
32 | 30 |
|
| 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 | + |
33 | 36 | // Enable the MRT clock
|
34 | 37 | LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 10);
|
35 | 38 |
|
36 | 39 | // Clear peripheral reset the MRT
|
37 | 40 | LPC_SYSCON->PRESETCTRL |= (1 << 7);
|
38 | 41 |
|
39 |
| - // Force load interval value |
| 42 | + // Force load interval value (Bit 0-30 is interval value, Bit 31 is Force Load bit) |
40 | 43 | 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); |
43 | 46 |
|
44 |
| - // Force load interval value |
| 47 | + // Force load interval value (Bit 0-30 is interval value, Bit 31 is Force Load bit) |
45 | 48 | 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 | + |
49 | 52 | // Set MRT interrupt vector
|
50 | 53 | NVIC_SetVector(US_TICKER_TIMER_IRQn, (uint32_t)us_ticker_irq_handler);
|
51 | 54 | NVIC_EnableIRQ(US_TICKER_TIMER_IRQn);
|
52 | 55 | }
|
53 | 56 |
|
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 | + |
56 | 60 | if (!us_ticker_inited)
|
57 | 61 | us_ticker_init();
|
58 | 62 |
|
59 | 63 | // 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; |
63 | 67 | }
|
64 | 68 |
|
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 | + |
71 | 82 | // Enable interrupt
|
72 | 83 | LPC_MRT->CTRL1 |= 1;
|
73 | 84 | }
|
74 | 85 |
|
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) |
77 | 89 | LPC_MRT->CTRL1 &= ~1;
|
78 | 90 | }
|
79 | 91 |
|
80 |
| -void us_ticker_clear_interrupt() |
81 |
| -{ |
| 92 | +void us_ticker_clear_interrupt() { |
| 93 | + |
| 94 | + //Timer1 for Timestamped interrupts (31 bits downcounter @ SystemCoreClock) |
82 | 95 | if (LPC_MRT->STAT1 & 1)
|
83 | 96 | LPC_MRT->STAT1 = 1;
|
84 | 97 |
|
| 98 | + //Timer0 for us counter (31 bits downcounter @ SystemCoreClock) |
85 | 99 | if (LPC_MRT->STAT0 & 1) {
|
86 | 100 | LPC_MRT->STAT0 = 1;
|
87 |
| - ticker_expired++; |
| 101 | + ticker_expired_count_us += ticker_fullcount_us; |
88 | 102 | }
|
89 | 103 | }
|
0 commit comments