Kernel improvements (#468)

* **New Features**
  * Plugin-style modules for platforms and devices with start/stop lifecycle and a runtime-consumable device tree array.

* **Refactor**
  * Consolidated initialization into a kernel/module startup flow.
  * Generated devicetree artifacts moved to the build Generated directory.

* **Changes**
  * Removed legacy no-op registration hooks and I2C lock/unlock wrappers.
  * Driver ownership and private state moved to explicit module/owner model.
  * Added ERROR_NOT_ALLOWED error code.
This commit is contained in:
Ken Van Hoeylandt
2026-01-31 09:18:02 +01:00
committed by GitHub
parent d62314f41f
commit c9185740d7
119 changed files with 1639 additions and 440 deletions
@@ -15,6 +15,7 @@ struct RecursiveMutex {
};
inline static void recursive_mutex_construct(struct RecursiveMutex* mutex) {
check(mutex->handle == NULL);
mutex->handle = xSemaphoreCreateRecursiveMutex();
}
+12 -1
View File
@@ -16,6 +16,7 @@ extern "C" {
#include <tactility/concurrent/mutex.h>
struct Driver;
struct DevicePrivate;
/** Enables discovering devices of the same type */
struct DeviceType {
@@ -46,10 +47,20 @@ struct Device {
bool added : 1;
} state;
/** Private data */
void* data;
struct DevicePrivate* device_private;
} internal;
};
/**
* Holds a device pointer and a compatible string.
* The device must not be constructed, added or started yet.
* This is used by the devicetree code generator and the application init sequence.
*/
struct CompatibleDevice {
struct Device* device;
const char* compatible;
};
/**
* Initialize the properties of a device.
*
+5 -4
View File
@@ -11,6 +11,8 @@ extern "C" {
struct Device;
struct DeviceType;
struct Module;
struct DriverPrivate;
struct Driver {
/** The driver name */
@@ -25,11 +27,10 @@ struct Driver {
const void* api;
/** Which type of devices this driver creates (can be NULL) */
const struct DeviceType* deviceType;
/** The module that owns this driver. When it is NULL, the system owns the driver and it cannot be removed from registration. */
const struct Module* owner;
/** Internal data */
struct {
/** Contains private data */
void* data;
} internal;
struct DriverPrivate* driver_private;
};
error_t driver_construct(struct Driver* driver);
@@ -22,6 +22,7 @@ typedef int error_t;
#define ERROR_TIMEOUT 8
#define ERROR_OUT_OF_MEMORY 9
#define ERROR_NOT_SUPPORTED 10
#define ERROR_NOT_ALLOWED 11
/** Convert an error_t to a human-readable text. Useful for logging. */
const char* error_to_string(error_t error);
@@ -0,0 +1,22 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <tactility/device.h>
#include <tactility/error.h>
#include <tactility/module.h>
/**
* Initialize the kernel with platform and device modules, and a device tree.
* @param platform_module The platform module to start. This module should not be constructed yet.
* @param device_module The device module to start. This module should not be constructed yet.
* @param devicetree_devices The list of generated devices from the devicetree. The array must be terminated by an entry { NULL, NULL }
* @return ERROR_NONE on success, otherwise an error code
*/
error_t kernel_init(struct Module* platform_module, struct Module* device_module, struct CompatibleDevice devicetree_devices[]);
#ifdef __cplusplus
}
#endif
+101
View File
@@ -0,0 +1,101 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include "error.h"
#include <stdbool.h>
struct ModuleParent;
struct ModuleParentPrivate;
/**
* A module is a collection of drivers or other functionality that can be loaded and unloaded at runtime.
*/
struct Module {
/**
* The name of the module, for logging/debugging purposes
* Should never be NULL.
*/
const char* name;
/**
* A function to initialize the module.
* Should never be NULL.
* This can be used to load drivers, initialize hardware, etc.
* @return ERROR_NONE if successful
*/
error_t (*start)(void);
/**
* Deinitializes the module.
* Should never be NULL.
* @return ERROR_NONE if successful
*/
error_t (*stop)(void);
struct {
bool started;
struct ModuleParent* parent;
} internal;
};
/**
* A module parent is a collection of modules that can be loaded and unloaded at runtime.
*/
struct ModuleParent {
/** The name of the parent module, for logging/debugging purposes */
const char* name;
struct ModuleParentPrivate* module_parent_private;
};
/**
* @brief Initialize the module parent.
* @warn This function does no validation on input or state.
* @param parent parent module
* @return ERROR_NONE if successful, ERROR_OUT_OF_MEMORY if allocation fails
*/
error_t module_parent_construct(struct ModuleParent* parent);
/**
* @brief Deinitialize the module parent. Must have no children when calling this.
* @warn This function does no validation on input.
* @param parent parent module
* @return ERROR_NONE if successful or ERROR_INVALID_STATE if the parent has children
*/
error_t module_parent_destruct(struct ModuleParent* parent);
/**
* @brief Set the parent of the module.
* @warning must call before module_start()
* @param module module
* @param parent nullable parent module
* @return ERROR_NONE if successful, ERROR_INVALID_STATE if the module is already started
*/
error_t module_set_parent(struct Module* module, struct ModuleParent* parent);
/**
* @brief Start the module.
* @param module module
* @return ERROR_NONE if already started, ERROR_INVALID_STATE if the module doesn't have a parent, or otherwise it returns the result of the module's start function
*/
error_t module_start(struct Module* module);
/**
* @brief Check if the module is started.
* @param module module to check
* @return true if the module is started, false otherwise
*/
bool module_is_started(struct Module* module);
/**
* @brief Stop the module.
* @param module module
* @return ERROR_NONE if successful or if the module wasn't started, or otherwise it returns the result of the module's stop function
*/
error_t module_stop(struct Module* module);
#ifdef __cplusplus
}
#endif