Skip to content

Peripherals

The peripherals library provides a light hardware abstraction layer to manage the GPIO, UART, SPI and I²C peripherals.

The provided version targets the Arduino and compatible SDKs, and could easily be adapted to other environments as all the functions come with a C-like syntax.

The strings could be either the String object provided by the Arduino SDK, or the C-standard char array supported by the cstring library. The option is set on the configuration header file.

Configure

#include "hV_HAL_Peripherals.h"

The pre-processor statement includes the peripherals library.

hV_HAL_begin();

hV_HAL_begin() proceeds with the general initialisation, especially for Linux.

Use

For each peripheral, the library exposes a limited set of functions with a C-like syntax.

GPIO

hV_HAL_GPIO_begin();

hV_HAL_GPIO_define(PIN1, OUTPUT);
hV_HAL_GPIO_set(PIN1);
hV_HAL_GPIO_clear(PIN1);

hV_HAL_GPIO_define(PIN2, INPUT_PULLUP);
uint8_t result = hV_HAL_GPIO_get(PIN2);

hV_HAL_GPIO_begin() initialises the GPIO expander.

hV_HAL_GPIO_define() defines the pin as output or input, with pull-up or pull-down options.

hV_HAL_GPIO_set() and hV_HAL_GPIO_clear turns the pin as output high and low.

hV_HAL_GPIO_get() reads the value from the pins as input.

UART

hV_HAL_Debug_begin(115200);

hV_HAL_Debug_print("%i", 3);

hV_HAL_Debug_begin() initialises the speed of the UART interface.

hV_HAL_Debug_print() sends a format and the values. It corresponds to the printf().

hV_HAL_Debug_println() does the same with a carriage return and a new line.

SPI

hV_HAL_SPI_configure();

uint8_t answer = hV_HAL_SPI_transfer(data);

hV_HAL_SPI_configure() initialises the SPI bus with the default parameters of 4 MHz, MSB first, mode 0.

hV_HAL_SPI_transfer() sends data and receives an answer.

I²C

hV_HAL_Wire_begin();

hV_HAL_Wire_writeRead(address, data, sizeData, answer, sizeAnswer);

hV_HAL_Wire_begin() initialises the I²C bus in fast mode at 400 kHz.

hV_HAL_Wire_writeRead() connects to the I²C device at the specified address, sends the data and optionnally retrieves the answer.

Miscellaneous

hV_HAL_delayMicroseconds(100);
hV_HAL_delayMilliseconds(100);
uint32_t chrono32 = hV_HAL_getMilliseconds()

uint32_t random hV_HAL_random(maxNumber);

hV_HAL_delayMicroseconds() and hV_HAL_delayMilliseconds() perform a non-blocking delay of the specified duration in microseconds and milliseconds.

hV_HAL_getMilliseconds() returns the number of milliseconds since the start of the system. As a 32-bit integer, it is limited to ~50 days.

hV_HAL_random() generates a pseudo-random number between 0 and maxNumber - 1.

End and exit

hV_HAL_end();

hV_HAL_end() enters an endless loop or exits with a 0 value for Linux.

hV_HAL_exit(code);

hV_HAL_exit(code) displays the code value, and enters an endless loop or exits with the code value for Linux.

Back to top