Bioscara
DALSA's DIY SCARA Robot Arm.
Loading...
Searching...
No Matches
Macros | Functions
uI2C.h File Reference

Low level utility for I2C communication on Raspberry Pi using lgpio library. More...

#include <cstring>
#include <errno.h>
#include <fcntl.h>
#include <iostream>
#include <termios.h>
#include <unistd.h>

Go to the source code of this file.

Macros

#define ACK   'O'
 
#define NACK   'N'
 
#define RFLAGS_SIZE   1
 Size of the return flags in bytes.
 
#define MAX_BUFFER   4
 Maximum size of I2C Payload in bytes.
 

Functions

int openI2CDevHandle (const int dev_addr)
 Initiates an I2C device on the bus.
 
int readFromI2CDev (const int dev_handle, const int reg, char *buffer, const int data_length)
 reads block of bytes from device to buffer
 
int writeToI2CDev (const int dev_handle, const int reg, char *tx_buffer, const int data_length, char *RFLAGS_buffer)
 writes block of bytes from buffer to device
 
int closeI2CDevHandle (int &dev_handle)
 close an I2C device on the bus
 

Detailed Description

Low level utility for I2C communication on Raspberry Pi using lgpio library.

Author
sbstorz
Version
0.1
Date
2025-05-28

lgpio needs to be installed and linked! Installation:

cd ~
sudo apt update
sudo apt install -y swig
wget https://github.com/joan2937/lg/archive/master.zip
unzip master.zip
cd lg-master
make
sudo make install
cd ..
sudo rm -rf lg-master
rm master.zip

bash

Macro Definition Documentation

◆ ACK

#define ACK   'O'

◆ MAX_BUFFER

#define MAX_BUFFER   4

Maximum size of I2C Payload in bytes.

4 bytes used to transmit floats and int32_t

◆ NACK

#define NACK   'N'

◆ RFLAGS_SIZE

#define RFLAGS_SIZE   1

Size of the return flags in bytes.

Only one byte used and hence set to 1.

Function Documentation

◆ closeI2CDevHandle()

int closeI2CDevHandle ( int &  dev_handle)

close an I2C device on the bus

Parameters
dev_handledevice handle obtained from openI2CDevHandle
Returns
0 on OK, negative on error.

◆ openI2CDevHandle()

int openI2CDevHandle ( const int  dev_addr)

Initiates an I2C device on the bus.

Parameters
dev_addr7-bit device adress [0 - 0x7F]
Returns
the device handle, negative on error.

◆ readFromI2CDev()

int readFromI2CDev ( const int  dev_handle,
const int  reg,
char *  buffer,
const int  data_length 
)

reads block of bytes from device to buffer

Parameters
dev_handledevice handle obtained from openI2CDevHandle
regthe command/data register
bufferpointer to data buffer to hold received values
data_lengthnumber of bytes to read
Returns
number of bytes read, negative on error.

◆ writeToI2CDev()

int writeToI2CDev ( const int  dev_handle,
const int  reg,
char *  tx_buffer,
const int  data_length,
char *  RFLAGS_buffer 
)

writes block of bytes from buffer to device

Parameters
dev_handledevice handle obtained from openI2CDevHandle
regthe command/data register
tx_bufferpointer to data buffer holding the data to send
data_lengthnumber of bytes to send
RFLAGS_bufferbuffer to hold returned flags
Returns
0 on OK, negative on error.