Malexis 90640 sensor
Dependencies: mbed
MLX90640_I2C_Driver.cpp@4:b9ae79d80127, 2018-04-20 (annotated)
- Committer:
- withboobs
- Date:
- Fri Apr 20 17:26:01 2018 +0000
- Revision:
- 4:b9ae79d80127
- Parent:
- 3:d2e64ef64905
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 | 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 | 3:d2e64ef64905 | 13 | int MLX90640_I2CRead_working(uint8_t slaveAddr, uint16_t startAddress, uint16_t nWordsRead, uint16_t *data) |
withboobs | 0:99e98f131071 | 14 | { |
withboobs | 0:99e98f131071 | 15 | uint8_t sa; |
withboobs | 0:99e98f131071 | 16 | int cnt = 0; |
withboobs | 0:99e98f131071 | 17 | int i = 0; |
withboobs | 0:99e98f131071 | 18 | char cmd[2] = {0,0}; |
withboobs | 0:99e98f131071 | 19 | char i2cData[1664] = {0}; |
withboobs | 0:99e98f131071 | 20 | uint16_t *p; |
withboobs | 0:99e98f131071 | 21 | |
withboobs | 0:99e98f131071 | 22 | p = data; |
withboobs | 0:99e98f131071 | 23 | sa = (slaveAddr << 1); |
withboobs | 0:99e98f131071 | 24 | cmd[0] = startAddress >> 8; |
withboobs | 0:99e98f131071 | 25 | cmd[1] = startAddress & 0x00FF; |
withboobs | 0:99e98f131071 | 26 | |
withboobs | 0:99e98f131071 | 27 | wait_us(5); |
withboobs | 3:d2e64ef64905 | 28 | if (i2c.write(sa, cmd, 2, 1)) |
withboobs | 0:99e98f131071 | 29 | { |
withboobs | 0:99e98f131071 | 30 | return -1; |
withboobs | 0:99e98f131071 | 31 | } |
withboobs | 0:99e98f131071 | 32 | |
withboobs | 0:99e98f131071 | 33 | sa = sa | 0x01; |
withboobs | 3:d2e64ef64905 | 34 | if (i2c.read(sa, i2cData, 2*nWordsRead, 0)) |
withboobs | 0:99e98f131071 | 35 | { |
withboobs | 0:99e98f131071 | 36 | return -1; |
withboobs | 0:99e98f131071 | 37 | } |
withboobs | 1:3f763d28c1be | 38 | //i2c.stop(); |
withboobs | 0:99e98f131071 | 39 | |
withboobs | 0:99e98f131071 | 40 | for(cnt=0; cnt < nWordsRead; cnt++) |
withboobs | 0:99e98f131071 | 41 | { |
withboobs | 0:99e98f131071 | 42 | i = cnt << 1; |
withboobs | 0:99e98f131071 | 43 | *p++ = (uint16_t)i2cData[i]*256 + (uint16_t)i2cData[i+1]; |
withboobs | 0:99e98f131071 | 44 | } |
withboobs | 0:99e98f131071 | 45 | |
withboobs | 0:99e98f131071 | 46 | return 0; |
withboobs | 0:99e98f131071 | 47 | } |
withboobs | 0:99e98f131071 | 48 | |
withboobs | 1:3f763d28c1be | 49 | int MLX90640_I2CRead(uint8_t slaveAddr, uint16_t startAddress, uint16_t nWordsRead, uint16_t *data) |
withboobs | 1:3f763d28c1be | 50 | { |
withboobs | 1:3f763d28c1be | 51 | uint8_t sa; |
withboobs | 1:3f763d28c1be | 52 | uint16_t cnt = 0; |
withboobs | 3:d2e64ef64905 | 53 | char *i2cData = (char *) data; |
withboobs | 1:3f763d28c1be | 54 | |
withboobs | 1:3f763d28c1be | 55 | sa = (slaveAddr << 1); |
withboobs | 3:d2e64ef64905 | 56 | startAddress = __REV16(startAddress); |
withboobs | 1:3f763d28c1be | 57 | |
withboobs | 1:3f763d28c1be | 58 | wait_us(5); |
withboobs | 3:d2e64ef64905 | 59 | if (i2c.write(sa, (char *) &startAddress, 2, 1)) |
withboobs | 1:3f763d28c1be | 60 | { |
withboobs | 1:3f763d28c1be | 61 | return -1; |
withboobs | 1:3f763d28c1be | 62 | } |
withboobs | 1:3f763d28c1be | 63 | |
withboobs | 1:3f763d28c1be | 64 | sa = sa | 0x01; |
withboobs | 3:d2e64ef64905 | 65 | if (i2c.read(sa, i2cData, nWordsRead<<1, 0)) |
withboobs | 1:3f763d28c1be | 66 | { |
withboobs | 1:3f763d28c1be | 67 | return -1; |
withboobs | 1:3f763d28c1be | 68 | } |
withboobs | 1:3f763d28c1be | 69 | |
withboobs | 3:d2e64ef64905 | 70 | for(cnt=0; cnt < nWordsRead; cnt++) |
withboobs | 1:3f763d28c1be | 71 | { |
withboobs | 3:d2e64ef64905 | 72 | data[cnt] = __REV16(data[cnt]); |
withboobs | 1:3f763d28c1be | 73 | } |
withboobs | 1:3f763d28c1be | 74 | |
withboobs | 1:3f763d28c1be | 75 | return 0; |
withboobs | 1:3f763d28c1be | 76 | } |
withboobs | 1:3f763d28c1be | 77 | |
withboobs | 0:99e98f131071 | 78 | void MLX90640_I2CFreqSet(int freq) |
withboobs | 0:99e98f131071 | 79 | { |
withboobs | 0:99e98f131071 | 80 | i2c.frequency(1000*freq); |
withboobs | 0:99e98f131071 | 81 | } |
withboobs | 0:99e98f131071 | 82 | |
withboobs | 0:99e98f131071 | 83 | int MLX90640_I2CWrite(uint8_t slaveAddr, unsigned int writeAddress, uint16_t data) |
withboobs | 0:99e98f131071 | 84 | { |
withboobs | 0:99e98f131071 | 85 | uint8_t sa; |
withboobs | 0:99e98f131071 | 86 | int ack = 0; |
withboobs | 0:99e98f131071 | 87 | char cmd[4] = {0,0,0,0}; |
withboobs | 0:99e98f131071 | 88 | static uint16_t dataCheck; |
withboobs | 0:99e98f131071 | 89 | |
withboobs | 0:99e98f131071 | 90 | |
withboobs | 0:99e98f131071 | 91 | sa = (slaveAddr << 1); |
withboobs | 0:99e98f131071 | 92 | cmd[0] = writeAddress >> 8; |
withboobs | 0:99e98f131071 | 93 | cmd[1] = writeAddress & 0x00FF; |
withboobs | 0:99e98f131071 | 94 | cmd[2] = data >> 8; |
withboobs | 0:99e98f131071 | 95 | cmd[3] = data & 0x00FF; |
withboobs | 0:99e98f131071 | 96 | |
withboobs | 1:3f763d28c1be | 97 | //i2c.stop(); |
withboobs | 0:99e98f131071 | 98 | wait_us(5); |
withboobs | 0:99e98f131071 | 99 | ack = i2c.write(sa, cmd, 4, 0); |
withboobs | 0:99e98f131071 | 100 | |
withboobs | 0:99e98f131071 | 101 | if (ack != 0x00) |
withboobs | 0:99e98f131071 | 102 | { |
withboobs | 0:99e98f131071 | 103 | return -1; |
withboobs | 0:99e98f131071 | 104 | } |
withboobs | 1:3f763d28c1be | 105 | //i2c.stop(); |
withboobs | 0:99e98f131071 | 106 | |
withboobs | 0:99e98f131071 | 107 | MLX90640_I2CRead(slaveAddr,writeAddress,1, &dataCheck); |
withboobs | 0:99e98f131071 | 108 | |
withboobs | 0:99e98f131071 | 109 | if ( dataCheck != data) |
withboobs | 0:99e98f131071 | 110 | { |
withboobs | 0:99e98f131071 | 111 | return -2; |
withboobs | 0:99e98f131071 | 112 | } |
withboobs | 0:99e98f131071 | 113 | |
withboobs | 0:99e98f131071 | 114 | return 0; |
withboobs | 0:99e98f131071 | 115 | } |
withboobs | 0:99e98f131071 | 116 | |
withboobs | 1:3f763d28c1be | 117 |