i2c communication between adafruit accelerometer breakout boards to BLE nano

Dependencies:   BLE_API i2c-serial-conflict nRF51822

Fork of corny2 by Zachary Newman

Committer:
nkosarek
Date:
Thu Apr 13 23:22:20 2017 +0000
Revision:
8:6c538756395a
Parent:
6:0a9f1dc921f1
working i2c code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
znew711 0:6a249a5be3a4 1 /*
znew711 0:6a249a5be3a4 2
znew711 0:6a249a5be3a4 3 Copyright (c) 2012-2014 RedBearLab
znew711 0:6a249a5be3a4 4
znew711 0:6a249a5be3a4 5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
znew711 0:6a249a5be3a4 6 and associated documentation files (the "Software"), to deal in the Software without restriction,
znew711 0:6a249a5be3a4 7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
znew711 0:6a249a5be3a4 8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
znew711 0:6a249a5be3a4 9 subject to the following conditions:
znew711 0:6a249a5be3a4 10 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
znew711 0:6a249a5be3a4 11
znew711 0:6a249a5be3a4 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
znew711 0:6a249a5be3a4 13 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
znew711 0:6a249a5be3a4 14 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
znew711 0:6a249a5be3a4 15 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
znew711 0:6a249a5be3a4 16 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
znew711 0:6a249a5be3a4 17
znew711 0:6a249a5be3a4 18 */
znew711 0:6a249a5be3a4 19
znew711 0:6a249a5be3a4 20 #include "mbed.h"
znew711 0:6a249a5be3a4 21 #include "wire.h"
znew711 0:6a249a5be3a4 22
znew711 0:6a249a5be3a4 23 #define BLE_Nano
znew711 0:6a249a5be3a4 24 //#define nRF_51822
znew711 0:6a249a5be3a4 25
znew711 0:6a249a5be3a4 26
znew711 0:6a249a5be3a4 27 #ifdef nRF_51822
znew711 0:6a249a5be3a4 28 #define SCL 28
znew711 0:6a249a5be3a4 29 #define SDA 29
znew711 0:6a249a5be3a4 30 #endif
znew711 0:6a249a5be3a4 31
znew711 0:6a249a5be3a4 32 #ifdef BLE_Nano
znew711 6:0a9f1dc921f1 33 #define SCL P0_8
znew711 6:0a9f1dc921f1 34 #define SDA P0_10
znew711 0:6a249a5be3a4 35 #endif
znew711 0:6a249a5be3a4 36
znew711 0:6a249a5be3a4 37 #define DEV_ADDR 0xA0
cpadua 3:3d08e2045bde 38 #define ADDR_ONE 0x30
cpadua 3:3d08e2045bde 39 #define ADDR_TWO 0x32
znew711 0:6a249a5be3a4 40 #define AXIS_X 0x00
znew711 0:6a249a5be3a4 41 #define AXIS_Y 0x01
znew711 0:6a249a5be3a4 42 #define AXIS_Z 0x02
znew711 0:6a249a5be3a4 43 #define REG_OUT_X_L 0x28
znew711 0:6a249a5be3a4 44 #define REG_CTRL1 0x20
znew711 0:6a249a5be3a4 45 #define REG_CTRL4 0x23
znew711 1:e2ba28405dd5 46 #define REG_WHOAMI 0x0F
znew711 0:6a249a5be3a4 47 #define RANGE_2G 0x00
znew711 1:e2ba28405dd5 48 #define DEVICE_ID 0x33
znew711 0:6a249a5be3a4 49
znew711 0:6a249a5be3a4 50
znew711 0:6a249a5be3a4 51 #define DATARATE_400HZ 0b0111 // 400Hz
znew711 0:6a249a5be3a4 52 #define DATARATE_200HZ 0b0110 // 200Hz
znew711 0:6a249a5be3a4 53 #define DATARATE_100HZ 0b0101 // 100Hz
znew711 0:6a249a5be3a4 54 #define DATARATE_50HZ 0b0100 // 50Hz
znew711 0:6a249a5be3a4 55 #define DATARATE_25HZ 0b0011 // 25Hz
znew711 0:6a249a5be3a4 56 #define DATARATE_10HZ 0b0010 // 10Hz
znew711 0:6a249a5be3a4 57 #define DATARATE_1HZ 0b0001 // 1Hz
znew711 0:6a249a5be3a4 58 #define DATARATE_POWERDOWN 0 // Power down
znew711 0:6a249a5be3a4 59 #define DATARATE_LOWPOWER_1K6HZ 0b1000 // Low power mode (1.6KHz)
znew711 0:6a249a5be3a4 60 #define DATARATE_LOWPOWER_5KHZ 0b1001 // Low power mode (5KHz) / Normal power mode (1.25KHz)
znew711 0:6a249a5be3a4 61
znew711 0:6a249a5be3a4 62 Serial pc(USBTX, USBRX);
znew711 0:6a249a5be3a4 63 TwoWire Wire = TwoWire(NRF_TWI0);
znew711 0:6a249a5be3a4 64
znew711 0:6a249a5be3a4 65 void AT24C512_WriteBytes(uint16_t addr, uint8_t *pbuf, uint16_t length, uint16_t i2cAddr)
znew711 0:6a249a5be3a4 66 {
znew711 0:6a249a5be3a4 67 Wire.beginTransmission(i2cAddr);
cpadua 3:3d08e2045bde 68 int err = Wire.write( (uint8_t)addr );
znew711 0:6a249a5be3a4 69 Wire.write(pbuf, length);
znew711 1:e2ba28405dd5 70 if (err != 0) {
nkosarek 8:6c538756395a 71 pc.printf("error on write write! %d\r\n", err);
znew711 1:e2ba28405dd5 72 }
znew711 1:e2ba28405dd5 73 uint8_t err8 = Wire.endTransmission();
znew711 1:e2ba28405dd5 74 if (err8 != 0) {
nkosarek 8:6c538756395a 75 pc.printf("error on write end transmission! %d\r\n", err8);
znew711 1:e2ba28405dd5 76 }
znew711 0:6a249a5be3a4 77 }
znew711 0:6a249a5be3a4 78
znew711 0:6a249a5be3a4 79 void AT24C512_ReadBytes(uint16_t addr, uint8_t *pbuf, uint16_t length, uint16_t i2cAddr)
znew711 0:6a249a5be3a4 80 {
znew711 0:6a249a5be3a4 81 Wire.beginTransmission(i2cAddr);
cpadua 3:3d08e2045bde 82 int err= Wire.write( (uint8_t)addr );
znew711 1:e2ba28405dd5 83 if (err != 0) {
nkosarek 8:6c538756395a 84 pc.printf("error on read write! %d\r\n", err);
znew711 1:e2ba28405dd5 85 }
znew711 1:e2ba28405dd5 86 uint8_t err8 = Wire.endTransmission();
znew711 1:e2ba28405dd5 87 if (err8 != 0) {
nkosarek 8:6c538756395a 88 pc.printf("error on read end transmission! %d\r\n", err8);
znew711 1:e2ba28405dd5 89 }
znew711 0:6a249a5be3a4 90
znew711 1:e2ba28405dd5 91 err8 = Wire.requestFrom(i2cAddr+1, length);
znew711 1:e2ba28405dd5 92 if (err != 0) {
nkosarek 8:6c538756395a 93 pc.printf("error on read request from! %d\r\n", err8);
znew711 1:e2ba28405dd5 94 }
znew711 0:6a249a5be3a4 95 while( Wire.available() > 0 )
znew711 0:6a249a5be3a4 96 {
znew711 0:6a249a5be3a4 97 *pbuf = Wire.read();
znew711 0:6a249a5be3a4 98 pbuf++;
znew711 0:6a249a5be3a4 99 }
znew711 0:6a249a5be3a4 100 }
znew711 0:6a249a5be3a4 101
znew711 0:6a249a5be3a4 102 //Set the bit at index 'bit' to 'value' on 'input' and return
znew711 0:6a249a5be3a4 103 uint8_t setBit(uint8_t input, uint8_t bit, uint8_t value) {
znew711 0:6a249a5be3a4 104 uint8_t mask = 1 << bit;
znew711 0:6a249a5be3a4 105 input &= ~mask;
znew711 0:6a249a5be3a4 106 if (value == 1) {
znew711 0:6a249a5be3a4 107 input |= mask;
znew711 0:6a249a5be3a4 108 }
znew711 0:6a249a5be3a4 109 return input;
znew711 0:6a249a5be3a4 110 }
znew711 0:6a249a5be3a4 111
znew711 0:6a249a5be3a4 112 uint16_t getAxis(uint16_t axis, uint16_t i2cAddr)
znew711 0:6a249a5be3a4 113 {
znew711 0:6a249a5be3a4 114 uint8_t base = REG_OUT_X_L + (2 * axis);
znew711 0:6a249a5be3a4 115 uint8_t* low = new uint8_t[1];
znew711 0:6a249a5be3a4 116 uint8_t* high = new uint8_t[1];
znew711 0:6a249a5be3a4 117 AT24C512_ReadBytes(base, low, 1, i2cAddr);
znew711 0:6a249a5be3a4 118 AT24C512_ReadBytes(base + 1, high, 1, i2cAddr);
znew711 0:6a249a5be3a4 119 uint16_t res = low[0] | (high[0] << 8);
znew711 0:6a249a5be3a4 120 return res;
znew711 0:6a249a5be3a4 121 }
znew711 0:6a249a5be3a4 122
znew711 0:6a249a5be3a4 123 void setRange(uint8_t range, uint16_t i2cAddr) {
znew711 0:6a249a5be3a4 124 uint8_t* val = new uint8_t[1];
znew711 0:6a249a5be3a4 125 AT24C512_ReadBytes(REG_CTRL4, val, 1, i2cAddr);//get value from the register
nkosarek 8:6c538756395a 126 val[0] &= ~(0b110000); //zero out lowest 2 bits of top 4 bits
znew711 0:6a249a5be3a4 127 val[0] |= (range << 4); // write in our new range
nkosarek 8:6c538756395a 128 pc.printf("REG_CTRL4 after setRange: 0x%x\r\n", *val);
znew711 0:6a249a5be3a4 129 AT24C512_WriteBytes(REG_CTRL4, val, 1, i2cAddr);
znew711 0:6a249a5be3a4 130 }
znew711 0:6a249a5be3a4 131
znew711 0:6a249a5be3a4 132 //Set whether we want to use high resolution or not
znew711 0:6a249a5be3a4 133 void setHighResolution(bool highRes, uint16_t i2cAddr) {
znew711 0:6a249a5be3a4 134 uint8_t* val = new uint8_t[1];
znew711 0:6a249a5be3a4 135 AT24C512_ReadBytes(REG_CTRL4, val, 1, i2cAddr);//get value from the register
znew711 0:6a249a5be3a4 136 uint8_t final;
znew711 0:6a249a5be3a4 137 if (highRes) {
znew711 0:6a249a5be3a4 138 final = setBit(val[0], 3, 1);
znew711 0:6a249a5be3a4 139 } else {
nkosarek 8:6c538756395a 140 final = setBit(val[0], 3, 0);
znew711 0:6a249a5be3a4 141 }
znew711 0:6a249a5be3a4 142 val[0] = final;
nkosarek 8:6c538756395a 143 pc.printf("REG_CTRL4 after setHiRes: 0x%x\r\n", *val);
znew711 0:6a249a5be3a4 144 AT24C512_WriteBytes(REG_CTRL4, val, 1, i2cAddr);
znew711 0:6a249a5be3a4 145 }
znew711 0:6a249a5be3a4 146
znew711 0:6a249a5be3a4 147 void setAxisStatus(uint8_t axis, bool enable, uint16_t i2cAddr) {
znew711 0:6a249a5be3a4 148 uint8_t* current = new uint8_t[1];
znew711 0:6a249a5be3a4 149 AT24C512_ReadBytes(REG_CTRL1, current, 1, i2cAddr);//get value from the register
znew711 0:6a249a5be3a4 150 uint8_t final;
znew711 0:6a249a5be3a4 151 if (enable == 1) {
znew711 0:6a249a5be3a4 152 final = setBit(current[0], axis, 1);
znew711 0:6a249a5be3a4 153 } else {
znew711 0:6a249a5be3a4 154 final = setBit(current[0], axis, 0);
znew711 0:6a249a5be3a4 155 }
znew711 0:6a249a5be3a4 156 current[0] = final;
nkosarek 8:6c538756395a 157 pc.printf("REG_CTRL1 after setAxisStatus: 0x%x\r\n", *current);
znew711 0:6a249a5be3a4 158 AT24C512_WriteBytes(REG_CTRL1, current, 1, i2cAddr);
znew711 0:6a249a5be3a4 159 }
znew711 0:6a249a5be3a4 160
znew711 0:6a249a5be3a4 161 void setDataRate(uint8_t dataRate, uint16_t i2cAddr) {
znew711 0:6a249a5be3a4 162 uint8_t* val = new uint8_t[1];
znew711 0:6a249a5be3a4 163 AT24C512_ReadBytes(REG_CTRL1, val, 1, i2cAddr);
nkosarek 8:6c538756395a 164 pc.printf("REG_CTRL1: 0x%x\r\n", *val);
znew711 0:6a249a5be3a4 165 val[0] &= 0b1111; //mask off lower bits
znew711 0:6a249a5be3a4 166 val[0] |= (dataRate << 4);
nkosarek 8:6c538756395a 167 pc.printf("REG_CTRL1: 0x%x\r\n", *val);
znew711 0:6a249a5be3a4 168 AT24C512_WriteBytes(REG_CTRL1, val, 1, i2cAddr);
znew711 0:6a249a5be3a4 169 }
znew711 0:6a249a5be3a4 170
znew711 0:6a249a5be3a4 171 void setBDU(bool bdu, uint16_t i2cAddr)
znew711 0:6a249a5be3a4 172 {
znew711 0:6a249a5be3a4 173 uint8_t* val = new uint8_t[1];
znew711 0:6a249a5be3a4 174 AT24C512_ReadBytes(REG_CTRL4, val, 1, i2cAddr);//get value from the register
nkosarek 8:6c538756395a 175 pc.printf("REG_CTRL4: 0x%x\r\n", *val);
znew711 0:6a249a5be3a4 176 uint8_t final;
nkosarek 8:6c538756395a 177 if (bdu) {
znew711 0:6a249a5be3a4 178 final = setBit(val[0], 7, 1);
znew711 0:6a249a5be3a4 179 } else {
nkosarek 8:6c538756395a 180 final = setBit(val[0], 7, 0);
znew711 0:6a249a5be3a4 181 }
znew711 0:6a249a5be3a4 182 val[0] = final;
nkosarek 8:6c538756395a 183 pc.printf("REG_CTRL4 after setBDU: 0x%x\r\n", *val);
znew711 0:6a249a5be3a4 184 AT24C512_WriteBytes(REG_CTRL4, val, 1, i2cAddr);
znew711 0:6a249a5be3a4 185 }
znew711 0:6a249a5be3a4 186
znew711 0:6a249a5be3a4 187 uint16_t getX(uint16_t i2cAddr)
znew711 0:6a249a5be3a4 188 {
znew711 0:6a249a5be3a4 189 return getAxis(AXIS_X, i2cAddr);
znew711 0:6a249a5be3a4 190 }
znew711 0:6a249a5be3a4 191
znew711 0:6a249a5be3a4 192 uint16_t getY(uint16_t i2cAddr)
znew711 0:6a249a5be3a4 193 {
znew711 0:6a249a5be3a4 194 return getAxis(AXIS_Y, i2cAddr);
znew711 0:6a249a5be3a4 195 }
znew711 0:6a249a5be3a4 196
znew711 0:6a249a5be3a4 197 uint16_t getZ(uint16_t i2cAddr)
znew711 0:6a249a5be3a4 198 {
znew711 0:6a249a5be3a4 199 return getAxis(AXIS_Z, i2cAddr);
znew711 0:6a249a5be3a4 200 }
znew711 0:6a249a5be3a4 201
znew711 0:6a249a5be3a4 202 int main(void)
znew711 0:6a249a5be3a4 203 {
znew711 0:6a249a5be3a4 204 pc.baud(9600);
znew711 0:6a249a5be3a4 205 wait(5);
znew711 0:6a249a5be3a4 206 //Wire.begin();
znew711 0:6a249a5be3a4 207 Wire.begin(SCL, SDA, TWI_FREQUENCY_100K);
cpadua 5:a52a03b6d13b 208
nkosarek 8:6c538756395a 209 pc.printf("\r\n\r\n\r\nStarting...\r\n");
nkosarek 8:6c538756395a 210
znew711 6:0a9f1dc921f1 211 wait(5);
znew711 1:e2ba28405dd5 212
znew711 0:6a249a5be3a4 213 setAxisStatus(AXIS_X, true, ADDR_ONE);
znew711 0:6a249a5be3a4 214 setAxisStatus(AXIS_Y, true, ADDR_ONE);
znew711 0:6a249a5be3a4 215 setAxisStatus(AXIS_Z, true, ADDR_ONE);
znew711 0:6a249a5be3a4 216 setDataRate(DATARATE_400HZ, ADDR_ONE);
znew711 0:6a249a5be3a4 217 setHighResolution(true, ADDR_ONE);
znew711 0:6a249a5be3a4 218 setBDU(true, ADDR_ONE);
znew711 0:6a249a5be3a4 219 setRange(RANGE_2G, ADDR_ONE);
znew711 0:6a249a5be3a4 220
znew711 0:6a249a5be3a4 221 setAxisStatus(AXIS_X, true, ADDR_TWO);
znew711 0:6a249a5be3a4 222 setAxisStatus(AXIS_Y, true, ADDR_TWO);
znew711 0:6a249a5be3a4 223 setAxisStatus(AXIS_Z, true, ADDR_TWO);
nkosarek 8:6c538756395a 224 setDataRate(DATARATE_400HZ, ADDR_TWO);
znew711 0:6a249a5be3a4 225 setHighResolution(true, ADDR_TWO);
znew711 0:6a249a5be3a4 226 setBDU(true, ADDR_TWO);
znew711 0:6a249a5be3a4 227 setRange(RANGE_2G, ADDR_TWO);
nkosarek 8:6c538756395a 228
nkosarek 8:6c538756395a 229 uint8_t* val = new uint8_t[1];
nkosarek 8:6c538756395a 230 *val = 0x88;
nkosarek 8:6c538756395a 231 AT24C512_WriteBytes(REG_CTRL4, val, 1, ADDR_ONE);
nkosarek 8:6c538756395a 232 AT24C512_WriteBytes(REG_CTRL4, val, 1, ADDR_TWO);
nkosarek 8:6c538756395a 233 AT24C512_ReadBytes(REG_CTRL4, val, 1, ADDR_ONE);
nkosarek 8:6c538756395a 234 pc.printf("REG_CTRL4, should be 0x88: 0x%x\r\n", *val);
nkosarek 8:6c538756395a 235
nkosarek 8:6c538756395a 236 uint8_t* whoami = new uint8_t[1];
nkosarek 8:6c538756395a 237 AT24C512_ReadBytes(REG_WHOAMI, whoami, 1, ADDR_ONE);
nkosarek 8:6c538756395a 238 pc.printf("REG_WHOAMI should be 0x33: 0x%x\r\n", *whoami);
nkosarek 8:6c538756395a 239 AT24C512_ReadBytes(REG_WHOAMI, whoami, 1, ADDR_TWO);
nkosarek 8:6c538756395a 240 pc.printf("REG_WHOAMI should be 0x33: 0x%x\r\n", *whoami);
nkosarek 8:6c538756395a 241 AT24C512_ReadBytes(0x1F, whoami, 1, ADDR_ONE);
nkosarek 8:6c538756395a 242
nkosarek 8:6c538756395a 243 /*
nkosarek 8:6c538756395a 244 ble.init();
nkosarek 8:6c538756395a 245 ble.onDisconnection(disconnectionCallback);
znew711 0:6a249a5be3a4 246
nkosarek 8:6c538756395a 247 //pc.attach( uartCB , pc.RxIrq);
nkosarek 8:6c538756395a 248
nkosarek 8:6c538756395a 249 // setup advertising
nkosarek 8:6c538756395a 250 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
nkosarek 8:6c538756395a 251 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
nkosarek 8:6c538756395a 252 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
nkosarek 8:6c538756395a 253 (const uint8_t *)"LUMBERJACK", sizeof("LUMBERJACK") - 1);
nkosarek 8:6c538756395a 254 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
nkosarek 8:6c538756395a 255 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
nkosarek 8:6c538756395a 256 // 100ms; in multiples of 0.625ms.
nkosarek 8:6c538756395a 257 ble.setAdvertisingInterval(160);
nkosarek 8:6c538756395a 258
nkosarek 8:6c538756395a 259 ble.addService(uartService);
nkosarek 8:6c538756395a 260
nkosarek 8:6c538756395a 261 ble.startAdvertising();
nkosarek 8:6c538756395a 262 pc.printf("Advertising Start \r\n");
nkosarek 8:6c538756395a 263
nkosarek 8:6c538756395a 264 uint16_t data = new uint16_t[6];
nkosarek 8:6c538756395a 265 */
znew711 0:6a249a5be3a4 266
znew711 0:6a249a5be3a4 267 while(1)
znew711 0:6a249a5be3a4 268 {
nkosarek 8:6c538756395a 269 pc.printf("Read data from AT24C512\r\n");
znew711 0:6a249a5be3a4 270 uint16_t x1 = getX(ADDR_ONE);
znew711 0:6a249a5be3a4 271 uint16_t y1 = getY(ADDR_ONE);
znew711 0:6a249a5be3a4 272 uint16_t z1 = getZ(ADDR_ONE);
znew711 0:6a249a5be3a4 273
znew711 0:6a249a5be3a4 274 uint16_t x2 = getX(ADDR_TWO);
znew711 0:6a249a5be3a4 275 uint16_t y2 = getY(ADDR_TWO);
znew711 0:6a249a5be3a4 276 uint16_t z2 = getZ(ADDR_TWO);
nkosarek 8:6c538756395a 277 pc.printf("Accel one: x %d y %d z %d\r\n", (int16_t)x1, (int16_t)y1, (int16_t)z1);
nkosarek 8:6c538756395a 278 pc.printf("Accel two: x %d y %d z %d\r\n", (int16_t)x2, (int16_t)y2, (int16_t)z2);
znew711 0:6a249a5be3a4 279 pc.printf("\r\n");
nkosarek 8:6c538756395a 280 /*
nkosarek 8:6c538756395a 281 data[0] = x1;
nkosarek 8:6c538756395a 282 data[1] = y1;
nkosarek 8:6c538756395a 283 data[2] = z1;
nkosarek 8:6c538756395a 284 data[3] = x2;
nkosarek 8:6c538756395a 285 data[4] = y2;
nkosarek 8:6c538756395a 286 data[5] = z2;
nkosarek 8:6c538756395a 287 ble.gattServer().write(0x15, data, sizeof(data));
nkosarek 8:6c538756395a 288 */
nkosarek 8:6c538756395a 289
znew711 0:6a249a5be3a4 290 wait(1);
znew711 0:6a249a5be3a4 291 }
nkosarek 8:6c538756395a 292
znew711 0:6a249a5be3a4 293 }