Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix timeout management in ROBUS protocol #484

Closed
8 changes: 8 additions & 0 deletions engine/core/src/service.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ void Service_AddAutoUpdateTarget(service_t *service, uint16_t target, uint16_t t
LUOS_ASSERT(service && (target != 0));
for (uint16_t i = 0; i < MAX_AUTO_REFRESH_NUMBER; i++)
{
// If this target ask something from this service just update the value
if ((service_ctx.auto_refresh[i].service == service) && (service_ctx.auto_refresh[i].target == target))
{
service_ctx.auto_refresh[i].time_ms = time_ms;
service_ctx.auto_refresh[i].last_update = LuosHAL_GetSystick();
return;
}
// If this slot is empty this mean that we didn't find the target in the list, just add it.
if (service_ctx.auto_refresh[i].time_ms == 0)
{
service_ctx.auto_refresh[i].service = service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void Convert_CustomJsonToMsg(service_t *service, uint16_t target_id, char *prope
// This function is called by the gate to convert a message into a piece of Json.
// This is typically used when a message is received by the gate with an unknown command.
// You can use it to compose your own piece of Json out of the message data.
void Convert_CustomMsgToJson(msg_t *msg, char *data)
void Convert_CustomMsgToJson(const msg_t *msg, char *data)
{
if (msg->header.cmd == LINEAR_POSITION_2D)
{
Expand Down
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case you want to enable the timer at DEFAULT_TIMEOUT. Instead of 0 you could use DEFAULT_TIMEOUT to improve readability.
(This would allow you to remove the boolean parameters, see other review comment)

// 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should remove those "test" comments.

}
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, you just want to shut down the timer so putting the value to 0 makes sense. You could remove the boolean value and just check if the value is 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
2 changes: 1 addition & 1 deletion tool_services/gate/TinyJSON/bootloader_ex.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ uint16_t Bootloader_StartData(char *data)
* @param service pointer, luos message
* @return None
******************************************************************************/
uint16_t Bootloader_LuosToJson(msg_t *msg, char *data)
uint16_t Bootloader_LuosToJson(const msg_t *msg, char *data)
{
uint16_t response_cmd = msg->header.cmd;
uint16_t node_id = RoutingTB_NodeIDFromID(msg->header.source);
Expand Down
2 changes: 1 addition & 1 deletion tool_services/gate/TinyJSON/bootloader_ex.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/*******************************************************************************
* Function
******************************************************************************/
uint16_t Bootloader_LuosToJson(msg_t *, char *);
uint16_t Bootloader_LuosToJson(const msg_t *, char *);
void Bootloader_JsonToLuos(service_t *, char *, json_t const *);
uint16_t Bootloader_StartData(char *);
void Bootloader_EndData(service_t *, char *, char *);
Expand Down
23 changes: 6 additions & 17 deletions tool_services/gate/TinyJSON/convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ __attribute__((weak)) void Convert_CustomJsonToMsg(service_t *service, uint16_t
* @param None
* @return None
******************************************************************************/
__attribute__((weak)) void Convert_CustomMsgToJson(msg_t *msg, char *data)
__attribute__((weak)) void Convert_CustomMsgToJson(const msg_t *msg, char *data)
{
return;
}
Expand Down Expand Up @@ -132,7 +132,7 @@ void Convert_DataToMsg(service_t *service, char *data)
return;
}

json_t const *services = json_getProperty(root, "services");
json_t const *services = json_getProperty(root, "s");
// Get services
if (services != 0)
{
Expand Down Expand Up @@ -543,12 +543,7 @@ void Convert_JsonToMsg(service_t *service, uint16_t id, luos_type_t type, char *
{
if (type != GATE_TYPE)
{
// remove any current updates
time = TimeOD_TimeFrom_s(0);
TimeOD_TimeToMsg(&time, &msg);
msg.header.cmd = UPDATE_PUB;
Luos_SendMsg(service, &msg);
// configure the new update value
// Configure the new update value
time = TimeOD_TimeFrom_s((float)json_getReal(jobj));
TimeOD_TimeToMsg(&time, &msg);
msg.header.cmd = UPDATE_PUB;
Expand Down Expand Up @@ -736,8 +731,8 @@ void Convert_JsonToMsg(service_t *service, uint16_t id, luos_type_t type, char *
// This function start a Json structure and return the string size.
uint16_t Convert_StartData(char *data)
{
memcpy(data, "{\"services\":{", sizeof("{\"services\":{"));
return (sizeof("{\"services\":{") - 1);
memcpy(data, "{\"s\":{", sizeof("{\"s\":{"));
return (sizeof("{\"s\":{") - 1);
}
// This function start a Service into a Json structure and return the string size.
uint16_t Convert_StartServiceData(char *data, char *alias)
Expand All @@ -746,7 +741,7 @@ uint16_t Convert_StartServiceData(char *data, char *alias)
return (uint16_t)strlen(data);
}
// This function create the Json content from a message and return the string size.
uint16_t Convert_MsgToData(msg_t *msg, char *data)
uint16_t Convert_MsgToData(const msg_t *msg, char *data)
{
float fdata;
switch (msg->header.cmd)
Expand Down Expand Up @@ -1086,12 +1081,6 @@ void Convert_RoutingTableData(service_t *service)
*(--json_ptr) = '\0';
// End the Json message
sprintf(json_ptr, "]}\n");
// Run loop before to flush residual msg on the pipe
Luos_Loop();
// reset all the msg in pipe link
PipeLink_Reset(service);
// call Luos loop to generap a Luos Task with this msg
Luos_Loop();
// Send the message to pipe
PipeLink_Send(service, json, strlen(json));
}
Expand Down
2 changes: 1 addition & 1 deletion tool_services/gate/TinyJSON/custom-json.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
#include "luos_engine.h"

void Convert_CustomJsonToMsg(service_t *service, uint16_t target_id, char *property, const json_t *jobj, char *json_str);
void Convert_CustomMsgToJson(msg_t *msg, char *data);
void Convert_CustomMsgToJson(const msg_t *msg, char *data);
const char *Convert_CustomStringFromType(luos_type_t type);
2 changes: 1 addition & 1 deletion tool_services/gate/convert.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void Convert_DataToMsg(service_t *service, char *data);
// Luos service information to Data convertion
uint16_t Convert_StartData(char *data);
uint16_t Convert_StartServiceData(char *data, char *alias);
uint16_t Convert_MsgToData(msg_t *msg, char *data);
uint16_t Convert_MsgToData(const msg_t *msg, char *data);
uint16_t Convert_EndServiceData(char *data);
void Convert_EndData(service_t *service, char *data, char *data_ptr);
void Convert_VoidData(service_t *service);
Expand Down
Loading
Loading