AK8963

Dependents:   FreeIMU

Committer:
tyftyftyf
Date:
Wed Mar 28 20:25:57 2018 +0000
Revision:
0:ec59a31e784b
Child:
1:f8ba8df44aab
wip

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 0:ec59a31e784b 36 #ifndef I2C_SDA
tyftyftyf 0:ec59a31e784b 37 #define I2C_SDA p28
tyftyftyf 0:ec59a31e784b 38 #define I2C_SCL p27
tyftyftyf 0:ec59a31e784b 39 #endif
tyftyftyf 0:ec59a31e784b 40
tyftyftyf 0:ec59a31e784b 41 #include "I2Cdev.h"
tyftyftyf 0:ec59a31e784b 42
tyftyftyf 0:ec59a31e784b 43 #define AK8963_ADDRESS_00 0x0C
tyftyftyf 0:ec59a31e784b 44 #define AK8963_ADDRESS_01 0x0D
tyftyftyf 0:ec59a31e784b 45 #define AK8963_ADDRESS_10 0x0E // default for InvenSense MPU-6050 evaluation board
tyftyftyf 0:ec59a31e784b 46 #define AK8963_ADDRESS_11 0x0F
tyftyftyf 0:ec59a31e784b 47 #define AK8963_DEFAULT_ADDRESS AK8963_ADDRESS_00
tyftyftyf 0:ec59a31e784b 48
tyftyftyf 0:ec59a31e784b 49 #define AK8963_RA_WIA 0x00
tyftyftyf 0:ec59a31e784b 50 #define AK8963_RA_INFO 0x01
tyftyftyf 0:ec59a31e784b 51 #define AK8963_RA_ST1 0x02
tyftyftyf 0:ec59a31e784b 52 #define AK8963_RA_HXL 0x03
tyftyftyf 0:ec59a31e784b 53 #define AK8963_RA_HXH 0x04
tyftyftyf 0:ec59a31e784b 54 #define AK8963_RA_HYL 0x05
tyftyftyf 0:ec59a31e784b 55 #define AK8963_RA_HYH 0x06
tyftyftyf 0:ec59a31e784b 56 #define AK8963_RA_HZL 0x07
tyftyftyf 0:ec59a31e784b 57 #define AK8963_RA_HZH 0x08
tyftyftyf 0:ec59a31e784b 58 #define AK8963_RA_ST2 0x09
tyftyftyf 0:ec59a31e784b 59 #define AK8963_RA_CNTL1 0x0A
tyftyftyf 0:ec59a31e784b 60 #define AK8963_RA_CNTL2 0x0B
tyftyftyf 0:ec59a31e784b 61 #define AK8963_RA_ASTC 0x0C
tyftyftyf 0:ec59a31e784b 62 #define AK8963_RA_TS1 0x0D // SHIPMENT TEST, DO NOT USE
tyftyftyf 0:ec59a31e784b 63 #define AK8963_RA_TS2 0x0E // SHIPMENT TEST, DO NOT USE
tyftyftyf 0:ec59a31e784b 64 #define AK8963_RA_I2CDIS 0x0F
tyftyftyf 0:ec59a31e784b 65 #define AK8963_RA_ASAX 0x10
tyftyftyf 0:ec59a31e784b 66 #define AK8963_RA_ASAY 0x11
tyftyftyf 0:ec59a31e784b 67 #define AK8963_RA_ASAZ 0x12
tyftyftyf 0:ec59a31e784b 68
tyftyftyf 0:ec59a31e784b 69 #define AK8963_ST1_DRDY_BIT 0 //Data ready bit
tyftyftyf 0:ec59a31e784b 70 #define AK8963_ST1_DOR_BIT 1 //Data overrun bit
tyftyftyf 0:ec59a31e784b 71 #define AK8963_ST2_HOFL_BIT 3 //Mag sensor overflow
tyftyftyf 0:ec59a31e784b 72 //#define AK8963_ST2_DERR_BIT 2
tyftyftyf 0:ec59a31e784b 73 #define AK8963_ST2_BITM 3 //Output bit, 0 = 14 bit, 1 = 16 bit
tyftyftyf 0:ec59a31e784b 74
tyftyftyf 0:ec59a31e784b 75 #define AK8963_CNTL1_MODE_BIT 3
tyftyftyf 0:ec59a31e784b 76 #define AK8963_CNTL1_MODE_LENGTH 4
tyftyftyf 0:ec59a31e784b 77 #define AK8963_MODE_POWERDOWN 0x0
tyftyftyf 0:ec59a31e784b 78 #define AK8963_MODE_SINGLE 0x1
tyftyftyf 0:ec59a31e784b 79 #define AK8963_MODE_CONT1 0x2 //Continuous mode 1
tyftyftyf 0:ec59a31e784b 80 #define AK8963_MODE_CONT2 0x6 //Continuous mode 2
tyftyftyf 0:ec59a31e784b 81 #define AK8963_MODE_EXT 0x4 //External Trigger mode 1
tyftyftyf 0:ec59a31e784b 82 #define AK8963_MODE_SELFTEST 0x8
tyftyftyf 0:ec59a31e784b 83 #define AK8963_MODE_FUSEROM 0xF
tyftyftyf 0:ec59a31e784b 84
tyftyftyf 0:ec59a31e784b 85 #define AK8963_CNTL1_OUT_BIT 4
tyftyftyf 0:ec59a31e784b 86 #define AK8963_CNTL1_OUT_LENGTH 1 //Output bit, 0 = 14 bit, 1 = 16 bit
tyftyftyf 0:ec59a31e784b 87
tyftyftyf 0:ec59a31e784b 88 #define AK8963_CNTL2_RESET 0x01
tyftyftyf 0:ec59a31e784b 89
tyftyftyf 0:ec59a31e784b 90 #define AK8963_ASTC_SELF_BIT 6
tyftyftyf 0:ec59a31e784b 91
tyftyftyf 0:ec59a31e784b 92 #define AK8963_I2CDIS_BIT 0
tyftyftyf 0:ec59a31e784b 93
tyftyftyf 0:ec59a31e784b 94 class AK8963 {
tyftyftyf 0:ec59a31e784b 95 public:
tyftyftyf 0:ec59a31e784b 96 AK8963();
tyftyftyf 0:ec59a31e784b 97 AK8963(bool useSPI, uint8_t address);
tyftyftyf 0:ec59a31e784b 98
tyftyftyf 0:ec59a31e784b 99 void initialize();
tyftyftyf 0:ec59a31e784b 100 bool testConnection();
tyftyftyf 0:ec59a31e784b 101
tyftyftyf 0:ec59a31e784b 102 // WIA register
tyftyftyf 0:ec59a31e784b 103 uint8_t getDeviceID();
tyftyftyf 0:ec59a31e784b 104
tyftyftyf 0:ec59a31e784b 105 // INFO register
tyftyftyf 0:ec59a31e784b 106 uint8_t getInfo();
tyftyftyf 0:ec59a31e784b 107
tyftyftyf 0:ec59a31e784b 108 // ST1 register
tyftyftyf 0:ec59a31e784b 109 bool getDataReady();
tyftyftyf 0:ec59a31e784b 110 bool getDataOverRun();
tyftyftyf 0:ec59a31e784b 111
tyftyftyf 0:ec59a31e784b 112 // H* registers
tyftyftyf 0:ec59a31e784b 113 void getHeading(int16_t *x, int16_t *y, int16_t *z);
tyftyftyf 0:ec59a31e784b 114
tyftyftyf 0:ec59a31e784b 115 // ST2 register
tyftyftyf 0:ec59a31e784b 116 bool getOverflowStatus();
tyftyftyf 0:ec59a31e784b 117 bool getOutBitST();
tyftyftyf 0:ec59a31e784b 118
tyftyftyf 0:ec59a31e784b 119 // CNTL register
tyftyftyf 0:ec59a31e784b 120 uint8_t getMode();
tyftyftyf 0:ec59a31e784b 121 void setModeRes(uint8_t mode, uint8_t Mscale);
tyftyftyf 0:ec59a31e784b 122 void reset();
tyftyftyf 0:ec59a31e784b 123
tyftyftyf 0:ec59a31e784b 124 // ASTC register
tyftyftyf 0:ec59a31e784b 125 void setSelfTest(bool enabled);
tyftyftyf 0:ec59a31e784b 126 void getSelfTest(int16_t *x, int16_t *y, int16_t *z);
tyftyftyf 0:ec59a31e784b 127
tyftyftyf 0:ec59a31e784b 128 // I2CDIS
tyftyftyf 0:ec59a31e784b 129 void disableI2C(); // um, why...?
tyftyftyf 0:ec59a31e784b 130
tyftyftyf 0:ec59a31e784b 131 // ASA* registers
tyftyftyf 0:ec59a31e784b 132 void getAdjustment(uint8_t *x, uint8_t *y, uint8_t *z);
tyftyftyf 0:ec59a31e784b 133
tyftyftyf 0:ec59a31e784b 134
tyftyftyf 0:ec59a31e784b 135 private:
tyftyftyf 0:ec59a31e784b 136 bool bSPI;
tyftyftyf 0:ec59a31e784b 137 uint8_t devAddr;
tyftyftyf 0:ec59a31e784b 138 uint8_t buffer[14];
tyftyftyf 0:ec59a31e784b 139 uint8_t mode;
tyftyftyf 0:ec59a31e784b 140 uint8_t Mscale;
tyftyftyf 0:ec59a31e784b 141
tyftyftyf 0:ec59a31e784b 142 I2Cdev i2Cdev;
tyftyftyf 0:ec59a31e784b 143 };
tyftyftyf 0:ec59a31e784b 144
tyftyftyf 0:ec59a31e784b 145 #endif /* _AK8963_H_ */