Driver for AK7451 Angle Senor(SPI).
Fork of AK7451 by
Revision 3:1e0cfed43467, committed 2017-04-28
- Comitter:
- masahikofukasawa
- Date:
- Fri Apr 28 20:31:42 2017 +0000
- Parent:
- 2:b1079549c6a1
- Commit message:
- release RevD7.014. Add register access.
Changed in this revision
ak7451.cpp | Show annotated file Show diff for this revision Revisions of this file |
ak7451.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/ak7451.cpp Tue Nov 01 17:48:23 2016 +0000 +++ b/ak7451.cpp Fri Apr 28 20:31:42 2017 +0000 @@ -155,6 +155,15 @@ } /** + * Gets device operation mode. + * + * @return Returns OperationMode. + */ +AK7451::OperationMode AK7451::getOperationMode(){ + return AK7451::operationMode; +} + +/** * Reads angle data from the device. * * @param angle pointer to read angle data buffer @@ -168,7 +177,7 @@ // status check if( (command[0] & AK7451_READ_ANGLE_STATE_MD) != 0 ){ - AK7451::setOperationMode(AK7451::AK7451_NORMAL_MODE); +// AK7451::setOperationMode(AK7451::AK7451_NORMAL_MODE); return AK7451::ERROR_IN_USER_MODE; }else if( (command[0] & AK7451_READ_ANGLE_STATE_ER) == 0 ){ return AK7451::ERROR_ABNORMAL_STRENGTH; @@ -200,7 +209,9 @@ // perform measurement status = AK7451::write(AK7451_OPCODE_ANGLE_MEASURE_CODE, AK7451_REG_ANG, data); if(status != AK7451::SUCCESS) return status; - + + wait(0.01); + // angle read status = AK7451::readRegister(AK7451_REG_ANG, data); if(status != AK7451::SUCCESS) return status; @@ -215,7 +226,7 @@ // abnormal_state code read status = AK7451::readRegister(AK7451_REG_ERRMON, data); if(status != AK7451::SUCCESS) return status; - *abnormal_state = data[1]; + *abnormal_state = data[1]&0x03; return AK7451::SUCCESS; } @@ -226,11 +237,19 @@ * @return Returns SUCCESS when succeeded, otherwise returns another code. */ AK7451::Status AK7451::setAngleZero(){ - if(AK7451::operationMode != AK7451::AK7451_USER_MODE) return AK7451::ERROR_IN_NORMAL_MODE; + + AK7451::Status status = AK7451::SUCCESS; + char angle[2] = {0x00,0x00}; char density; char abnormal_state; - AK7451::Status status; + + // store the original mode + AK7451::OperationMode org = AK7451::operationMode; + + // set to user mode + status = AK7451::setOperationMode(AK7451::AK7451_USER_MODE); + if(status != AK7451::SUCCESS) return status; // initialize ZP register. Set ZP value to zero. status = AK7451::writeRegister(0x06,angle); @@ -243,6 +262,12 @@ // set read angle to ZP EEPROM status = AK7451::setAngleZero(angle); + if(status != AK7451::SUCCESS) return status; + + // back to the original mode + status = AK7451::setOperationMode(org); + if(status != AK7451::SUCCESS) return status; + return status; } @@ -254,7 +279,8 @@ * @return Returns SUCCESS when succeeded, otherwise returns another code. */ AK7451::Status AK7451::setAngleZero(const char *angle){ - if(AK7451::operationMode != AK7451::AK7451_USER_MODE) return AK7451::ERROR_IN_NORMAL_MODE; +// if(AK7451::operationMode != AK7451::AK7451_USER_MODE) return AK7451::ERROR_IN_NORMAL_MODE; + AK7451::writeRegister(AK7451_REG_ZP, angle); return AK7451::writeEEPROM(AK7451_REG_ZP, angle); } @@ -264,9 +290,10 @@ /** - * Reads data from device. - * @param buf buffer to store the read data - * @param length bytes to be read + * Reads data from device. + * @param operation_code OPCODE + * @param address memory/register addredd + * @param *data pointer to the read buffer. length=2 fixed. * @return Returns SUCCESS when succeeded, otherwise returns another. */ AK7451::Status AK7451::read(char operation_code, char address, char *data) { @@ -287,8 +314,9 @@ /** * Writes data to the device. - * @param data data to be written - * @param length of the data + * @param operation_code OPCODE + * @param address memory/register addredd + * @param *data pointer to the read buffer. length=2 fixed. * @return Returns SUCCESS when succeeded, otherwise returns another. */ AK7451::Status AK7451::write(char operation_code, char address, const char *data) {
--- a/ak7451.h Tue Nov 01 17:48:23 2016 +0000 +++ b/ak7451.h Fri Apr 28 20:31:42 2017 +0000 @@ -114,6 +114,13 @@ Status setOperationMode(OperationMode mode); /** + * Gets device operation mode. + * + * @return Returns OperationMode. + */ + OperationMode getOperationMode(); + + /** * Reads angle data from the device. * * @param angle pointer to read angle data buffer @@ -167,16 +174,18 @@ /** * Reads data from device. - * @param buf buffer to store the read data - * @param length bytes to be read + * @param operation_code OPCODE + * @param address memory/register addredd + * @param *data pointer to the read buffer. length=2 fixed. * @return Returns SUCCESS when succeeded, otherwise returns another. */ Status read(char operation_code, char address, char *data); /** * Writes data to the device. - * @param data data to be written - * @param length of the data + * @param operation_code OPCODE + * @param address memory/register addredd + * @param *data pointer to the read buffer. length=2 fixed. * @return Returns SUCCESS when succeeded, otherwise returns another. */ Status write(char operation_code, char address, const char *data);