Peripherals¶
The peripherals library provides a light hardware abstraction layer to manage the GPIO, UART, SPI and I²C peripherals. It also provides miscellaneous functions for time and numbers.
The provided version targets the Arduino and compatible SDKs, and could easily be adapted to other SDKs 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. It configures and starts the peripherals GPIO, UART, SPI, and I²C if needed.
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 peripherals.
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_begin()
initialises the UART interface.
hV_HAL_printf("Value= %i", 3);
hV_HAL_crlf();
The resulting line sent to the console is
Value= 3
hV_HAL_printf()
sends a format and the values. It corresponds to the printf()
.
hV_HAL_crlf()
performs a carriage return and line feed.
Legacy version 5
The following functions are used as synonyms.
hV_HAL_Debug_begin(115200);
hV_HAL_Debug_print("%i", 3);
hV_HAL_Debug_begin()
configures the speed of the UART interface and launches it.
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.
Optionally,
hV_HAL_Debug_flush();
hV_HAL_Debug_flush()
ensures the UART buffer has been sent.
Log messages¶
The log system displays messages with a level to the UART interface.
The UART interface is initialised by hV_HAL_begin()
.
hV_HAL_log(LEVEL_INFO, "Value= %i", 3);
hV_HAL_log()
sends a log message with a level, a format and the values.
The resulting line sent to the console is
hV . Value= 3
Level | Symbol | Details |
---|---|---|
LEVEL_CRITICAL |
* | Non-handled error |
LEVEL_ERROR |
+ | Handled error |
LEVEL_WARNING |
! | Warning |
LEVEL_INFO |
. | Information |
LEVEL_DEBUG |
- | Debug |
LEVEL_SYSTEM |
= | System |
hV_HAL_selectLevel(LEVEL_CRITICAL | LEVEL_ERROR);
hV_HAL_selectLevel()
set a filter to the messages and prints those with the mentioned levels.
The example above will only print the messages with a critical or an error level.
SPI¶
hV_HAL_SPI_begin();
uint8_t answer = hV_HAL_SPI_transfer(data);
hV_HAL_SPI_begin()
configures the SPI bus at 8 MHz, MSB first, and launches it.
hV_HAL_SPI_transfer()
sends data and receives an answer.
Optionally,
hV_HAL_SPI_end();
//
hV_HAL_SPI_begin();
hV_HAL_SPI_end()
deactivates the SPI bus, to be reactivated with hV_HAL_SPI_begin()
.
Legacy version 5
The following functions are used as synonyms.
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, and launches it.
hV_HAL_SPI_transfer()
sends data and receives an answer.
Optionally,
hV_HAL_SPI_end();
// ...
hV_HAL_SPI_begin();
hV_HAL_SPI_end()
deactivates the SPI bus, to be reactivated with hV_HAL_SPI_begin()
.
hV_HAL_SPI_begin()
launches the SPI bus.
I²C¶
hV_HAL_Wire_begin();
hV_HAL_Wire_writeRead(address, data, sizeData, answer, sizeAnswer);
hV_HAL_Wire_begin()
configures the I²C bus in fast mode at 400 kHz and launches it.
hV_HAL_Wire_writeRead()
connects to the I²C device at the specified address, sends the data and optionnally retrieves the answer.
Optionally,
hV_HAL_Wire_end();
// ...
hV_HAL_Wire_begin();
hV_HAL_Wire_end()
deactivates the I²C bus, to be reactivated with hV_HAL_Wire_begin()
.
Time¶
hV_HAL_delayMicroseconds(100);
hV_HAL_delayMilliseconds(100);
uint32_t chrono32 = hV_HAL_getMilliseconds()
hV_HAL_delayMicroseconds()
performs a blocking delay of the specified duration in microseconds. hV_HAL_delayMilliseconds()
performs a non-blocking delay of the specified duration in 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.
Miscellaneous¶
uint32_t random hV_HAL_random(maxNumber);
hV_HAL_random()
generates a pseudo-random number between 0
and maxNumber - 1
, both included.
int32_t outputValue = hV_HAL_map(inputValue, inputMin, inputMax, outputMin, outputMax)
hV_HAL_map()
scales a value.
Formula is
Terminate¶
hV_HAL_end();
hV_HAL_end()
enters an endless loop for micro-controllers or exits with a 0
value for Linux. It corresponds to hV_HAL_exit(0)
.
hV_HAL_exit(code);
hV_HAL_exit(code)
displays the code
value, and enters an endless loop for micro-controllers or exits with the code
value for Linux.