Site Tools


Sidebar

Translations of this page?:



TechWiki

GW60 Rollo

  • GW60 Superrollo
    • Hardware
    • Programming
    • S/W extension
  • TRADFRI Modul
    • Modul H/W
    • Programming
    • Tools
  • GW60 TRADFRI adapter
    • Schematic
    • Programming adapter
    • Housing
    • Integration

Laser Cutter

3D Printer

QuadCopter

RC car

Wiki

Misc

i2c

This is an old revision of the document!


I2C

On stout I2C interface is supported as device driver in Yocto.

/dev/i2c-0
/dev/i2c-1
/dev/i2c-2
/dev/i2c-7

Example program (part of retest)

i2c.h
/*
 * i2c.h
 *
 *  Created on: Jan 3, 2016
 *      Author: rcar
 */
 
#ifndef I2C_H_
#define I2C_H_
 
#include <stdint.h>
 
void i2cOpen(uint8_t devno);
void i2cClose();
void i2cSetAddress(uint8_t address);
uint8_t i2cReadData8(uint8_t reg);
int16_t i2cReadData16(uint8_t reg);
void i2cWriteData8(uint8_t reg, uint8_t value);
void i2cWriteData16(uint8_t reg, uint16_t value);
void i2cDetectDevice(void);
void i2cReadRCValues(void);
 
#endif /* I2C_H_ */
i2c.c
/*
 * i2c.c
 *
 *  Created on: Jan 3, 2016
 *      Author: rcar
 */
 
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
 
#include "i2c.h"
#include "mpu.h"
 
// I2C Linux device handle
int g_i2cFile;
 
//------------------------------------------------------------------------
// open the Linux device
void i2cOpen(uint8_t devno)
{
	char device[20];
	snprintf(device, sizeof device, "/dev/i2c-%d", devno);
	g_i2cFile = open(device, O_RDWR);
	if (g_i2cFile < 0) {
		perror("i2cOpen");
		exit(1);
	}
}
 
//------------------------------------------------------------------------
// close the Linux device
void i2cClose()
{
	close(g_i2cFile);
}
 
//------------------------------------------------------------------------
// set the I2C slave address for all subsequent I2C device transfers
void i2cSetAddress(uint8_t address)
{
	if (ioctl(g_i2cFile, I2C_SLAVE, address) < 0) {
		perror("i2cSetAddress");
		exit(1);
	}
}
 
//------------------------------------------------------------------------
// read 8-bit value from register reg
uint8_t i2cReadData8(uint8_t reg) {
 
	uint8_t  data;
 
	if (write(g_i2cFile, &reg, 1) != 1) {
		perror("i2cReadData8 set register");
		return 0;
	}
	if (read(g_i2cFile, &data, 1) != 1) {
		perror("i2cReadData8 read value");
		return 0;
	}
	return data;
}
 
//------------------------------------------------------------------------
// read a 16 bit value from a register pair
// read high byte of value from register reg,
// and low byte of value from register reg+1
int16_t i2cReadData16(uint8_t reg) {
 
	uint8_t  data[2];
 
	if (write(g_i2cFile, &reg, 1) != 1) {
		perror("i2cReadData8 set register");
		return 0;
	}
	if (read(g_i2cFile, data, 2) != 2) {
		perror("i2cReadData8 read value");
		return 0;
	}
	return data[1] | (data[0] << 8);
}
 
//------------------------------------------------------------------------
// write a 8 bit value to a register reg
void i2cWriteData8(uint8_t reg, uint8_t value)
{
	uint8_t data[2];
	data[0] = reg;
	data[1] = value;
	if (write(g_i2cFile, data, 2) != 2) {
		perror("i2cWriteData8");
	}
}
 
//------------------------------------------------------------------------
// write a 16 bit value to a register pair
// write high byte of value to register reg,
// and low byte of value to register reg+1
void i2cWriteData16(uint8_t reg, uint16_t value)
{
	uint8_t data[3];
	data[0] = reg;
	data[1] = (value >> 8) & 0xff;
	data[2] = value & 0xff;
	if (write(g_i2cFile, data, 3) != 3) {
		perror("i2cWriteData16");
	}
}
 
//------------------------------------------------------------------------
// try to detect both i2c devices
// MPU-6050 has a fixed ID of 0x68 at reg 0x75
// RC-Mixer has a fixed ID of 0x34 at reg 0x1F
void i2cDetectDevice(void) {
 
	uint16_t result;
 
	// MPU-6050
	i2cOpen(2);					// is at i2c-2
	i2cSetAddress(MPU6050_ADDRESS);		// address of MPU-6050
	result = i2cReadData8(MPU6050_RA_WHO_AM_I);
	printf("Check Addr:0x68 Reg:0x75 Data:%x", result);
	if (result == 0x68) printf("  => MPU-6050 found!\n");
		else printf("  => MPU-6050 missing!\n");
	i2cClose();
 
	// RC-Mixer
	i2cOpen(1);					// is at i2c-1
	i2cSetAddress(0x34);		// address of RC-Mixer
	result = i2cReadData8(31);
	printf("check Addr:0x34 Reg:0x1F Data:%x", result);
	if (result == 0x34) printf("  => RC-Mixer found!\n");
		else printf("  => RC-Mixer missing!\n");
	i2cClose();
}
 
//------------------------------------------------------------------------
// get RC values from RC-Mixer
void i2cReadRCValues(void) {
 
	uint16_t result;
	int i;
 
	// RC-Mixer
	i2cOpen(1);					// is at i2c-1
	i2cSetAddress(0x34);		// address of RC-Mixer
 
	printf("RC value from RC-Mixer\n");
	printf("CH1   CH2   CH3   CH4   CH5   PWM3  PWM4  STEER MOTOR\n");
	for(i=0;i<9;i++) {
		result = i2cReadData16(i*2);
		printf("%04d  ",result);
	}
	printf("\n");
	i2cClose();
}
i2c.1452461511.txt.gz · Last modified: 2016/01/10 21:31 (external edit)