diff options
| -rw-r--r-- | main.c | 22 |
1 files changed, 15 insertions, 7 deletions
@@ -26,24 +26,32 @@ struct { int front; int back; CAN_FIFOMailBox_TypeDef data[CAN_FRAME_QUEUE_DEPTH]; -} can_frames = {}; +} can_frame_queue = {}; +/* + * Retrieves the oldest current frame from our CAN received frame FIFO queue. + * Returns false if we have read all received frames, otherwise returns true. + * + * The CAN filters should be set up in a way such that this function is called + * at least as often as the hardware receives a frame, as any CAN frames that + * the hardware receives after our queue is full will just be dropped. + */ bool can_recv(CAN_FIFOMailBox_TypeDef *frame) { - if (can_frames.front == can_frames.back) return false; - *frame = can_frames.data[can_frames.front]; - can_frames.front = (can_frames.front + 1) % CAN_FRAME_QUEUE_DEPTH; + if (can_frame_queue.front == can_frame_queue.back) return false; + *frame = can_frame_queue.data[can_frame_queue.front]; + can_frame_queue.front = (can_frame_queue.front + 1) % CAN_FRAME_QUEUE_DEPTH; return true; } // This will get called when we get a new frame in FIFO1 void canrx1_handler() { // if the queue is full just drop the frame - if ((can_frames.back + 1) % CAN_FRAME_QUEUE_DEPTH == can_frames.front) { + if ((can_frame_queue.back + 1) % CAN_FRAME_QUEUE_DEPTH == can_frame_queue.front) { CAN1->RF1R |= CAN_RF1R_RFOM1; return; } - can_frames.data[can_frames.back] = CAN1->sFIFOMailBox[1]; - can_frames.back = (can_frames.back + 1) % CAN_FRAME_QUEUE_DEPTH; + can_frame_queue.data[can_frame_queue.back] = CAN1->sFIFOMailBox[1]; + can_frame_queue.back = (can_frame_queue.back + 1) % CAN_FRAME_QUEUE_DEPTH; // tell the FIFO we are done with this frame CAN1->RF1R |= CAN_RF1R_RFOM1; } |
