prevent modification of GPS position during dump
[protos/imu.git] / i2c_helper.c
1 #include <stdio.h>
2 #include <string.h>
3
4 #include <timer.h>
5
6 #include <aversive/wait.h>
7 #include <uart.h>
8 #include <i2c.h>
9
10
11
12
13 uint8_t read_reg(uint8_t address_dev, uint8_t address_reg, uint8_t * value)
14 {
15         uint8_t err = 0;
16         err = i2c_send(address_dev, &address_reg, 1, I2C_CTRL_SYNC);
17         if (err) {
18                 printf("read reg: i2c error send\r\n");
19                 return err;
20         }
21         err = i2c_recv(address_dev, 1, I2C_CTRL_SYNC);
22         if (err) {
23                 printf("read reg: i2c error recv\r\n");
24                 return err;
25         }
26         err = i2c_get_recv_buffer(value, 1);
27         if (err != 1) {
28                 printf("read reg: i2c error get recv\r\n");
29                 return 0xff;
30         }
31         return 0;
32
33 }
34
35
36 uint8_t read_reg_len(uint8_t address_dev, uint8_t address_reg, uint8_t * values, uint8_t len)
37 {
38         uint8_t err = 0;
39         err = i2c_send(address_dev, &address_reg, 1, I2C_CTRL_SYNC);
40         if (err) {
41                 printf("read reg len: i2c error send\r\n");
42                 return err;
43         }
44         err = i2c_recv(address_dev, len, I2C_CTRL_SYNC);
45         if (err) {
46                 printf("read reg len: i2c error recv\r\n");
47                 return err;
48         }
49         err = i2c_get_recv_buffer(values, len);
50         if (err != len) {
51                 printf("read reg len: i2c error get recv\r\n");
52                 return 0xFF;
53         }
54         return 0;
55
56 }