Malexis 90640 sensor
Dependencies: mbed
MLX90640_I2C_Driver.cpp@0:99e98f131071, 2018-03-27 (annotated)
- Committer:
- withboobs
- Date:
- Tue Mar 27 13:34:11 2018 +0000
- Revision:
- 0:99e98f131071
- Child:
- 1:3f763d28c1be
nothing to say
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:99e98f131071 | 6 | I2C i2c(I2C_SDA, I2C_SCL); |
withboobs | 0:99e98f131071 | 7 | |
withboobs | 0:99e98f131071 | 8 | void MLX90640_I2CInit( void ) |
withboobs | 0:99e98f131071 | 9 | { |
withboobs | 0:99e98f131071 | 10 | i2c.stop(); |
withboobs | 0:99e98f131071 | 11 | } |
withboobs | 0:99e98f131071 | 12 | |
withboobs | 0:99e98f131071 | 13 | int MLX90640_I2CRead(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 | 0:99e98f131071 | 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 | 0:99e98f131071 | 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 | 0:99e98f131071 | 55 | void MLX90640_I2CFreqSet(int freq) |
withboobs | 0:99e98f131071 | 56 | { |
withboobs | 0:99e98f131071 | 57 | i2c.frequency(1000*freq); |
withboobs | 0:99e98f131071 | 58 | } |
withboobs | 0:99e98f131071 | 59 | |
withboobs | 0:99e98f131071 | 60 | int MLX90640_I2CWrite(uint8_t slaveAddr, unsigned int writeAddress, uint16_t data) |
withboobs | 0:99e98f131071 | 61 | { |
withboobs | 0:99e98f131071 | 62 | uint8_t sa; |
withboobs | 0:99e98f131071 | 63 | int ack = 0; |
withboobs | 0:99e98f131071 | 64 | char cmd[4] = {0,0,0,0}; |
withboobs | 0:99e98f131071 | 65 | static uint16_t dataCheck; |
withboobs | 0:99e98f131071 | 66 | |
withboobs | 0:99e98f131071 | 67 | |
withboobs | 0:99e98f131071 | 68 | sa = (slaveAddr << 1); |
withboobs | 0:99e98f131071 | 69 | cmd[0] = writeAddress >> 8; |
withboobs | 0:99e98f131071 | 70 | cmd[1] = writeAddress & 0x00FF; |
withboobs | 0:99e98f131071 | 71 | cmd[2] = data >> 8; |
withboobs | 0:99e98f131071 | 72 | cmd[3] = data & 0x00FF; |
withboobs | 0:99e98f131071 | 73 | |
withboobs | 0:99e98f131071 | 74 | i2c.stop(); |
withboobs | 0:99e98f131071 | 75 | wait_us(5); |
withboobs | 0:99e98f131071 | 76 | ack = i2c.write(sa, cmd, 4, 0); |
withboobs | 0:99e98f131071 | 77 | |
withboobs | 0:99e98f131071 | 78 | if (ack != 0x00) |
withboobs | 0:99e98f131071 | 79 | { |
withboobs | 0:99e98f131071 | 80 | return -1; |
withboobs | 0:99e98f131071 | 81 | } |
withboobs | 0:99e98f131071 | 82 | i2c.stop(); |
withboobs | 0:99e98f131071 | 83 | |
withboobs | 0:99e98f131071 | 84 | MLX90640_I2CRead(slaveAddr,writeAddress,1, &dataCheck); |
withboobs | 0:99e98f131071 | 85 | |
withboobs | 0:99e98f131071 | 86 | if ( dataCheck != data) |
withboobs | 0:99e98f131071 | 87 | { |
withboobs | 0:99e98f131071 | 88 | return -2; |
withboobs | 0:99e98f131071 | 89 | } |
withboobs | 0:99e98f131071 | 90 | |
withboobs | 0:99e98f131071 | 91 | return 0; |
withboobs | 0:99e98f131071 | 92 | } |
withboobs | 0:99e98f131071 | 93 |