start io subsystem

This commit is contained in:
2025-06-14 22:16:35 +01:00
parent 34ea87ded0
commit 49d5847980
8 changed files with 95 additions and 17 deletions

View File

@ -1,31 +1,31 @@
NAME = counter
OBJS = init.o leds.o ls7366r.o trigger.o io.o
KDIR := /lib/modules/$(shell uname -r)/build
DTC_FLAGS := -@ -I dts -O dtb
obj-m += $(NAME).o
$(NAME)-objs := init.o leds.o ls7366r.o trigger.o
$(NAME)-objs := $(OBJS)
all: $(NAME).ko $(NAME).dtbo
echo Builded Device Tree Overlay and kernel module
$(NAME).ko: init.c leds.c ls7366r.c trigger.c
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
$(NAME).ko:
make -C $(KDIR) M=$(PWD) modules
$(NAME).dtbo: $(NAME).dts
dtc -@ -I dts -O dtb -o $@ $<
dtc $(DTC_FLAGS) -o $@ $<
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
make -C $(KDIR) M=$(PWD) clean
rm -rf $(NAME).dtbo
load:
sudo dtoverlay -d . $(NAME).dtbo
@echo "Overlay loaded"
sudo insmod ./$(NAME).ko
@echo "LKM loaded"
sudo dtoverlay -d . $(NAME).dtbo && echo "Overlay loaded"
sudo insmod ./$(NAME).ko && echo "LKM loaded"
unload:
sudo rmmod ./$(NAME).ko
@echo "LKM unloaded"
sudo dtoverlay -R $(NAME)
@echo "Overlay unloaded"
sudo rmmod ./$(NAME).ko && echo "LKM unloaded"
sudo dtoverlay -R $(NAME) && echo "Overlay unloaded"
list:
sudo dtoverlay -l

19
driver/counter_io_calls.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef _COUNTER_IO_CALLS_H_
#define _COUNTER_IO_CALLS_H_
#ifndef __KERNEL__
#include <stdint.h>
#define u64 uint64_t
#define u32 uint32_t
#endif
struct observation {
u64 ts_sec;
u64 ts_nsec;
u32 value;
};
#endif /* _COUNTER_IO_CALLS_H_ */

View File

@ -6,6 +6,8 @@
#include "ls7366r.h"
#include "trigger.h"
#include "leds.h"
#include "io.h"
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Wolfgang Hottgenroth");
@ -15,6 +17,12 @@ MODULE_DESCRIPTION("Driver for the MainsCnt measurement hat");
/* module loading and unloading */
static int __init my_init(void) {
printk("counter - Loading IO subsystem...\n");
if (io_init()) {
printk("counter - Error! Could not load IO subsystem\n");
return -1;
}
printk("counter - Loading the leds driver...\n");
if(platform_driver_register(&leds_driver)) {
printk("counter - Error! Could not load leds driver\n");
@ -45,6 +53,9 @@ static void __exit my_exit(void) {
printk("counter - Unloading the interrupt driver...\n");
platform_driver_unregister(&interrupt_driver);
printk("counter - Unloading IO subsystem...\n");
io_exit();
}
module_init(my_init);

12
driver/io.c Normal file
View File

@ -0,0 +1,12 @@
#include <linux/module.h>
#include <linux/init.h>
#include "io.h"
int io_init(void) {
return 0;
}
void io_exit(void) {
}

8
driver/io.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef _IO_H_
#define _IO_H_
int io_init(void);
void io_exit(void);
#endif /* _IO_H_ */

View File

@ -6,6 +6,8 @@
#include <linux/of_device.h>
#include <linux/gpio/consumer.h>
#include "leds.h"
/* leds stuff */
static int dt_leds_probe(struct platform_device *pdev);
@ -72,3 +74,26 @@ static void dt_leds_remove(struct platform_device *pdev) {
gpiod_put(red_led);
gpiod_put(blue_led);
}
void led_ctrl(enum led_color_e color, enum led_state_e state) {
struct gpio_desc *led;
switch (color) {
case e_RED:
led = red_led;
break;
case e_BLUE:
led = blue_led;
break;
}
int st;
switch (state) {
case e_ON:
st = 1;
break;
case e_OFF:
st = 0;
break;
}
gpiod_set_value(led, st);
}

View File

@ -5,4 +5,9 @@
extern struct platform_driver leds_driver;
enum led_color_e { e_RED, e_BLUE };
enum led_state_e { e_ON, e_OFF };
void led_ctrl(enum led_color_e color, enum led_state_e state);
#endif /* _LEDS_H_ */

View File

@ -12,6 +12,7 @@
#include <linux/timekeeping.h>
#include "ls7366r.h"
#include "leds.h"
@ -65,12 +66,9 @@ static void counter_worker(struct work_struct *work) {
if (data->last_value) {
u32 period = counter_value - data->last_value;
struct timespec64 duration = timespec64_sub(data->current_timestamp, data->last_timestamp);
printk("counter %lld.%09lu %lld.%09lu %u\n",
printk("counter %lld.%09lu %u\n",
(s64)((data->current_timestamp).tv_sec),
((data->current_timestamp).tv_nsec),
(s64)duration.tv_sec,
duration.tv_nsec,
period);