From 607817389c0827bf55434bf3edb04d0d613d3332 Mon Sep 17 00:00:00 2001 From: Nicola Gutierrez Date: Thu, 17 Feb 2022 17:22:27 +0100 Subject: [PATCH] Add SCS manager bozza --- Inc/mmr_can_scs_manager.h | 37 ++++++++++++++++++++++++ Inc/mmr_can_timer_scs.h | 17 +---------- Src/mmr_can_scs_manager.c | 61 +++++++++++++++++++++++++++++++++++++++ Src/mmr_can_send_scs.c | 6 ++++ Src/mmr_can_timer_scs.c | 30 +++++++------------ 5 files changed, 115 insertions(+), 36 deletions(-) create mode 100644 Inc/mmr_can_scs_manager.h create mode 100644 Src/mmr_can_scs_manager.c diff --git a/Inc/mmr_can_scs_manager.h b/Inc/mmr_can_scs_manager.h new file mode 100644 index 0000000..17b9184 --- /dev/null +++ b/Inc/mmr_can_scs_manager.h @@ -0,0 +1,37 @@ +#ifndef INC_MMR_CAN_SCS_MANAGER_H_ +#define INC_MMR_CAN_SCS_MANAGER_H_ + +#include "mmr_can.h" +#include "mmr_can_timer_scs.h" + +/** + * TODO: + * - GetCurrentTime + * - ACK + * - Reset timer + * - Setup method + */ + + +/** + * @brief Represents the base-struct to manage a single RTR + * and allows to interface with the associated SCS's timer + */ +typedef struct { + MmrCanMessageId scsId; + CanId receiverId; + int rtr; +} RTRresponse; + + +/** + * It is used to iterate the RTRresponse array in the CheckSCS function + */ +static int counter; + + +bool MMR_CAN_SetRTRresponse(MmrCanMessageId scsId, CanId receiverId, int rtr); +TimerRange MMR_CAN_GetCurrentTime(); +void MMR_CAN_CheckSCS(); // What is necessary to do ritrasmission (mmr_can_send_scs) + +#endif // !INC_MMR_CAN_SCS_MANAGER_H_ \ No newline at end of file diff --git a/Inc/mmr_can_timer_scs.h b/Inc/mmr_can_timer_scs.h index d9bcf29..170922f 100644 --- a/Inc/mmr_can_timer_scs.h +++ b/Inc/mmr_can_timer_scs.h @@ -4,7 +4,6 @@ #include "mmr_can_message_id.h" #include "mmr_can_types.h" - /** * Scs_timer represents the max number of counter * that need to checked w/current time @@ -19,8 +18,6 @@ */ typedef uint32_t TimerRange; - - typedef struct { MmrCanMessageId scsId; CanId receiverId; @@ -28,18 +25,6 @@ typedef struct { } MmrTimerSCS; -/** - * @brief Represents the base-struct to manage a single RTR - * and allows to interface with the associated SCS's timer - */ -typedef struct { - MmrCanMessageId scsId; - CanId receiverId; - int rtr; -} RTRresponse; - - - /** * All time units are to be considered as milliseconds (1ms) */ @@ -54,7 +39,7 @@ bool MMR_CAN_SetTimerSCS(MmrCanMessageId scsId, CanId receiverId, TimerRange cur * @param currentTime from __HAL_TIM_GET_COUNTER(&htimX) or directly from the CNT register * @param thresholdDelay by rules 500ms */ -bool MMR_CAN_GetTimerSCS(RTRresponse *rtrResponse, TimerRange currentTime, TimerRange thresholdDelay); +bool MMR_CAN_GetTimerSCS(MmrCanMessageId scsId, CanId receiverId, int *rtr, TimerRange currentTime, TimerRange thresholdDelay); #endif // !INC_MMR_CAN_TIMER_SCS_H_ diff --git a/Src/mmr_can_scs_manager.c b/Src/mmr_can_scs_manager.c new file mode 100644 index 0000000..3ab9a2a --- /dev/null +++ b/Src/mmr_can_scs_manager.c @@ -0,0 +1,61 @@ +#include "mmr_can_scs_manager.h" + +static RTRresponse arr_scs[MMR_SCS_NR_TIMERS]; + +static RTRresponse* findResponse(MmrCanMessageId scsId, CanId receiverId); + + +bool SetRTRresponse(MmrCanMessageId scsId, CanId receiverId, int rtr) +{ + RTRresponse *response = findResponse(0, 0); + if (response == NULL) return false; + + response->scsId = scsId; + response->receiverId = receiverId; + response->rtr = 0; + + return MMR_CAN_SetTimerSCS(response->scsId, response->receiverId, MMR_CAN_GetCurrentTime); +} + +void CheckSCS() +{ + if (counter>=MMR_SCS_NR_TIMERS) counter=0; + + RTRresponse *_rtrResponse = &arr_scs[counter]; + + if(!MMR_CAN_GetTimerSCS(_rtrResponse->scsId, _rtrResponse->receiverId, &_rtrResponse->rtr, MMR_CAN_GetCurrentTime, 500)) + { + // do something + } + + counter++; + + if (_rtrResponse->rtr==1) + { + // Return ritrasmission + } + + if (_rtrResponse->rtr>1) + { + // Return Safe State + } +} + + +TimerRange MMR_CAN_GetCurrentTime() +{ + return 0; //TODO: HAL.... +} + +RTRresponse* findResponse(MmrCanMessageId scsId, CanId receiverId) { + int i = 0; + for (; i < MMR_SCS_NR_TIMERS; i++) { + RTRresponse *response = arr_scs + i; + + if (response->scsId == scsId && response->receiverId == receiverId) { + return response; + } + } + + return NULL; +} \ No newline at end of file diff --git a/Src/mmr_can_send_scs.c b/Src/mmr_can_send_scs.c index 9df0218..fd70002 100644 --- a/Src/mmr_can_send_scs.c +++ b/Src/mmr_can_send_scs.c @@ -1,4 +1,5 @@ #include "mmr_can.h" +#include "mmr_can_scs_manager.h" HalStatus MMR_CAN_SendSCS( @@ -16,6 +17,11 @@ HalStatus MMR_CAN_SendSCS( .mailbox = mailbox, .length = 0, }; + + if(!MMR_CAN_SetRTRresponse()) + { + // do something + } return MMR_CAN_Send(hcan, packet); } diff --git a/Src/mmr_can_timer_scs.c b/Src/mmr_can_timer_scs.c index 6331b5b..76ba3cd 100644 --- a/Src/mmr_can_timer_scs.c +++ b/Src/mmr_can_timer_scs.c @@ -1,6 +1,6 @@ -#include "mmr_can.h" +#include "mmr_can_timer_scs.h" -static MmrTimerSCS arr_timer[MMR_SCS_NR_TIMERS] = {}; +static MmrTimerSCS arr_timer[MMR_SCS_NR_TIMERS]; static MmrTimerSCS* findTimer(MmrCanMessageId scsId, CanId receiverId); @@ -17,9 +17,7 @@ void MMR_CAN_InitTimerSCS() { bool MMR_CAN_ClearTimerSCS(MmrCanMessageId scsId, CanId receiverId) { MmrTimerSCS *timer = findTimer(scsId, receiverId); - if (timer == NULL) { - return false; - } + if (timer == NULL) return false; timer->scsId = 0; timer->receiverId = 0; @@ -29,10 +27,8 @@ bool MMR_CAN_ClearTimerSCS(MmrCanMessageId scsId, CanId receiverId) { bool MMR_CAN_SetTimerSCS(MmrCanMessageId scsId, CanId receiverId, TimerRange currentTime) { - MmrTimerSCS *timer = findTimer(scsId, receiverId); - if (timer == NULL) { - return false; - } + MmrTimerSCS *timer = findTimer(0, 0); + if (timer == NULL) return false; timer->scsId = scsId; timer->receiverId = receiverId; @@ -41,18 +37,12 @@ bool MMR_CAN_SetTimerSCS(MmrCanMessageId scsId, CanId receiverId, TimerRange cur } -bool MMR_CAN_GetTimerSCS(RTRresponse *rtrResponse, TimerRange currentTime, TimerRange thresholdDelay) { - MmrTimerSCS *timer = findTimer( - rtrResponse->scsId, - rtrResponse->receiverId - ); - - if (timer == NULL) { - return false; - } +bool MMR_CAN_GetTimerSCS(MmrCanMessageId scsId, CanId receiverId, int *rtr, TimerRange currentTime, TimerRange thresholdDelay) { + MmrTimerSCS *timer = findTimer(scsId, receiverId); + if (timer == NULL) return false; if (currentTime - timer->counter >= thresholdDelay) { - rtrResponse->rtr++; + rtr++; } return true; } @@ -63,7 +53,7 @@ MmrTimerSCS* findTimer(MmrCanMessageId scsId, CanId receiverId) { for (; i < MMR_SCS_NR_TIMERS; i++) { MmrTimerSCS *timer = arr_timer + i; - if (timer->scsId == scsId && arr_timer->receiverId == receiverId) { + if (timer->scsId == scsId && timer->receiverId == receiverId) { return timer; } }