Skip to content

Commit

Permalink
Moved mutex, semaphore creation and fifo renewal to a new SendCarCAN_…
Browse files Browse the repository at this point in the history
…Init function so that other tasks can put things into the queue before SendCarCAN is ready. Additionally, created a new static task in SendCarCAN.c called PutIOState which periodically sends messages over CarCAN.
  • Loading branch information
KnockbackNemo committed Nov 11, 2023
1 parent 9043b7b commit ce1d74a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 107 deletions.
68 changes: 0 additions & 68 deletions Apps/Src/CAN_Queue.c

This file was deleted.

84 changes: 54 additions & 30 deletions Apps/Src/SendCarCAN.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
#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 SENDCARCAN_LOOP_DLY_MS 5u // 50How 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

// Task_PutIOState
OS_TCB putIOState_TCB;
CPU_STK putIOState_Stk[TASK_SEND_CAR_CAN_STACK_SIZE];

//fifo
#define FIFO_TYPE CANDATA_t
#define FIFO_SIZE 256
Expand All @@ -33,10 +37,7 @@ static SendCarCAN_Q_t CANFifo;
static OS_SEM CarCAN_Sem4;
static OS_MUTEX CarCAN_Mtx;

static uint8_t IOStateDlyCounter = 0;


static void putIOState(void);
static void Task_PutIOState(void *p_arg);

