The Linux Hardware Abstraction Layer (HAL) is used with the C/C++ bindings for microcontrollers to communicate with Bricklets over SPI.
This HAL was tested with the Raspberry Pi and Pi Zero with the HAT Brick or HAT Zero Brick, but should work with all Linux based systems with spidev support. A more performant HAL specifically for the Raspberry Pi is also available here.
Note
The Raspberry Pi 3, 4 and Zero scale their clock speed dynamically. Unfortunately this also scales the SPI clock speed. You have lock the GPU core frequency to make sure, the SPI clock is stable. We recommend setting the core_freq in config.txt to 250. See here for details, especially for the Pi 4B, where the core_freq is influenced by other boot options.
If you select another core_freq than 250, you have to compensate by multiplying
BRICKLET_STACK_SPI_CONFIG_MAX_SPEED_HZ
by 250 / [your core_freq in mhz].
Note
A level shifter is necessary for boards that use a logic level of 5 Volt.
This HAL includes an example driver sketch that can be used to run any example provided with the bindings, as well as a Makefile to compile any example. To use the Makefile, create the following folder layout:
After creating the folder structure, you have to modify the Makefile for your example:
List the device used in your selected example under SOURCES_DEVICES
, for example for the Industrial Digital In 4 Bricklet 2.0
SOURCES_DEVICES := src/bindings/bricklet_industrial_digital_in_4_v2.c
and add the example source file itself to SOURCES_EXAMPLE
, for example
SOURCES_EXAMPLE := example_edge_count.c
Next you have to modify the port assignment in the sketch to fit to your set-up (see this section). If you connect multiple Bricklets to the same SPI bus (this is only possible with a tri-state buffer chip), you have to connect all their chip select pins to the Board and list them in the port assignment, even if you don't want to communicate with the Bricklets yet. This makes sure the signals are correctly separated.
The HAT Brick or HAT Zero Brick already contain the required buffer chip and their port assignment is listed in the example driver.
Then you maybe have to change the path to the spidev in the example driver.
The default "/dev/spidev0.0"
is correct for the :HAT Brick and the
HAT Zero Brick.
You can then compile the program with make
.
If you want to cross-compile from another machine, use make CROSS_COMPILE=[compiler prefix]
for example make CROSS_COMPILE=arm-linux-gnueabihf-
for the Raspberry Pi.
To run the compiled program, make sure that no Brick Daemon is running on the device and
start ./uc_example
.
A port is specified as an instance of the TF_Port
structure:
struct TF_Port {
int chip_select_pin;
char port_name;
}
Revelant members are int chip_select_pin
and char port_name
.
The chip select pin is the pin that has to be pulled to be able to communicate to the port.
The port name is a single character that identifies the port. The name is injected into the
result of tf_[device]_get_identity
calls if the device is connected directly to the host.
The example_driver.c
contains an example port specification.
Most functions of the HAL return an error code (e_code
).
Possible error codes are (as defined in errors.h
):
The HAL defines the following further error codes:
Use tf_hal_strerror()
to get
an error string for an error code.
tf_hal_create
(TF_HAL *hal, const char *spidev_path, TF_Port *ports, uint8_t port_count)¶Creates a HAL object that can be used to list the available devices. It is also required for the constructor of Bricks and Bricklets.
spidev_path
is the path to the spidev to use, for example "/dev/spidev0.0".ports
is an array of port specifications, as described hereport_count
is the length of the ports
array.
tf_hal_destroy
(TF_HAL *hal)¶Destroys the given TF_HAL
.
tf_hal_set_timeout
(TF_HAL *hal, uint32_t timeout_us)¶Sets the timeout in microseconds for getters and for setters for which the response expected flag is activated.
The default timeout is 2500000 (2.5 seconds).
tf_hal_get_timeout
(TF_HAL *hal)¶Returns the timeout as set by tf_hal_set_timeout()
.
tf_hal_get_device_info
(TF_HAL *hal, uint16_t index, char ret_uid[7], char *ret_port_name, uint16_t *ret_device_id)¶Returns the UID, port and device identifier of the nth (=index) detected device.
This function returns TF_E_DEVICE_NOT_FOUND
if the index was equal or higher than the number of detected devices.
To list all devices you can call this function in a loop with growing index until TF_E_DEVICE_NOT_FOUND
is returned once.
tf_hal_callback_tick
(TF_HAL *hal, uint32_t timeout_us)¶Polls for callbacks on all devices with an registered callback handler. Will block for the given timeout in microseconds.
This function can be used in a non-blocking fashion by calling it with a timeout of 0. The bindings will then poll a single device for one callback, by clocking out a single byte over SPI, returning immediately if no callback is available. If the device starts sending a callback packet, it will be received, acked and the callback handler will be called.
The polling is scheduled round-robin over multiple calls, so even if you only poll with a timeout of 0, all devices will be polled as fairly as possble.
tf_hal_deadline_elapsed
(TF_HAL *hal, uint32_t deadline_us)¶Returns true if the deadline in microseconds is elapsed, false otherwise. Robust against
overflows up to UINT32_MAX / 2
.
tf_hal_get_error_counters
(TF_HAL *hal, char port_name, uint32_t *ret_spitfp_error_count_checksum, uint32_t *ret_spitfp_error_count_frame, uint32_t *ret_tfp_error_count_frame, uint32_t *ret_tfp_error_count_unexpected)¶Returns the error counters for the given port. The following errors are counted:
spitfp_error_count_checksum
: Received SPITFP packets that where ignored because of a wrong checksumspitfp_error_count_frame
: Received SPITFP packets with an invalid lengthtfp_error_count_frame
: Received TFP packets with an invalid lengthtfp_error_count_unexpected
: Received TFP packets that where unexpected because they responded to unknown requests
tf_hal_log_error
(const char *format, ...)¶Logs an error if the log level in bindings/config.h
is TF_LOG_LEVEL_ERROR
or more.
Supports a subset of the standard printf syntax. See tf_hal_printf()
for details.
tf_hal_log_info
(const char *format, ...)¶Logs an information if the log level in bindings/config.h
is TF_LOG_LEVEL_INFO
or more.
Supports a subset of the standard printf syntax. See tf_hal_printf()
for details.
tf_hal_log_debug
(const char *format, ...)¶Logs a debug message if the log level in bindings/config.h
is TF_LOG_LEVEL_DEBUG
or more.
Supports a subset of the standard printf syntax. See tf_hal_printf()
for details.
tf_hal_printf
(const char *format, ...)¶This function is a minimalistic printf implementation. The following placeholders are supported:
%[prefix]u
: An unsigned integer value printed in base 10%[prefix]d
: A signed integer value printed in base 10%[prefix]b
: An unsigned integer value printed in base 2%[prefix]x
and %[prefix]X
: An unsigned integer value printed in base 16, in both cases with lower-case letters%c
: A single character%s
: A zero terminated string%%
: A percent signWith the prefix, you can control the width of printed integers.
Valid prefixes are I8
, I16
, I32
and I64
.
For example using the placeholder %I16x
will print a 16 bit integer hexadecimal.
No padding, grouping , l-modifiers or similar, or floats are supported.
The newline character \n
is translated to the platform specific newline character(s).
tf_hal_strerror
(int e_code)¶Returns an error string for the given error code.
tf_get_device_display_name
(uint16_t device_id)¶Returns the display name for the given device identifier.