Skip to content

Commit 9f6b2c4

Browse files
committed
[LPC15XX] Fixed µs_ticker implementation
Re-wrote µs_ticker implementation to use SCT3 instead of RIT in order to fix a serious rollover bug at 1:11:34.
1 parent fd757d3 commit 9f6b2c4

File tree

2 files changed

+43
-33
lines changed

2 files changed

+43
-33
lines changed

hal/targets/hal/TARGET_NXP/TARGET_LPC15XX/pwmout_api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static LPC_SCT0_Type *SCTs[4] = {
2727
};
2828

2929
// bit flags for used SCTs
30-
static unsigned char sct_used = 0;
30+
static unsigned char sct_used = (1 << 3);
3131
static int get_available_sct(void) {
3232
int i;
3333
for (i=0; i<4; i++) {

hal/targets/hal/TARGET_NXP/TARGET_LPC15XX/us_ticker.c

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,57 +17,67 @@
1717
#include "us_ticker_api.h"
1818
#include "PeripheralNames.h"
1919

20-
#define US_TICKER_TIMER_IRQn RIT_IRQn
20+
#define US_TICKER_TIMER_IRQn SCT3_IRQn
2121

2222
int us_ticker_inited = 0;
2323

2424
void us_ticker_init(void) {
25-
if (us_ticker_inited) return;
25+
if (us_ticker_inited)
26+
return;
27+
2628
us_ticker_inited = 1;
27-
28-
// Enable the RIT clock
29-
LPC_SYSCON->SYSAHBCLKCTRL1 |= (1 << 1);
30-
31-
// Clear peripheral reset the RIT
32-
LPC_SYSCON->PRESETCTRL1 |= (1 << 1);
33-
LPC_SYSCON->PRESETCTRL1 &= ~(1 << 1);
34-
35-
LPC_RIT->MASK = 0;
36-
LPC_RIT->MASK_H = 0;
37-
38-
LPC_RIT->COUNTER = 0;
39-
LPC_RIT->COUNTER_H = 0;
40-
41-
LPC_RIT->COMPVAL = 0xffffffff;
42-
LPC_RIT->COMPVAL_H = 0x0000ffff;
43-
44-
// Timer enable, enable for debug
45-
LPC_RIT->CTRL = 0xC;
46-
29+
30+
// Enable the SCT3 clock
31+
LPC_SYSCON->SYSAHBCLKCTRL1 |= (1 << 5);
32+
33+
// Clear peripheral reset the SCT3
34+
LPC_SYSCON->PRESETCTRL1 |= (1 << 5);
35+
LPC_SYSCON->PRESETCTRL1 &= ~(1 << 5);
36+
37+
// Configure SCT3 as a 1MHz 32-bit counter with no auto limiting or match reload
38+
char sctClkDiv = ((SystemCoreClock + 1000000 - 1) / 1000000) - 1;
39+
LPC_SCT3->CONFIG = (1 << 7) | (1 << 0);
40+
LPC_SCT3->CTRL = (sctClkDiv << 5) | (1 << 3) | (1 << 2);
41+
42+
// Configure SCT3 event 0 to fire on match register 0
43+
LPC_SCT3->EV0_STATE = (1 << 0);
44+
LPC_SCT3->EV0_CTRL = (0x1 << 12);
45+
46+
// Start SCT3
47+
LPC_SCT3->CTRL &= ~(1 << 2);
48+
49+
// Set SCT3 interrupt vector
4750
NVIC_SetVector(US_TICKER_TIMER_IRQn, (uint32_t)us_ticker_irq_handler);
4851
NVIC_EnableIRQ(US_TICKER_TIMER_IRQn);
4952
}
5053

5154
uint32_t us_ticker_read() {
5255
if (!us_ticker_inited)
5356
us_ticker_init();
54-
55-
uint64_t temp;
56-
temp = LPC_RIT->COUNTER | ((uint64_t)LPC_RIT->COUNTER_H << 32);
57-
temp /= (SystemCoreClock/1000000);
58-
return (uint32_t)temp;
57+
58+
// Return SCT3 count value
59+
return LPC_SCT3->COUNT;
5960
}
6061

6162
void us_ticker_set_interrupt(timestamp_t timestamp) {
62-
uint64_t temp = ((uint64_t)timestamp * (SystemCoreClock/1000000));
63-
LPC_RIT->COMPVAL = (temp & 0xFFFFFFFFL);
64-
LPC_RIT->COMPVAL_H = ((temp >> 32)& 0x0000FFFFL);
63+
// Set SCT3 match register 0 (critical section)
64+
int wasMasked = __disable_irq();
65+
LPC_SCT3->CTRL |= (1 << 2);
66+
LPC_SCT3->MATCH0 = (uint32_t)timestamp;
67+
LPC_SCT3->CTRL &= ~(1 << 2);
68+
if (!wasMasked)
69+
__enable_irq();
70+
71+
// Enable interrupt on SCT3 event 0
72+
LPC_SCT3->EVEN = (1 << 0);
6573
}
6674

6775
void us_ticker_disable_interrupt(void) {
68-
LPC_RIT->CTRL |= (1 << 3);
76+
// Disable interrupt on SCT3 event 0
77+
LPC_SCT3->EVEN = 0;
6978
}
7079

7180
void us_ticker_clear_interrupt(void) {
72-
LPC_RIT->CTRL |= (1 << 0);
81+
// Clear SCT3 event 0 interrupt flag
82+
LPC_SCT3->EVFLAG = (1 << 0);
7383
}

0 commit comments

Comments
 (0)