Skip to content

Commit

Permalink
feat: kernel module
Browse files Browse the repository at this point in the history
  • Loading branch information
fengyc committed Jun 25, 2023
1 parent edbb0f3 commit 47769bc
Show file tree
Hide file tree
Showing 3 changed files with 248 additions and 243 deletions.
12 changes: 8 additions & 4 deletions pisugar-module/Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
TARGET = pisugar_3_battery

obj-m += pisugar_3_battery.o

KERN_VER=$(shell uname -r)
KERN_VER = $(shell uname -r)

SOURCEDIR = $(shell pwd)
BUILDDIR = $(shell pwd)/build

all:
make -C /lib/modules/$(KERN_VER)/build M=$(shell pwd) modules
make -C /lib/modules/$(KERN_VER)/build M=$(SOURCEDIR) modules

clean:
rm -f *.cmd *.ko *.o Module.symvers modules.order *.mod.c

rm -f *.cmd *.ko *.o Module.symvers modules.order *.mod.c .pisugar_3_battery.* .modules.* .Module.symvers.* pisugar_3_battery.mod
51 changes: 50 additions & 1 deletion pisugar-module/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,63 @@
# PiSugar Linux kernel modules

Make kernel modules:
Linux kernel modules for PiSugar 3.

## Preparing for building RPI kernel module

### Linux distributions with kernel symbols

Congratulations if your PI is running a linux distribution that has `/lib/modules/$(uname -r)/build/` directory, e.g. ubuntu-server, you don't need to manually build RPI kernel, and that will save a lot of time.

Install linux-headers
```shell
sudo apt install linux-headers-$(uname -r)
```

### Raspberry Pi OS

As kernel symbols is not included in Raspberry Pi OS (no `/lib/modules/$(uname -r)/build`), so you need to compile the kernel and generate the kernel symbols by youself.

To build the kernel, see official doc: https://www.raspberrypi.com/documentation/computers/linux_kernel.html

** It seems like the precompiled symbols could be downloaded from [here](https://github.com/raspberrypi/firmware), but I could not figure out the correct steps. **

First, get RPI OS tag, e.g. 1.20230405
```shell
dpkg -l | grep kernel
```

Clone kernel repository, and create a symbol link
```shell
git clone --depth 1 --branch <PI_OS_TAG> https://github.com/raspberrypi/linux.git
sudo ln -s "$(pwd)/linux" /usr/src/linux
```

Build RPI kernel.

When it is done, copy and rename `linux` folder to PI `/lib/modules/$(uname -r)/build`.

## Compiling kernel module

Clone this repository, make kernel modules:

make

Load module:

sudo insmod pisugar_3_battery.ko
# or
sudo insmod pisugar_3_battery.ko i2c_bus=1 i2c_addr=0x57

Then you will see extra battry files in `/sys/class/power_supply`

ls /sys/class/power_supply

Remove module:

sudo rmmod pisugar_3_battery.ko

Now, you can enable [battery monitor plugin](https://github.com/raspberrypi-ui/lxplug-ptbatt).

## License

GPL
Loading

0 comments on commit 47769bc

Please sign in to comment.