Malexis 90640 sensor

Dependencies:   mbed

Committer:
withboobs
Date:
Thu Apr 19 18:23:43 2018 +0000
Revision:
1:3f763d28c1be
Parent:
0:99e98f131071
Child:
3:d2e64ef64905
nothing to say;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
withboobs 0:99e98f131071 1 #include "mbed.h"
withboobs 0:99e98f131071 2 #include "MLX90640_I2C_Driver.h"
withboobs 0:99e98f131071 3
withboobs 0:99e98f131071 4
withboobs 0:99e98f131071 5 //int * I2CReadMLX90640(int,int,int);
withboobs 1:3f763d28c1be 6 I2C i2c(PB_7, PB_6);
withboobs 0:99e98f131071 7
withboobs 1:3f763d28c1be 8 void MLX90640_I2CInit()
withboobs 1:3f763d28c1be 9 {
withboobs 0:99e98f131071 10 i2c.stop();
withboobs 0:99e98f131071 11 }
withboobs 0:99e98f131071 12
withboobs 1:3f763d28c1be 13 int MLX90640_I2CRead_working(uint8_t slaveAddr,unsigned int startAddress,unsigned int nWordsRead, uint16_t *data)
withboobs 0:99e98f131071 14 {
withboobs 0:99e98f131071 15 uint8_t sa;
withboobs 0:99e98f131071 16 int ack = 0;
withboobs 0:99e98f131071 17 int cnt = 0;
withboobs 0:99e98f131071 18 int i = 0;
withboobs 0:99e98f131071 19 char cmd[2] = {0,0};
withboobs 0:99e98f131071 20 char i2cData[1664] = {0};
withboobs 0:99e98f131071 21 uint16_t *p;
withboobs 0:99e98f131071 22
withboobs 0:99e98f131071 23 p = data;
withboobs 0:99e98f131071 24 sa = (slaveAddr << 1);
withboobs 0:99e98f131071 25 cmd[0] = startAddress >> 8;
withboobs 0:99e98f131071 26 cmd[1] = startAddress & 0x00FF;
withboobs 0:99e98f131071 27
withboobs 1:3f763d28c1be 28 //i2c.stop();
withboobs 0:99e98f131071 29 wait_us(5);
withboobs 0:99e98f131071 30 ack = i2c.write(sa, cmd, 2, 1);
withboobs 0:99e98f131071 31
withboobs 0:99e98f131071 32 if (ack != 0x00)
withboobs 0:99e98f131071 33 {
withboobs 0:99e98f131071 34 return -1;
withboobs 0:99e98f131071 35 }
withboobs 0:99e98f131071 36
withboobs 0:99e98f131071 37 sa = sa | 0x01;
withboobs 0:99e98f131071 38 ack = i2c.read(sa, i2cData, 2*nWordsRead, 0);
withboobs 0:99e98f131071 39
withboobs 0:99e98f131071 40 if (ack != 0x00)
withboobs 0:99e98f131071 41 {
withboobs 0:99e98f131071 42 return -1;
withboobs 0:99e98f131071 43 }
withboobs 1:3f763d28c1be 44 //i2c.stop();
withboobs 0:99e98f131071 45
withboobs 0:99e98f131071 46 for(cnt=0; cnt < nWordsRead; cnt++)
withboobs 0:99e98f131071 47 {
withboobs 0:99e98f131071 48 i = cnt << 1;
withboobs 0:99e98f131071 49 *p++ = (uint16_t)i2cData[i]*256 + (uint16_t)i2cData[i+1];
withboobs 0:99e98f131071 50 }
withboobs 0:99e98f131071 51
withboobs 0:99e98f131071 52 return 0;
withboobs 0:99e98f131071 53 }
withboobs 0:99e98f131071 54
withboobs 1:3f763d28c1be 55 int MLX90640_I2CRead(uint8_t slaveAddr, uint16_t startAddress, uint16_t nWordsRead, uint16_t *data)
withboobs 1:3f763d28c1be 56 {
withboobs 1:3f763d28c1be 57 uint8_t sa;
withboobs 1:3f763d28c1be 58 uint16_t ack = 0;
withboobs 1:3f763d28c1be 59 uint16_t cnt = 0;
withboobs 1:3f763d28c1be 60 uint16_t i = 0;
withboobs 1:3f763d28c1be 61 char cmd[2] = {0,0};
withboobs 1:3f763d28c1be 62 char * i2cData = (char *) data;
withboobs 1:3f763d28c1be 63 uint16_t *p;
withboobs 1:3f763d28c1be 64
withboobs 1:3f763d28c1be 65 p = data;
withboobs 1:3f763d28c1be 66 sa = (slaveAddr << 1);
withboobs 1:3f763d28c1be 67 cmd[0] = startAddress >> 8;
withboobs 1:3f763d28c1be 68 cmd[1] = startAddress & 0x00FF;
withboobs 1:3f763d28c1be 69
withboobs 1:3f763d28c1be 70 //i2c.stop();
withboobs 1:3f763d28c1be 71 wait_us(5);
withboobs 1:3f763d28c1be 72 ack = i2c.write(sa, cmd, 2, 1);
withboobs 1:3f763d28c1be 73
withboobs 1:3f763d28c1be 74 if (ack != 0x00)
withboobs 1:3f763d28c1be 75 {
withboobs 1:3f763d28c1be 76 return -1;
withboobs 1:3f763d28c1be 77 }
withboobs 1:3f763d28c1be 78
withboobs 1:3f763d28c1be 79 sa = sa | 0x01;
withboobs 1:3f763d28c1be 80 ack = i2c.read(sa, (char *)i2cData, nWordsRead<<1, 0);
withboobs 1:3f763d28c1be 81
withboobs 1:3f763d28c1be 82 if (ack != 0x00)
withboobs 1:3f763d28c1be 83 {
withboobs 1:3f763d28c1be 84 return -1;
withboobs 1:3f763d28c1be 85 }
withboobs 1:3f763d28c1be 86 //i2c.stop();
withboobs 1:3f763d28c1be 87
withboobs 1:3f763d28c1be 88 for(i=0; i<nWordsRead; i++)
withboobs 1:3f763d28c1be 89 {
withboobs 1:3f763d28c1be 90 p[i] = __REV16(p[i]);
withboobs 1:3f763d28c1be 91 }
withboobs 1:3f763d28c1be 92
withboobs 1:3f763d28c1be 93 return 0;
withboobs 1:3f763d28c1be 94 }
withboobs 1:3f763d28c1be 95
withboobs 0:99e98f131071 96 void MLX90640_I2CFreqSet(int freq)
withboobs 0:99e98f131071 97 {
withboobs 0:99e98f131071 98 i2c.frequency(1000*freq);
withboobs 0:99e98f131071 99 }
withboobs 0:99e98f131071 100
withboobs 0:99e98f131071 101 int MLX90640_I2CWrite(uint8_t slaveAddr, unsigned int writeAddress, uint16_t data)
withboobs 0:99e98f131071 102 {
withboobs 0:99e98f131071 103 uint8_t sa;
withboobs 0:99e98f131071 104 int ack = 0;
withboobs 0:99e98f131071 105 char cmd[4] = {0,0,0,0};
withboobs 0:99e98f131071 106 static uint16_t dataCheck;
withboobs 0:99e98f131071 107
withboobs 0:99e98f131071 108
withboobs 0:99e98f131071 109 sa = (slaveAddr << 1);
withboobs 0:99e98f131071 110 cmd[0] = writeAddress >> 8;
withboobs 0:99e98f131071 111 cmd[1] = writeAddress & 0x00FF;
withboobs 0:99e98f131071 112 cmd[2] = data >> 8;
withboobs 0:99e98f131071 113 cmd[3] = data & 0x00FF;
withboobs 0:99e98f131071 114
withboobs 1:3f763d28c1be 115 //i2c.stop();
withboobs 0:99e98f131071 116 wait_us(5);
withboobs 0:99e98f131071 117 ack = i2c.write(sa, cmd, 4, 0);
withboobs 0:99e98f131071 118
withboobs 0:99e98f131071 119 if (ack != 0x00)
withboobs 0:99e98f131071 120 {
withboobs 0:99e98f131071 121 return -1;
withboobs 0:99e98f131071 122 }
withboobs 1:3f763d28c1be 123 //i2c.stop();
withboobs 0:99e98f131071 124
withboobs 0:99e98f131071 125 MLX90640_I2CRead(slaveAddr,writeAddress,1, &dataCheck);
withboobs 0:99e98f131071 126
withboobs 0:99e98f131071 127 if ( dataCheck != data)
withboobs 0:99e98f131071 128 {
withboobs 0:99e98f131071 129 return -2;
withboobs 0:99e98f131071 130 }
withboobs 0:99e98f131071 131
withboobs 0:99e98f131071 132 return 0;
withboobs 0:99e98f131071 133 }
withboobs 0:99e98f131071 134
withboobs 1:3f763d28c1be 135