Skip to content

Commit 7504050

Browse files
Ganesh RamachandranGanesh Ramachandran
authored andcommitted
Added usticker feature
To enable the feature US_TICKER, file is newly implemented with Timer B, which has proper clock prescale and free running after match interrupt. Old us_ticker driver file (Timer A) is producing up to 100us tolerance, which causes to fail US_TICKER feature. Hence, changed the peripheral ticker A to B.
1 parent c356f1f commit 7504050

File tree

2 files changed

+51
-50
lines changed

2 files changed

+51
-50
lines changed
Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* mbed Microcontroller Library
2-
* (C)Copyright TOSHIBA ELECTRONIC DEVICES & STORAGE CORPORATION 2017 All rights reserved
2+
* (C)Copyright TOSHIBA ELECTRONIC DEVICES & STORAGE CORPORATION 2018 All rights reserved
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,51 +13,57 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
#include <stdbool.h>
1617
#include "us_ticker_api.h"
17-
#include "mbed_critical.h"
18+
#include "tmpm066_tmrb.h"
19+
#include "tmpm066_intifsd.h"
1820

19-
#define TMR16A_100US 0x960 // fsys = fc = 24MHz, Ttmra = 1/24us, 100us*24us = 2400 = 0x960
20-
#define TMR16A_SYSCK ((uint32_t)0x00000001)
21-
#define TMR16A_RUN ((uint32_t)0x00000001)
22-
#define TMR16A_STOP ((uint32_t)0x00000000)
23-
#define OVERFLOW_32BIT (0xFFFFFFFF / 0x64)
21+
#define MAX_TICK_16_BIT 0xFFFF
2422

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
23+
static bool us_ticker_inited = false; // Is ticker initialized yet?
2824

29-
void INT16A0_IRQHandler(void)
25+
void INTTB7_IRQHandler(void)
3026
{
31-
us_ticker++;
32-
33-
if (us_ticker > OVERFLOW_32BIT) {
34-
us_ticker = 0;
35-
}
27+
us_ticker_irq_handler();
3628
}
3729

38-
void INT16A1_IRQHandler(void)
30+
const ticker_info_t* us_ticker_get_info()
3931
{
40-
us_ticker_irq_handler();
32+
static const ticker_info_t info = {
33+
3000000, // 3MHz,
34+
16 // 16 bit counter
35+
};
36+
return &info;
4137
}
4238

4339
// initialize us_ticker
4440
void us_ticker_init(void)
4541
{
46-
// Enable clock supply to TA0
47-
CG_SetFcPeriphA(CG_FC_PERIPH_TMR16A, ENABLE);
42+
TMRB_InitTypeDef m_tmrb0;
43+
4844
if (us_ticker_inited) {
45+
us_ticker_disable_interrupt();
4946
return;
5047
}
51-
us_ticker_inited = 1;
48+
us_ticker_inited = true;
5249

50+
// TSB_TB7 using free-run
51+
m_tmrb0.Mode = TMRB_INTERVAL_TIMER;
52+
m_tmrb0.ClkDiv = TMRB_CLK_DIV_8;
53+
m_tmrb0.UpCntCtrl = TMRB_FREE_RUN;
54+
m_tmrb0.TrailingTiming = MAX_TICK_16_BIT;
55+
m_tmrb0.LeadingTiming = MAX_TICK_16_BIT;
56+
57+
// Enable channel 0
58+
TMRB_Enable(TSB_TB7);
5359
// Stops and clear count operation
54-
TSB_T16A0->RUN = TMR16A_STOP;
55-
TSB_T16A0->CR = TMR16A_SYSCK;
56-
// Permits INTTA0 interrupt
57-
NVIC_EnableIRQ(INT16A0_IRQn);
58-
// Match counter set to max value
59-
TSB_T16A0->RG = TMR16A_100US;
60-
TSB_T16A0->RUN = TMR16A_RUN;
60+
TMRB_SetRunState(TSB_TB7, TMRB_STOP);
61+
// Mask All interrupts
62+
TMRB_SetINTMask(TSB_TB7, TMRB_MASK_MATCH_LEADINGTIMING_INT | TMRB_MASK_MATCH_TRAILINGTIMING_INT | TMRB_MASK_OVERFLOW_INT);
63+
// Initialize timer
64+
TMRB_Init(TSB_TB7, &m_tmrb0);
65+
// Starts TSB_TB7
66+
TMRB_SetRunState(TSB_TB7, TMRB_RUN);
6167
}
6268

