AK8963

Dependents:   FreeIMU

Committer:
tyftyftyf
Date:
Wed Apr 18 20:24:00 2018 +0000
Revision:
2:51199a6440be
Parent:
1:f8ba8df44aab
change to I2C 1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tyftyftyf 0:ec59a31e784b 1 // I2Cdev library collection - AK8963 I2C device class header file
tyftyftyf 0:ec59a31e784b 2 // Based on AKM AK8963/B datasheet, 12/2009
tyftyftyf 0:ec59a31e784b 3 // 8/27/2011 by Jeff Rowberg <jeff@rowberg.net> modified by Merlin 6-14-14 for 8963
tyftyftyf 0:ec59a31e784b 4 // Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
tyftyftyf 0:ec59a31e784b 5 //
tyftyftyf 0:ec59a31e784b 6 // Changelog:
tyftyftyf 0:ec59a31e784b 7 // 2011-08-27 - initial release
tyftyftyf 0:ec59a31e784b 8 // 2014-06-14 - modified for AK8963 based on Kris Wieners MPU-9250 Sketch
tyftyftyf 0:ec59a31e784b 9 /* ============================================
tyftyftyf 0:ec59a31e784b 10 I2Cdev device library code is placed under the MIT license
tyftyftyf 0:ec59a31e784b 11 Copyright (c) 2011 Jeff Rowberg
tyftyftyf 0:ec59a31e784b 12
tyftyftyf 0:ec59a31e784b 13 Permission is hereby granted, free of charge, to any person obtaining a copy
tyftyftyf 0:ec59a31e784b 14 of this software and associated documentation files (the "Software"), to deal
tyftyftyf 0:ec59a31e784b 15 in the Software without restriction, including without limitation the rights
tyftyftyf 0:ec59a31e784b 16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
tyftyftyf 0:ec59a31e784b 17 copies of the Software, and to permit persons to whom the Software is
tyftyftyf 0:ec59a31e784b 18 furnished to do so, subject to the following conditions:
tyftyftyf 0:ec59a31e784b 19
tyftyftyf 0:ec59a31e784b 20 The above copyright notice and this permission notice shall be included in
tyftyftyf 0:ec59a31e784b 21 all copies or substantial portions of the Software.
tyftyftyf 0:ec59a31e784b 22
tyftyftyf 0:ec59a31e784b 23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
tyftyftyf 0:ec59a31e784b 24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
tyftyftyf 0:ec59a31e784b 25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
tyftyftyf 0:ec59a31e784b 26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
tyftyftyf 0:ec59a31e784b 27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
tyftyftyf 0:ec59a31e784b 28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
tyftyftyf 0:ec59a31e784b 29 THE SOFTWARE.
tyftyftyf 0:ec59a31e784b 30 ===============================================
tyftyftyf 0:ec59a31e784b 31 */
tyftyftyf 0:ec59a31e784b 32
tyftyftyf 0:ec59a31e784b 33 #ifndef _AK8963_H_
tyftyftyf 0:ec59a31e784b 34 #define _AK8963_H_
tyftyftyf 0:ec59a31e784b 35
tyftyftyf 1:f8ba8df44aab 36 #include "mbed.h"
tyftyftyf 1:f8ba8df44aab 37
tyftyftyf 0:ec59a31e784b 38 #ifndef I2C_SDA
tyftyftyf 2:51199a6440be 39 #define I2C_SDA p9
tyftyftyf 2:51199a6440be 40 #define I2C_SCL p10
tyftyftyf 0:ec59a31e784b 41 #endif
tyftyftyf 0:ec59a31e784b 42
tyftyftyf 1:f8ba8df44aab 43 #include "I2CdevAK8963.h"
tyftyftyf 0:ec59a31e784b 44
tyftyftyf 0:ec59a31e784b 45 #define AK8963_ADDRESS_00 0x0C
tyftyftyf 0:ec59a31e784b 46 #define AK8963_ADDRESS_01 0x0D
tyftyftyf 0:ec59a31e784b 47 #define AK8963_ADDRESS_10 0x0E // default for InvenSense MPU-6050 evaluation board
tyftyftyf 0:ec59a31e784b 48 #define AK8963_ADDRESS_11 0x0F
tyftyftyf 0:ec59a31e784b 49 #define AK8963_DEFAULT_ADDRESS AK8963_ADDRESS_00
tyftyftyf 0:ec59a31e784b 50
tyftyftyf 0:ec59a31e784b 51 #define AK8963_RA_WIA 0x00
tyftyftyf 0:ec59a31e784b 52 #define AK8963_RA_INFO 0x01
tyftyftyf 0:ec59a31e784b 53 #define AK8963_RA_ST1 0x02
tyftyftyf 0:ec59a31e784b 54 #define AK8963_RA_HXL 0x03
tyftyftyf 0:ec59a31e784b 55 #define AK8963_RA_HXH 0x04
tyftyftyf 0:ec59a31e784b 56 #define AK8963_RA_HYL 0x05
tyftyftyf 0:ec59a31e784b 57 #define AK8963_RA_HYH 0x06
tyftyftyf 0:ec59a31e784b 58 #define AK8963_RA_HZL 0x07
tyftyftyf 0:ec59a31e784b 59 #define AK8963_RA_HZH 0x08
tyftyftyf 0:ec59a31e784b 60 #define AK8963_RA_ST2 0x09
tyftyftyf 0:ec59a31e784b 61 #define AK8963_RA_CNTL1 0x0A
tyftyftyf 0:ec59a31e784b 62 #define AK8963_RA_CNTL2 0x0B
tyftyftyf 0:ec59a31e784b 63 #define AK8963_RA_ASTC 0x0C
tyftyftyf 0:ec59a31e784b 64 #define AK8963_RA_TS1 0x0D // SHIPMENT TEST, DO NOT USE
tyftyftyf 0:ec59a31e784b 65 #define AK8963_RA_TS2 0x0E // SHIPMENT TEST, DO NOT USE
tyftyftyf 0:ec59a31e784b 66 #define AK8963_RA_I2CDIS 0x0F
tyftyftyf 0:ec59a31e784b 67 #define AK8963_RA_ASAX 0x10
tyftyftyf 0:ec59a31e784b 68 #define AK8963_RA_ASAY 0x11
tyftyftyf 0:ec59a31e784b 69 #define AK8963_RA_ASAZ 0x12
tyftyftyf 0:ec59a31e784b 70
tyftyftyf 0:ec59a31e784b 71 #define AK8963_ST1_DRDY_BIT 0 //Data ready bit
tyftyftyf 0:ec59a31e784b 72 #define AK8963_ST1_DOR_BIT 1 //Data overrun bit
tyftyftyf 0:ec59a31e784b 73 #define AK8963_ST2_HOFL_BIT 3 //Mag sensor overflow
tyftyftyf 0:ec59a31e784b 74 //#define AK8963_ST2_DERR_BIT 2
tyftyftyf 0:ec59a31e784b 75 #define AK8963_ST2_BITM 3 //Output bit, 0 = 14 bit, 1 = 16 bit
tyftyftyf 0:ec59a31e784b 76
tyftyftyf 0:ec59a31e784b 77 #define AK8963_CNTL1_MODE_BIT 3
tyftyftyf 0:ec59a31e784b 78 #define AK8963_CNTL1_MODE_LENGTH 4
tyftyftyf 0:ec59a31e784b 79 #define AK8963_MODE_POWERDOWN 0x0
tyftyftyf 0:ec59a31e784b 80 #define AK8963_MODE_SINGLE 0x1
tyftyftyf 0:ec59a31e784b 81 #define AK8963_MODE_CONT1 0x2 //Continuous mode 1
tyftyftyf 0:ec59a31e784b 82 #define AK8963_MODE_CONT2 0x6 //Continuous mode 2
tyftyftyf 0:ec59a31e784b 83 #define AK8963_MODE_EXT 0x4 //External Trigger mode 1
tyftyftyf 0:ec59a31e784b 84 #define AK8963_MODE_SELFTEST 0x8
tyftyftyf 0:ec59a31e784b 85 #define AK8963_MODE_FUSEROM 0xF
tyftyftyf 0:ec59a31e784b 86
tyftyftyf 0:ec59a31e784b 87 #define AK8963_CNTL1_OUT_BIT 4
tyftyftyf 0:ec59a31e784b 88 #define AK8963_CNTL1_OUT_LENGTH 1 //Output bit, 0 = 14 bit, 1 = 16 bit
tyftyftyf 0:ec59a31e784b 89
tyftyftyf 0:ec59a31e784b 90 #define AK8963_CNTL2_RESET 0x01
tyftyftyf 0:ec59a31e784b 91
tyftyftyf 0:ec59a31e784b 92 #define AK8963_ASTC_SELF_BIT 6
tyftyftyf 0:ec59a31e784b 93
tyftyftyf 0:ec59a31e784b 94 #define AK8963_I2CDIS_BIT 0
tyftyftyf 0:ec59a31e784b 95
tyftyftyf 0:ec59a31e784b 96 class AK8963 {
tyftyftyf 0:ec59a31e784b 97 public:
tyftyftyf 0:ec59a31e784b 98 AK8963();
tyftyftyf 0:ec59a31e784b 99 AK8963(bool useSPI, uint8_t address);
tyftyftyf 0:ec59a31e784b 100
tyftyftyf 0:ec59a31e784b 101 void initialize();
tyftyftyf 0:ec59a31e784b 102 bool testConnection();
tyftyftyf 0:ec59a31e784b 103
tyftyftyf 0:ec59a31e784b 104 // WIA register
tyftyftyf 0:ec59a31e784b 105 uint8_t getDeviceID();
tyftyftyf 0:ec59a31e784b 106
tyftyftyf 0:ec59a31e784b 107 // INFO register
tyftyftyf 0:ec59a31e784b 108 uint8_t getInfo();
tyftyftyf 0:ec59a31e784b 109
tyftyftyf 0:ec59a31e784b 110 // ST1 register
tyftyftyf 0:ec59a31e784b 111 bool getDataReady();
tyftyftyf 0:ec59a31e784b 112 bool getDataOverRun();
tyftyftyf 0:ec59a31e784b 113
tyftyftyf 0:ec59a31e784b 114 // H* registers
tyftyftyf 0:ec59a31e784b 115 void getHeading(int16_t *x, int16_t *y, int16_t *z);
tyftyftyf 0:ec59a31e784b 116
tyftyftyf 0:ec59a31e784b 117 // ST2 register
tyftyftyf 0:ec59a31e784b 118 bool getOverflowStatus();
tyftyftyf 0:ec59a31e784b 119 bool getOutBitST();
tyftyftyf 0:ec59a31e784b 120
tyftyftyf 0:ec59a31e784b 121 // CNTL register
tyftyftyf 0:ec59a31e784b 122 uint8_t getMode();
tyftyftyf 0:ec59a31e784b 123 void setModeRes(uint8_t mode, uint8_t Mscale);
tyftyftyf 0:ec59a31e784b 124 void reset();
tyftyftyf 0:ec59a31e784b 125
tyftyftyf 0:ec59a31e784b 126 // ASTC register
tyftyftyf 0:ec59a31e784b 127 void setSelfTest(bool enabled);
tyftyftyf 0:ec59a31e784b 128 void getSelfTest(int16_t *x, int16_t *y, int16_t *z);
tyftyftyf 0:ec59a31e784b 129
tyftyftyf 0:ec59a31e784b 130 // I2CDIS
tyftyftyf 0:ec59a31e784b 131 void disableI2C(); // um, why...?
tyftyftyf 0:ec59a31e784b 132
tyftyftyf 0:ec59a31e784b 133 // ASA* registers
tyftyftyf 0:ec59a31e784b 134 void getAdjustment(uint8_t *x, uint8_t *y, uint8_t *z);
tyftyftyf 0:ec59a31e784b 135
tyftyftyf 0:ec59a31e784b 136
tyftyftyf 0:ec59a31e784b 137 private:
tyftyftyf 0:ec59a31e784b 138 bool bSPI;
tyftyftyf 0:ec59a31e784b 139 uint8_t devAddr;
tyftyftyf 0:ec59a31e784b 140 uint8_t buffer[14];
tyftyftyf 0:ec59a31e784b 141 uint8_t mode;
tyftyftyf 0:ec59a31e784b 142 uint8_t Mscale;
tyftyftyf 0:ec59a31e784b 143
tyftyftyf 1:f8ba8df44aab 144 AK8963I2C::I2Cdev i2Cdev;
tyftyftyf 0:ec59a31e784b 145 };
tyftyftyf 0:ec59a31e784b 146
tyftyftyf 0:ec59a31e784b 147 #endif /* _AK8963_H_ */