Rleg  2
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
mag_functions.c
Go to the documentation of this file.
1 
8 #include"imu_functions.h"
9 
10 int mag_write_reg(int i2c_dev, uint8_t reg, uint8_t data)
11 {
12  uint8_t reg_data[2];
13 
14  reg_data[0] = reg;
15  reg_data[1] = data;
16 
17  if( MAG_MODE<reg )
18  {
19  //perror("Write unsucessful: Not a valid writable register");
20  return -1;
21  }
22 
23  if (write(i2c_dev, &reg_data, 2) != 2) {
24  //perror("Write unsuccessful");
25  return -1;
26  }
27 
28  return 1;
29 }
30 
31 uint8_t* mag_read_reg(int i2c_dev, uint8_t reg, uint8_t count)
32 {
33  uint8_t data[13];
34  int i;
35 
36  //data=(uint8_t*)malloc((count+1)*sizeof(data));
37 
38  data[0] = reg;
39 
40  if (write(i2c_dev, &data, 1) != 1) {
41  //perror("write before read");
42  return NULL;
43  }
44  data[0] = 0;
45  if (read(i2c_dev, &data, count) != count) {
46  //perror("read");
47  return NULL;
48  }
49 
50  return data;
51 }
52 
53 int mag_init(int i2c_dev, uint8_t rate, uint8_t range, uint8_t samples_avg, uint8_t meas_mode, uint8_t op_mode)
54 {
55  uint8_t data=0;
56 
57  switch(rate){
58  case 15:
59  data=0x10;
60  break;
61  case 0:
62  data=0x00;
63  break;
64  case 1:
65  data=0x04;
66  break;
67  case 3:
68  data=0x08;
69  break;
70  case 7:
71  data=0x0C;
72  break;
73  case 30:
74  data=0x14;
75  break;
76  case 75:
77  data=0x18;
78  break;
79  default:
80  perror("Wrong rate value");
81  break;
82  }
83 
84  switch(samples_avg){
85  case 8:
86  data=data|0x60;
87  break;
88  case 1:
89  break;
90  case 2:
91  data=data|0x20;
92  break;
93  case 4:
94  data=data|0x40;
95  break;
96  default:
97  perror("Wrong samples_avg value");
98  break;
99  }
100 
101  data+=meas_mode;
102 
103  if(mag_write_reg(i2c_dev, MAG_CONFIG_A, data)<0){
104  perror("Write in Configuration register A unsuccesful");
105  return -1;
106  }
107 
108  if(mag_write_reg(i2c_dev, MAG_CONFIG_B, range<<5)<0){
109  perror("Write in configuration register B unsuccesful");
110  return -1;
111  }
112 
113  if(mag_write_reg(i2c_dev, MAG_MODE, op_mode)<0){
114  perror("Write in mode register unsuccesful");
115  return -1;
116  }
117 
118  return 1;
119 }
120 
121 int mag_read_all_data(int i2c_dev, short int *data)
122 {
123  uint8_t i, j;
124  uint8_t *data8;
125  union result
126  {
127  unsigned short int usgnd[3];
128  short int sgnd[3];
129  } result;
130  //result=(union result*)malloc(3*sizeof(union result));
131  if( (data8=mag_read_reg(i2c_dev,MAG_DATAX1,6))==NULL)
132  {
133  //perror("Read accelerometer register failed");
134  return FAILURE;
135  }
136  //result.usgnd=(unsigned int*)malloc(3*sizeof(unsigned int));
137  j=0;
138  for(i=0; i<3; i++)
139  {
140  result.usgnd[j]=0;
141  result.usgnd[j]=result.usgnd[j]|((unsigned short int)data8[i*2+1])|(((unsigned int)data8[i*2])<<8);
142  data[j]=result.sgnd[j];
143  j=2-i; // trick used to change the sorting of the data from XZY to XYZ
144  }
145  return SUCCESS;
146 }
147 
148 short int mag_read_data(int i2c_dev, int axis)
149 {
150  uint8_t *data;
151  union result
152  {
153  unsigned int usgnd;
154  int sgnd;
155  }result;
156 
157  switch(axis){
158  case 'X':
159  data=mag_read_reg(i2c_dev,MAG_DATAX1,2);
160  break;
161  case 'Y':
162  data=mag_read_reg(i2c_dev,MAG_DATAY1,2);
163  break;
164  case 'Z':
165  data=mag_read_reg(i2c_dev,MAG_DATAZ1,2);
166  break;
167  default:
168  //perror("Wrong argument for axis in mag_read_data");
169  return -1;
170  }
171  result.usgnd=0;
172  result.usgnd=result.usgnd|(((unsigned short int)data[1]))|(((unsigned short int)data[0])<<8);
173  return result.sgnd;
174 }
int mag_init(int i2c_dev, uint8_t rate, uint8_t range, uint8_t samples_avg, uint8_t meas_mode, uint8_t op_mode)
INITIALIZE MAGNETOMETER.
Definition: mag_functions.c:53
#define MAG_DATAZ1
Definition: imu_regs.h:69
short int mag_read_data(int i2c_dev, int axis)
READ DATA (X, Y or Z)
#define MAG_CONFIG_A
Definition: imu_regs.h:64
int mag_write_reg(int i2c_dev, uint8_t reg, uint8_t data)
Rev 0 - 11/11/2012 RLEG project - 2012.
Definition: mag_functions.c:10
#define MAG_MODE
Definition: imu_regs.h:66
#define MAG_DATAX1
Definition: imu_regs.h:67
#define FAILURE
Definition: calibration.h:7
Rev 0 - 06/11/2012 RLEG project - 2012.
#define SUCCESS
Definition: calibration.h:6
uint8_t * mag_read_reg(int i2c_dev, uint8_t reg, uint8_t count)
READ COUNT 8-BIT REGISTER IN SEQUENCE.
Definition: mag_functions.c:31
#define MAG_CONFIG_B
Definition: imu_regs.h:65
int mag_read_all_data(int i2c_dev, short int *data)
READ ALL DATA AT ONCE (X, Y and Z)
#define MAG_DATAY1
Definition: imu_regs.h:71