Skip to content

Commit cee857e

Browse files
Ganesh Ramachandranadbridge
authored andcommitted
Interrupt and Overflow check for us_ticker
1 parent 6b6d072 commit cee857e

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

targets/TARGET_TOSHIBA/TARGET_TMPM066/us_ticker.c

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@
1414
* limitations under the License.
1515
*/
1616
#include "us_ticker_api.h"
17+
#include "mbed_critical.h"
1718

1819
#define TMR16A_100US 0x960 // fsys = fc = 24MHz, Ttmra = 1/24us, 100us*24us = 2400 = 0x960
1920
#define TMR16A_SYSCK ((uint32_t)0x00000001)
2021
#define TMR16A_RUN ((uint32_t)0x00000001)
2122
#define TMR16A_STOP ((uint32_t)0x00000000)
23+
#define OVERFLOW_32BIT (0xFFFFFFFF / 0x64)
2224

23-
static uint8_t us_ticker_inited = 0; // Is ticker initialized yet?
24-
static volatile uint32_t acc_us_ticker = 0;
25-
26-
// 16Bb high timer counter
27-
static volatile uint32_t us_ticker_16h = 0;
25+
static uint8_t us_ticker_inited = 0; // Is ticker initialized yet?
26+
static volatile uint32_t ticker_int_counter = 0; // Amount of overflows until user interrupt
27+
static volatile uint32_t us_ticker = 0; // timer counter
2828

2929
void INT16A0_IRQHandler(void)
3030
{
31-
us_ticker_16h++;
32-
if (us_ticker_16h >= 0xFFFF) {
33-
acc_us_ticker++;
34-
us_ticker_16h = 0;
31+
us_ticker++;
32+
33+
if (us_ticker > OVERFLOW_32BIT) {
34+
us_ticker = 0;
3535
}
3636
}
3737

@@ -67,18 +67,30 @@ uint32_t us_ticker_read(void)
6767
if (!us_ticker_inited) {
6868
us_ticker_init();
6969
}
70-
ret_val = (((acc_us_ticker << 16) + us_ticker_16h) * 100);
70+
71+
uint32_t tickerbefore = 0;
72+
do {
73+
tickerbefore = us_ticker;
74+
ret_val = (us_ticker * 100);
75+
} while (tickerbefore != us_ticker);
76+
7177
return ret_val;
7278
}
7379

7480
void us_ticker_set_interrupt(timestamp_t timestamp)
7581
{
7682
int delta = 0;
83+
7784
// Stops and clear count operation
7885
TSB_T16A1->RUN = TMR16A_STOP;
7986
TSB_T16A1->CR = TMR16A_SYSCK;
8087
// Set the compare register
8188
delta = (int)(timestamp - us_ticker_read());
89+
if (delta < 0) {
90+
// Ticker interrupt handle
91+
us_ticker_irq_handler();
92+
return;
93+
}
8294
TSB_T16A1->RG = delta;
8395
// Set Interrupt
8496
NVIC_EnableIRQ(INT16A1_IRQn);
@@ -98,5 +110,5 @@ void us_ticker_disable_interrupt(void)
98110

99111
void us_ticker_clear_interrupt(void)
100112
{
101-
//no flags to clear
113+
NVIC_ClearPendingIRQ(INT16A1_IRQn);
102114
}

0 commit comments

Comments
 (0)