public
Last active

ev3 iic poc

  • Download Gist
iic.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include "lms2012.h"
//The ports are designated as PORT_NUMBER-1
const char PORT = 0x0;
const int MAX_SAMPLES = 100;
int main()
{
int file,dcmfile;
IIC *pIic;
IICDAT IicDat;
int i;
int idx;
//Open the device file
if((file = open(IIC_DEVICE_NAME, O_RDWR | O_SYNC)) == -1)
{
printf("Failed to open device\n");
return -1;
}
pIic = (IIC*)mmap(0, sizeof(IIC), PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, file, 0);
if (pIic == MAP_FAILED)
{
printf("Map failed\n");
return -1;
}
 
 
if ((dcmfile=open(DCM_DEVICE_NAME,O_RDWR | O_SYNC))==-1)
{
printf("Failed to open dcm device");
return -1;
}
DEVCON devcon;
devcon.Connection[PORT]=CONN_NONE;
devcon.Type[PORT]=0;
devcon.Mode[PORT]=0;
ioctl(file,IIC_SET_CONN, &devcon);
sleep(1);
devcon.Connection[PORT]=CONN_NXT_IIC;
devcon.Type[PORT]=TYPE_IIC_UNKNOWN;
devcon.Mode[PORT]=255;
ioctl(file, IIC_SET_CONN, &devcon);
sleep(1);
ioctl(file, IIC_SET_CONN, &devcon);
sleep(1);
 
DATA8 buf[4] = {'-','-','-','-'};
buf[PORT]='F';
write(dcmfile,buf,4);
 
 
// Setup I2C comunication
printf("Device is ready\n");
 
// setup mindsensor psp controller command
IicDat.Port = PORT;
IicDat.Time = 0;
IicDat.Repeat = 0;
IicDat.RdLng = -8;
IicDat.WrLng = 3;
// Set the device I2C address
IicDat.WrData[0] = 0x01;
// Specify the register that will be read (0x41 = command)
IicDat.WrData[1] = 0x41;
IicDat.WrData[2] = 0x49;
IicDat.Result=-1;
while (IicDat.Result)
{
ioctl(file,IIC_SETUP,&IicDat);
};
 
// read the button bits
int round;
for (round=0;round<100;round++)
{
IicDat.Port = PORT;
IicDat.Time = 0;
IicDat.Repeat = 0;
IicDat.RdLng = -8;
IicDat.WrLng = 2;
// Set the device I2C address
IicDat.WrData[0] = 0x01;
// Specify the register that will be read (0x42 = button bits map)
IicDat.WrData[1] = 0x42;
IicDat.Result=-1;
memset (IicDat.RdData,0x00,IIC_DATA_LENGTH);
while (IicDat.Result)
{
ioctl(file,IIC_SETUP,&IicDat);
};
 
printf("%02X\n",(unsigned)(unsigned char)IicDat.RdData[0]);
}
 
// Close the device file
printf("Clossing device\n");
close(dcmfile);
close(file);
return 0;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.