i2c-bus: Typed I2C communication

[ chip, hardware, i2c, library, mit, raspberrypi ] [ Propose Tags ] [ Report a vulnerability ]

Library for I2C communication with utilities for working with registers, etc.

Just import the module I2C, and you will typically get everything you need.

Features

  • Connections to chips on I2C buses.

  • An exception type I2CErr

  • A module I2C.Raw to perform direct write and read using Storable types.

  • A module I2C.Register to work with chips that use registers.

  • Types for binary serialization between host architecture and chip hardware.

  • Handy utilities through Template Haskell to define chips, registers and (sub)fields of registers.

  • Settings to control the code generation by Template Haskell.

  • An API specification for backend implementations. Currently, only a Linux backend is implemented.


[Skip to Readme]

Downloads

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0
Change log CHANGELOG.md
Dependencies base (>=4.20 && <4.23), data-default (>=0.8 && <0.9), relude (>=1.2 && <1.3), template-haskell (>=2.22 && <2.25), unix (>=2.8 && <2.9) [details]
License MIT
Copyright karamellpelle@hotmail.com
Author karamellpelle@hotmail.com
Maintainer karamellpelle@hotmail.com
Uploaded by unsafePerformIO at 2026-10-17T12:20:13Z
Revised Revision 1 made by unsafePerformIO at 2026-10-17T13:06:01Z
Category hardware, chip, i2c, raspberrypi
Home page https://github.com/karamellpelle/i2c-bus
Bug tracker https://github.com/karamellpelle/i2c-bus/issues
Source repo head: git clone https://github.com/karamellpelle/i2c-bus
Distributions
Downloads 3 total (3 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user [build log]
All reported builds failed as of 2026-10-17 [all 2 reports]

Readme for i2c-bus-0.1.0

[back to package description]

i2c-bus

Library for typed I2C communication with utilities for working with registers. Importing the module I2C will typically give you everything you need unless you need access to low level functionality provided by your backend.

Features

  • Connections to chips on I2C buses.
  • An exception type I2CErr.
  • A module I2C.Raw to perform direct write and read using Storable types.
  • A module I2C.Register to work with chips that use registers.
  • Types for binary serialization between host architecture and chip hardware.
  • Handy utilities through Template Haskell to define chips, registers and (sub)fields of registers.
  • Settings to control the code generation by Template Haskell.
  • An API specification for backend implementations. Currently, there is only a Linux backend implemented.
  • safe FFI imports so that the calls do not block Haskell concurrency.

Examples

{-# LANGUAGE TemplateHaskell #-}
import I2C

$(chip "PCF8575")

testPCF8575 :: IO ()
testPCF8575 = do
    chip <- openChip @PCF8575 "/dev/i2c-1" 0x20
    
    forM_ [0..0x00FF] $ \ix -> do
        rawwrite @Word16LE chip ix
        threadDelay 400000


$(chip "MYCHIP")

$(register8 ''MYCHIP 0x22 "MY8" 0x83)
-- ^ a register of type 'Word8' named MY8 at address 0x22 with default 
--   value 0x83. this generates a type 'MY8' and a value 
--   'regMY8 :: Register MYCHIP MY8'

$(field ''MY8   "A_FIELD"  "0000***0")
-- ^ MY8 contains a 3 bit (sub)field named A_FIELD. this generates a 
--   type 'A_FIELD' with get and set functions over the 'MY8' type.

$(field ''MY8   "A_BIT"    "00*00000")
-- ^ MY8 also has a one bit field named A_BIT. this generates a type
--   'A_BIT' with get and set functions over the 'MY8' type, and 
--   additional functions for bit manipulation (bitset, bitclear, bittoggle).

$(register ''MYCHIP 0x33 "MY_A" ''A)
-- ^ a register of type 'A' named MY_A at address 0x33. 
--   this generates a value 'regMY_A :: Register MYCHIP A'. the type
--   'A' must be an instance of 'Storable', and its implementation has to
--   be relative to the chip hardware.

See tests/GHCI.hs and tests/ssd1306 for more examples.

Example