Skip to content

Porting to non Arduino Platforms

Jan Gromeš edited this page Apr 9, 2023 · 12 revisions

Porting to non-Arduino Platforms

There are several things that have to be done to port RadioLib to a non-Arduino platform. First, since RadioLib is written in C++, you have to make sure your build system can compile C++ files. Next, you have to make sure your build system DOES NOT define the macro ARDUINO. If this macro is defined, RadioLib will attempt to automatically detect which Arduino is being used. If the macro is not defined, RadioLib will attempt to include the file noarduino.h, which has to be provided by your build system (e.g. by using GCC's -I flag).

The noarduino.h file has to define all the platform macros shown in Custom Build Configuration - this includes callbacks to Arduino functions, like digitalRead(), or tone(). Therefore, your platform has to define an equivalent of these functions. For example, in STM32 HAL, there's a function HAL_GPIO_WritePin(), which behaves similarly as Arduino digitalWrite(), but has an extra argument - the GPIO port. Therefore, an appropriate wrapper should look something like this:

// noarduino.h
// forward declaration
void wrap_HAL_GPIO_WritePin(uint8_t pin, uint8_t value);

// callback argument macro
// the first argument is the return type of the wrapper function
// the second argument is the name of the corresponding Arduino function
// all other arguments are the arguments of the wrapper function
#define RADIOLIB_CB_ARGS_DIGITAL_WRITE              (void, digitalWrite, uint32_t pin, GPIO_PinState value)

// noarduino.c/cpp
// now the implementation - pin number also includes the GPIOx port
void wrap_HAL_GPIO_WritePin(uint32_t pin, GPIO_PinState value) {
  if(pin > 16) {
    HAL_GPIO_WritePin(GPIOB, uint16_t pin, GPIO_PinState value);
  } else {
    HAL_GPIO_WritePin(GPIOA, uint16_t pin, GPIO_PinState value);
  }
}

This has to be done for all Arduino functions listed on the page Custom Build Configuration.

Once done, the last thing left to do is to initialize all callbacks defined in the noarduino.h file. The callbacks themselves are generated by RadioLib - the user only has to call the initalizing functions:

// some random NSS/IRQ/RST/GPIO pins
Module mod(1, 2, 3, 4);
SX1278 radio(&mod);

(...)
mod.setCb_digitalRead(::wrap_HAL_GPIO_ReadPin);
mod.setCb_digitalWrite(::wrap_HAL_GPIO_WritePin);
// etc. for all the callbacks
(...)

// we can go now!
radio.begin();