Skip to content

Commit

Permalink
Bundle CAN message type with JSONCAN (#1077)
Browse files Browse the repository at this point in the history
### Summary
<!-- Quick summary of changes, optional -->

Autogenerate our standard CAN message struct, rather than including it
from shared code. This is useful on DIMOS so it doesn't have to use any
shared firmware code to use JSONCAN.

### Changelist 
<!-- Give a list of the changes covered in this PR. This will help both
you and the reviewer keep this PR within scope. -->

- Move the standard CAN message struct into `Io_CanTx.h`, autogenerated
via JSONCAN
- Delete `Io_SharedCanMsg.h`
- Delete some old unused fake functions in various test files
- Generate example JSONCAN source code (what caused most of this diff,
since I forgot to in previous PRs where I changed a bunch of JSONCAN
stuff)

### Testing Done
<!-- Outline the testing that was done to demonstrate the changes are
solid. This could be unit tests, integration tests, testing on the car,
etc. Include relevant code snippets, screenshots, etc as needed. -->

None, shouldn't cause any differences in the embedded binaries.

### Resolved Issues
<!-- Link any issues that this PR resolved like so: `Resolves #1, #2,
and #5` (Note: Using this format, Github will automatically close the
issue(s) when this PR is merged in). -->

### Checklist
*Please change `[ ]` to `[x]` when you are ready.*
- [x] I have read and followed the code conventions detailed in
[README.md](../README.md) (*This will save time for both you and the
reviewer!*).
- [x] If this pull request is longer then **500** lines, I have provided
*explicit* justification in the summary above explaining why I *cannot*
break this up into multiple pull requests (*Small PR's are faster and
less painful for everyone involved!*).
  • Loading branch information
gtaharaedmonds authored Nov 7, 2023
1 parent 236bf6f commit 8b34663
Show file tree
Hide file tree
Showing 30 changed files with 475 additions and 413 deletions.
16 changes: 8 additions & 8 deletions firmware/shared/src/io/Io_SharedCan.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#include "Io_SharedCan.h"
#include "Io_SharedFreeRTOS.h"

#define CAN_TX_MSG_FIFO_ITEM_SIZE sizeof(struct CanMsg)
#define CAN_TX_MSG_FIFO_ITEM_SIZE sizeof(CanMsg)
#define CAN_TX_MSG_FIFO_LENGTH 20

#define CAN_RX_MSG_FIFO_ITEM_SIZE sizeof(struct CanMsg)
#define CAN_RX_MSG_FIFO_ITEM_SIZE sizeof(CanMsg)
#define CAN_RX_MSG_FIFO_LENGTH 20

// The following filter IDs/masks must be used with 16-bit Filter Scale
Expand Down Expand Up @@ -78,7 +78,7 @@ static struct StaticSemaphore CanTxBinarySemaphore = {
* @brief Transmits a CAN message
* @param message CAN message to transmit
*/
static HAL_StatusTypeDef Io_TransmitCanMessage(struct CanMsg *message);
static HAL_StatusTypeDef Io_TransmitCanMessage(CanMsg *message);

/**
* @brief Shared callback function to be used in each RX FIFO callback
Expand Down Expand Up @@ -124,7 +124,7 @@ static ErrorStatus Io_InitializeAllOpenFilters(CAN_HandleTypeDef *hcan)
return SUCCESS;
}

static HAL_StatusTypeDef Io_TransmitCanMessage(struct CanMsg *message)
static HAL_StatusTypeDef Io_TransmitCanMessage(CanMsg *message)
{
// Indicates the mailbox used for transmission, not currently used
uint32_t mailbox = 0;
Expand Down Expand Up @@ -158,7 +158,7 @@ static inline void Io_CanRxCallback(CAN_HandleTypeDef *hcan, uint32_t rx_fifo)
static uint32_t canrx_overflow_count = { 0 };

CAN_RxHeaderTypeDef header;
struct CanMsg message;
CanMsg message;

if (HAL_CAN_GetRxMessage(hcan, rx_fifo, &header, &message.data[0]) == HAL_OK)
{
Expand Down Expand Up @@ -235,7 +235,7 @@ void Io_SharedCan_Init(
sharedcan_hcan = hcan;
}

void Io_SharedCan_TxMessageQueueSendtoBack(const struct CanMsg *message)
void Io_SharedCan_TxMessageQueueSendtoBack(const CanMsg *message)
{
// Track how many times the CAN TX FIFO has overflowed
static uint32_t cantx_overflow_count = { 0 };
Expand Down Expand Up @@ -276,7 +276,7 @@ void Io_SharedCan_TxMessageQueueSendtoBack(const struct CanMsg *message)
}
}

void Io_SharedCan_DequeueCanRxMessage(struct CanMsg *message)
void Io_SharedCan_DequeueCanRxMessage(CanMsg *message)
{
// Get a message from the RX queue and process it, else block forever.
while (xQueueReceive(can_rx_msg_fifo.handle, message, portMAX_DELAY) != pdTRUE)
Expand All @@ -289,7 +289,7 @@ void Io_SharedCan_TransmitEnqueuedCanTxMessagesFromTask(void)

while (uxQueueMessagesWaiting(can_tx_msg_fifo.handle) > 0)
{
struct CanMsg message;
CanMsg message;

while (HAL_CAN_GetTxMailboxesFreeLevel(sharedcan_hcan) == 0U)
;
Expand Down
6 changes: 3 additions & 3 deletions firmware/shared/src/io/Io_SharedCan.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "hw_hal.h"
#include "App_CanTx.h"
#include "Io_SharedCanMsg.h"
#include "Io_CanTx.h"

#define CAN_PAYLOAD_MAX_NUM_BYTES 8 // Maximum number of bytes in a CAN payload
#define CAN_ExtID_NULL 0 // Set CAN Extended ID to 0 because we are not using it
Expand All @@ -28,14 +28,14 @@ void Io_SharedCan_Init(
* Send a message to the back of the CAN TX queue
* @param message CAN message to send
*/
void Io_SharedCan_TxMessageQueueSendtoBack(const struct CanMsg *message);
void Io_SharedCan_TxMessageQueueSendtoBack(const CanMsg *message);

/**
* Read a message from the CAN RX queue
* @note If there is no message in the CAN RX queue, this function will block
* indefinitely until a message becomes available
*/
void Io_SharedCan_DequeueCanRxMessage(struct CanMsg *message);
void Io_SharedCan_DequeueCanRxMessage(CanMsg *message);

/**
* Transmit messages in the CAN TX queue over CAN bus
Expand Down
10 changes: 0 additions & 10 deletions firmware/shared/src/io/Io_SharedCanMsg.h

This file was deleted.

2 changes: 1 addition & 1 deletion firmware/thruna/BMS/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,7 @@ void RunTaskCanRx(void *argument)
/* Infinite loop */
for (;;)
{
struct CanMsg message;
CanMsg message;
Io_SharedCan_DequeueCanRxMessage(&message);
Io_CanRx_UpdateRxTableWithMessage(&message);
}
Expand Down
4 changes: 0 additions & 4 deletions firmware/thruna/BMS/Test/Src/Test_Faults.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

namespace FaultTest
{
FAKE_VOID_FUNC(send_non_periodic_msg_BMS_STARTUP, const struct CanMsgs_bms_startup_t *);
FAKE_VOID_FUNC(send_non_periodic_msg_BMS_WATCHDOG_TIMEOUT, const struct CanMsgs_bms_watchdog_timeout_t *);
FAKE_VALUE_FUNC(float, get_pwm_frequency);
FAKE_VALUE_FUNC(float, get_pwm_duty_cycle);
FAKE_VALUE_FUNC(uint16_t, get_seconds_since_power_on);
Expand Down Expand Up @@ -116,8 +114,6 @@ class BmsFaultTest : public BaseStateMachineTest
state_machine = App_SharedStateMachine_Create(world, App_GetInitState());
App_AllStates_Init();

RESET_FAKE(send_non_periodic_msg_BMS_STARTUP);
RESET_FAKE(send_non_periodic_msg_BMS_WATCHDOG_TIMEOUT);
RESET_FAKE(get_pwm_frequency);
RESET_FAKE(get_pwm_duty_cycle);
RESET_FAKE(get_seconds_since_power_on);
Expand Down
2 changes: 1 addition & 1 deletion firmware/thruna/DCM/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ void RunTaskCanRx(void *argument)

for (;;)
{
struct CanMsg message;
CanMsg message;
Io_SharedCan_DequeueCanRxMessage(&message);
Io_CanRx_UpdateRxTableWithMessage(&message);
}
Expand Down
4 changes: 0 additions & 4 deletions firmware/thruna/DCM/Test/Src/Test_StateMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ extern "C"

namespace StateMachineTest
{
FAKE_VOID_FUNC(send_non_periodic_msg_DCM_STARTUP, const struct CanMsgs_dcm_startup_t *);
FAKE_VOID_FUNC(send_non_periodic_msg_DCM_WATCHDOG_TIMEOUT, const struct CanMsgs_dcm_watchdog_timeout_t *);
FAKE_VALUE_FUNC(uint32_t, get_current_ms);
FAKE_VOID_FUNC(heartbeat_timeout_callback, enum HeartbeatOneHot, enum HeartbeatOneHot);
FAKE_VOID_FUNC(turn_on_red_led);
Expand Down Expand Up @@ -78,8 +76,6 @@ class DcmStateMachineTest : public BaseStateMachineTest
// Default to starting the state machine in the `init` state
state_machine = App_SharedStateMachine_Create(world, App_GetInitState());

RESET_FAKE(send_non_periodic_msg_DCM_STARTUP);
RESET_FAKE(send_non_periodic_msg_DCM_WATCHDOG_TIMEOUT);
RESET_FAKE(get_current_ms);
RESET_FAKE(get_acceleration_x);
RESET_FAKE(get_acceleration_y);
Expand Down
2 changes: 1 addition & 1 deletion firmware/thruna/DIM/src/cubemx/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ void RunTaskCanRx(void *argument)
/* Infinite loop */
for (;;)
{
struct CanMsg message;
CanMsg message;
Io_SharedCan_DequeueCanRxMessage(&message);
Io_CanRx_UpdateRxTableWithMessage(&message);
}
Expand Down
2 changes: 1 addition & 1 deletion firmware/thruna/FSM/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ void RunTaskCanRx(void *argument)
/* Infinite loop */
for (;;)
{
struct CanMsg message;
CanMsg message;
Io_SharedCan_DequeueCanRxMessage(&message);
Io_CanRx_UpdateRxTableWithMessage(&message);
}
Expand Down
2 changes: 1 addition & 1 deletion firmware/thruna/PDM/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ void RunTaskCanRx(void *argument)

for (;;)
{
struct CanMsg message;
CanMsg message;
Io_SharedCan_DequeueCanRxMessage(&message);
Io_CanRx_UpdateRxTableWithMessage(&message);
}
Expand Down
9 changes: 0 additions & 9 deletions firmware/thruna/PDM/Test/Src/Test_StateMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ extern "C"

namespace StateMachineTest
{
FAKE_VOID_FUNC(send_non_periodic_msg_PDM_STARTUP, const struct CanMsgs_pdm_startup_t *);
FAKE_VOID_FUNC(send_non_periodic_msg_PDM_AIR_SHUTDOWN, const struct CanMsgs_pdm_air_shutdown_t *);
FAKE_VOID_FUNC(send_non_periodic_msg_PDM_MOTOR_SHUTDOWN, const struct CanMsgs_pdm_motor_shutdown_t *);
FAKE_VOID_FUNC(send_non_periodic_msg_PDM_WATCHDOG_TIMEOUT, const struct CanMsgs_pdm_watchdog_timeout_t *);

FAKE_VALUE_FUNC(float, get_bat_voltage);
FAKE_VALUE_FUNC(float, get_acc_voltage);
FAKE_VALUE_FUNC(float, get_boost_voltage);
Expand Down Expand Up @@ -96,10 +91,6 @@ class PdmStateMachineTest : public BaseStateMachineTest
state_machine = App_SharedStateMachine_Create(world, App_GetInitState());

// Reset fake functions
RESET_FAKE(send_non_periodic_msg_PDM_STARTUP);
RESET_FAKE(send_non_periodic_msg_PDM_AIR_SHUTDOWN);
RESET_FAKE(send_non_periodic_msg_PDM_MOTOR_SHUTDOWN);
RESET_FAKE(send_non_periodic_msg_PDM_WATCHDOG_TIMEOUT);
RESET_FAKE(GetAux1Current);
RESET_FAKE(GetAux2Current);
RESET_FAKE(GetLeftInverterCurrent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,62 +12,21 @@

/* ------------------------- Function Definitions ------------------------- */

void App_CanAlerts_SetWarning(JCT_WarningId alert_id, bool set_alert)
void App_CanAlerts_JCT_Warning_Warning_Test_Set(bool set_alert)
{
switch (alert_id)
// Increment alert counter.
if (set_alert && !App_CanTx_JCT_Warning_Warning_Test_Get())
{
case JCT_TEST:
{
App_CanTx_JCT_Warnings_JCT_TEST_Set(set_alert);
break;
}
default:
{
// Do nothing
break;
}
}
}

void App_CanAlerts_SetFault(JCT_FaultId alert_id, bool set_alert)
{
switch (alert_id)
{
default:
{
// Do nothing
break;
}
}
}

bool App_CanAlerts_GetWarning(JCT_WarningId alert_id)
{
switch (alert_id)
{
case JCT_TEST:
{
return App_CanTx_JCT_Warnings_JCT_TEST_Get();
break;
}
default:
{
return false;
break;
}
App_CanTx_JCT_Warning_Warning_TestCount_Set(App_CanTx_JCT_Warning_Warning_TestCount_Get() + 1);
}

// Set alert.
App_CanTx_JCT_Warning_Warning_Test_Set(set_alert);
}

bool App_CanAlerts_GetFault(JCT_FaultId alert_id)
bool App_CanAlerts_JCT_Warning_Warning_Test_Get()
{
switch (alert_id)
{
default:
{
return false;
break;
}
}
return App_CanTx_JCT_Warning_Warning_Test_Get();
}

bool App_CanAlerts_BoardHasWarning(CanAlertBoard board)
Expand All @@ -76,12 +35,12 @@ bool App_CanAlerts_BoardHasWarning(CanAlertBoard board)
{
case FSM_ALERT_BOARD:
{
if (App_CanRx_FSM_TEST1_Get())
if (App_CanRx_FSM_Warning_Warning_Test1_Get())
{
return true;
}

if (App_CanRx_FSM_TEST2_Get())
if (App_CanRx_FSM_Warning_Warning_Test2_Get())
{
return true;
}
Expand All @@ -90,7 +49,7 @@ bool App_CanAlerts_BoardHasWarning(CanAlertBoard board)
}
case JCT_ALERT_BOARD:
{
if (App_CanTx_JCT_Warnings_JCT_TEST_Get())
if (App_CanTx_JCT_Warning_Warning_Test_Get())
{
return true;
}
Expand All @@ -113,7 +72,7 @@ bool App_CanAlerts_BoardHasFault(CanAlertBoard board)
{
case FSM_ALERT_BOARD:
{
if (App_CanRx_FSM_FAULT_TEST3_Get())
if (App_CanRx_FSM_Fault_Fault_Test3_Get())
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ typedef enum
} CanAlertBoard;
typedef enum
{
JCT_TEST = 0,
JCT_Warning_Warning_Test = 0,
NUM_JCT_WARNINGS = 1,
} JCT_WarningId;

Expand All @@ -32,24 +32,14 @@ typedef enum
/* ------------------------- Function Prototypes -------------------------- */

/**
* Set or clear a warning.
* Set or clear an alert for this board.
*/
void App_CanAlerts_SetWarning(JCT_WarningId alert_id, bool set_alert);
void App_CanAlerts_JCT_Warning_Warning_Test_Set(bool set_alert);

/**
* Set or clear a fault.
* Return whether or not a specific alert transmitted by this board is set.
*/
void App_CanAlerts_SetFault(JCT_FaultId alert_id, bool set_alert);

/**
* Return whether or not a specific warning transmitted by this board is set.
*/
bool App_CanAlerts_GetWarning(JCT_WarningId alert_id);

/**
* Return whether or not a specific fault transmitted by this board is set.
*/
bool App_CanAlerts_GetFault(JCT_FaultId alert_id);
bool App_CanAlerts_JCT_Warning_Warning_Test_Get(void);

/**
* Return whether or not a board has set a warning.
Expand Down
Loading

0 comments on commit 8b34663

Please sign in to comment.