john mayer
/
AK8963
Preliminary changes
Diff: ak8963.cpp
- Revision:
- 3:232745b180d9
- Parent:
- 2:9dff393e5e86
--- a/ak8963.cpp Fri Sep 04 23:39:10 2015 +0000 +++ b/ak8963.cpp Tue Oct 08 16:44:21 2019 +0000 @@ -50,6 +50,8 @@ #define LEN_ONE_BYTE 1 AK8963::AK8963(I2C *conn, SlaveAddress addr) { + + printf("Slave Address = %#02x\n", addr); slaveAddress = addr; connection = conn; getSensitivityAdjustment(&sensitivityAdjustment); @@ -74,13 +76,13 @@ AK8963::Status AK8963::read(char registerAddress, char *buf, int length) { // Writes a start address - if (connection->write((slaveAddress << 1), ®isterAddress, LEN_ONE_BYTE) != 0) { + if (connection->write((slaveAddress << 1 | 0x01), ®isterAddress, LEN_ONE_BYTE) != 0) { // I2C write failed. return AK8963::ERROR_I2C_WRITE; } // Reads register data - if (connection->read((slaveAddress << 1), buf, length) != 0) { + if (connection->read((slaveAddress << 1 ), buf, length) != 0) { // I2C read failed. return AK8963::ERROR_I2C_READ; } @@ -99,7 +101,7 @@ } // Writes data - if (connection->write((slaveAddress << 1), data, bufLength) != 0) { + if (connection->write((slaveAddress << 1 ), data, bufLength) != 0) { // I2C write failed. return AK8963::ERROR_I2C_WRITE; } @@ -112,16 +114,21 @@ // Gets the ST1 register value. char st1Value = 0; + + printf("ST! Register = %#02x\n", st1Value); if ((status=AK8963::read(AK8963_REG_ST1, &st1Value, LEN_ONE_BYTE)) != AK8963::SUCCESS) { // I2C read failed. return status; } + printf("ST! Register = %#02x\n", st1Value); + + // Sets a return status corresponds to the obtained value. if ((st1Value & AK8963_BIT_MASK_DRDY) > 0) { status = AK8963::DATA_READY; } else { - status = AK8963::NOT_DATA_READY; + status = AK8963::DATA_READY;// AK8963::NOT_DATA_READY; } return status; @@ -146,20 +153,96 @@ if (mode != AK8963::MODE_POWER_DOWN) { if ((status=AK8963::write(AK8963_REG_CNTL1, &buf, LEN_ONE_BYTE)) != AK8963::SUCCESS) { // I2C write failed. + + printf("FAIL to switch to power down"); + return status; + } + + printf("CNTL1 Register = %#02x\n", buf); + if ((status=AK8963::read(AK8963_REG_WIA, &buf, LEN_ONE_BYTE)) != AK8963::SUCCESS) { return status; } + printf("CNTL1 Register = %#02x\n", buf); + + + printf("starting waiting\n"); + wait(2); wait_us(AK8963_WAIT_POWER_DOWN_US); + printf("Ended Waiting\n"); + } + + buf = 0xFF; + if ((status=AK8963::read(AK8963_REG_CNTL1, &buf, LEN_ONE_BYTE)) != AK8963::SUCCESS) { + return status; + } + printf("CNTL1 Register = %#02x\n", buf); + + buf = 0xFF; + if ((status=AK8963::read(AK8963_REG_CNTL2, &buf, LEN_ONE_BYTE)) != AK8963::SUCCESS) { + return status; + } + printf("CNTL2 Register = %#02x\n", buf); + + + /* buf = 0x01; + if ((status=AK8963::write(AK8963_REG_CNTL2, &buf, LEN_ONE_BYTE)) != AK8963::SUCCESS) { + // I2C write failed. + printf ("Error writing"); + return status; + + }*/ // Switch to the specified mode. - buf = (mode | AK8963_CNTL1_OUTPUT_16BIT); + //buf = (mode | AK8963_CNTL1_OUTPUT_16BIT); + buf = 0x16; if ((status=AK8963::write(AK8963_REG_CNTL1, &buf, LEN_ONE_BYTE)) != AK8963::SUCCESS) { // I2C write failed. + printf ("Error writing to CNTL1"); return status; + } + + wait(2); + wait_us(AK8963_WAIT_POWER_DOWN_US); - wait_us(AK8963_WAIT_POWER_DOWN_US); + buf = 0xFF; + if ((status=AK8963::read(AK8963_REG_WIA, &buf, LEN_ONE_BYTE)) != AK8963::SUCCESS) { + return status; + } + printf("WHOAMI Register = %#02x\n", buf); + + buf = 0xFF; + if ((status=AK8963::read(AK8963_REG_INFO, &buf, LEN_ONE_BYTE)) != AK8963::SUCCESS) { + return status; + } + printf("INFO Register = %#02x\n", buf); + + buf = 0xFF; + if ((status=AK8963::read(AK8963_REG_ST1, &buf, LEN_ONE_BYTE)) != AK8963::SUCCESS) { + return status; + } + printf("ST1 Register = %#02x\n", buf); + + buf = 0xFF; + if ((status=AK8963::read(AK8963_REG_ST2, &buf, LEN_ONE_BYTE)) != AK8963::SUCCESS) { + return status; + } + printf("ST2 Register = %#02x\n", buf); + + buf = 0xFF; + if ((status=AK8963::read(AK8963_REG_CNTL1, &buf, LEN_ONE_BYTE)) != AK8963::SUCCESS) { + return status; + } + printf("CNTL1 Register = %#02x\n", buf); + + buf = 0xFF; + if ((status=AK8963::read(AK8963_REG_CNTL2, &buf, LEN_ONE_BYTE)) != AK8963::SUCCESS) { + return status; + } + printf("CNTL2 Register = %#02x\n", buf); + return AK8963::SUCCESS; }