-
Notifications
You must be signed in to change notification settings - Fork 14
Ayopierre/grlib can #569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Ayopierre/grlib can #569
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @ayoopierre, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request introduces a new GRCANFD (CAN with Flexible Data-Rate) driver for the GRLIB CAN controller. It provides the necessary components to interact with the hardware, including a core driver, a user-space interface library, and a test application. This integration is a foundational step for future developments, specifically for LibCSP.
Highlights
- New GRCANFD Driver Core: Implemented the
grlib-can-corestatic library, providing low-level hardware interaction, interrupt handling, circular buffer management for TX/RX, and functions for device configuration and baud rate calculation. - GRCANFD Driver Binary: Introduced the
grlib-can-driverbinary, which acts as the main driver application, handling message-based communication from user-space applications to control the CAN hardware. - User-Space Interface Library: Added
grlib-can-if, a static library that provides a simplified API for user applications to open, close, configure, send, and receive CAN messages from the driver. - CAN Test Application: Included
can-test-app, a simple binary demonstrating the usage of thegrlib-can-iflibrary to interact with the GRCANFD device, performing basic send/receive and configuration operations. - Build System Integration: Updated
_targets/Makefile.riscv64-noelvto include the newgrlib-can-driverandcan-test-appcomponents, and added a dedicatedMakefilefor building all GRCANFD-related components.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
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 introduces a new GRCANFD driver, including the core logic, a driver binary, an interface library, and a test application. The implementation is comprehensive but has several critical and high-severity issues related to correctness, resource management, and robustness. These issues should be addressed to ensure the driver is reliable.
| return 0; | ||
| case can_reset: | ||
| grlibCan_resetDevice(device); | ||
| msg->o.err = EOK; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| grlibCan_cleanupResources(driverInstance.devices); | ||
| portDestroy(driverInstance.port); | ||
| grlibCan_unregisterDevices(&oid, driverInstance.devices, driverInstance.num); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block of code is unreachable because grlibCan_messageThread has an infinite loop and will never return. This is a significant issue because the cleanup logic will never be executed, potentially leading to resource leaks. If the driver is intended to run forever, this cleanup code should be removed. If shutdown is a possibility, it must be handled correctly (e.g., via signal handlers), and the cleanup logic must be fixed.
| device->ctrlReg = (1 << 1); | ||
|
|
||
| /* Sleeping until reset finished */ | ||
| while ((device->ctrlReg & (1u << 1)) != 0) { } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can/grlib-can/grlib-can-core.c
Outdated
|
|
||
| /* Configure ids to listen to */ | ||
| device->confReg = 0; | ||
| device->confReg |= (1 << 2) | (1 << 1) | (1 << 1) | (1 << 6); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can/grlib-can/grlib-can-core.c
Outdated
| dev->rxBufAdd = mmap(NULL, _PAGE_SIZE, | ||
| PROT_WRITE | PROT_READ, MAP_UNCACHED | MAP_ANONYMOUS, -1, 0); | ||
| if (dev->rxBufAdd == MAP_FAILED) { | ||
| (void)munmap((void *)dev->txBufAdd, sizeof(grlibCan_msg_t) * dev->txBufSz); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can/grlib-can/grlib-can-core.c
Outdated
| } | ||
|
|
||
| if (dev->rxBufAdd != MAP_FAILED) { | ||
| (void)munmap((void *)dev->rxBufAdd, sizeof(grlibCan_msg_t) * dev->rxBufSz); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if (msg->i.size < sizeof(uint32_t)) { | ||
| msg->o.err = -1; | ||
| return -1; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The size check for can_getStatus is incorrect. It checks msg->i.size, but this operation writes to an output buffer and takes no input data. The check should be on msg->o.size to ensure the output buffer provided by the caller is large enough to hold the status.
| if (msg->i.size < sizeof(uint32_t)) { | |
| msg->o.err = -1; | |
| return -1; | |
| } | |
| case can_getStatus: | |
| if (msg->o.size < sizeof(uint32_t)) { | |
| msg->o.err = -EINVAL; | |
| return -1; | |
| } |
can/grlib-can/Makefile
Outdated
| NAME := grlib-can-driver | ||
| LOCAL_SRCS := grlib-can-driver.c | ||
| DEP_LIBS := grlib-can-core | ||
| LOCAL_CFLAGS = -Wno-error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, PR isn't draft, so this should be removed.
can/grlib-can/can-test-app.c
Outdated
| while (lookup(canDevice, NULL, &canDev) < 0) { | ||
| printf("Device does not exist\n"); | ||
| return EXIT_FAILURE; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This while loop is logically equivalent to an if statement. If lookup() fails, the program exits; otherwise, it continues. Using an if statement would represent the code's intent more clearly. If the goal is to wait for the device, the loop should include a sleep mechanism to avoid busy-waiting and high CPU usage.
| while (lookup(canDevice, NULL, &canDev) < 0) { | |
| printf("Device does not exist\n"); | |
| return EXIT_FAILURE; | |
| } | |
| if (lookup(canDevice, NULL, &canDev) < 0) { | |
| printf("Device does not exist\n"); | |
| return EXIT_FAILURE; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's ok as is.
|
|
||
| printf("Successfully checked CAN device oid\n"); | ||
|
|
||
| printf("Can device open: %d\n", grlibCan_open(canDev)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| @@ -0,0 +1,76 @@ | |||
| #include <errno.h> | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing file header
| LOCAL_SRCS := can-test-app.c | ||
| DEP_LIBS := grlib-can-if | ||
| LOCAL_CFLAGS = -Wno-error | ||
| include $(binary.mk) No newline at end of file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No new line at the end of the file
can/grlib-can/Makefile
Outdated
| NAME := grlib-can-driver | ||
| LOCAL_SRCS := grlib-can-driver.c | ||
| DEP_LIBS := grlib-can-core | ||
| LOCAL_CFLAGS = -Wno-error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, PR isn't draft, so this should be removed.
can/grlib-can/can-test-app.c
Outdated
| while (lookup(canDevice, NULL, &canDev) < 0) { | ||
| printf("Device does not exist\n"); | ||
| return EXIT_FAILURE; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's ok as is.
can/grlib-can/grlib-can-core.c
Outdated
| { | ||
| grlibCan_dev_t *dev = (grlibCan_dev_t *)arg; | ||
| volatile grlibCan_hwDev_t *device = dev->device; | ||
| volatile uint32_t pending = device->penIsr; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This volatile does nothing - device->penIsr is volatile, local copy on stack is not.
can/grlib-can/grlib-can-core.c
Outdated
| msg.type = mtCreate; | ||
| msg.oid = dir; | ||
| msg.i.create.type = otDev; | ||
| msg.i.create.mode = 0; | ||
| msg.i.create.dev.port = oid->port; /* Port number assigned by portCreate */ | ||
| msg.i.create.dev.id = i; /* Id assigned by the driver itself */ | ||
| msg.i.data = buf; /* Filename */ | ||
| msg.i.size = strlen(msg.i.data); | ||
| msg.o.data = NULL; | ||
| msg.o.size = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use createdev()
| while (lookup("/dev", NULL, &dir) < 0) { | ||
| usleep(100000); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way for /dev on be present at this point?
| } | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps some handling of abnormally big diff?
|
|
||
| /* Core driver functions */ | ||
| /* Create devices */ | ||
| int grlibCan_initDevices(grlibCan_dev_t *dev, int num); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep two empty lines
can/grlib-can/grlib-can-driver.c
Outdated
| } | ||
| msg->o.err = 0; | ||
| *(uint32_t *)(msg->o.data) = device->device->statReg; | ||
| msg->o.size = sizeof(uint32_t); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Server cannot modify these fields, it will be ignored.
8f0c524 to
55f1456
Compare
can/grlib-can/grlib-can-driver.c
Outdated
| #include <sys/mman.h> | ||
| #include <posix/utils.h> | ||
|
|
||
| /* Platform specific incldues */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[codespell] reported by reviewdog 🐶
incldues ==> includes
|
|
||
| uint32_t nomBdRate; /* Nominal baud-rate */ | ||
| uint32_t dataBdRate; /* Data transfer baud-rate */ | ||
| uint32_t transDelComp; /* Tranmission delay compensation */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[codespell] reported by reviewdog 🐶
Tranmission ==> Transmission
55f1456 to
365435a
Compare
Unit Test Results7 918 tests - 283 7 393 ✅ - 284 41m 52s ⏱️ - 1m 52s For more details on these failures, see this check. Results for commit b030c67. ± Comparison against base commit 10baaaf. This pull request removes 291 and adds 8 tests. Note that renamed tests count towards both.♻️ This comment has been updated with latest results. |
365435a to
b2e7c9c
Compare
can/grlib-can/grlib-can-driver.c
Outdated
| msg->o.err = ret; | ||
| return ret; | ||
| case can_readSync: | ||
| /* Places number of pending frames in RX cirucular buffer, if buffer cannot fit whole CAN frame */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[codespell] reported by reviewdog 🐶
cirucular ==> circular
b2e7c9c to
3b23cc5
Compare
3b23cc5 to
b030c67
Compare
Description
Motivation and Context
Types of changes
How Has This Been Tested?
Checklist: