Rleg  2
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
Functions
Functions to Magnetometer HMC5883
Collaboration diagram for Functions to Magnetometer HMC5883:

Functions

uint8_t * mag_read_reg (int i2c_dev, uint8_t reg, uint8_t count)
 READ COUNT 8-BIT REGISTER IN SEQUENCE. More...
 
int mag_read_all_data (int i2c_dev, short int *data)
 READ ALL DATA AT ONCE (X, Y and Z) More...
 
short int mag_read_data (int i2c_dev, int axis)
 READ DATA (X, Y or Z) More...
 

Detailed Description

Function Documentation

int mag_read_all_data ( int  i2c_dev,
short int *  data 
)

READ ALL DATA AT ONCE (X, Y and Z)

Parameters
i2c_dev
[out]*data

Definition at line 121 of file mag_functions.c.

References FAILURE, MAG_DATAX1, mag_read_reg(), and SUCCESS.

Referenced by read_all_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 }
#define MAG_DATAX1
Definition: imu_regs.h:67
#define FAILURE
Definition: calibration.h:7
#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

Here is the call graph for this function:

Here is the caller graph for this function:

short int mag_read_data ( int  i2c_dev,
int  axis 
)

READ DATA (X, Y or Z)

Parameters
i2c_dev
axisset the axis ('X' or 'Y' or 'Z')

Definition at line 148 of file mag_functions.c.

References MAG_DATAX1, MAG_DATAY1, MAG_DATAZ1, and mag_read_reg().

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 }
#define MAG_DATAZ1
Definition: imu_regs.h:69
#define MAG_DATAX1
Definition: imu_regs.h:67
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_DATAY1
Definition: imu_regs.h:71

Here is the call graph for this function:

uint8_t* mag_read_reg ( int  i2c_dev,
uint8_t  reg,
uint8_t  count 
)

READ COUNT 8-BIT REGISTER IN SEQUENCE.

Parameters
i2c_dev
reg
countnumber of registers in sequence (1-13)

Definition at line 31 of file mag_functions.c.

Referenced by mag_read_all_data(), and mag_read_data().

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 }

Here is the caller graph for this function: