i2c communication between adafruit accelerometer breakout boards to BLE nano

Dependencies:   BLE_API i2c-serial-conflict nRF51822

Fork of corny2 by Zachary Newman

Files at this revision

API Documentation at this revision

Comitter:
nkosarek
Date:
Thu Apr 13 23:22:20 2017 +0000
Parent:
7:f16ce95a1668
Commit message:
working i2c code

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
wire.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r f16ce95a1668 -r 6c538756395a main.cpp
--- a/main.cpp	Tue Apr 11 22:13:51 2017 +0000
+++ b/main.cpp	Thu Apr 13 23:22:20 2017 +0000
@@ -68,11 +68,11 @@
     int err = Wire.write( (uint8_t)addr );
     Wire.write(pbuf, length);
     if (err != 0) {
-        pc.printf("error on write write! %d\n", err);
+        pc.printf("error on write write! %d\r\n", err);
     }
     uint8_t err8 = Wire.endTransmission();
     if (err8 != 0) {
-        pc.printf("error on write end transmission! %d\n", err8);
+        pc.printf("error on write end transmission! %d\r\n", err8);
     }
 }
 
@@ -81,16 +81,16 @@
     Wire.beginTransmission(i2cAddr);
     int err= Wire.write( (uint8_t)addr );
     if (err != 0) {
-        pc.printf("error on read write! %d\n", err);
+        pc.printf("error on read write! %d\r\n", err);
     }
     uint8_t err8 = Wire.endTransmission();
     if (err8 != 0) {
-        pc.printf("error on read end transmission! %d\n", err8);
+        pc.printf("error on read end transmission! %d\r\n", err8);
     }
        
     err8 = Wire.requestFrom(i2cAddr+1, length);
     if (err != 0) {
-        pc.printf("error on read request from! %d\n", err8);
+        pc.printf("error on read request from! %d\r\n", err8);
     }
     while( Wire.available() > 0 )
     {
@@ -123,8 +123,9 @@
 void setRange(uint8_t range, uint16_t i2cAddr) {
     uint8_t* val = new uint8_t[1];
     AT24C512_ReadBytes(REG_CTRL4, val, 1, i2cAddr);//get value from the register
-    val[0] &= ~(0b110000); //zero out lowest 4 bits
+    val[0] &= ~(0b110000); //zero out lowest 2 bits of top 4 bits
     val[0] |= (range << 4); // write in our new range
+    pc.printf("REG_CTRL4 after setRange: 0x%x\r\n", *val);
     AT24C512_WriteBytes(REG_CTRL4, val, 1, i2cAddr);
 }
 
@@ -136,9 +137,10 @@
     if (highRes) {
         final = setBit(val[0], 3, 1);
     } else {
-        final = setBit(val[0], 3, 1);
+        final = setBit(val[0], 3, 0);
     }
     val[0] = final;
+    pc.printf("REG_CTRL4 after setHiRes: 0x%x\r\n", *val);
     AT24C512_WriteBytes(REG_CTRL4, val, 1, i2cAddr);
 }
 
@@ -152,14 +154,17 @@
         final = setBit(current[0], axis, 0);
     }
     current[0] = final;
+    pc.printf("REG_CTRL1 after setAxisStatus: 0x%x\r\n", *current);
     AT24C512_WriteBytes(REG_CTRL1, current, 1, i2cAddr);
 }
 
 void setDataRate(uint8_t dataRate, uint16_t i2cAddr) {
     uint8_t* val = new uint8_t[1];
     AT24C512_ReadBytes(REG_CTRL1, val, 1, i2cAddr);
+    pc.printf("REG_CTRL1: 0x%x\r\n", *val);
     val[0] &= 0b1111; //mask off lower bits
     val[0] |= (dataRate << 4);
+    pc.printf("REG_CTRL1: 0x%x\r\n", *val);
     AT24C512_WriteBytes(REG_CTRL1, val, 1, i2cAddr);
 }
     
