0% found this document useful (0 votes)
2 views

can i use custom make file to implement plug and p

Custom makefiles can be used in STM32 projects to conditionally include drivers at build time, allowing for modular driver implementations. While true runtime driver loading like Linux kernel modules is not supported, alternatives such as function pointer tables and vector table modification can be utilized for runtime driver selection. These techniques enable flexible and configurable driver implementations in embedded systems despite the limitations of STM32 microcontrollers.

Uploaded by

Nimit Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

can i use custom make file to implement plug and p

Custom makefiles can be used in STM32 projects to conditionally include drivers at build time, allowing for modular driver implementations. While true runtime driver loading like Linux kernel modules is not supported, alternatives such as function pointer tables and vector table modification can be utilized for runtime driver selection. These techniques enable flexible and configurable driver implementations in embedded systems despite the limitations of STM32 microcontrollers.

Uploaded by

Nimit Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

can i use custom make file to implement plug and

play and conditional runtime driver linking like


loadable ernel modules in my stm32 project?
Standard STM32 microcontrollers don't support true Linux-style loadable kernel modules, but
you can achieve some similar functionality through custom makefiles and specific runtime
techniques.

Custom Makefiles in STM32 Projects


Yes, you can create and use custom makefiles with STM32 projects as an alternative to the IDE-
generated ones:

# Example custom makefile for conditional driver inclusion


ENABLE_DRIVER_X ?= 1

ifeq ($(ENABLE_DRIVER_X), 1)
C_SOURCES += Drivers/my_driver_x.c
C_INCLUDES += -IDrivers/my_driver_x
CFLAGS += -DDRIVER_X_ENABLED
endif

This approach lets you conditionally include drivers at build time based on configuration
variables [1] [2] .

Runtime Driver Selection Options


While STM32 MCUs cannot dynamically load code like Linux kernel modules [3] [4] , you have
several alternatives:
1. Function Pointer Tables:
Create tables of function pointers that can be modified at runtime to point to different driver
implementations:
typedef struct {
void (*init)(void);
uint8_t (*read)(void);
void (*write)(uint8_t);
} driver_interface_t;

// Switch between implementations at runtime


driver_interface_t* current_driver = &driver_implementation_A;
2. Vector Table Modification:
For interrupt-driven peripherals, you can copy the vector table to RAM and modify it to point
to different interrupt handlers at runtime [5] :
// Copy vector table to RAM and update VTOR register
memcpy(ram_vector_table, (uint32_t*)SCB->VTOR, vector_table_size);
SCB->VTOR = (uint32_t)ram_vector_table;

// Now you can modify entries


ram_vector_table[DMA1_CHANNEL2_IRQ_POS] = (uint32_t)my_custom_handler;

3. Compile-Time Configuration with Build Variants:


Create different build targets in your makefile for various driver combinations:
.PHONY: build_with_driver_A build_with_driver_B

build_with_driver_A:
$(MAKE) DRIVER_CONFIG=A

build_with_driver_B:
$(MAKE) DRIVER_CONFIG=B

Practical Implementation
For a reusable driver approach:
1. Create a folder structure for your custom drivers (e.g., my_Driver/) [1]
2. Add the driver paths to your makefile:
C_INCLUDES += -Imy_Driver
C_SOURCES += my_Driver/my_Driver.c

3. Use conditional compilation in your code:


#ifdef USE_CUSTOM_LED_DRIVER
#include "my_Driver/my_Driver.h"
#else
#include "standard_driver.h"
#endif

Unlike Linux kernel modules [3] [4] , true runtime code loading isn't possible on most STM32
MCUs. However, these techniques provide flexible alternatives for modular, configurable driver
implementations in embedded systems.

1. https://www.reddit.com/r/stm32/comments/18nwwnu/developing_a_custom_driver_with_stm32_and_vsc
ode/
2. https://github.com/bbrown1867/stm32-makefile
3. https://docs.kernel.org/kbuild/makefiles.html
4. https://fastbitlab.com/building-a-linux-kernel-module/
5. https://stackoverflow.com/questions/76154215/stm32-replace-entry-in-vector-table-at-runtime

You might also like