Skip to content

CAN status and tasks stuff#13

Open
changxu-liu wants to merge 3 commits into
mainfrom
CAN_and_tasks_stuff
Open

CAN status and tasks stuff#13
changxu-liu wants to merge 3 commits into
mainfrom
CAN_and_tasks_stuff

Conversation

@changxu-liu

Copy link
Copy Markdown
Contributor

No description provided.

@changxu-liu changxu-liu changed the title CAN and tasks stuff CAN status and tasks stuff Jun 25, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request modularizes the Power Distribution Unit (PDU) Mk1 firmware by splitting tasks into dedicated files under tasks/ and adding test suites under test/, alongside introducing CAN status transmission. Several critical issues were identified in the review: FreeRTOS task functions must be updated to accept a void * parameter to match the expected prototype and avoid undefined behavior; an unaligned memory access in Task_CANSendStatus.c should be resolved using byte shifting; and the hfdcan3 pointer must be properly declared/initialized before dereferencing. Additionally, non-standard emojis in macro names should be replaced with ASCII, copy-paste task names in tests should be corrected, and a redundant loop around Error_Handler() should be cleaned up.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +5 to +6
void Task_ReadCurrents()
{

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

FreeRTOS task functions must match the TaskFunction_t prototype, which is defined as void (*)(void *). Defining a task function with no parameters (i.e., void Task_Name(void)) and passing it to xTaskCreateStatic results in a function pointer signature mismatch, which is undefined behavior in C and can lead to stack corruption or register mismatches depending on the compiler and calling convention. Please update the signature to accept a void * parameter.

Suggested change
void Task_ReadCurrents()
{
void Task_ReadCurrents(void *argument)
{

Comment on lines +5 to +6
void Task_CanSendStatus()
{

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

FreeRTOS task functions must match the TaskFunction_t prototype, which is defined as void (*)(void *). Defining a task function with no parameters (i.e., void Task_Name(void)) and passing it to xTaskCreateStatic results in a function pointer signature mismatch, which is undefined behavior in C and can lead to stack corruption or register mismatches depending on the compiler and calling convention. Please update the signature to accept a void * parameter.

void Task_CanSendStatus(void *argument)
{

Comment on lines +49 to +50
*((uint16_t*) (ch_data+PDU_MK1_CAN_PDUSTATUS_VOLTAGE_INDEX)) = ((uint16_t) (0)); // no voltage data yet
*((uint16_t*) (ch_data+PDU_MK1_CAN_PDUSTATUS_CURRENT_INDEX)) = ((uint16_t) (PDU_Mk1_CurrentSensing_GetCurrentsPtr()[ch_to_send]*PDU_MK1_CAN_PDUSTATUS_CURRENT_INVFACTOR));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Casting ch_data + PDU_MK1_CAN_PDUSTATUS_CURRENT_INDEX (which resolves to an odd address, 3) to a uint16_t* and dereferencing it performs an unaligned memory access. On some ARM Cortex-M processors, this can trigger a HardFault. Furthermore, it violates strict aliasing rules in C, which can lead to undefined compiler optimizations. A safer, portable, and standard-compliant approach is to use byte shifting to serialize the 16-bit values into the byte array.

		uint16_t voltage = 0;
		ch_data[PDU_MK1_CAN_PDUSTATUS_VOLTAGE_INDEX] = voltage & 0xFF;
		ch_data[PDU_MK1_CAN_PDUSTATUS_VOLTAGE_INDEX + 1] = (voltage >> 8) & 0xFF;

		uint16_t current = (uint16_t)(PDU_Mk1_CurrentSensing_GetCurrentsPtr()[ch_to_send] * PDU_MK1_CAN_PDUSTATUS_CURRENT_INVFACTOR);
		ch_data[PDU_MK1_CAN_PDUSTATUS_CURRENT_INDEX] = current & 0xFF;
		ch_data[PDU_MK1_CAN_PDUSTATUS_CURRENT_INDEX + 1] = (current >> 8) & 0xFF;

Comment on lines +5 to +6
void Task_HSSControl()
{

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

FreeRTOS task functions must match the TaskFunction_t prototype, which is defined as void (*)(void *). Defining a task function with no parameters (i.e., void Task_Name(void)) and passing it to xTaskCreateStatic results in a function pointer signature mismatch, which is undefined behavior in C and can lead to stack corruption or register mismatches depending on the compiler and calling convention. Please update the signature to accept a void * parameter.

Suggested change
void Task_HSSControl()
{
void Task_HSSControl(void *argument)
{

Comment on lines +5 to +6
void Task_Init()
{

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

FreeRTOS task functions must match the TaskFunction_t prototype, which is defined as void (*)(void *). Defining a task function with no parameters (i.e., void Task_Name(void)) and passing it to xTaskCreateStatic results in a function pointer signature mismatch, which is undefined behavior in C and can lead to stack corruption or register mismatches depending on the compiler and calling convention. Please update the signature to accept a void * parameter.

Suggested change
void Task_Init()
{
void Task_Init(void *argument)
{

Comment on lines +52 to +58
if (can_fd_send(hfdcan3, &tx_header, ch_data, portMAX_DELAY) != CAN_OK){
while(1)
{
printf("ERROR:CAN_SEND_PDUSTATUS\n");
Error_Handler();
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The while(1) loop around Error_Handler() is redundant because Error_Handler() already contains an infinite loop and never returns. Simplifying this block improves code readability.

		if (can_fd_send(hfdcan3, &tx_header, ch_data, portMAX_DELAY) != CAN_OK){
			printf("ERROR:CAN_SEND_PDUSTATUS\n");
			Error_Handler();
		}

Comment thread firmware/PowerDistributionUnit_Mk1/tasks/src/Task_HSSControl.c
Comment thread firmware/PowerDistributionUnit_Mk1/tasks/src/Task_Init.c
Comment on lines +107 to +108
task_hsscontrol = xTaskCreateStatic(Task_HSSControl,
"Current Sense Task",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The task name is set to "Current Sense Task", but the task function being created is Task_HSSControl. This copy-paste error can make debugging and task monitoring confusing when using FreeRTOS tools. Please update the task name to "HSS Control Task".

    task_hsscontrol = xTaskCreateStatic(Task_HSSControl,
                    "HSS Control Task",

Comment on lines +84 to +85
task_hsscontrol = xTaskCreateStatic(Task_HSSControl,
"Current Sense Task",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The task name is set to "Current Sense Task", but the task function being created is Task_HSSControl. This copy-paste error can make debugging and task monitoring confusing when using FreeRTOS tools. Please update the task name to "HSS Control Task".

    task_hsscontrol = xTaskCreateStatic(Task_HSSControl,
                    "HSS Control Task",

hfdcan3->Instance = FDCAN3;
hfdcan3->Init.ClockDivider = FDCAN_CLOCK_DIV1;
hfdcan3->Init.FrameFormat = FDCAN_FRAME_CLASSIC;
hfdcan3->Init.Mode = FDCAN_MODE_EXTERNAL_LOOPBACK;

@changxu-liu changxu-liu Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FDCAN_MODE_NORMAL?


// DEFINES --------------------------------------------------------------------

#define TASK_CANSENDSTATUS_STACK_SIZE configMINIMAL_STACK_SIZE+3000

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe see if CAN make less f a t?


// DEFINES --------------------------------------------------------------------

#define TASK_INIT_STACK_SIZE configMINIMAL_STACK_SIZE+5000

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also f a t?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops remove file for now


// DEFINES --------------------------------------------------------------------

#define TASK_READCURRENTS_STACK_SIZE configMINIMAL_STACK_SIZE+3000

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f a t?

}
}


Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra line.


vTaskDelay(pdMS_TO_TICKS(TASK_HSSCONTROL_INTERVAL_MS));
}
} No newline at end of file

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add blank line.

PDU_Mk1_StartTasks();
// task kills itself
vTaskDelete(NULL);
} No newline at end of file

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add blank line.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops remove file for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant