Coder Social home page Coder Social logo

odolgy / stwi Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 1.0 20 KB

Cross-platform software TWI (I2C) driver for master device

License: MIT License

C 95.09% Makefile 4.91%
i2c i2c-master i2c-bus cross-platform gpio embedded mcu c-language peripheral-drivers two-wire

stwi's Introduction

STWI

STWI is a cross-platform software TWI (or I2C) driver for master device.

Why to use software driver?

The benefits of using hardware-based drivers are undeniable. But in some cases this is not possible:

  • There are no free pins controlled by the driver;
  • Peripheral device was connected to wrong pins due to schematic mistake.

But that doesn't mean you should stop experimenting and prototyping. Software driver based on GPIO pins is the solution you need.

Why to use STWI library?

There are some good platform-specific libraries like SoftI2CMaster for Arduino. But STWI is written in plain C and can be used on any platform. It is not the fastest solution, but you can easily run it on any MCU.

The code is covered with unit tests and the result is predictable. You can see the expected SCL and SDA oscillograms by cloning the repo and running tests (see "test/main.c" and "test/Makefile").

Supported features:

  • Clock stretching on bit level;
  • Low-level operations such as generating start and stop conditions, reading or writing one bit;
  • Complex read and write operations from 8-bit or 16-bit registers (as EEPROM requires) of devices with 7-bit address;
  • Only one master is supported.

How to use

  1. Configure SCL and SDA pins as Open-Drain pins with pull-up as bus specification requires.
  2. Connect STWI driver to the hardware platform by declaring these functions:
void write_scl(struct stwi const *bus, stwi_pin_state_t state)
{
    // Write SCL pin here
}

void write_sda(struct stwi const *bus, stwi_pin_state_t state)
{
    // Write SDA pin here
}

stwi_pin_state_t read_scl(struct stwi const *bus)
{
    // Read SCL pin here
}

stwi_pin_state_t read_sda(struct stwi const *bus)
{
    // Read SDA pin here
}

void timeout_start(struct stwi const *bus)
{
    // Start timeout for clock stretching or just do nothing
}

bool timeout_check(struct stwi const *bus)
{
    // Check timeout for clock stretching or just return false
}

void delay(struct stwi const *bus)
{
    // Wait TWI_CLOCK_FREQ / 4
}
  1. Declare stwi structure:
struct stwi const stwi = {
    .write_scl = write_scl,
    .write_sda = write_sda,
    .read_scl = read_scl,
    .read_sda = read_sda,
    .delay = delay,
    .timeout_start = timeout_start,
    .timeout_check = timeout_check,
};
  1. Communicate with peripheral devices using the functions in "stwi.h".

stwi's People

Contributors

odolgy avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.