diff --git a/Apps/Src/SendCarCAN.c b/Apps/Src/SendCarCAN.c index cdc5bd063..477ffae46 100644 --- a/Apps/Src/SendCarCAN.c +++ b/Apps/Src/SendCarCAN.c @@ -1,15 +1,17 @@ #include "common.h" #include "os_cfg_app.h" - -#include "Tasks.h" -#include "SendCarCAN.h" - #include "CANbus.h" #include "Minions.h" #include "Contactors.h" #include "Pedals.h" #include "Tasks.h" #include "SendCarCAN.h" +#include "SendTritium.h" + +#define IO_STATE_DLY_MS 250u +#define SENDCARCAN_LOOP_DLY_MS 50u // How often we should check the CAN queue (in ms delay). Must send faster than queue messages get put in +#define IO_STATE_DLY_COUNT (IO_STATE_DLY_MS / SENDCARCAN_LOOP_DLY_MS) // The number of periods to wait before sending IO state message +#define IO_STATE_TMR_DLY_TS ((IO_STATE_TMR_DLY_MS * OS_CFG_TMR_TASK_RATE_HZ) / (1000u))// **** delete? // fifo #define FIFO_TYPE CANDATA_t @@ -17,18 +19,15 @@ #define FIFO_NAME SendCarCAN_Q #include "fifo.h" -#define SENDCARCAN_QUEUE_CHECK_MS 50 // How often we should check the CAN queue (in ms delay). Must send faster than queue messages get put in static SendCarCAN_Q_t CANFifo; static OS_SEM CarCAN_Sem4; static OS_MUTEX CarCAN_Mtx; -static OS_SEM PutIOTimerAlert_Sem4; -#define IO_STATE_TMR_DLY_MS 250u -#define IO_STATE_TMR_DLY_TS ((IO_STATE_TMR_DLY_MS * OS_CFG_TMR_TASK_RATE_HZ) / (1000u)) -static OS_TMR IOStateTmr; +static uint8_t IOStateDlyCounter = 0; + + -static void putIOStateTimerAlert(void *p_tmr, void *p_arg); static void putIOState(void); /** @@ -63,36 +62,23 @@ void Task_SendCarCAN(void *p_arg){ OSSemCreate(&CarCAN_Sem4, "CarCAN_Sem4", 0, &err); assertOSError(OS_SEND_CAN_LOC, err); - OSSemCreate(&PutIOTimerAlert_Sem4, "PutIOTimerAlert_Sem4", 0, &err); - assertOSError(OS_SEND_CAN_LOC, err); - SendCarCAN_Q_renew(&CANFifo); - // Set up timer to put car data message every IO_STATE_TMR_DLY_MS ms - OSTmrCreate(&IOStateTmr, - "IO State CAN Message Timer", - 0, - IO_STATE_TMR_DLY_TS, - OS_OPT_TMR_PERIODIC, - putIOStateTimerAlert, - NULL, - &err - ); - assertOSError(OS_SEND_CAN_LOC, err); + // Set up timer to put car data message every IO_STATE_TMR_DLY_MS ms //*************** - OSTmrStart(&IOStateTmr, &err); - assertOSError(OS_SEND_CAN_LOC, err); CANDATA_t message; memset(&message, 0, sizeof(CANDATA_t)); while (1) { - OSSemPend(&PutIOTimerAlert_Sem4, 0, OS_OPT_PEND_NON_BLOCKING, &ticks, &err); - if (err != OS_ERR_PEND_WOULD_BLOCK) { // The timer fired; it's time to send an IO state message + + if (++IOStateDlyCounter >= IO_STATE_DLY_COUNT) { // Send IO State message approximately every IO_STATE_DLY_MS + IOStateDlyCounter = 0; putIOState(); } + // Check if there's something to send in the queue (either IOState or Car state from sendTritium) OSSemPend(&CarCAN_Sem4, 0, OS_OPT_PEND_NON_BLOCKING, &ticks, &err); if (err != OS_ERR_PEND_WOULD_BLOCK) { // We have a message in the queue to send @@ -109,7 +95,7 @@ void Task_SendCarCAN(void *p_arg){ if(res) CANbus_Send(message, true, CARCAN); } - OSTimeDlyHMSM(0, 0, 0, SENDCARCAN_QUEUE_CHECK_MS, OS_OPT_TIME_HMSM_STRICT, &err); + OSTimeDlyHMSM(0, 0, 0, SENDCARCAN_LOOP_DLY_MS, OS_OPT_TIME_HMSM_STRICT, &err); assertOSError(OS_SEND_CAN_LOC, err); } @@ -140,10 +126,3 @@ static void putIOState(void){ SendCarCAN_Put(message); } - -// Callback function for the putIOState timer -static void putIOStateTimerAlert(void *p_tmr, void *p_arg){ - OS_ERR err; - OSSemPost(&PutIOTimerAlert_Sem4, OS_OPT_POST_1, &err); - assertOSError(OS_SEND_CAN_LOC, err); -} \ No newline at end of file