Device::I2C(3pm)

๐Ÿ“ฆ
version 0.06 ยท 5 versions
, in ubuntu stonking

โœจ NAME โœจ

okay bestie, welcome to Device::I2C โ€” it's the <hotpink>Device::I2C(3pm)</hotpink> page (a User Contributed Perl Documentation situation, iykyk) and it does exactly one iconic thing:

Control and read hardware devices with i2c(SMBus). Yes. Actual physical chips. From Perl. Slay. ๐Ÿ’…


๐ŸŒธ VERSION ๐ŸŒธ

This is version 0.06. Baby number, but she's doing the most. ๐ŸŽ€


๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

here's the whole vibe in five lines, no cap:

use Device::I2C;
use Fcntl;
$dev = Device::I2C->new('/dev/i2c-1', O_RDWR);
$dev->checkDevice(0x4c);
print $dev->readByteData(0x20);

likeโ€ฆ make the object, check the device, read the byte. main character energy fr fr. โœจ


๐Ÿ’œ DESCRIPTION ๐Ÿ’œ

So. This is a perl interface to the I2C interface, and it does that through the libi2c-dev library. That's the whole premise. ๐Ÿซถ

๐Ÿ“ Prerequisites ๐Ÿ“

Not me having to say this but you do need the deps first. Here's how, per distro:

Debian and derivative distros (this includes raspbian โ€” yes, that's how the original spells it, don't @ me), use this to install the dependencies:

sudo apt-get install libi2c-dev i2c-tools build-essential

Angstrom Linux bestie, you get this instead:

opkg install i2c-tools
opkg install i2c-tools-dev

ArchLINUX, do these steps:

pacman -S base-devel
pacman -S i2c-tools

๐Ÿงš Special Instructions for enabling the I2C driver on a Raspberry Pi ๐Ÿงš

bestie. listen. You will need to comment out the driver from the blacklist, because currently the I2C driver isn't being loaded. Iconic behaviour from a default config honestly. ๐Ÿ’€

sudo vim /etc/modprobe.d/raspi-blacklist.conf

Replace this line:

blacklist i2c-bcm2708

with this:

#blacklist i2c-bcm2708

You now need to edit the modules conf file:

sudo vim /etc/modules

Add these two lines:

i2c-dev
i2c-bcm2708

