diff --git a/src/06-mini-project/kernel/blink/blink.c b/src/06-mini-project/kernel/blink/blink.c new file mode 100644 index 0000000..8c81c48 --- /dev/null +++ b/src/06-mini-project/kernel/blink/blink.c @@ -0,0 +1,64 @@ +#include "blink.h" + +static struct task_struct* blink_thread; +atomic_t blink_period_ms; + +int thread_blinkThread(void* data); + +void blink_init(void) { + int ret = 0; + pr_info("Initialize blink thread\n"); + + atomic_set(&blink_period_ms, DEFAULT_PERIOD_MS); + + ret = gpio_request(GPIO_PIN, "blink-led"); + if(ret) { + pr_err("Failed to request GPIO pin %d, error %d\n", GPIO_PIN, ret); + return; + } + + ret = gpio_direction_output(GPIO_PIN, 0); + if(ret) { + pr_err("Failed to set GPIO pin %d direction, error %d\n", GPIO_PIN, ret); + gpio_free(GPIO_PIN); + return; + } + + blink_thread = kthread_run(thread_blinkThread, NULL, "blink_thread"); + if (IS_ERR(blink_thread)) { + pr_err("Failed to create blink thread\n"); + gpio_free(GPIO_PIN); + return; + } + + pr_info("Blink thread initialized\n"); +} + +void blink_exit(void) { + pr_info("Exiting blink thread\n"); + kthread_stop(blink_thread); + gpio_free(GPIO_PIN); + pr_info("Blink thread exited\n"); +} + +int thread_blinkThread(void* data) { + + bool state = false; + int period = 0; + + pr_info("Blink started\n"); + + while (!kthread_should_stop()) { + gpio_set_value(GPIO_PIN, state); + state = !state; + period = atomic_read(&blink_period_ms); + msleep(period >> 1); + } + + return 0; +} + +void adjust_frequency(int new_period_ms) { + pr_info("Adjusting blink period to %d ms\n", new_period_ms); + atomic_set(&blink_period_ms, new_period_ms); +} \ No newline at end of file diff --git a/src/06-mini-project/kernel/blink/blink.h b/src/06-mini-project/kernel/blink/blink.h new file mode 100644 index 0000000..6c59fb7 --- /dev/null +++ b/src/06-mini-project/kernel/blink/blink.h @@ -0,0 +1,24 @@ +#include // needed by all modules +#include // needed for macros +#include // needed for debugging + + +#include +#include + +#include + +#include + + +#define GPIO_PIN 10 +#define DEFAULT_PERIOD_MS 1000 + +/** + * Allow to set a new frequency for the blinky led + * @param new_period_ms The new period in ms for the blinky led + */ +void adjust_frequency(int new_period_ms); + +void blink_init(void); +void blink_exit(void); diff --git a/src/06-mini-project/kernel/main.c b/src/06-mini-project/kernel/main.c index 0450f78..d89e13b 100644 --- a/src/06-mini-project/kernel/main.c +++ b/src/06-mini-project/kernel/main.c @@ -2,13 +2,11 @@ #include // needed by all modules #include // needed for macros #include // needed for debugging -#include -#include -#include #include "linux/printk.h" #include "temperature/temp.h" +#include "blink/blink.h" static int __init temp_regulator_init(void) { pr_info("Linux module temp_regulator loading...\n"); @@ -27,6 +25,12 @@ static int __init temp_regulator_init(void) { temp / 1000, temp % 1000); + blink_init(); + ssleep(5); + adjust_frequency(500); + ssleep(5); + adjust_frequency(5000); + pr_info("Linux module temp_regulator loaded\n"); return 0; } @@ -37,6 +41,7 @@ static void __exit temp_regulator_exit(void) { /* Release sensor resources */ temp_exit(); + blink_exit(); pr_info("Linux module temp_regulator unloaded\n"); }