Skip to content

Commit

Permalink
fix timeout management on ROBUS protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
houkhouk committed May 6, 2024
1 parent d03b7f3 commit 8c199af
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
37 changes: 25 additions & 12 deletions network/robus_network/HAL/STM32G4/robus_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ _CRITICAL void RobusHAL_SetRxState(uint8_t Enable)
_CRITICAL void ROBUS_COM_IRQHANDLER()
{
// Reset timeout to it's default value
RobusHAL_ResetTimeout(DEFAULT_TIMEOUT);
RobusHAL_ResetTimeout(true, 0);
// reception management
if ((LL_USART_IsActiveFlag_RXNE(ROBUS_COM) != RESET) && (LL_USART_IsEnabledIT_RXNE(ROBUS_COM) != RESET))
{
Expand Down Expand Up @@ -305,7 +305,7 @@ _CRITICAL void RobusHAL_ComTransmit(uint8_t *data, uint16_t size)
// Enable Transmission complete interrupt because we only have one.
LL_USART_EnableIT_TC(ROBUS_COM);
}
RobusHAL_ResetTimeout(DEFAULT_TIMEOUT);
RobusHAL_ResetTimeout(true, 0);
}
/******************************************************************************
* @brief set state of Txlock detection pin
Expand Down Expand Up @@ -339,7 +339,7 @@ _CRITICAL uint8_t RobusHAL_GetTxLockState(void)
#ifdef USART_ISR_BUSY
if (LL_USART_IsActiveFlag_BUSY(ROBUS_COM) == true)
{
RobusHAL_ResetTimeout(DEFAULT_TIMEOUT);
RobusHAL_ResetTimeout(true, 0);
result = true;
}
#else
Expand All @@ -353,7 +353,7 @@ _CRITICAL uint8_t RobusHAL_GetTxLockState(void)
{
if (result == true)
{
RobusHAL_ResetTimeout(DEFAULT_TIMEOUT);
RobusHAL_ResetTimeout(true, 0);
}
}
}
Expand Down Expand Up @@ -388,15 +388,28 @@ static void RobusHAL_TimeoutInit(void)
* @param None
* @return None
******************************************************************************/
_CRITICAL void RobusHAL_ResetTimeout(uint16_t nbrbit)
_CRITICAL void RobusHAL_ResetTimeout(uint16_t enable, uint16_t nbrbit)
{
LL_TIM_DisableCounter(ROBUS_TIMER);
NVIC_ClearPendingIRQ(ROBUS_TIMER_IRQ); // Clear IT pending NVIC
LL_TIM_ClearFlag_UPDATE(ROBUS_TIMER);
LL_TIM_SetCounter(ROBUS_TIMER, 0); // Reset counter
if (nbrbit != 0)
{
uint32_t arr_val, diff;
arr_val = LL_TIM_ReadReg(ROBUS_TIMER, ARR); // Get actual timeout value
diff = arr_val-LL_TIM_ReadReg(ROBUS_TIMER, CNT); // Compute remaining time before timeout

if( diff < DEFAULT_TIMEOUT ){
// HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, SET);
LL_TIM_DisableCounter(ROBUS_TIMER);
NVIC_ClearPendingIRQ(ROBUS_TIMER_IRQ); // Clear IT pending NVIC
LL_TIM_ClearFlag_UPDATE(ROBUS_TIMER);

LL_TIM_SetAutoReload(ROBUS_TIMER, DEFAULT_TIMEOUT);
LL_TIM_SetCounter(ROBUS_TIMER, 0);
// HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, RESET);
}
if(nbrbit != 0){
LL_TIM_SetAutoReload(ROBUS_TIMER, nbrbit); // reload value
LL_TIM_SetCounter(ROBUS_TIMER, 0); // Reset counter
}

if(enable){
LL_TIM_EnableCounter(ROBUS_TIMER);
}
}
Expand Down Expand Up @@ -544,7 +557,7 @@ _CRITICAL void PINOUT_IRQHANDLER(uint16_t GPIO_Pin)
if ((GPIO_Pin == TX_LOCK_DETECT_PIN) && (TX_LOCK_DETECT_IRQ != DISABLE))
{
ctx.tx.lock = true;
RobusHAL_ResetTimeout(DEFAULT_TIMEOUT);
RobusHAL_ResetTimeout(true, 0);
EXTI->IMR1 &= ~TX_LOCK_DETECT_PIN;
}
else
Expand Down
2 changes: 1 addition & 1 deletion network/robus_network/HAL/STM32G4/robus_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void RobusHAL_SetRxState(uint8_t Enable);
void RobusHAL_ComTransmit(uint8_t *data, uint16_t size);
uint8_t RobusHAL_GetTxLockState(void);
void RobusHAL_SetRxDetecPin(uint8_t Enable);
void RobusHAL_ResetTimeout(uint16_t nbrbit);
void RobusHAL_ResetTimeout(uint16_t enable, uint16_t nbrbit);
void RobusHAL_SetPTPDefaultState(uint8_t PTPNbr);
void RobusHAL_SetPTPReverseState(uint8_t PTPNbr);
void RobusHAL_PushPTP(uint8_t PTPNbr);
Expand Down
2 changes: 1 addition & 1 deletion network/robus_network/src/reception.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ _CRITICAL void Recep_GetCollision(luos_phy_t *phy_robus, volatile uint8_t *data)
// Collision detection end
data_count = 0;
RobusHAL_SetRxState(false);
RobusHAL_ResetTimeout(0);
RobusHAL_ResetTimeout(false, 0);
if (ctx.tx.status == TX_NOK)
{
// Switch to catch Ack.
Expand Down
2 changes: 1 addition & 1 deletion network/robus_network/src/transmission.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ _CRITICAL void Transmit_End(void)
// A tx_task failed
nbrRetry++;
// compute a delay before retry
RobusHAL_ResetTimeout(20 * nbrRetry * (Phy_GetNodeId() + 1));
RobusHAL_ResetTimeout(true, 20 * nbrRetry * (Phy_GetNodeId() + 1));
// Lock the trasmission to be sure no one can send something from this node until next timeout.
ctx.tx.lock = true;
ctx.tx.status = TX_DISABLE;
Expand Down

0 comments on commit 8c199af

Please sign in to comment.