diff --git a/Inc/mmr_can.h b/Inc/mmr_can.h index 0544c05..75b7499 100644 --- a/Inc/mmr_can.h +++ b/Inc/mmr_can.h @@ -8,6 +8,7 @@ #include "mmr_can_types.h" #include "mmr_can_optimize.h" #include "mmr_can_binary_literals.h" +#include "mmr_can_timer_scs.h" #ifndef MMR_CAN_RX_FIFO #define MMR_CAN_RX_FIFO CAN_RX_FIFO0 diff --git a/Inc/mmr_can_timer_scs.h b/Inc/mmr_can_timer_scs.h new file mode 100644 index 0000000..6a13ba9 --- /dev/null +++ b/Inc/mmr_can_timer_scs.h @@ -0,0 +1,60 @@ +#ifndef INC_MMR_CAN_TIMER_SCS_H_ +#define INC_MMR_CAN_TIMER_SCS_H_ + +#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 + */ +#ifndef MMR_SCS_NR_TIMERS +#define MMR_SCS_NR_TIMERS 5 +#endif + +/** + * It depends on how many bits the board devotes to the timer + * Check the datasheet + */ +typedef uint32_t TimerRange; + + + +typedef struct { + MmrCanMessageId scsId; + CanId receiverId; + TimerRange counter; +} 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 microseconds ( 1ms ) + */ +bool MMR_CAN_ClearTimerSCS(MmrCanMessageId scsId, CanId receiverId); +bool MMR_CAN_SetTimerSCS(MmrCanMessageId scsId, CanId receiverId, TimerRange currentTime); + +/** + * @brief The delay is taken individually so that the 'manager' + * can first check the ACK and eventually reset the associated arr_timer + * + * @param rtrResponse the 'manager' will have to manage an array of struct RTRresponse + * @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); + + +#endif // !INC_MMR_CAN_TIMER_SCS_H_ \ No newline at end of file diff --git a/Src/mmr_can_timer_scs.c b/Src/mmr_can_timer_scs.c new file mode 100644 index 0000000..6331b5b --- /dev/null +++ b/Src/mmr_can_timer_scs.c @@ -0,0 +1,72 @@ +#include "mmr_can.h" + +static MmrTimerSCS arr_timer[MMR_SCS_NR_TIMERS] = {}; + +static MmrTimerSCS* findTimer(MmrCanMessageId scsId, CanId receiverId); + + +void MMR_CAN_InitTimerSCS() { + for (int i = 0; i < MMR_SCS_NR_TIMERS; i++) + { + arr_timer[i].scsId = 0; + arr_timer[i].receiverId = 0; + arr_timer[i].counter = 0; + } +} + + +bool MMR_CAN_ClearTimerSCS(MmrCanMessageId scsId, CanId receiverId) { + MmrTimerSCS *timer = findTimer(scsId, receiverId); + if (timer == NULL) { + return false; + } + + timer->scsId = 0; + timer->receiverId = 0; + timer->counter = 0; + return true; +} + + +bool MMR_CAN_SetTimerSCS(MmrCanMessageId scsId, CanId receiverId, TimerRange currentTime) { + MmrTimerSCS *timer = findTimer(scsId, receiverId); + if (timer == NULL) { + return false; + } + + timer->scsId = scsId; + timer->receiverId = receiverId; + timer->counter = currentTime; + return true; +} + + +bool MMR_CAN_GetTimerSCS(RTRresponse *rtrResponse, TimerRange currentTime, TimerRange thresholdDelay) { + MmrTimerSCS *timer = findTimer( + rtrResponse->scsId, + rtrResponse->receiverId + ); + + if (timer == NULL) { + return false; + } + + if (currentTime - timer->counter >= thresholdDelay) { + rtrResponse->rtr++; + } + return true; +} + + +MmrTimerSCS* findTimer(MmrCanMessageId scsId, CanId receiverId) { + int i = 0; + for (; i < MMR_SCS_NR_TIMERS; i++) { + MmrTimerSCS *timer = arr_timer + i; + + if (timer->scsId == scsId && arr_timer->receiverId == receiverId) { + return timer; + } + } + + return NULL; +}