group::I2C

This module implements an I2C multi-master or slave bus.

The following is an example of how to write the I2C in an OS environment:

#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include "mcu/mcu.h"

int access_i2c(){
     int fd;
     i2c_attr_t attr;
     i2c_reqattr_t req;
     u8 data[8];

     fd = open("/dev/i2c0", O_RDWR);
     if ( fd < 0 ){
          printf("Error opening peripheral (%d)\n", errno);
     } else {
     attr.bitrate = 4000000; //Use a 400KHz I2C clock frequency
          attr.pin_assign = 0; //Use GPIO configuration zero (see device specific documentation for details)
          if( ioctl(fd, I_SETATTR, &attr) < 0 ){
               printf("Failed to set peripheral configuration (%d)\n", errno);
               return -1;
     }

     //Now use ioctl to setup a transfer operation
     req.slave_addr = 0x3F; //This is the right-justified 7-bit or 10-bit slave address
     req.transfer = I2C_TRANSFER_NORMAL; //This is a "normal", see i2c_trasfer_mode_t for details
     ioctl(fd, I_I2C_SETUP, &req);

     //now read the I2C:  This will 1) Start 2) write the 8-bit ptr value 3) Restart 4) Read 8 bytes and 5) Stop
     lseek(fd, 0, SEEK_SET); //this is the ptr value
     if ( read(fd, data, 8) < 0 ){
          printf("Error Reading adc0 (%d)\n"< errno);
          return -1;
     }
     }
     close(fd);
     return 0;
}

Details

defineI2C_VERSION


defineI2C_IOC_IDENT_CHAR


defineI2C_TEN_BIT_ADDR_MASK


defineI2C_TEN_BIT_ADDR


defineI_I2C_GETVERSION


defineI_I2C_GETINFO


defineI_I2C_SETATTR


defineI_I2C_SETACTION


defineI_I2C_TOTAL


enum@18

  • I2C_ERROR_NONE No errors
  • I2C_ERROR_START Error while starting
  • I2C_ERROR_WRITE Error while writing
  • I2C_ERROR_ACK Ack Error (most common error for a mis-wired hardware)
  • I2C_ERROR_STOP Error while stopping
  • I2C_ERROR_MASTER_ACK The master could not create an ACK
  • I2C_ERROR_BUS_BUSY The Bus is busy (happens with multi-masters on bus)
  • I2C_ERROR_LONG_SLEW
  • I2C_ERROR_ARBITRATION_LOST Arbitration lost on multi-master bus
  • I2C_ERROR_TIMEOUT Timeout error
  • I2C_ERROR_OVERFLOW Overflow error

This enumeration lists the errors which may occur when running an I2C operation.


enumi2c_flag_t

  • I2C_FLAG_NONE
  • I2C_FLAG_SET_MASTER Operate as a master I2C bus
  • I2C_FLAG_SET_SLAVE Operate as a slave (ignored if master is set)
  • I2C_FLAG_IS_SLAVE_ACK_GENERAL_CALL If slave operation, ack general call
  • I2C_FLAG_IS_PULLUP Enable internal pullups if available (ignore otherwise)
  • I2C_FLAG_PREPARE_PTR_DATA This prepares the driver to write the ptr then read/write data
  • I2C_FLAG_IS_PTR_16 This tells the driver to write a 16-bit ptr then read/write data (use with I2C_FLAG_PREPARE_PTR_DATA or I2C_FLAG_PREPARE_PTR)
  • I2C_FLAG_PREPARE_PTR This will write the ptr value only without writing or reading any data.
  • I2C_FLAG_PREPARE_DATA This will read/write data without first writing the pointer information
  • I2C_FLAG_IS_SLAVE_ADDR0 If hardware supports multiple slave addrs, use the first slot (default)
  • I2C_FLAG_IS_SLAVE_ADDR1 If hardware supports multiple slave addrs, use the second slot
  • I2C_FLAG_IS_SLAVE_ADDR2 If hardware supports multiple slave addrs, use the third slot
  • I2C_FLAG_IS_SLAVE_ADDR3 If hardware supports multiple slave addrs, use the fourth slot
  • I2C_FLAG_IS_SLAVE_PTR_8 Use a 8-bit address pointer when accessing data (default)
  • I2C_FLAG_IS_SLAVE_PTR_16 Use a 16-bit address pointer when accessing data (set automatically is size > 255)
  • I2C_FLAG_RESET Reset the state of the I2C
  • I2C_FLAG_STRETCH_CLOCK
  • I2C_FLAG_IS_NO_STOP Don’t issue a stop condition when complete (use with I2C_FLAG_PREPARE_DATA)


date: “2019-06-27” layout: post title: struct::i2c_info_t katex: true

categories: struct

Methods

Details

public u32o_flags

Bitmask of supported flags


public u32o_events

Bitmask of supported events


public u32freq

Maximum supported bitrate


public u32err

The error of the most recent transaction


public u32resd



date: “2019-06-27” layout: post title: struct::i2c_pin_assignment_t katex: true

categories: struct

Methods

Details

publicmcu_pin_tsda

Data pin


publicmcu_pin_tscl

Clock pin



date: “2019-06-27” layout: post title: struct::i2c_attr_t katex: true

categories: struct

Methods

Details

public u32o_flags

Attribute flags


publici2c_pin_assignment_tpin_assignment

The pin assignment


public u32freq

The bit frequency


public i2c_slave_addr_tslave_addr

Slave address


public u32size

Memory size when setting up slave


public void *data

Memory pointer to slave data


public u32resd


X

Thanks for Coming!

Subscribe to news and updates