Skip to content

Add ISR Queue#2

Open
fauniashen wants to merge 10 commits into
mainfrom
feature/can_isr_queue
Open

Add ISR Queue#2
fauniashen wants to merge 10 commits into
mainfrom
feature/can_isr_queue

Conversation

@fauniashen
Copy link
Copy Markdown

@fauniashen fauniashen commented Mar 21, 2026

Replaces current message processing (happens in ISR) and adds a frame (from hardware queue) to a (software) queue instead. Queue and messages are processed outside of ISR.

@fauniashen fauniashen force-pushed the feature/can_isr_queue branch from b9cb535 to 46fcc91 Compare March 21, 2026 20:47
Comment thread pinecan/boards/stm32l4/pinecanBoard.h
Comment thread pinecan/pinecan.c Outdated
void handleRxFrame(CanardCANFrame *rxFrame) {
// TODO: this rx frame is deleted after this function returns. If/when adding a queue, make sure to handle this correctly
canardHandleRxFrame(data.canard, rxFrame, getUptimeMs() * 1000U);
//make vrb of canardcanframe in pinecanboard.c make it equal to the frame im going to pop(make a peek), then pass into handlerxframe
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Just take a look through and remove any comments you wrote during development

Copy link
Copy Markdown
Contributor

@Penguronik Penguronik left a comment

Choose a reason for hiding this comment

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

Great work! Nice job making sure the queue didn't have race conditions that can be tricky but I couldn't find any issues with it. Mostly some nits to follow how we have the internal headers set up, and a few small suggestions.

Comment thread pinecan/boards/stm32l4/pinecanBoard.c
Comment thread pinecan/boards/stm32l4/pinecanBoard.h
Comment on lines +150 to +189
// queue functions for circular buffer, included in .h

bool enqueueRxQueue(const CanardCANFrame *frame)
{
if (count >= RX_QUEUE_SIZE)
return false; // queue full

rxQueue[head] = *frame; // copy entire frame
head = (head + 1) % RX_QUEUE_SIZE;
count++;

return true;
}

bool dequeueRxQueue(CanardCANFrame *frame)
{
if (count == 0)
return false; // queue empty

*frame = rxQueue[tail];
tail = (tail + 1) % RX_QUEUE_SIZE;
count--;

return true;
}

CanardCANFrame* peekRxQueue()
{
if (count == 0)
return NULL; // queue empty
return &rxQueue[tail];
}

void processCanardRxQueue()
{
CanardCANFrame *nextRxframe = peekRxQueue();
if (nextRxframe != NULL)
handleRxFrame(nextRxframe);
return;
} No newline at end of file
Copy link
Copy Markdown
Contributor

@Penguronik Penguronik Mar 23, 2026

Choose a reason for hiding this comment

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

I think move this stuff to be on board agnostic instead of board specific part of pinecan. looks like they all use the CanardCANFrame type which is common throughout all boards (if they were using CAN_RxHeaderTypeDef for example we'd have to keep it in board specific as that type is different between boards)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also file missing newline at end of file, you'll see github show that with a little red circle with line through it at the bottom of the file view. It's not gonna break anything but its best practice to avoid

Comment thread pinecan/pinecan.c
Comment thread pinecan/boards/stm32l4/pinecanBoard.h
Comment thread pinecan/pinecan.c

void processCanardRxQueue()
{
CanardCANFrame *nextRxframe = peekRxQueue();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

any reason we peek here and dequeue inside of the function instead of dequeuing here and passing it to handleRxFrame?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

especially since dequeue does a copy of the value anyways so no issue with race conditions i can think of there, and also since we call dequeue later we don't save any cycles by doing a peek here instead

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree. handleRxFrame shouldn't be popping the queue

Comment thread pinecan/boards/stm32l4/pinecanBoard.c

// queue functions for circular buffer, included in .h

bool enqueueRxQueue(const CanardCANFrame *frame)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe it's worth adding pinecan error convention so that each function returns a specific error code rather than a boolean

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Out of scope for this PR but should think about this now rather than later @SpiroJyro @Penguronik

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yea wont block this pr but can def create an enum with at least just PC_OK PC_ERROR or smthng and add more as we see fit

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Added PINECAN_ERROR and PINECAN_OK in a new PR, can change this depending on order of PRs that get merged

Comment thread pinecan/boards/stm32l4/pinecanBoard.c
Comment thread pinecan/boards/stm32l4/pinecanBoard.c
Comment thread pinecan/boards/stm32l4/pinecanBoard.c

void processCanardRxQueue()
{
CanardCANFrame *nextRxframe = peekRxQueue();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree. handleRxFrame shouldn't be popping the queue

Comment thread pinecan/boards/stm32l4/pinecanBoard.h
Comment thread pinecan/boards/stm32l4/pinecanBoard.h
Comment thread pinecan/boards/stm32l4/pinecanBoard.h
Co-authored-by: Roni Kant <35043400+Penguronik@users.noreply.github.com>
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.

4 participants