code with new accelerometers

Dependencies:   BLE_API i2c-serial-conflict nRF51822

Fork of accel_to_blenano_i2c by Nicholas Kosarek

Revision:
1:e2ba28405dd5
Parent:
0:6a249a5be3a4
Child:
2:2082f0f50590
diff -r 6a249a5be3a4 -r e2ba28405dd5 main.cpp
--- a/main.cpp	Tue Apr 04 03:17:10 2017 +0000
+++ b/main.cpp	Mon Apr 10 02:19:13 2017 +0000
@@ -30,8 +30,8 @@
 #endif
 
 #ifdef BLE_Nano
-#define SCL         7  
-#define SDA         6
+#define SCL         8  
+#define SDA         10
 #endif
 
 #define DEV_ADDR    0xA0
@@ -43,7 +43,9 @@
 #define REG_OUT_X_L 0x28
 #define REG_CTRL1   0x20
 #define REG_CTRL4   0x23
+#define REG_WHOAMI  0x0F
 #define RANGE_2G    0x00
+#define DEVICE_ID   0x33
 
 
 #define DATARATE_400HZ          0b0111 // 400Hz 
@@ -63,20 +65,36 @@
 void AT24C512_WriteBytes(uint16_t addr, uint8_t *pbuf, uint16_t length, uint16_t i2cAddr)
 {
     Wire.beginTransmission(i2cAddr);
-    Wire.write( (uint8_t)addr>>8 );
+    int err = Wire.write( (uint8_t)addr>>8 );
     Wire.write( (uint8_t)addr );
     Wire.write(pbuf, length);
-    Wire.endTransmission();
+    if (err != 0) {
+        pc.printf("error on write write! %d\n", err);
+    }
+    uint8_t err8 = Wire.endTransmission();
+    if (err8 != 0) {
+        pc.printf("error on write end transmission! %d\n", err8);
+    }
 }
 
 void AT24C512_ReadBytes(uint16_t addr, uint8_t *pbuf, uint16_t length, uint16_t i2cAddr)
 {
     Wire.beginTransmission(i2cAddr);
-    Wire.write( (uint8_t)addr>>8 );
-    Wire.write( (uint8_t)addr );    
-    Wire.endTransmission();
+    int err = Wire.write( (uint8_t)addr>>8 );
+    Wire.write( (uint8_t)addr );
+    Wire.write(pbuf, length);
+    if (err != 0) {
+        pc.printf("error on read write! %d\n", err);
+    }
+    uint8_t err8 = Wire.endTransmission();
+    if (err8 != 0) {
+        pc.printf("error on read end transmission! %d\n", err8);
+    }
        
-    Wire.requestFrom(i2cAddr+1, length);
+    err8 = Wire.requestFrom(i2cAddr+1, length);
+    if (err != 0) {
+        pc.printf("error on read request from! %d\n", err8);
+    }
     while( Wire.available() > 0 )
     {
         *pbuf = Wire.read();
@@ -185,6 +203,12 @@
     Wire.begin(SCL, SDA, TWI_FREQUENCY_100K);
     pc.printf("IIC Demo Start \r\n");
     
+    uint8_t* whoami = new uint8_t[1];
+    AT24C512_ReadBytes(REG_WHOAMI, whoami, 1, ADDR_ONE);
+    pc.printf("Whoami: %d\r\n", whoami[1]);
+    
+    wait(15);
+    
     setAxisStatus(AXIS_X, true, ADDR_ONE);
     setAxisStatus(AXIS_Y, true, ADDR_ONE);
     setAxisStatus(AXIS_Z, true, ADDR_ONE);