6369
uint32_t us_ticker_read(void)
@@ -68,47 +74,42 @@ uint32_t us_ticker_read(void)
6874
us_ticker_init();
6975
}
7076

71-
uint32_t tickerbefore = 0;
72-
do {
73-
tickerbefore = us_ticker;
74-
ret_val = (us_ticker * 100);
75-
} while (tickerbefore != us_ticker);
77+
ret_val = (uint32_t)TMRB_GetUpCntValue(TSB_TB7);
7678

7779
return ret_val;
7880
}
7981

8082
void us_ticker_set_interrupt(timestamp_t timestamp)
8183
{
82-
uint32_t delta = 0;
83-
84-
// Stops and clear count operation
85-
TSB_T16A1->RUN = TMR16A_STOP;
86-
TSB_T16A1->CR = TMR16A_SYSCK;
87-
// Set the compare register
88-
delta = (timestamp - us_ticker_read());
89-
TSB_T16A1->RG = delta;
90-
// Set Interrupt
91-
NVIC_EnableIRQ(INT16A1_IRQn);
92-
// Start TMR_TA1 timer
93-
TSB_T16A1->RUN = TMR16A_RUN;
84+
NVIC_DisableIRQ(INTTB7_IRQn);
85+
NVIC_ClearPendingIRQ(INTTB7_IRQn);
86+
TMRB_ChangeTrailingTiming(TSB_TB7, timestamp);
87+
//Mask all Interrupts except trailing edge interrupt
88+
TMRB_SetINTMask(TSB_TB7, TMRB_MASK_MATCH_LEADINGTIMING_INT | TMRB_MASK_OVERFLOW_INT);
89+
NVIC_EnableIRQ(INTTB7_IRQn);
9490
}
9591

9692
void us_ticker_fire_interrupt(void)
9793
{
98-
NVIC_SetPendingIRQ(INT16A1_IRQn);
94+
NVIC_SetPendingIRQ(INTTB7_IRQn);
95+
NVIC_EnableIRQ(INTTB7_IRQn);
9996
}
10097

10198
void us_ticker_disable_interrupt(void)
10299
{
103-
NVIC_DisableIRQ(INT16A1_IRQn);
100+
// Mask All interrupts
101+
TMRB_SetINTMask(TSB_TB7, TMRB_MASK_MATCH_LEADINGTIMING_INT | TMRB_MASK_MATCH_TRAILINGTIMING_INT | TMRB_MASK_OVERFLOW_INT);
102+
// Also clear and disable interrupts by NVIC
103+
NVIC_ClearPendingIRQ(INTTB7_IRQn);
104+
NVIC_DisableIRQ(INTTB7_IRQn);
104105
}
105106

106107
void us_ticker_clear_interrupt(void)
107108
{
108-
//no flags to clear
109+
INTIFSD_ClearINTReq(INTIFSD_INT_SRC_TMRB_7_MDOVF);
110+
NVIC_ClearPendingIRQ(INTTB7_IRQn);
109111
}
110112

111113
void us_ticker_free(void)
112114
{
113-
114115
}

targets/targets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4367,7 +4367,7 @@
43674367
"extra_labels": ["TOSHIBA"],
43684368
"macros": ["__TMPM066__", "CMSIS_VECTAB_VIRTUAL", "CMSIS_VECTAB_VIRTUAL_HEADER_FILE=\"cmsis_nvic.h\""],
43694369
"supported_toolchains": ["GCC_ARM", "ARM", "IAR"],
4370-
"device_has": ["ANALOGIN", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "SERIAL", "SLEEP", "I2C", "I2CSLAVE", "STDIO_MESSAGES", "PWMOUT"],
4370+
"device_has": ["USTICKER", "ANALOGIN", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "SERIAL", "SLEEP", "I2C", "I2CSLAVE", "STDIO_MESSAGES", "PWMOUT"],
43714371
"device_name": "TMPM066FWUG",
43724372
"detect_code": ["7011"],
43734373
"release_versions": ["5"]

0 commit comments

Comments
 (0)