Skip to content
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

Add an LED driver example using GPIO #290

Merged
merged 1 commit into from
Mar 10, 2025
Merged

Conversation

jeremy90307
Copy link
Contributor

@jeremy90307 jeremy90307 commented Dec 18, 2024

Add an LED driver example using GPIO and add related knowledge.

Test detail:

  • Tested on Raspberry Pi 5B with Raspberry Pi OS (Debian 12, Linux version 6.12.1-v8-16k+)

  • Verify that LED sample compiles and loads successfully

  • Verify that LED turns on and off sucessfully

Summary by Bito

This pull request introduces a new LED driver example for the Raspberry Pi 5B, featuring a new source file 'led.c' for GPIO control. The Makefile has been updated accordingly, and comprehensive documentation on GPIO and LED control has been added to 'lkmpg.tex'. The implementation has been successfully tested on the Raspberry Pi 5B.

Unit tests added: False

Estimated effort to review (1-5, lower is better): 2

@jeremy90307 jeremy90307 force-pushed the master branch 3 times, most recently from 3a799e4 to a64cd81 Compare December 18, 2024 12:11
examples/led.c Outdated
{
pr_info("device_open(%p)\n", file);

try_module_get(THIS_MODULE);
Copy link
Contributor

Choose a reason for hiding this comment

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

See if try_module_get is required or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have noticed this issue, such as issue #52, and there are still several examples using it. Let me fix it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we want to remove try_module_get, we also need to stop using register_chrdev and use cdev_init and cdev_add instead.

Because with register_chrdev, we need to manually increase or decrease the module's reference count.

Copy link
Contributor

Choose a reason for hiding this comment

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

The try_module_get() function should generally be avoided. The only legitimate use case is when you need to reference another module before interacting with it, such as in file operations code. While try_module_get(THIS_MODULE) appears multiple times in the kernel source code, these instances are typically bugs that need to be fixed rather than examples to follow.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In my modified code, register_chrdev is not included. Should I use the preprocessor to make the changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because I saw that cdev already appeared in Linux v2.6.

@jeremy90307 jeremy90307 force-pushed the master branch 2 times, most recently from 998113c to 9c56420 Compare March 8, 2025 21:49
lkmpg.tex Outdated
Therefore, we will further use Device Files to control the LED on and off.

In the implementation, a pull-down resistor is used.
The positive electrode of the LED is connected to GPIO4, and the negative electrode is connected to GND.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you provide some hyperlinks which illustrate the GPIO assignments?

Use GPIO to control LED on/off and add related GPIO knowledge.

Test detail:

- Tested on Raspberry Pi 5B with Raspberry Pi OS (Debian 12, Linux
  version 6.12.1-v8-16k+)

- Verify that LED example compiles and loads successfully

- Verify that LED turns on and off sucessfully
@jserv jserv merged commit e061eaf into sysprog21:master Mar 10, 2025
1 check passed
@jserv
Copy link
Contributor

jserv commented Mar 10, 2025

Thank @jeremy90307 for contributing!

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.

None yet

2 participants