diff --git a/engine/core/src/luos_utils.c b/engine/core/src/luos_utils.c index b009a9966..03d0562fb 100644 --- a/engine/core/src/luos_utils.c +++ b/engine/core/src/luos_utils.c @@ -58,6 +58,8 @@ void Luos_JumpToBootloader(void) ******************************************************************************/ _CRITICAL void Luos_assert(char *file, uint32_t line) { + // Reset phy to avoid locking the phys + Phy_Reset(); // prepare a message as a node. // To do that we have to reset the service ID and clear PTP states to unlock others. diff --git a/engine/core/src/routing_table.c b/engine/core/src/routing_table.c index 1eb006cb9..5af94fb67 100644 --- a/engine/core/src/routing_table.c +++ b/engine/core/src/routing_table.c @@ -971,7 +971,7 @@ search_result_t *RTFilter_ID(search_result_t *result, uint16_t id) if (result->result_table[entry_nbr]->id != id) { // if we find an other id, erase it from the research table - memcpy(&result->result_table[entry_nbr], &result->result_table[entry_nbr + 1], sizeof(routing_table_t *) * (result->result_nbr - entry_nbr)); + memcpy(&result->result_table[entry_nbr], &result->result_table[entry_nbr + 1], sizeof(routing_table_t *) * (result->result_nbr - entry_nbr - 1)); result->result_nbr--; } else @@ -1006,7 +1006,7 @@ search_result_t *RTFilter_Type(search_result_t *result, luos_type_t type) if (result->result_table[entry_nbr]->type != type) { // if we find an other type, erase it from the research table - memcpy(&result->result_table[entry_nbr], &result->result_table[entry_nbr + 1], sizeof(routing_table_t *) * (result->result_nbr - entry_nbr)); + memcpy(&result->result_table[entry_nbr], &result->result_table[entry_nbr + 1], sizeof(routing_table_t *) * (result->result_nbr - entry_nbr - 1)); result->result_nbr--; } else @@ -1042,7 +1042,7 @@ search_result_t *RTFilter_Node(search_result_t *result, uint16_t node_id) if (RoutingTB_NodeIDFromID(result->result_table[entry_nbr]->id) != node_id) { // if we find an other node_id, erase it from the research table - memcpy(&result->result_table[entry_nbr], &result->result_table[entry_nbr + 1], sizeof(routing_table_t *) * (result->result_nbr - entry_nbr)); + memcpy(&result->result_table[entry_nbr], &result->result_table[entry_nbr + 1], sizeof(routing_table_t *) * (result->result_nbr - entry_nbr - 1)); result->result_nbr--; } else @@ -1078,7 +1078,7 @@ search_result_t *RTFilter_Alias(search_result_t *result, char *alias) if (strstr(result->result_table[entry_nbr]->alias, alias) == 0) { // if we find an other node_id, erase it from the research table - memcpy(&result->result_table[entry_nbr], &result->result_table[entry_nbr + 1], sizeof(routing_table_t *) * (result->result_nbr - entry_nbr)); + memcpy(&result->result_table[entry_nbr], &result->result_table[entry_nbr + 1], sizeof(routing_table_t *) * (result->result_nbr - entry_nbr - 1)); result->result_nbr--; } else @@ -1114,7 +1114,7 @@ search_result_t *RTFilter_Service(search_result_t *result, service_t *service) if (result->result_table[entry_nbr]->id != service->id) { // if we find an other id, erase it from the research table - memcpy(&result->result_table[entry_nbr], &result->result_table[entry_nbr + 1], sizeof(routing_table_t *) * (result->result_nbr - entry_nbr)); + memcpy(&result->result_table[entry_nbr], &result->result_table[entry_nbr + 1], sizeof(routing_table_t *) * (result->result_nbr - entry_nbr - 1)); result->result_nbr--; } else diff --git a/network/robus_network/HAL/ATSAMD21/robus_hal.c b/network/robus_network/HAL/ATSAMD21/robus_hal.c index ef9cd21f4..3f9b89e51 100644 --- a/network/robus_network/HAL/ATSAMD21/robus_hal.c +++ b/network/robus_network/HAL/ATSAMD21/robus_hal.c @@ -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 } } /****************************************************************************** diff --git a/network/robus_network/HAL/ATSAMD21_ARDUINO/robus_hal.c b/network/robus_network/HAL/ATSAMD21_ARDUINO/robus_hal.c index 2cae033b6..acdf9789b 100644 --- a/network/robus_network/HAL/ATSAMD21_ARDUINO/robus_hal.c +++ b/network/robus_network/HAL/ATSAMD21_ARDUINO/robus_hal.c @@ -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 } } /****************************************************************************** diff --git a/network/robus_network/HAL/ATSAMD21_MBED/robus_hal.c b/network/robus_network/HAL/ATSAMD21_MBED/robus_hal.c index f629f9edc..08fe377eb 100644 --- a/network/robus_network/HAL/ATSAMD21_MBED/robus_hal.c +++ b/network/robus_network/HAL/ATSAMD21_MBED/robus_hal.c @@ -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 } } /****************************************************************************** diff --git a/network/robus_network/HAL/ESP32/robus_hal.c b/network/robus_network/HAL/ESP32/robus_hal.c index 9dde9711f..66505f739 100644 --- a/network/robus_network/HAL/ESP32/robus_hal.c +++ b/network/robus_network/HAL/ESP32/robus_hal.c @@ -412,15 +412,32 @@ static void RobusHAL_TimeoutInit(void) ******************************************************************************/ _CRITICAL void RobusHAL_ResetTimeout(uint16_t nbrbit) { - // disable Counter - timer_hal_set_counter_value(&timeout_hal_context, 0); - timer_hal_set_counter_enable(&timeout_hal_context, TIMER_PAUSE); - timer_hal_clear_intr_status(&timeout_hal_context); - timer_hal_intr_disable(&timeout_hal_context); + uint64_t arr_val, diff, cnt_val; + timer_get_alarm_value(ROBUS_TIMER_GROUP, ROBUS_TIMER, &arr_val); // Get actual timeout value + timer_get_counter_value(ROBUS_TIMER_GROUP, ROBUS_TIMER, &cnt_val); // Get counter value + diff = arr_val - cnt_val; // Compute remaining time before timeout + + if (diff < DEFAULT_TIMEOUT) + { + // disable Counter + timer_hal_set_counter_enable(&timeout_hal_context, TIMER_PAUSE); + timer_hal_clear_intr_status(&timeout_hal_context); + timer_hal_intr_disable(&timeout_hal_context); + + timer_hal_set_alarm_value(&timeout_hal_context, DEFAULT_TIMEOUT); + timer_hal_set_counter_value(&timeout_hal_context, 0); + } + + if (nbrbit != 0 && nbrbit != DEFAULT_TIMEOUT) + { + // Reset counter + timer_hal_set_alarm_value(&timeout_hal_context, nbrbit); + timer_hal_set_counter_value(&timeout_hal_context, 0); + } - // Reset counter if (nbrbit != 0) { + // Start counter timer_hal_intr_enable(&timeout_hal_context); timer_hal_set_counter_enable(&timeout_hal_context, TIMER_START); } diff --git a/network/robus_network/HAL/STM32F0/robus_hal.c b/network/robus_network/HAL/STM32F0/robus_hal.c index b806995a1..d59a384b9 100644 --- a/network/robus_network/HAL/STM32F0/robus_hal.c +++ b/network/robus_network/HAL/STM32F0/robus_hal.c @@ -389,13 +389,31 @@ static void RobusHAL_TimeoutInit(void) ******************************************************************************/ _CRITICAL void RobusHAL_ResetTimeout(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) + /* TODO: optimize further by getting rid of the need to reach for the ARR value. + The same result can be achieved by only setting the start value of the counter and letting it count down toward 0. + An IRQ can then be generated when reaching 0. This way diff = counter_value. + */ + + 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) + { + 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); + } + if (nbrbit != 0 && nbrbit != DEFAULT_TIMEOUT) { LL_TIM_SetAutoReload(ROBUS_TIMER, nbrbit); // reload value + LL_TIM_SetCounter(ROBUS_TIMER, 0); // Reset counter + } + if (nbrbit != 0) + { LL_TIM_EnableCounter(ROBUS_TIMER); } } diff --git a/network/robus_network/HAL/STM32F4/robus_hal.c b/network/robus_network/HAL/STM32F4/robus_hal.c index 39fd5aaf5..a2033d566 100644 --- a/network/robus_network/HAL/STM32F4/robus_hal.c +++ b/network/robus_network/HAL/STM32F4/robus_hal.c @@ -393,13 +393,31 @@ static void RobusHAL_TimeoutInit(void) ******************************************************************************/ _CRITICAL void RobusHAL_ResetTimeout(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) + /* TODO: optimize further by getting rid of the need to reach for the ARR value. + The same result can be achieved by only setting the start value of the counter and letting it count down toward 0. + An IRQ can then be generated when reaching 0. This way diff = counter_value. + */ + + 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) + { + 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); + } + if (nbrbit != 0 && nbrbit != DEFAULT_TIMEOUT) { LL_TIM_SetAutoReload(ROBUS_TIMER, nbrbit); // reload value + LL_TIM_SetCounter(ROBUS_TIMER, 0); // Reset counter + } + if (nbrbit != 0) + { LL_TIM_EnableCounter(ROBUS_TIMER); } } diff --git a/network/robus_network/HAL/STM32G4/robus_hal.c b/network/robus_network/HAL/STM32G4/robus_hal.c index 6ddc0b427..0984a7bed 100644 --- a/network/robus_network/HAL/STM32G4/robus_hal.c +++ b/network/robus_network/HAL/STM32G4/robus_hal.c @@ -390,13 +390,31 @@ static void RobusHAL_TimeoutInit(void) ******************************************************************************/ _CRITICAL void RobusHAL_ResetTimeout(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) + /* TODO: optimize further by getting rid of the need to reach for the ARR value. + The same result can be achieved by only setting the start value of the counter and letting it count down toward 0. + An IRQ can then be generated when reaching 0. This way diff = counter_value. + */ + + 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) + { + 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); + } + if (nbrbit != 0 && nbrbit != DEFAULT_TIMEOUT) { LL_TIM_SetAutoReload(ROBUS_TIMER, nbrbit); // reload value + LL_TIM_SetCounter(ROBUS_TIMER, 0); // Reset counter + } + if (nbrbit != 0) + { LL_TIM_EnableCounter(ROBUS_TIMER); } } diff --git a/network/robus_network/HAL/STM32L0/robus_hal.c b/network/robus_network/HAL/STM32L0/robus_hal.c index acefb3a4e..85d62dffe 100644 --- a/network/robus_network/HAL/STM32L0/robus_hal.c +++ b/network/robus_network/HAL/STM32L0/robus_hal.c @@ -386,15 +386,33 @@ static void RobusHAL_TimeoutInit(void) * @param None * @return None ******************************************************************************/ -void RobusHAL_ResetTimeout(uint16_t nbrbit) +_CRITICAL void RobusHAL_ResetTimeout(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) + /* TODO: optimize further by getting rid of the need to reach for the ARR value. + The same result can be achieved by only setting the start value of the counter and letting it count down toward 0. + An IRQ can then be generated when reaching 0. This way diff = counter_value. + */ + + 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) + { + 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); + } + if (nbrbit != 0 && nbrbit != DEFAULT_TIMEOUT) { LL_TIM_SetAutoReload(ROBUS_TIMER, nbrbit); // reload value + LL_TIM_SetCounter(ROBUS_TIMER, 0); // Reset counter + } + if (nbrbit != 0) + { LL_TIM_EnableCounter(ROBUS_TIMER); } } diff --git a/network/robus_network/HAL/STM32L4/robus_hal.c b/network/robus_network/HAL/STM32L4/robus_hal.c index cc7dac4c7..ed3145e3b 100644 --- a/network/robus_network/HAL/STM32L4/robus_hal.c +++ b/network/robus_network/HAL/STM32L4/robus_hal.c @@ -390,13 +390,30 @@ static void RobusHAL_TimeoutInit(void) ******************************************************************************/ _CRITICAL void RobusHAL_ResetTimeout(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) + /* TODO: optimize further by getting rid of the need to reach for the ARR value. + The same result can be achieved by only setting the start value of the counter and letting it count down toward 0. + An IRQ can then be generated when reaching 0. This way diff = counter_value. + */ + 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) + { + 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); + } + if (nbrbit != 0 && nbrbit != DEFAULT_TIMEOUT) { LL_TIM_SetAutoReload(ROBUS_TIMER, nbrbit); // reload value + LL_TIM_SetCounter(ROBUS_TIMER, 0); // Reset counter + } + if (nbrbit != 0) + { LL_TIM_EnableCounter(ROBUS_TIMER); } } diff --git a/network/robus_network/src/robus.c b/network/robus_network/src/robus.c index afa7365f9..a95cd1f5b 100644 --- a/network/robus_network/src/robus.c +++ b/network/robus_network/src/robus.c @@ -40,6 +40,9 @@ robus_encaps_t encaps[MAX_MSG_NB]; // Store all the CRC for each msg to transmit ******************************************************************************/ void Robus_Init(void) { + // Init Reception + Recep_Init(); + // Init hal RobusHAL_Init(); @@ -49,9 +52,6 @@ void Robus_Init(void) // Init transmission Transmit_Init(); - // Init Receiption - Recep_Init(); - // Instantiate the phy struct phy_robus = Phy_Create(Robus_JobHandler, Robus_RunTopology, Robus_Reset); LUOS_ASSERT(phy_robus);