@@ -167,13 +172,15 @@
 {
     uint8_t* val = new uint8_t[1];
     AT24C512_ReadBytes(REG_CTRL4, val, 1, i2cAddr);//get value from the register
+    pc.printf("REG_CTRL4: 0x%x\r\n", *val);
     uint8_t final;
-    if (bdu == true) {
+    if (bdu) {
         final = setBit(val[0], 7, 1);
     } else {
-        final = setBit(val[0], 7, 1);
+        final = setBit(val[0], 7, 0);
     }
     val[0] = final;
+    pc.printf("REG_CTRL4 after setBDU: 0x%x\r\n", *val);
     AT24C512_WriteBytes(REG_CTRL4, val, 1, i2cAddr);
 }
 
@@ -198,42 +205,9 @@
     wait(5);
     //Wire.begin();
     Wire.begin(SCL, SDA, TWI_FREQUENCY_100K);
-    uint8_t address;
-    int error;
-    int nDevices;
-
-    pc.printf("Scanning...\n");
 
-    nDevices = 0;
-    for(address = 1; address < 127; address++ ) 
-    {
-        pc.printf("address: %x\n", address);
-    // The i2c_scanner uses the return value of
-    // the Write.endTransmisstion to see if
-    // a device did acknowledge to the address.
-        Wire.beginTransmission(address << 1);
-        error = Wire.endTransmission();
-        pc.printf("Error Code: %d\n", error);
-        if (error == 0)
-        {
-          pc.printf("I2C device found at address 0x%x\n", address);
-          nDevices++;
-        }
-        else if (error==4) 
-        {
-          pc.printf("Unknown error at address 0x%x\n", address);
-        }    
-    }
-    if (nDevices == 0)
-        pc.printf("No I2C devices found\n");
-    else {
-        pc.printf("%d I2C devices found\n", nDevices);
-    }
-    
-    uint8_t* whoami = new uint8_t[1];
-    AT24C512_ReadBytes(REG_WHOAMI, whoami, 1, ADDR_ONE);
-    pc.printf("Whoami: %d\r\n", whoami[1]);
-    
+    pc.printf("\r\n\r\n\r\nStarting...\r\n");
+
     wait(5);
     
     setAxisStatus(AXIS_X, true, ADDR_ONE);
@@ -247,16 +221,52 @@
     setAxisStatus(AXIS_X, true, ADDR_TWO);
     setAxisStatus(AXIS_Y, true, ADDR_TWO);
     setAxisStatus(AXIS_Z, true, ADDR_TWO);
-    setDataRate(DATARATE_400HZ, ADDR_ONE);
+    setDataRate(DATARATE_400HZ, ADDR_TWO);
     setHighResolution(true, ADDR_TWO);
     setBDU(true, ADDR_TWO);
     setRange(RANGE_2G, ADDR_TWO);
+
+    uint8_t* val = new uint8_t[1];
+    *val = 0x88;
+    AT24C512_WriteBytes(REG_CTRL4, val, 1, ADDR_ONE);
+    AT24C512_WriteBytes(REG_CTRL4, val, 1, ADDR_TWO);
+    AT24C512_ReadBytes(REG_CTRL4, val, 1, ADDR_ONE);
+    pc.printf("REG_CTRL4, should be 0x88: 0x%x\r\n", *val);
+
+    uint8_t* whoami = new uint8_t[1];
+    AT24C512_ReadBytes(REG_WHOAMI, whoami, 1, ADDR_ONE);
+    pc.printf("REG_WHOAMI should be 0x33: 0x%x\r\n", *whoami);
+    AT24C512_ReadBytes(REG_WHOAMI, whoami, 1, ADDR_TWO);
+    pc.printf("REG_WHOAMI should be 0x33: 0x%x\r\n", *whoami);
+    AT24C512_ReadBytes(0x1F, whoami, 1, ADDR_ONE);
+
+    /*
+    ble.init();
+    ble.onDisconnection(disconnectionCallback);
     
-    wait(0.1);
+    //pc.attach( uartCB , pc.RxIrq);
+    
+    // setup advertising 
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
+    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
+                                    (const uint8_t *)"LUMBERJACK", sizeof("LUMBERJACK") - 1);
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
+                                    (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
+    // 100ms; in multiples of 0.625ms. 
+    ble.setAdvertisingInterval(160);
+
+    ble.addService(uartService);
+    
+    ble.startAdvertising(); 
+    pc.printf("Advertising Start \r\n");
+    
+    uint16_t data = new uint16_t[6];
+    */
     
     while(1)
     {
-        pc.printf("Read data from AT24C512 \r\n");
+        pc.printf("Read data from AT24C512\r\n");
         uint16_t x1 = getX(ADDR_ONE);
         uint16_t y1 = getY(ADDR_ONE);
         uint16_t z1 = getZ(ADDR_ONE);
@@ -264,9 +274,20 @@
         uint16_t x2 = getX(ADDR_TWO);
         uint16_t y2 = getY(ADDR_TWO);
         uint16_t z2 = getZ(ADDR_TWO);
-        pc.printf("Accel one: x %d y %d z %d\r\n", x1, y1, z1);
-        pc.printf("Accel two: x %d y %d z %d\r\n", x2, y2, z2);
+        pc.printf("Accel one: x %d y %d z %d\r\n", (int16_t)x1, (int16_t)y1, (int16_t)z1);
+        pc.printf("Accel two: x %d y %d z %d\r\n", (int16_t)x2, (int16_t)y2, (int16_t)z2);
         pc.printf("\r\n");
+        /*
+        data[0] = x1;
+        data[1] = y1;
+        data[2] = z1;
+        data[3] = x2;
+        data[4] = y2;
+        data[5] = z2;
+        ble.gattServer().write(0x15, data, sizeof(data));
+        */
+
         wait(1);
     }
+
 }
diff -r f16ce95a1668 -r 6c538756395a wire.cpp
--- a/wire.cpp	Tue Apr 11 22:13:51 2017 +0000
+++ b/wire.cpp	Thu Apr 13 23:22:20 2017 +0000
@@ -149,7 +149,7 @@
 uint8_t TwoWire::twi_master_write(uint8_t *data, uint8_t data_length, uint8_t issue_stop_condition)
 {
     uint32_t timeout = MAX_TIMEOUT_LOOPS;
-    
+
     if(data_length == 0)
     {
         return 1;