64
src/06-mini-project/kernel/blink/blink.c
Normal file
64
src/06-mini-project/kernel/blink/blink.c
Normal file
@@ -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);
|
||||
}
|
||||
24
src/06-mini-project/kernel/blink/blink.h
Normal file
24
src/06-mini-project/kernel/blink/blink.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <linux/module.h> // needed by all modules
|
||||
#include <linux/init.h> // needed for macros
|
||||
#include <linux/kernel.h> // needed for debugging
|
||||
|
||||
|
||||
#include <linux/kthread.h>
|
||||
#include <linux/delay.h>
|
||||
|
||||
#include <linux/gpio.h>
|
||||
|
||||
#include <linux/atomic.h>
|
||||
|
||||
|
||||
#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);
|
||||
@@ -2,13 +2,11 @@
|
||||
#include <linux/module.h> // needed by all modules
|
||||
#include <linux/init.h> // needed for macros
|
||||
#include <linux/kernel.h> // needed for debugging
|
||||
#include <linux/kthread.h>
|
||||
#include <linux/delay.h>
|
||||
#include <linux/err.h>
|
||||
|
||||
#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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user