Added usticker feature

pull/8238/head
Ganesh Ramachandran 2018-09-25 12:51:08 +05:30
parent 6fca23ece1
commit 9474603791
2 changed files with 38 additions and 59 deletions

View File

@ -1,5 +1,5 @@
/* mbed Microcontroller Library /* mbed Microcontroller Library
* (C)Copyright TOSHIBA ELECTRONIC DEVICES & STORAGE CORPORATION 2017 All rights reserved * (C)Copyright TOSHIBA ELECTRONIC DEVICES & STORAGE CORPORATION 2018 All rights reserved
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -13,14 +13,14 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <stdbool.h>
#include "us_ticker_api.h" #include "us_ticker_api.h"
#include "tmpm46b_tmrb.h" #include "tmpm46b_tmrb.h"
#define TMR16A_100US 0xFFFF #define MAX_TICK_16_BIT 0xFFFF
#define TMRB_CLK_DIV 0x3 #define TMRB_CLK_DIV 0x3
static uint8_t us_ticker_inited = 0; // Is ticker initialized yet? static bool us_ticker_inited = false; // Is ticker initialized yet?
static volatile uint32_t us_ticker = 0; // timer counter
const ticker_info_t* us_ticker_get_info() const ticker_info_t* us_ticker_get_info()
{ {
@ -35,34 +35,32 @@ const ticker_info_t* us_ticker_get_info()
void us_ticker_init(void) void us_ticker_init(void)
{ {
TMRB_InitTypeDef m_tmrb0; TMRB_InitTypeDef m_tmrb0;
TMRB_FFOutputTypeDef FFStruct;
if (us_ticker_inited) { if (us_ticker_inited) {
us_ticker_disable_interrupt();
return; return;
} }
us_ticker_inited = 1; us_ticker_inited = true;
// TSB_TB0 using free-run
m_tmrb0.Mode = TMRB_INTERVAL_TIMER;
m_tmrb0.ClkDiv = TMRB_CLK_DIV;
m_tmrb0.UpCntCtrl = TMRB_FREE_RUN;
m_tmrb0.TrailingTiming = MAX_TICK_16_BIT;
m_tmrb0.LeadingTiming = MAX_TICK_16_BIT;
// Enable channel 0 // Enable channel 0
TMRB_Enable(TSB_TB0); TMRB_Enable(TSB_TB0);
// Stops and clear count operation // Stops and clear count operation
TMRB_SetRunState(TSB_TB0, TMRB_STOP); TMRB_SetRunState(TSB_TB0, TMRB_STOP);
// Disable to TBxFF0 reverse trigger // Mask All interrupts
FFStruct.FlipflopCtrl = TMRB_FLIPFLOP_CLEAR; TMRB_SetINTMask(TSB_TB0, TMRB_MASK_MATCH_LEADING_INT | TMRB_MASK_MATCH_TRAILING_INT | TMRB_MASK_OVERFLOW_INT);
FFStruct.FlipflopReverseTrg =TMRB_DISABLE_FLIPFLOP;
TMRB_SetFlipFlop(TSB_TB0, &FFStruct);
// TSB_TB0 using free-run
m_tmrb0.Mode = TMRB_INTERVAL_TIMER;
m_tmrb0.ClkDiv = TMRB_CLK_DIV;
m_tmrb0.UpCntCtrl = TMRB_AUTO_CLEAR;
m_tmrb0.TrailingTiming = TMR16A_100US;
m_tmrb0.LeadingTiming = TMR16A_100US;
TMRB_Init(TSB_TB0, &m_tmrb0); TMRB_Init(TSB_TB0, &m_tmrb0);
// Enable TMRB when system is in idle mode // Enable TMRB when system is in idle mode
TMRB_SetIdleMode(TSB_TB0, ENABLE); TMRB_SetIdleMode(TSB_TB0, ENABLE);
// Starts TSB_TB0 // Starts TSB_TB0
TMRB_SetRunState(TSB_TB0, TMRB_RUN); TMRB_SetRunState(TSB_TB0, TMRB_RUN);
NVIC_SetVector(INTTB0_IRQn, (uint32_t)us_ticker_irq_handler);
} }
uint32_t us_ticker_read(void) uint32_t us_ticker_read(void)
@ -80,59 +78,40 @@ uint32_t us_ticker_read(void)
void us_ticker_set_interrupt(timestamp_t timestamp) void us_ticker_set_interrupt(timestamp_t timestamp)
{ {
TMRB_InitTypeDef m_tmrb1; NVIC_DisableIRQ(INTTB0_IRQn);
TMRB_FFOutputTypeDef FFStruct; NVIC_ClearPendingIRQ(INTTB0_IRQn);
TMRB_ChangeTrailingTiming(TSB_TB0, timestamp);
const uint32_t now_ticks = us_ticker_read(); // Mask all Interrupts except trailing edge interrupt
uint32_t delta_ticks = TMRB_SetINTMask(TSB_TB0, TMRB_MASK_MATCH_LEADING_INT | TMRB_MASK_OVERFLOW_INT);
timestamp >= now_ticks ? timestamp - now_ticks : (uint32_t)((uint64_t) timestamp + 0xFFFF - now_ticks); NVIC_EnableIRQ(INTTB0_IRQn);
if (delta_ticks == 0) {
/* The requested delay is less than the minimum resolution of this counter. */
delta_ticks = 1;
}
// Ticker interrupt handle
TMRB_Enable(TSB_TB1);
TMRB_SetRunState(TSB_TB1, TMRB_STOP);
NVIC_SetVector(INTTB1_IRQn, (uint32_t)us_ticker_irq_handler);
NVIC_EnableIRQ(INTTB1_IRQn);
// Split delta for preventing the Multiply overflowing
FFStruct.FlipflopCtrl = TMRB_FLIPFLOP_CLEAR;
FFStruct.FlipflopReverseTrg = TMRB_DISABLE_FLIPFLOP;
TMRB_SetFlipFlop(TSB_TB1, &FFStruct);
// TSB_TB0 using free-run
m_tmrb1.Mode = TMRB_INTERVAL_TIMER;
m_tmrb1.ClkDiv = TMRB_CLK_DIV;
m_tmrb1.UpCntCtrl = TMRB_AUTO_CLEAR;
m_tmrb1.TrailingTiming = delta_ticks;
m_tmrb1.LeadingTiming = delta_ticks;
TMRB_Init(TSB_TB1, &m_tmrb1);
TMRB_SetINTMask(TSB_TB1,TMRB_MASK_OVERFLOW_INT | TMRB_MASK_MATCH_LEADING_INT);
// Enable TMRB when system is in idle mode
TMRB_SetIdleMode(TSB_TB1, ENABLE);
TMRB_SetRunState(TSB_TB1, TMRB_RUN);
} }
void us_ticker_fire_interrupt(void) void us_ticker_fire_interrupt(void)
{ {
NVIC_SetPendingIRQ(INTTB1_IRQn); NVIC_SetPendingIRQ(INTTB0_IRQn);
NVIC_EnableIRQ(INTTB0_IRQn);
} }
void us_ticker_disable_interrupt(void) void us_ticker_disable_interrupt(void)
{ {
// Also disable interrupts by NVIC // Mask All interrupts
NVIC_DisableIRQ(INTTB1_IRQn); TMRB_SetINTMask(TSB_TB0, TMRB_MASK_MATCH_LEADING_INT | TMRB_MASK_MATCH_TRAILING_INT | TMRB_MASK_OVERFLOW_INT);
// Also clear and disable interrupts by NVIC
NVIC_ClearPendingIRQ(INTTB0_IRQn);
NVIC_DisableIRQ(INTTB0_IRQn);
} }
void us_ticker_clear_interrupt(void) void us_ticker_clear_interrupt(void)
{ {
// No flag to clear NVIC_ClearPendingIRQ(INTTB0_IRQn);
} }
void us_ticker_free(void) void us_ticker_free(void)
{ {
TMRB_SetINTMask(TSB_TB0, TMRB_MASK_MATCH_LEADING_INT | TMRB_MASK_MATCH_TRAILING_INT | TMRB_MASK_OVERFLOW_INT);
NVIC_ClearPendingIRQ(INTTB0_IRQn);
NVIC_DisableIRQ(INTTB0_IRQn);
TMRB_SetRunState(TSB_TB0, TMRB_STOP);
TMRB_Disable(TSB_TB0);
us_ticker_inited = false;
} }

View File

@ -4416,7 +4416,7 @@
"extra_labels": ["TOSHIBA"], "extra_labels": ["TOSHIBA"],
"macros": ["__TMPM46B__"], "macros": ["__TMPM46B__"],
"supported_toolchains": ["GCC_ARM", "ARM", "IAR"], "supported_toolchains": ["GCC_ARM", "ARM", "IAR"],
"device_has": ["ANALOGIN", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SERIAL_FC", "SPI", "I2C", "STDIO_MESSAGES", "TRNG", "FLASH", "SLEEP"], "device_has": ["USTICKER", "ANALOGIN", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SERIAL_FC", "SPI", "I2C", "STDIO_MESSAGES", "TRNG", "FLASH", "SLEEP"],
"device_name": "TMPM46BF10FG", "device_name": "TMPM46BF10FG",
"detect_code": ["7013"], "detect_code": ["7013"],
"release_versions": ["5"], "release_versions": ["5"],