initial code for i2c communication with accelerometers

Dependencies:   BLE_API mbed-dev nRF51822

Fork of capstone_i2c by Zachary Newman

Committer:
cpadua
Date:
Tue Apr 11 20:39:41 2017 +0000
Revision:
5:a52a03b6d13b
Parent:
3:3d08e2045bde
fixed i2c-api.c

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 1:e2ba28405dd5 33 #define SCL 8
znew711 1:e2ba28405dd5 34 #define SDA 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) {
znew711 1:e2ba28405dd5 71 pc.printf("error on write write! %d\n", err);
znew711 1:e2ba28405dd5 72 }
znew711 1:e2ba28405dd5 73 uint8_t err8 = Wire.endTransmission();
znew711 1:e2ba28405dd5 74 if (err8 != 0) {
znew711 1:e2ba28405dd5 75 pc.printf("error on write end transmission! %d\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) {
znew711 1:e2ba28405dd5 84 pc.printf("error on read write! %d\n", err);
znew711 1:e2ba28405dd5 85 }
znew711 1:e2ba28405dd5 86 uint8_t err8 = Wire.endTransmission();
znew711 1:e2ba28405dd5 87 if (err8 != 0) {
znew711 1:e2ba28405dd5 88 pc.printf("error on read end transmission! %d\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) {
znew711 1:e2ba28405dd5 93 pc.printf("error on read request from! %d\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
znew711 0:6a249a5be3a4 126 val[0] &= ~(0b110000); //zero out lowest 4 bits
znew711 0:6a249a5be3a4 127 val[0] |= (range << 4); // write in our new range
znew711 0:6a249a5be3a4 128 AT24C512_WriteBytes(REG_CTRL4, val, 1, i2cAddr);
znew711 0:6a249a5be3a4 129 }
znew711 0:6a249a5be3a4 130
znew711 0:6a249a5be3a4 131 //Set whether we want to use high resolution or not
znew711 0:6a249a5be3a4 132 void setHighResolution(bool highRes, uint16_t i2cAddr) {
znew711 0:6a249a5be3a4 133 uint8_t* val = new uint8_t[1];
znew711 0:6a249a5be3a4 134 AT24C512_ReadBytes(REG_CTRL4, val, 1, i2cAddr);//get value from the register
znew711 0:6a249a5be3a4 135 uint8_t final;
znew711 0:6a249a5be3a4 136 if (highRes) {
znew711 0:6a249a5be3a4 137 final = setBit(val[0], 3, 1);
znew711 0:6a249a5be3a4 138 } else {
znew711 0:6a249a5be3a4 139 final = setBit(val[0], 3, 1);
znew711 0:6a249a5be3a4 140 }
znew711 0:6a249a5be3a4 141 val[0] = final;
znew711 0:6a249a5be3a4 142 AT24C512_WriteBytes(REG_CTRL4, val, 1, i2cAddr);
znew711 0:6a249a5be3a4 143 }
znew711 0:6a249a5be3a4 144
znew711 0:6a249a5be3a4 145 void setAxisStatus(uint8_t axis, bool enable, uint16_t i2cAddr) {
znew711 0:6a249a5be3a4 146 uint8_t* current = new uint8_t[1];
znew711 0:6a249a5be3a4 147 AT24C512_ReadBytes(REG_CTRL1, current, 1, i2cAddr);//get value from the register
znew711 0:6a249a5be3a4 148 uint8_t final;
znew711 0:6a249a5be3a4 149 if (enable == 1) {
znew711 0:6a249a5be3a4 150 final = setBit(current[0], axis, 1);
znew711 0:6a249a5be3a4 151 } else {
znew711 0:6a249a5be3a4 152 final = setBit(current[0], axis, 0);
znew711 0:6a249a5be3a4 153 }
znew711 0:6a249a5be3a4 154 current[0] = final;
znew711 0:6a249a5be3a4 155 AT24C512_WriteBytes(REG_CTRL1, current, 1, i2cAddr);
znew711 0:6a249a5be3a4 156 }
znew711 0:6a249a5be3a4 157
znew711 0:6a249a5be3a4 158 void setDataRate(uint8_t dataRate, uint16_t i2cAddr) {
znew711 0:6a249a5be3a4 159 uint8_t* val = new uint8_t[1];
znew711 0:6a249a5be3a4 160 AT24C512_ReadBytes(REG_CTRL1, val, 1, i2cAddr);
znew711 0:6a249a5be3a4 161 val[0] &= 0b1111; //mask off lower bits
znew711 0:6a249a5be3a4 162 val[0] |= (dataRate << 4);
znew711 0:6a249a5be3a4 163 AT24C512_WriteBytes(REG_CTRL1, val, 1, i2cAddr);
znew711 0:6a249a5be3a4 164 }
znew711 0:6a249a5be3a4 165
znew711 0:6a249a5be3a4 166 void setBDU(bool bdu, uint16_t i2cAddr)
znew711 0:6a249a5be3a4 167 {
znew711 0:6a249a5be3a4 168 uint8_t* val = new uint8_t[1];
znew711 0:6a249a5be3a4 169 AT24C512_ReadBytes(REG_CTRL4, val, 1, i2cAddr);//get value from the register
znew711 0:6a249a5be3a4 170 uint8_t final;
znew711 0:6a249a5be3a4 171 if (bdu == true) {
znew711 0:6a249a5be3a4 172 final = setBit(val[0], 7, 1);
znew711 0:6a249a5be3a4 173 } else {
znew711 0:6a249a5be3a4 174 final = setBit(val[0], 7, 1);
znew711 0:6a249a5be3a4 175 }
znew711 0:6a249a5be3a4 176 val[0] = final;
znew711 0:6a249a5be3a4 177 AT24C512_WriteBytes(REG_CTRL4, val, 1, i2cAddr);
znew711 0:6a249a5be3a4 178 }
znew711 0:6a249a5be3a4 179
znew711 0:6a249a5be3a4 180 uint16_t getX(uint16_t i2cAddr)
znew711 0:6a249a5be3a4 181 {
znew711 0:6a249a5be3a4 182 return getAxis(AXIS_X, i2cAddr);
znew711 0:6a249a5be3a4 183 }
znew711 0:6a249a5be3a4 184
znew711 0:6a249a5be3a4 185 uint16_t getY(uint16_t i2cAddr)
znew711 0:6a249a5be3a4 186 {
znew711 0:6a249a5be3a4 187 return getAxis(AXIS_Y, i2cAddr);
znew711 0:6a249a5be3a4 188 }
znew711 0:6a249a5be3a4 189
znew711 0:6a249a5be3a4 190 uint16_t getZ(uint16_t i2cAddr)
znew711 0:6a249a5be3a4 191 {
znew711 0:6a249a5be3a4 192 return getAxis(AXIS_Z, i2cAddr);
znew711 0:6a249a5be3a4 193 }
znew711 0:6a249a5be3a4 194
znew711 0:6a249a5be3a4 195 int main(void)
znew711 0:6a249a5be3a4 196 {
znew711 0:6a249a5be3a4 197 pc.baud(9600);
znew711 0:6a249a5be3a4 198 wait(5);
znew711 0:6a249a5be3a4 199 //Wire.begin();
znew711 0:6a249a5be3a4 200 Wire.begin(SCL, SDA, TWI_FREQUENCY_100K);
cpadua 5:a52a03b6d13b 201 uint8_t address;
cpadua 5:a52a03b6d13b 202 int error;
cpadua 5:a52a03b6d13b 203 int nDevices;
cpadua 5:a52a03b6d13b 204
cpadua 5:a52a03b6d13b 205 pc.printf("Scanning...\n");
cpadua 5:a52a03b6d13b 206
cpadua 5:a52a03b6d13b 207 nDevices = 0;
cpadua 5:a52a03b6d13b 208 for(address = 1; address < 127; address++ )
cpadua 5:a52a03b6d13b 209 {
cpadua 5:a52a03b6d13b 210 pc.printf("address: %x\n", address);
cpadua 5:a52a03b6d13b 211 // The i2c_scanner uses the return value of
cpadua 5:a52a03b6d13b 212 // the Write.endTransmisstion to see if
cpadua 5:a52a03b6d13b 213 // a device did acknowledge to the address.
cpadua 5:a52a03b6d13b 214 Wire.beginTransmission(address << 1);
cpadua 5:a52a03b6d13b 215 error = Wire.endTransmission();
cpadua 5:a52a03b6d13b 216 pc.printf("Error Code: %d\n", error);
cpadua 5:a52a03b6d13b 217 if (error == 0)
cpadua 5:a52a03b6d13b 218 {
cpadua 5:a52a03b6d13b 219 pc.printf("I2C device found at address 0x%x\n", address);
cpadua 5:a52a03b6d13b 220 nDevices++;
cpadua 2:2082f0f50590 221 }
cpadua 5:a52a03b6d13b 222 else if (error==4)
cpadua 5:a52a03b6d13b 223 {
cpadua 5:a52a03b6d13b 224 pc.printf("Unknown error at address 0x%x\n", address);
cpadua 5:a52a03b6d13b 225 }
cpadua 2:2082f0f50590 226 }
cpadua 5:a52a03b6d13b 227 if (nDevices == 0)
cpadua 2:2082f0f50590 228 pc.printf("No I2C devices found\n");
cpadua 5:a52a03b6d13b 229 else {
cpadua 5:a52a03b6d13b 230 pc.printf("%d I2C devices found\n", nDevices);
cpadua 5:a52a03b6d13b 231 }
cpadua 2:2082f0f50590 232
znew711 1:e2ba28405dd5 233 uint8_t* whoami = new uint8_t[1];
znew711 1:e2ba28405dd5 234 AT24C512_ReadBytes(REG_WHOAMI, whoami, 1, ADDR_ONE);
znew711 1:e2ba28405dd5 235 pc.printf("Whoami: %d\r\n", whoami[1]);
znew711 1:e2ba28405dd5 236
znew711 1:e2ba28405dd5 237 wait(15);
znew711 1:e2ba28405dd5 238
znew711 0:6a249a5be3a4 239 setAxisStatus(AXIS_X, true, ADDR_ONE);
znew711 0:6a249a5be3a4 240 setAxisStatus(AXIS_Y, true, ADDR_ONE);
znew711 0:6a249a5be3a4 241 setAxisStatus(AXIS_Z, true, ADDR_ONE);
znew711 0:6a249a5be3a4 242 setDataRate(DATARATE_400HZ, ADDR_ONE);
znew711 0:6a249a5be3a4 243 setHighResolution(true, ADDR_ONE);
znew711 0:6a249a5be3a4 244 setBDU(true, ADDR_ONE);
znew711 0:6a249a5be3a4 245 setRange(RANGE_2G, ADDR_ONE);
znew711 0:6a249a5be3a4 246
znew711 0:6a249a5be3a4 247 setAxisStatus(AXIS_X, true, ADDR_TWO);
znew711 0:6a249a5be3a4 248 setAxisStatus(AXIS_Y, true, ADDR_TWO);
znew711 0:6a249a5be3a4 249 setAxisStatus(AXIS_Z, true, ADDR_TWO);
znew711 0:6a249a5be3a4 250 setDataRate(DATARATE_400HZ, ADDR_ONE);
znew711 0:6a249a5be3a4 251 setHighResolution(true, ADDR_TWO);
znew711 0:6a249a5be3a4 252 setBDU(true, ADDR_TWO);
znew711 0:6a249a5be3a4 253 setRange(RANGE_2G, ADDR_TWO);
znew711 0:6a249a5be3a4 254
znew711 0:6a249a5be3a4 255 wait(0.1);
znew711 0:6a249a5be3a4 256
znew711 0:6a249a5be3a4 257 while(1)
znew711 0:6a249a5be3a4 258 {
znew711 0:6a249a5be3a4 259 pc.printf("Read data from AT24C512 \r\n");
znew711 0:6a249a5be3a4 260 uint16_t x1 = getX(ADDR_ONE);
znew711 0:6a249a5be3a4 261 uint16_t y1 = getY(ADDR_ONE);
znew711 0:6a249a5be3a4 262 uint16_t z1 = getZ(ADDR_ONE);
znew711 0:6a249a5be3a4 263
znew711 0:6a249a5be3a4 264 uint16_t x2 = getX(ADDR_TWO);
znew711 0:6a249a5be3a4 265 uint16_t y2 = getY(ADDR_TWO);
znew711 0:6a249a5be3a4 266 uint16_t z2 = getZ(ADDR_TWO);
znew711 0:6a249a5be3a4 267 pc.printf("Accel one: x %d y %d z %d\r\n", x1, y1, z1);
znew711 0:6a249a5be3a4 268 pc.printf("Accel two: x %d y %d z %d\r\n", x2, y2, z2);
znew711 0:6a249a5be3a4 269 pc.printf("\r\n");
znew711 0:6a249a5be3a4 270 wait(1);
znew711 0:6a249a5be3a4 271 }
znew711 0:6a249a5be3a4 272 }