The Background
For testing the wireless module from my
LoRa Wireless Project,
I needed a quick and easy way to perform initial tests and verify the functionality of the PCB.
For this purpose, I used a standard Nucleo development board together with the phenomenal
RadioLib library.
Since my board was not supported out of the box, and I generally prefer working with the standard STM32 HAL
or even the low-level libraries, I started looking into how to create a simple translation layer for
RadioLib.
After some research and testing, I developed a lightweight HAL translation layer that allows RadioLib to
communicate with the STM32 HAL. This made it possible to use RadioLib with my hardware while keeping the
underlying STM32 implementation as simple as possible.
Description
The library consists of two files: STM32HAL.cpp and its corresponding header file, STM32HAL.hpp.
The header is divided into two main parts.
The first part consists of inline functions and definitions that translate the STM32 GPIO_TypeDef types into
the
standard GPIO definitions used by RadioLib.
The second part extends the RadioLibHal class by implementing the essential functions required to provide a
functional HAL implementation for STM32.
Where possible, the functions are defined as static inline to allow the compiler to optimise them away,
reducing unnecessary function calls and processing overhead.
Additional functionality is implemented using preprocessor definitions (#define), allowing optional features
to be resolved at compile time rather than introducing runtime overhead.
The HAL overrides the essential methods of the original RadioLibHal class, which are implemented in the
source file. Only the unused SPI transfer functions are left empty and defined directly in the header file.
One additional feature I introduced, which is not part of the original RadioLib HAL interface, is the
getLastSpiTransferStatus() method.
This method provides information about the most recent SPI transfer status. It turned out to be particularly
useful during the early development stages, as it allowed me to identify and investigate SPI communication
errors more easily.
The Delay Function micros
One important limitation of this implementation is the handling of microseconds.
The STM32 HAL does not provide a standard microsecond function out of the box. Implementing one would
require an additional timer or another timing mechanism.
Since I wanted to keep the library as simple as possible and avoid introducing additional hardware
dependencies, I decided to implement the microsecond method using the existing millisecond HAL_GetTick()
function.
As a result, a requested microsecond sind start is effectively converted into a millisecond delay, making it
approximately 1000 times longer than intended.
This is obviously not ideal, but it was a deliberate design decision to keep the implementation simple and
avoid imposing additional hardware requirements.
If precise microsecond timing is required for a particular application, the micros function must be
implemented accordingly, for example by using a hardware timer or another suitable timing mechanism.
For my initial development and testing purposes, this limitation was acceptable and allowed me to get the
library up and running without introducing unnecessary complexity.
With these limitations in mind, the library worked as intended with my hardware and provided a reliable
foundation for the initial testing and development. It was also used to perform the first measurements,
which are presented in the hardware description of the project.
The Result
A small excerpt of the working C++ code is shown in the SPI snippet on the right.
The main focus is the MOSI data, which shows the following sequence:
8F 00 00 8A 01 88 60 96 01 86 B8 9D 8A
This sequence contains several SX1280 configuration commands. 0x8F is SetBufferBaseAddress, setting both the
TX and RX base addresses to 0x00. 0x8A 0x01 is SetPacketType, configuring the radio to use LoRa modulation.
This capture shows the beginning of a successful initialization sequence from one of the working modules.
As always the source to the project can be found on
Github.