manitou

maple hweeprom.pde

Sep 8th, 2012
465
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // maple's i2c lib
  2. //   run against i2c eeprom  24LC128   5ms write cycle,  400KHz max
  3. //  http://ww1.microchip.com/downloads/en/devicedoc/21191m.pdf
  4. //    write 1, read 1
  5. //  I2C2 sda,scl PB11,PB10  D30,D29  need pullups
  6.  
  7. /*
  8.   *  does maple wire lib enable pullups?    left in ground rail
  9.   *   grnd 1    5   3.3v       circle 1  left  notch at top
  10.   *   grnd 2    6   grnd or float
  11.   *   grnd 3    7 SCL  maple 29  4.7K pullup
  12.   *   grnd 4    8 SDA  maple 30  4.7K pullup
  13.   */
  14.  
  15. #include "i2c.h"
  16. #define I2CID (0xA0 >> 1)
  17.  
  18. void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {
  19.     int32 i,err1;
  20.     i2c_msg msgs[1];            // one command
  21.     uint8 msg_data[128];   
  22.  
  23.     msg_data[0] = ((int)(eeaddresspage >> 8)); // MSB
  24.     msg_data[1] = ((int)(eeaddresspage & 0xFF)); // LSB
  25.     for(i=0;i<length;i++) {
  26.         msg_data[i+2] = data[i];
  27.     }
  28.     msgs[0].addr = deviceaddress;
  29.     msgs[0].flags = 0;             // write
  30.     msgs[0].length = length + 2; //  address and data
  31.     msgs[0].data = msg_data;
  32.     err1=i2c_master_xfer(I2C2, msgs, 1,50);  // 1 command  50ms timeout
  33. }
  34.  
  35. void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte* data, byte length ) {
  36.     int32 err1;
  37.     i2c_msg msgs[2];            // addr command, read command
  38.     uint8 msg_data[2]; 
  39.  
  40.     msg_data[0] = ((int)(eeaddress >> 8)); // MSB
  41.     msg_data[1] = ((int)(eeaddress & 0xFF)); // LSB
  42.     msgs[0].addr = deviceaddress;
  43.     msgs[0].flags = 0;     // write addr
  44.     msgs[0].length = 2 ;
  45.     msgs[0].data = msg_data;
  46.    
  47.     msgs[1].addr = deviceaddress;
  48.     msgs[1].flags = I2C_MSG_READ;     // read
  49.     msgs[1].length = length ;
  50.     msgs[1].data = data;
  51.     err1=i2c_master_xfer(I2C2, msgs, 2,50);  // 2 commands  50ms timeout
  52.   if(err1 != 0) {
  53.     if (err1 == I2C_ERROR_PROTOCOL) {
  54.       SerialUSB.print("Protocol Error Occurred, ");
  55.     }
  56.     else if(err1 == I2C_ERROR_TIMEOUT) {
  57.       SerialUSB.print("Timeout Occurred, ");
  58.     }
  59.     SerialUSB.print("State currently: ");
  60.     SerialUSB.print(I2C2->state,HEX);
  61.     SerialUSB.print(", Error flags: ");
  62.     SerialUSB.println(I2C2->error_flags,HEX);
  63.     // Attempts to recover
  64.     //delayMicroseconds(5);
  65.     //i2c_bus_reset(I2C2);
  66.     //i2c_master_enable(I2C2, I2C_BUS_RESET);
  67.     i2c_disable(I2C2);i2c_master_enable(I2C2, 0);
  68.   }
  69.  
  70. }
  71.  
  72.  
  73. void setup(){
  74.     byte buff[64];
  75.     int i;
  76.     unsigned int t;
  77.  
  78.     i2c_master_enable(I2C2, 0);  // 100KHz   or I2C_FAST_MODE 400 khz
  79.     while(!SerialUSB.available() ) {
  80.         SerialUSB.println("hit a key");
  81.                 delay(1500);
  82.     }
  83.     SerialUSB.read();
  84.     for(i=0;i< sizeof(buff); i++) buff[i]=i+100;
  85.  
  86.     t=micros();
  87.     i2c_eeprom_write_page(I2CID, 128, buff, sizeof(buff));
  88.     t=micros()-t;
  89.     delay(10);          // time for write to finish, errors otherwise
  90.     SerialUSB.print("blk write ");
  91.     SerialUSB.println(t,DEC);
  92. }
  93.  
  94. void loop(){
  95.     unsigned int t;
  96.     byte buff[64];
  97.    
  98.     t=micros();
  99.     i2c_eeprom_read_buffer(I2CID, 0,buff,sizeof(buff));
  100.     t=micros()-t;
  101.     SerialUSB.print("blk read ");
  102.     SerialUSB.println(t,DEC);
  103.     SerialUSB.println((char)buff[1]);
  104.     SerialUSB.println((char)buff[2]);
  105.  
  106.     i2c_eeprom_read_buffer(I2CID, 128,buff,sizeof(buff));  
  107.     SerialUSB.println(buff[1],DEC);
  108.     SerialUSB.println(buff[63],DEC);
  109.     delay(2000);
  110. }
RAW Paste Data