Skip to content

Commit

Permalink
Fix ATSAMD21 Robus timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-rabault committed May 23, 2024
1 parent aece26b commit 8ec6581
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
21 changes: 16 additions & 5 deletions network/robus_network/HAL/ATSAMD21/robus_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,24 @@ static void RobusHAL_TimeoutInit(void)
******************************************************************************/
_CRITICAL void RobusHAL_ResetTimeout(uint16_t nbrbit)
{
NVIC_ClearPendingIRQ(ROBUS_TIMER_IRQ); // clear IT pending
ROBUS_TIMER->COUNT16.TC_INTFLAG = TC_INTFLAG_OVF_Msk; // clear flag
ROBUS_TIMER->COUNT16.TC_CTRLA &= ~TC_CTRLA_ENABLE_Msk;
uint32_t diff;
// We use the end of the timer as trigger.
diff = (0xFFFF - ROBUS_TIMER->COUNT16.TC_COUNT) / timoutclockcnt; // Compute remaining time before timeout

if (diff < DEFAULT_TIMEOUT)
{
ROBUS_TIMER->COUNT16.TC_CTRLA &= ~TC_CTRLA_ENABLE_Msk; // Disable timer
NVIC_ClearPendingIRQ(ROBUS_TIMER_IRQ); // Clear IT pending
ROBUS_TIMER->COUNT16.TC_INTFLAG = TC_INTFLAG_OVF_Msk; // clear flag
ROBUS_TIMER->COUNT16.TC_COUNT = 0xFFFF - (timoutclockcnt * DEFAULT_TIMEOUT); // Load the timer with it's default value
}
if (nbrbit != 0 && nbrbit != DEFAULT_TIMEOUT)
{
ROBUS_TIMER->COUNT16.TC_COUNT = 0xFFFF - (timoutclockcnt * nbrbit); // Load the timer
}
if (nbrbit != 0)
{
ROBUS_TIMER->COUNT16.TC_COUNT = 0xFFFF - (timoutclockcnt * nbrbit);
ROBUS_TIMER->COUNT16.TC_CTRLA |= TC_CTRLA_ENABLE_Msk;
ROBUS_TIMER->COUNT16.TC_CTRLA |= TC_CTRLA_ENABLE_Msk; // Enable timer
}
}
/******************************************************************************
Expand Down
21 changes: 16 additions & 5 deletions network/robus_network/HAL/ATSAMD21_ARDUINO/robus_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -397,13 +397,24 @@ static void RobusHAL_TimeoutInit(void)
******************************************************************************/
_CRITICAL void RobusHAL_ResetTimeout(uint16_t nbrbit)
{
NVIC_ClearPendingIRQ(ROBUS_TIMER_IRQ); // clear IT pending
ROBUS_TIMER->COUNT16.INTFLAG.bit.OVF = 1;
ROBUS_TIMER->COUNT16.CTRLA.reg &= ~TC_CTRLA_ENABLE;
uint32_t diff;
// We use the end of the timer as trigger.
diff = (0xFFFF - ROBUS_TIMER->COUNT16.COUNT.reg) / timoutclockcnt; // Compute remaining time before timeout

if (diff < DEFAULT_TIMEOUT)
{
ROBUS_TIMER->COUNT16.CTRLA.reg &= ~TC_CTRLA_ENABLE; // Disable timer
NVIC_ClearPendingIRQ(ROBUS_TIMER_IRQ); // Clear IT pending
ROBUS_TIMER->COUNT16.INTFLAG.bit.OVF = 1; // Clear IT flag
ROBUS_TIMER->COUNT16.COUNT.reg = 0xFFFF - (timoutclockcnt * DEFAULT_TIMEOUT); // Load the timer with it's default value
}
if (nbrbit != 0 && nbrbit != DEFAULT_TIMEOUT)
{
ROBUS_TIMER->COUNT16.COUNT.reg = 0xFFFF - (timoutclockcnt * nbrbit); // Load the timer
}
if (nbrbit != 0)
{
ROBUS_TIMER->COUNT16.COUNT.reg = 0xFFFF - (timoutclockcnt * nbrbit);
ROBUS_TIMER->COUNT16.CTRLA.reg |= TC_CTRLA_ENABLE;
ROBUS_TIMER->COUNT16.CTRLA.reg |= TC_CTRLA_ENABLE; // Enable timer
}
}
/******************************************************************************
Expand Down
21 changes: 16 additions & 5 deletions network/robus_network/HAL/ATSAMD21_MBED/robus_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,13 +396,24 @@ static void RobusHAL_TimeoutInit(void)
******************************************************************************/
_CRITICAL void RobusHAL_ResetTimeout(uint16_t nbrbit)
{
NVIC_ClearPendingIRQ(ROBUS_TIMER_IRQ); // clear IT pending
ROBUS_TIMER->COUNT16.INTFLAG.bit.OVF = 1;
ROBUS_TIMER->COUNT16.CTRLA.reg &= ~TC_CTRLA_ENABLE;
uint32_t diff;
// We use the end of the timer as trigger.
diff = (0xFFFF - ROBUS_TIMER->COUNT16.COUNT.reg) / timoutclockcnt; // Compute remaining time before timeout

if (diff < DEFAULT_TIMEOUT)
{
ROBUS_TIMER->COUNT16.CTRLA.reg &= ~TC_CTRLA_ENABLE; // Disable timer
NVIC_ClearPendingIRQ(ROBUS_TIMER_IRQ); // Clear IT pending
ROBUS_TIMER->COUNT16.INTFLAG.bit.OVF = 1; // Clear IT flag
ROBUS_TIMER->COUNT16.COUNT.reg = 0xFFFF - (timoutclockcnt * DEFAULT_TIMEOUT); // Load the timer with it's default value
}
if (nbrbit != 0 && nbrbit != DEFAULT_TIMEOUT)
{
ROBUS_TIMER->COUNT16.COUNT.reg = 0xFFFF - (timoutclockcnt * nbrbit); // Load the timer
}
if (nbrbit != 0)
{
ROBUS_TIMER->COUNT16.COUNT.reg = 0xFFFF - (timoutclockcnt * nbrbit);
ROBUS_TIMER->COUNT16.CTRLA.reg |= TC_CTRLA_ENABLE;
ROBUS_TIMER->COUNT16.CTRLA.reg |= TC_CTRLA_ENABLE; // Enable timer
}
}
/******************************************************************************
Expand Down

0 comments on commit 8ec6581

Please sign in to comment.