-
Notifications
You must be signed in to change notification settings - Fork 1
/
MLX90641_I2C_Driver.cpp
73 lines (55 loc) · 1.77 KB
/
MLX90641_I2C_Driver.cpp
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
/**
* @copyright (C) 2017 Melexis N.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <iostream>
#include <MLX90641_I2C_Driver.h>
#include <unistd.h>
#include <stdint.h>
#include <bcm2835.h>
int fd,init;
void MLX90641_I2CInit()
{
}
void MLX90641_I2CFreqSet(int freq)
{
}
int MLX90641_I2CRead(uint8_t slaveAddr, uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data)
{
if(!init){
bcm2835_init();
bcm2835_i2c_begin();
bcm2835_i2c_set_baudrate(400000);
init = 1;
}
int result;
char cmd[2] = {(char)(startAddress >> 8), (char)(startAddress & 0xFF)};
bcm2835_i2c_setSlaveAddress(slaveAddr);
char buf[1664];
uint16_t *p = data;
result = bcm2835_i2c_write_read_rs(cmd, 2, buf, nMemAddressRead*2);
for(int count = 0; count < nMemAddressRead; count++){
int i = count << 1;
*p++ = ((uint16_t)buf[i] << 8) | buf[i+1];
}
return 0;
}
int MLX90641_I2CWrite(uint8_t slaveAddr, uint16_t writeAddress, uint16_t data)
{
int result;
char cmd[4] = {(char)(writeAddress >> 8), (char)(writeAddress & 0x00FF), (char)(data >> 8), (char)(data & 0x00FF)};
result = bcm2835_i2c_write(cmd, 4);
return 0;
}