/**
* @brief print SendCarCAN_Q put/get for debug purposes
Expand Down Expand Up @@ -68,11 +69,10 @@ void SendCarCAN_Put(CANDATA_t message){
}

/**
* @brief Grabs the latest messages from the queue and sends over CarCAN
* @brief Initialize SendCarCAN
*/
void Task_SendCarCAN(void *p_arg){
void SendCarCAN_Init(void) {
OS_ERR err;
CPU_TS ticks;

OSMutexCreate(&CarCAN_Mtx, "CarCAN_Mtx", &err);
assertOSError(err);
Expand All @@ -81,37 +81,52 @@ void Task_SendCarCAN(void *p_arg){
assertOSError(err);

SendCarCAN_Q_renew(&CANFifo);
}

/**
* @brief Grabs the latest messages from the queue and sends over CarCAN
*/
void Task_SendCarCAN(void *p_arg){
OS_ERR err;
CPU_TS ticks;

CANDATA_t message;
memset(&message, 0, sizeof(CANDATA_t));

// PutIOState
OSTaskCreate(
(OS_TCB*)&putIOState_TCB,
(CPU_CHAR*)"PutIOState",
(OS_TASK_PTR)Task_PutIOState,
(void*)NULL,
(OS_PRIO)TASK_SEND_CAR_CAN_PRIO,
(CPU_STK*)putIOState_Stk,
(CPU_STK_SIZE)WATERMARK_STACK_LIMIT,
(CPU_STK_SIZE)TASK_SEND_CAR_CAN_STACK_SIZE,
(OS_MSG_QTY)0,
(OS_TICK)0,
(void*)NULL,
(OS_OPT)(OS_OPT_TASK_STK_CLR),
(OS_ERR*)&err
);
assertOSError(err);

while (1) {


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
assertOSError(err);

OSMutexPend(&CarCAN_Mtx, 0, OS_OPT_PEND_BLOCKING, &ticks, &err);
assertOSError(err);

bool res = SendCarCAN_Q_get(&CANFifo, &message);
assertOSError(err);
OSSemPend(&CarCAN_Sem4, 0, OS_OPT_PEND_BLOCKING, &ticks, &err);
assertOSError(err);

OSMutexPost(&CarCAN_Mtx, OS_OPT_POST_NONE, &err);
OSMutexPend(&CarCAN_Mtx, 0, OS_OPT_PEND_BLOCKING, &ticks, &err);
assertOSError(err);

bool res = SendCarCAN_Q_get(&CANFifo, &message);
assertOSError(err);

if(res) CANbus_Send(message, true, CARCAN);
}

OSTimeDlyHMSM(0, 0, 0, SENDCARCAN_LOOP_DLY_MS, OS_OPT_TIME_HMSM_STRICT, &err);
OSMutexPost(&CarCAN_Mtx, OS_OPT_POST_NONE, &err);
assertOSError(err);

if(res) CANbus_Send(message, true, CARCAN);

}
}
Expand Down Expand Up @@ -141,3 +156,12 @@ static void putIOState(void){

SendCarCAN_Put(message);
}

/**
* @brief sends IO information over CarCAN every IO_STATE_DLY_MS
*/
static void Task_PutIOState(void *p_arg) {
OS_ERR err;
putIOState();
OSTimeDlyHMSM(0, 0, 0, IO_STATE_DLY_MS, OS_OPT_TIME_HMSM_STRICT, &err);
}
1 change: 1 addition & 0 deletions Apps/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ void Task_Init(void *p_arg){

// Initialize applications
UpdateDisplay_Init();
SendCarCAN_Init();

// Initialize SendTritium
OSTaskCreate(
Expand Down
18 changes: 9 additions & 9 deletions Tests/Test_App_SendCarCAN.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,17 @@ void Task1(void *arg)
{
OS_ERR err;

OS_CPU_SysTickInit(SystemCoreClock / (CPU_INT32U)OSCfg_TickRate_Hz);
CPU_Init();
BSP_UART_Init(UART_2);
Pedals_Init();
CANbus_Init(CARCAN, (CANId_t*)carCANFilterList, NUM_CARCAN_FILTERS); // Set filter list to NULL to receive all messages
CANbus_Init(MOTORCAN, NULL, NUM_MOTORCAN_FILTERS);
SendCarCAN_Init();
Minions_Init();
Display_Init();
UpdateDisplay_Init();

OS_CPU_SysTickInit(SystemCoreClock / (CPU_INT32U)OSCfg_TickRate_Hz);

// Initialize SendCarCAN
OSTaskCreate(
(OS_TCB*)&SendCarCAN_TCB,
Expand All @@ -113,7 +113,7 @@ void Task1(void *arg)
(OS_OPT)(OS_OPT_TASK_STK_CLR),
(OS_ERR*)&err
);
assertOSError(OS_MAIN_LOC, err);
assertOSError(err);

// Initialize SendTritium
OSTaskCreate(
Expand All @@ -131,7 +131,7 @@ void Task1(void *arg)
(OS_OPT)(OS_OPT_TASK_STK_CLR),
(OS_ERR*)&err
);
assertOSError(OS_MAIN_LOC, err);
assertOSError(err);

// Initialize ReadTritium
OSTaskCreate(
Expand All @@ -149,7 +149,7 @@ void Task1(void *arg)
(OS_OPT)(OS_OPT_TASK_STK_CLR),
(OS_ERR*)&err
);
assertOSError(OS_MAIN_LOC, err);
assertOSError(err);

// Create task to read CANbus
OSTaskCreate(
Expand All @@ -167,7 +167,7 @@ void Task1(void *arg)
(OS_OPT)(OS_OPT_TASK_STK_CLR),
(OS_ERR*)&err
);
assertOSError(OS_MAIN_LOC, err);
assertOSError(err);

// Initialize UpdateDisplay
OSTaskCreate(
Expand All @@ -185,7 +185,7 @@ void Task1(void *arg)
(OS_OPT)(OS_OPT_TASK_STK_CLR),
(OS_ERR*)&err
);
assertOSError(OS_MAIN_LOC, err);
assertOSError(err);


while (1) {
Expand Down Expand Up @@ -237,8 +237,8 @@ int main(void)
(OS_OPT)(OS_OPT_TASK_STK_CLR),
(OS_ERR *)&err
);
assertOSError(OS_MAIN_LOC, err);
assertOSError(err);

OSStart(&err);
assertOSError(OS_MAIN_LOC, err);
assertOSError(err);
}

0 comments on commit ce1d74a

Please sign in to comment.