Guest User

Maple - i2c.h example

a guest
Oct 11th, 2011
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.61 KB | None | 0 0
  1. #include "Wire.h"
  2.  
  3. /* the c++ frontend libraries, dubbed Wirish, are always included in sketches.
  4. However, low level libraries, dubbed libmaple, must be included manually */
  5. #include "wirish.h"
  6. #include "i2c.h"
  7.  
  8. #define TIMEOUT 30
  9.  
  10. #define XL345_ADDR    0x53 // use the 7 bit I2C address of the xl345
  11. #define XL345_DEVID   0xE5
  12. #define ITG3200_ADDR  0x69
  13. #define ITG3200_DEVID 0x69
  14.  
  15. /* AD0 is tied to VCC  */
  16. #define ITG_3200_I2C_ADDRESS    0x69
  17.  
  18. #define ITG_3200_WHO_AM_I        0x0
  19. #define ITG_3200_SMPLRT_DIV     0x15
  20. #define ITG_3200_DLPF_FS        0x16
  21. #define ITG_3200_INT_CFG        0x17
  22. #define ITG_3200_INT_STATUS     0x1A
  23. #define ITG_3200_TEMP_OUT_H     0x1B
  24. #define ITG_3200_TEMP_OUT_L     0x1B
  25. #define ITG_3200_GYRO_XOUT_H    0x1D
  26. #define ITG_3200_GYRO_XOUT_L    0x1E
  27. #define ITG_3200_GYRO_YOUT_H    0x1F
  28. #define ITG_3200_GYRO_YOUT_L    0x20
  29. #define ITG_3200_GYRO_ZOUT_H    0x21
  30. #define ITG_3200_GYRO_ZOUT_L    0x22
  31. #define ITG_3200_PWR_MGM        0x3E
  32.  
  33. // ADXL Control Registers
  34. #define ADXLREG_BW_RATE      0x2C
  35. #define ADXLREG_POWER_CTL    0x2D
  36. #define ADXLREG_DATA_FORMAT  0x31
  37. #define ADXLREG_FIFO_CTL     0x38
  38. #define ADXLREG_BW_RATE      0x2C
  39. #define ADXLREG_TAP_AXES     0x2A
  40. #define ADXLREG_DUR          0x21
  41.  
  42. //ADXL Data Registers
  43. #define ADXLREG_DEVID        0x00
  44. #define ADXLREG_DATAX0       0x32
  45. #define ADXLREG_DATAX1       0x33
  46. #define ADXLREG_DATAY0       0x34
  47. #define ADXLREG_DATAY1       0x35
  48. #define ADXLREG_DATAZ0       0x36
  49. #define ADXLREG_DATAZ1       0x37
  50.  
  51. static uint8 itg3200_read(uint8 reg) {
  52.     Wire.beginTransmission(ITG3200_ADDR);
  53.     Wire.send(reg);
  54.     Wire.endTransmission();
  55.     Wire.requestFrom(ITG3200_ADDR, 1);
  56.  
  57.     int now = millis();
  58.     while (!Wire.available()) {
  59.       if (millis() - now > TIMEOUT) {
  60.         SerialUSB.println("TIMEOUT!");
  61.         return 0;
  62.       }
  63.     }
  64.     return Wire.receive();
  65. }
  66.  
  67. void itg3200_init_wire() {
  68.     Wire.beginTransmission(ITG3200_ADDR);
  69.     Wire.send(ITG_3200_WHO_AM_I);
  70.     Wire.endTransmission();
  71.     Wire.requestFrom(ITG3200_ADDR, 1);
  72.  
  73.     while (!Wire.available())
  74.         ;
  75.     uint8 id = Wire.receive();
  76.  
  77.     SerialUSB.print("Chip id: 0x");
  78.     SerialUSB.println(id, HEX);
  79. }
  80.  
  81. void writeADXL(uint8 reg, uint8 data) {
  82.   /* all i2c transactions send and receive arrays of i2c_msg objects */
  83.   i2c_msg msgs[1]; // we dont do any bursting here, so we only need one i2c_msg object
  84.   uint8 msg_data[2];
  85.  
  86.   msg_data = {reg,data};
  87.   msgs[0].addr = XL345_ADDR;
  88.   msgs[0].flags = 0; // write
  89.   msgs[0].length = 2;
  90.   msgs[0].data = msg_data;
  91.   i2c_master_xfer(I2C2, msgs, 1,0);
  92. }
  93.  
  94. void xl345_init() {
  95.   /* all i2c transactions send and receive arrays of i2c_msg objects */
  96.   i2c_msg msgs[1]; // we dont do any bursting here, so we only need one i2c_msg object
  97.   uint8 msg_data[2];
  98.  
  99.   /* first make sure everything is wired up properly, by reading the device ID
  100.     The device ID is located in address 0x00 and should always read back 0xE5
  101.    
  102.     To read this device ID, we must first WRITE "0x00" to the device to set the
  103.     address to read on the next transaction. Then we must read 1 byte.
  104.    */
  105.  
  106.   /* i2c_msg objects have fields:
  107.      addr - the 7 bit i2c address of the receiving device
  108.      flags - could be I2C_MSG_READ and/or I2C_MSG_10BIT_ADDR, or 0 for regular write
  109.      length - the number of bytes to transact
  110.      data - pointer to data array being xmitted/received
  111.    */
  112.   msg_data = {0x00,0x00};
  113.   msgs[0].addr = XL345_ADDR;
  114.   msgs[0].flags = 0;
  115.   msgs[0].length = 1; // just one byte for the address to read, 0x00
  116.   msgs[0].data = msg_data;
  117.   i2c_master_xfer(I2C2, msgs, 1,0);
  118.  
  119.   msgs[0].addr = XL345_ADDR;
  120.   msgs[0].flags = I2C_MSG_READ;
  121.   msgs[0].length = 1; // just one byte for the address to read, 0x00
  122.   msgs[0].data = msg_data;
  123.   i2c_master_xfer(I2C2, msgs, 1,0);
  124.  
  125.   /* now we check msg_data for our 0xE5 magic number */
  126.   uint8 dev_id = msg_data[0];
  127.   SerialUSB.print("Read device ID from xl345: ");
  128.   SerialUSB.println(dev_id,HEX);
  129.  
  130.   if (dev_id != XL345_DEVID) {
  131.     SerialUSB.println("Error, incorrect xl345 devid!");
  132.     SerialUSB.println("Halting program, hit reset...");
  133.     waitForButtonPress(0);
  134.   }
  135.  
  136.   /* to enable the xl345 into read mode we need to send three i2c messages
  137.     each messages represents a write into a particular configuration register
  138.     of the xl345.
  139.    
  140.       register 0x2C is "BW_RATE" register, which we set to 0x0A (
  141.       register 0x2D is the "POWER_CTRL" register, which we set to 0x08 (
  142.       register 0x31 is the "DATa_FORMAT" register, which we set to 0x04 (
  143.   */
  144.  
  145.   writeADXL(ADXLREG_POWER_CTL, 0x00);
  146.   writeADXL(ADXLREG_POWER_CTL, 0x08);
  147.  
  148. }
  149.  
  150. void itg3200_init() {
  151.   /* initialize the itg-3200 3 axis gyro. See xl345_init for more info on i2c */
  152.   i2c_msg msgs[1];
  153.   uint8 msg_data[2];
  154.  
  155.   /* verify that itg is properly connected by WHOAMI register (should read ITG3200_ADDR) */
  156.   msg_data = {0x00,0x00};
  157.   msgs[0].addr = ITG3200_ADDR;
  158.   msgs[0].flags = 0;
  159.   msgs[0].length = 1; // just one byte for the address to read, 0x00
  160.   msgs[0].data = msg_data;
  161.   i2c_master_xfer(I2C2, msgs, 1,0);
  162.  
  163.   msgs[0].flags = I2C_MSG_READ;
  164.   msgs[0].length = 1; // just one byte for the address to read, 0x00
  165.   msgs[0].data = msg_data;
  166.   i2c_master_xfer(I2C2, msgs, 1,0);
  167.  
  168.   /* now we check msg_data for our 0xE5 magic number */
  169.   uint8 dev_id = msg_data[0];
  170.   SerialUSB.print("Read device ID from itg3200: ");
  171.   SerialUSB.println(dev_id,HEX);
  172.  
  173.   if (dev_id != ITG3200_DEVID) {
  174.     SerialUSB.println("Error, incorrect itg3200 devid!");
  175.     SerialUSB.println("Halting program, hit reset...");
  176.     waitForButtonPress(0);
  177.   }
  178.  
  179. }
  180.  
  181. uint8 readITG(uint8 reg) {
  182.     i2c_msg msgs[1]; // we dont do any bursting here, so we only need one i2c_msg object
  183.   uint8 msg_data[1];
  184.  
  185.   msg_data = {0x00};
  186.   msg_data[0] = reg;
  187.  
  188.   msgs[0].addr = ITG3200_ADDR;
  189.   msgs[0].flags = 0;
  190.   msgs[0].length = 1; // just one byte for the address to read, 0x00
  191.   msgs[0].data = msg_data;
  192.   i2c_master_xfer(I2C2, msgs, 1,50);
  193.  
  194.   msgs[0].addr = ITG3200_ADDR;
  195.   msgs[0].flags = I2C_MSG_READ;
  196.   msgs[0].length = 1; // just one byte for the address to read, 0x00
  197.   msgs[0].data = msg_data;
  198.   i2c_master_xfer(I2C2, msgs, 1,50);
  199.  
  200.   return msg_data[0];
  201. }
  202.  
  203. int itg_read_x_w() {
  204.   return ((itg3200_read(ITG_3200_GYRO_XOUT_H) << 8) |
  205.          (itg3200_read(ITG_3200_GYRO_XOUT_L)));
  206. }
  207.  
  208. int itg_read_y_w() {
  209.   return ((itg3200_read(ITG_3200_GYRO_YOUT_H) << 8) |
  210.          (itg3200_read(ITG_3200_GYRO_YOUT_L)));
  211. }
  212.  
  213. int itg_read_z_w() {
  214.   return ((itg3200_read(ITG_3200_GYRO_ZOUT_H) << 8) |
  215.          (itg3200_read(ITG_3200_GYRO_ZOUT_L)));
  216. }
  217.  
  218. int itg_read_x() {
  219.   return ((readITG(ITG_3200_GYRO_XOUT_H) << 8) |
  220.          (readITG(ITG_3200_GYRO_XOUT_L)));
  221. }
  222. int itg_read_y() {
  223.   return ((readITG(ITG_3200_GYRO_YOUT_H) << 8) |
  224.          (readITG(ITG_3200_GYRO_YOUT_L)));
  225. }
  226. int itg_read_z() {
  227.   return ((readITG(ITG_3200_GYRO_ZOUT_H) << 8) |
  228.          (readITG(ITG_3200_GYRO_ZOUT_L)));
  229. }
  230.  
  231. void readADXL_burst(uint8 reg, uint8 len, uint8 *msg_data) {
  232.   i2c_msg msgs[1];
  233.   msg_data[0] = reg;
  234.  
  235.   msgs[0].addr = XL345_ADDR;
  236.   msgs[0].flags = 0;
  237.   msgs[0].length = 1; // just one byte for the address to read, 0x00
  238.   msgs[0].data = msg_data;
  239.   i2c_master_xfer(I2C2, msgs, 1,0);
  240.  
  241.   msgs[0].addr = XL345_ADDR;
  242.   msgs[0].flags = I2C_MSG_READ;
  243.   msgs[0].length = len; // just one byte for the address to read, 0x00
  244.   msgs[0].data = msg_data;
  245.   i2c_master_xfer(I2C2, msgs, 1,0);
  246. }
  247.  
  248. int xl345_read_axis(uint8 AXIS_REG) {
  249.   int axis = 0;
  250.   uint8 temp[2]={0};
  251.  
  252.   readADXL_burst(AXIS_REG,2,temp);
  253.  
  254.   axis=((temp[1]<<8)|temp[0]);
  255.   return axis;
  256. }
  257.  
  258. void setup() {
  259.   /* start the sketch when we hit the button, timeout=0 (wait forever) */
  260.   waitForButtonPress(0);
  261.   SerialUSB.println("Initializing sensors");
  262.  
  263.   /* configure I2C port 2 (pins 0, 1) with no special option flags (second argument)*/
  264.   i2c_master_enable(I2C2, 0);
  265.   //Wire.begin(0,1);
  266.   delay(100); // give time for the peripheral to come up
  267.  
  268.   xl345_init();  
  269.   itg3200_init();
  270.   //itg3200_init_wire();
  271.   waitForButtonPress();
  272. }
  273.  
  274. void loop() {
  275.   delay(250);
  276.   int x = itg_read_x();
  277.   int y = itg_read_y();
  278.   int z = itg_read_z();
  279.  
  280.   SerialUSB.print(x,HEX);
  281.   SerialUSB.print(", ");
  282.   SerialUSB.print(y,HEX);
  283.   SerialUSB.print(", ");
  284.   SerialUSB.print(z,HEX);
  285.  
  286.   SerialUSB.print("  ");
  287.  
  288.   x = xl345_read_axis(ADXLREG_DATAX0);
  289.   y = xl345_read_axis(ADXLREG_DATAY0);
  290.   z = xl345_read_axis(ADXLREG_DATAZ0);
  291.   SerialUSB.print(x,HEX);
  292.   SerialUSB.print(", ");
  293.   SerialUSB.print(y,HEX);
  294.   SerialUSB.print(", ");
  295.   SerialUSB.println(z,HEX);
  296. }
  297.  
  298.  
Advertisement
Add Comment
Please, Sign In to add comment