Best Practices for Sharing HAL Code Across Multiple Libraries in a Multi-Core STM32 Project
Hello STM Community,
I am working on an STM32 project that utilizes both the CM7 and CM4 cores. I have structured my project to include a static library named Utilities, where I'm facing some challenges with accessing the HAL functionality.
Project Structure:
Utilities
├── .settings
├── Debug
├── Inc
│ └── SOS.hpp
└── Src
└── SOS.cpp
In SOS.cpp, I implemented a function to flash an LED in an SOS pattern using HAL functions:
#include "../Inc/SOS.hpp"
#include <cstdint>
namespace STM::Utilities {
void SOS::Run() {
const uint32_t shortDelay = 200; // Short flash duration
const uint32_t longDelay = 500; // Long flash duration
const uint32_t endDelay = 1000; // Pause duration between signals
auto flashLED = [](bool longFlash) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
HAL_Delay(longFlash ? longDelay : shortDelay);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
HAL_Delay(shortDelay);
};
while (true) { // Continuously send SOS signals
for (int i = 0; i < 3; ++i) flashLED(false); // Three short flashes
for (int i = 0; i < 3; ++i) flashLED(true); // Three long flashes
for (int i = 0; i < 3; ++i) flashLED(false); // Three short flashes
HAL_Delay(endDelay);
}
}
}
Issue: Clearly I'm encountering compiler errors related to the HAL function calls and the GPIO port/pin definitions. The HAL functionality is accessible within the separate CM7 and CM4 core projects, but not directly within my Utilities library. Of course the CubeMX generated main.c would stay in place but I'd like to be able to operate on those resources from my libraries.
Question: I anticipate the need to create additional static libraries similar to Utilities. To maintain modularity and avoid redundancy, I prefer not to duplicate the HAL code in each library (I don't even know if that is possible). Instead, I'd like to set up a shared HAL library that can be accessed by all other libraries. Could anyone guide me on how to achieve this? Specifically, I'm looking for the best practices to manage and share HAL dependencies in a multi-core STM32 project without embedding the HAL code into each static library.
OR
Is there another mechanism for accessing the HAL from a static library that I am not aware of?
Thank you for any insights or examples you can provide!