Now run this command (and replace 1 with 0 for older model Pi, she's flexible like that):

sudo i2cdetect -y 1

If that doesnt work on your system you may alternatively use this:

sudo i2cdetect -r 1

and you should now see the addresses of the i2c devices connected to your i2c bus. ๐ŸŽ‰ look at you go. โœจ


๐Ÿ‘‘ METHODS ๐Ÿ‘‘

the entire menu of things you can do to your little silicon children. one bullet each, no skipping, we don't do that here. ๐Ÿ’…

๐ŸŒˆ fileError

returns IO::Handle->error() for the device handle since the last clearerr. that's it, that's the function. ๐Ÿคทโ€โ™€๏ธ

๐ŸŒˆ checkDevice

$self->checkDevice($register_address)

Check device. โœ…

๐ŸŒˆ selectDevice

$self->selectDevice($register_address)

Select device. ๐ŸŽฏ

๐ŸŒˆ writeQuick

$self->writeQuick($value)

This sends a single bit to the device, at the place of the Rd/Wr bit. one bit. a single crumb of information. obsessed. ๐Ÿ“

๐ŸŒˆ readByte

$self->readByte()

This reads a single byte from a device, without specifying a device register. Some devices are so simple that this interface is enough; for others, it is a shorthand if you want to read the same register as in the previous I2C command. lowkey efficient of her. โœจ

๐ŸŒˆ writeByte

$self->writeByte()

This operation is the reverse of readByte: it sends a single byte to a device. โฌ…๏ธโžก๏ธ

๐ŸŒˆ readByteData

$self->readByteData($register_address)

This reads a single byte from a device, from a designated register. The register is specified through the Comm byte. ๐Ÿ“ฎ

๐ŸŒˆ writeByteData

$self->writeByteData($register_address,$value)

This writes a single byte to a device, to a designated register. The register is specified through the Comm byte. This is the opposite of the Read Byte operation. ๐Ÿ”

๐ŸŒˆ readNBytes

$self->readNBytes($lowest_byte_address, $number_of_bytes);

Read together N bytes of Data in linear register order. i.e. to read from 0x28,0x29,0x2a:

$self->readNBytes(0x28,3);

look at that, three addresses in one breath. so efficient. ๐Ÿฆ‹

๐ŸŒˆ readWordData

$self->readWordData($register_address)

This operation is very like Read Byte; again, data is read from a device, from a designated register that is specified through the Comm byte. But this time, the data is a complete word (16 bits). glow up from one byte. ๐Ÿ’…

๐ŸŒˆ writeWordData

$self->writeWordData($register_address,$value)

This is the opposite of the Read Word operation. 16 bits of data is written to a device, to the designated register that is specified through the Comm byte. ๐Ÿซถ

๐ŸŒˆ processCall

$self->processCall($register_address,$value)

This command selects a device register (through the Comm byte), sends 16 bits of data to it, and reads 16 bits of data in return. it's a whole conversation. talk to your chip. โœจ

๐ŸŒˆ writeBlockData

$self->writeBlockData($register_address, $values)

Writes a maximum of 32 bytes in a single block to the i2c device. The supplied $values should be an array ref containing the bytes to be written. ๐Ÿงบ

The register address should be one that is at the beginning of a contiguous block of registers of equal length to the array of values passed.

โš ๏ธ Not adhering to this will almost certainly result in unexpected behaviour in the device. bestie I am BEGGING you to check your register block before you hit enter. ๐Ÿ˜ญ

๐ŸŒˆ readBlockData

$self->readBlockData($register_address, $numBytes)

Read $numBytes form the given register address, data is returned as array. (yes, "form". we keep the typos, we don't erase history. ๐Ÿ™ˆ)

The register address is often 0x00 or the value your device expects. ๐ŸŽฏ

Common usage with micro controllers that receive and send large amounts of data: they almost always needs a 'command' to be written to them then they send a response, e.g:

  1. send 'command' with writeBlockData, or writeByteData, for example 'get last telegram'
  2. read 'response' with readBlockData of size $numBytes, controller is sending the last telegram

a whole back-and-forth. texting your microcontroller. ๐Ÿ’ฌ

๐ŸŒˆ DEMOLISH

Destructor. ๐Ÿชฆ she's gone, she served, she left. ๐Ÿ’€


๐Ÿ’Ž CONSTANTS ๐Ÿ’Ž

these are the constants she gives you:

  • I2C_SLAVE
  • I2C_SLAVE_FORCE

that is genuinely all the original says about them, so that's all I'm telling you. no extra lore, no guessing. ๐Ÿซถ


๐Ÿงธ CREATING YOUR OWN CHIPSET DRIVERS ๐Ÿงธ

Writing your own chipset driver for your own i2c devices is quiet simple. (not me, that's the page. ๐Ÿ˜Œ) You just need to know the i2c address of your device and the registers that you need to read or write.

Example in the Device::I2C::ADV7611. go look, it's giving reference implementation. ๐Ÿ‘‘


๐Ÿ“š NOTES ๐Ÿ“š

Based on the Device::SMBus without Moo. On my device Moo based script started 5 second. ๐ŸŒ

The SMBus was defined by Intel in 1995. It carries clock, data, and instructions and is based on Philips' I2C serial bus protocol. Its clock frequency range is 10 kHz to 100 kHz. (PMBus extends this to 400 kHz.) Its voltage levels and timings are more strictly defined than those of I2C, but devices belonging to the two systems are often successfully mixed on the same bus. SMBus is used as an interconnect in several platform management standards including: ASF, DASH, IPMI.

history lesson AND hardware, we contain multitudes. ๐Ÿ’œ


๐ŸŒŸ USAGE ๐ŸŒŸ

  • This module provides a simplified object oriented interface to the libi2c-dev library for accessing electronic peripherals connected on the I2C bus. ๐Ÿฆ‹

๐Ÿ”ญ SEE ALSO ๐Ÿ”ญ

  • Device::SMBus
  • Device::I2C::ADV7611
  • IO::File
  • Fcntl

the friend group. all iconic. โœจ


๐Ÿ†˜ SUPPORT ๐Ÿ†˜

๐Ÿ› Bugs / Feature Requests ๐Ÿ›

Please report any bugs or feature requests through github at https://github.com/sv99/perl-device-i2c/issues. You will be notified automatically of any progress on your issue. ๐Ÿ‘€

๐Ÿ“ฆ Source Code ๐Ÿ“ฆ

This is open source software. The code repository is available for public review and contribution under the terms of the license.

https://github.com/sv99/perl-device-i2c

git clone git://github.com/sv99/perl-device-i2c.git

clone it, read it, contribute to it. we love a public repo. ๐Ÿซถ


โœ๏ธ AUTHOR โœ๏ธ

Slava Volkov <sv99@inbox.ru>


๐Ÿค CONTRIBUTOR ๐Ÿค

Slava Volkov <svolkov att cpan dott org>


This software is copyright (c) 2016 by Slava Volkov.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.


and that's the whole page bestie!! ๐ŸŽ€๐Ÿ’– this was perl v5.42.3, dated 2026-08-19, from Device::I2C(3pm) โ€” now go forth, talk to your i2c bus, and slay. periodt. ๐Ÿ‘‘โœจ๐Ÿ’…

๐Ÿ“ฆ where this page comes from