1 /*
2  * Copyright (C) 2016-2017 STMicroelectronics
3  *
4  * Author: Denis Ciocca <denis.ciocca@st.com>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *    http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #ifndef __LSM6DSM_I2C_MASTER_AK09916__
20 #define __LSM6DSM_I2C_MASTER_AK09916__
21 
22 #ifdef LSM6DSM_I2C_MASTER_AK09916
23 #ifndef LSM6DSM_I2C_MASTER_MAGNETOMETER_ENABLED
24 #define LSM6DSM_I2C_MASTER_MAGNETOMETER_ENABLED         1
25 #else /* LSM6DSM_I2C_MASTER_MAGNETOMETER_ENABLED */
26 #error "Another magnetometer is already selected! One magn per time can be used."
27 #endif /* LSM6DSM_I2C_MASTER_MAGNETOMETER_ENABLED */
28 #endif /* LSM6DSM_I2C_MASTER_AK09916 */
29 
30 #define AK09916_KSCALE                                  0.15          /* MAGN scale in uT/LSB */
31 #define AK09916_I2C_ADDRESS                             (0x0c)
32 
33 /* AK09916 registers */
34 #define AK09916_WAI_ADDR                                (0x00)
35 #define AK09916_CNTL2_ADDR                              (0x31)
36 #define AK09916_CNTL3_ADDR                              (0x32)
37 #define AK09916_OUTDATA_ADDR                            (0x11)
38 #define AK09916_STATUS_DATA_ADDR                        (0x18)
39 
40 #define AK09916_SW_RESET                                (0x01)
41 #define AK09916_POWER_ON_VALUE                          (0x00)
42 #define AK09916_POWER_OFF_VALUE                         (0x00)
43 #define AK09916_OUTDATA_LEN                             (0x06)
44 #define AK09916_ENABLE_SELFTEST_MODE                    (0x10)
45 
46 /* Selftest related */
47 #define AK09916_SELFTEST_HIGH_THR_XYZ_LSB               200
48 #define AK09916_SELFTEST_LOW_THR_XY_LSB                 -200
49 #define AK09916_SELFTEST_LOW_THR_Z_LSB                  -1000
50 
51 
52 /* AK09916 default base registers status */
53 /* AK09916_CNTL2_BASE: control register 2 default settings */
54 #define AK09916_CNTL2_BASE                             ((0 << 7) |    /* (0) */ \
55                                                         (0 << 6) |    /* (0) */ \
56                                                         (0 << 5) |    /* (0) */ \
57                                                         (0 << 4) |    /* MODE4 */ \
58                                                         (0 << 3) |    /* MODE3 */ \
59                                                         (0 << 2) |    /* MODE2 */ \
60                                                         (0 << 1) |    /* MODE1 */ \
61                                                         (0 << 0))     /* MODE0 */
62 
63 #ifdef LSM6DSM_I2C_MASTER_AK09916
64 /* MUST BE SAME LENGTH OF LSM6DSMMagnRates */
65 static uint8_t AK09916MagnRatesRegValue[] = {
66     0x02, /* Expected 0.8125Hz, ODR = 10Hz */
67     0x02, /* Expected 1.625Hz, ODR = 10Hz */
68     0x02, /* Expected 3.25Hz, ODR = 10Hz */
69     0x02, /* Expected 6.5Hz, ODR = 10Hz */
70     0x04, /* Expected 12.5Hz, ODR = 20Hz */
71     0x06, /* Expected 26Hz, ODR = 50Hz */
72     0x08, /* Expected 52Hz, ODR = 100Hz */
73     0x08, /* Expected 104Hz, ODR = 100Hz */
74 };
75 #endif /* LSM6DSM_I2C_MASTER_AK09916 */
76 
77 #endif /* __LSM6DSM_I2C_MASTER_AK09916__ */
78