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.
Legacy version 6
Deprecated functions and synonyms have been removed from the peripherals library.
For more information, please refer to Migrate from release 6 to release 7.
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.
The names of the functions start with hV_HAL
, then the peripherals like _GPIO
, and finally the command like _begin()
, to form hV_HAL_GPIO_begin()
.
GPIO¶
hV_HAL_GPIO_begin();
hV_HAL_GPIO_define(PIN1, OUTPUT);
hV_HAL_GPIO_set(PIN1);
hV_HAL_GPIO_clear(PIN1);
// hV_HAL_GPIO_write(PIN1, HIGH);
hV_HAL_GPIO_define(PIN2, INPUT_PULLUP);
uint8_t result = hV_HAL_GPIO_get(PIN2);
// uint8_t result = hV_HAL_GPIO_read(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, with hV_HAL_GPIO_write()
as synonym
hV_HAL_GPIO_get()
reads the value from the pins as input, with hV_HAL_GPIO_read()
as synonym.
Serial¶
hV_HAL_Serial_begin(115200);
hV_HAL_Serial_begin()
initialises the UART interface.
hV_HAL_Serial_printf("Value= %i", 0);
hV_HAL_Serial_crlf();
The resulting line sent to the console is
Value= 0
hV_HAL_Serial_printf()
sends a format and the values. It corresponds to the standard function printf()
.
hV_HAL_Serial_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.
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¶
I²C is also called Wire.
hV_HAL_Wire_begin();
hV_HAL_Wire_transfer(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_transfer()
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.
Log system¶
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", 1);
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= 1
Level | Symbol | Details |
---|---|---|
LEVEL_CRITICAL |
* | Non-handled error |
LEVEL_ERROR |
+ | Handled error |
LEVEL_WARNING |
! | Warning |
LEVEL_INFO |
. | Information |
LEVEL_DEBUG |
- | Debug |
LEVEL_SYSTEM |
= | System |
Option | Details |
---|---|
WITH_COMMENT |
Line starting with // |
WITH_NO_CRLF |
No carriage return-line feed |
WITH_NO_LEVEL |
Level not printed |
hV_HAL_log(LEVEL_INFO | WITH_COMMENT, "Value= %i", 2);
hV_HAL_log(LEVEL_INFO | WITH_NO_CRLF, "Value= %i and ", 3);
hV_HAL_log(LEVEL_INFO | WITH_NO_LEVEL, "Value= %i", 4);
The resulting lines sent to the console are
// hV . Value= 2 hV . Value= 3 and Value= 4
hV_HAL_setFilter(LEVEL_CRITICAL | LEVEL_ERROR);
hV_HAL_setFilter()
sets 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.
hV_HAL_setOption(WITH_COMMENT);
hV_HAL_log(LEVEL_INFO, "Value= %i", 5);
hV_HAL_setOption()
defines a default option for all the messages sent.
The resulting line sent to the console is
// hV . Value= 5
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.