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_LSM303AGR__
20 #define __LSM6DSM_I2C_MASTER_LSM303AGR__
21 
22 #ifdef LSM6DSM_I2C_MASTER_LSM303AGR
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_LSM303AGR */
29 
30 #define LSM303AGR_KSCALE                                0.15f         /* MAGN scale in uT/LSB */
31 #define LSM303AGR_I2C_ADDRESS                           (0x1e)
32 
33 /* LSM303AGR registers */
34 #define LSM303AGR_WAI_ADDR                              (0x4f)
35 #define LSM303AGR_CFG_REG_A_M_ADDR                      (0x60)
36 #define LSM303AGR_CFG_REG_B_M_ADDR                      (0x61)
37 #define LSM303AGR_CFG_REG_C_M_ADDR                      (0x62)
38 #define LSM303AGR_OUTDATA_ADDR                          (0x68)
39 
40 #define LSM303AGR_SW_RESET                              (0x20)
41 #define LSM303AGR_POWER_ON_VALUE                        (0x00)
42 #define LSM303AGR_POWER_OFF_VALUE                       (0x03)
43 #define LSM303AGR_OUTDATA_LEN                           (0x06)
44 #define LSM303AGR_OFFSET_CANCELLATION                   (0x02)
45 #define LSM303AGR_ENABLE_SELFTEST                       (0x02)
46 
47 /* Selftest related */
48 #define LSM303AGR_SELFTEST_HIGH_THR_LSB                 333
49 #define LSM303AGR_SELFTEST_LOW_THR_LSB                  10
50 
51 
52 /* LSM303AGR default base registers status */
53 /* LSM303AGR_CFG_REG_A_M_BASE: configuration register 1 default settings */
54 #define LSM303AGR_CFG_REG_A_M_BASE                     ((1 << 7) |    /* COMP_TEMP_EN */ \
55                                                         (0 << 6) |    /* REBOOT */ \
56                                                         (0 << 5) |    /* SOFT_RST */ \
57                                                         (0 << 4) |    /* LP */ \
58                                                         (0 << 3) |    /* ODR1 */ \
59                                                         (0 << 2) |    /* ODR0 */ \
60                                                         (0 << 1) |    /* MD1 */ \
61                                                         (0 << 0))     /* MD0 */
62 
63 /* LSM303AGR_CFC_REG_C_M_BASE: configuration register 3 default settings */
64 #define LSM303AGR_CFG_REG_C_M_BASE                     ((0 << 7) |    /* (0) */ \
65                                                         (0 << 6) |    /* INT_MAG_PIN */ \
66                                                         (0 << 5) |    /* I2C_DIS */ \
67                                                         (1 << 4) |    /* BDU */ \
68                                                         (0 << 3) |    /* BLE */ \
69                                                         (0 << 2) |    /* (0) */ \
70                                                         (0 << 1) |    /* SELFT_TEST */ \
71                                                         (0 << 0))     /* INT_MAG */
72 
73 #ifdef LSM6DSM_I2C_MASTER_LSM303AGR
74 /* MUST BE SAME LENGTH OF LSM6DSMMagnRates */
75 static uint8_t LSM303AGRMagnRatesRegValue[] = {
76     0x00, /* Expected 0.8125Hz, ODR = 10Hz */
77     0x00, /* Expected 1.625Hz, ODR = 10Hz */
78     0x00, /* Expected 3.25Hz, ODR = 10Hz */
79     0x00, /* Expected 6.5Hz, ODR = 10Hz */
80     0x04, /* Expected 12.5Hz, ODR = 20Hz */
81     0x0c, /* Expected 26Hz, ODR = 100Hz */
82     0x0c, /* Expected 52Hz, ODR = 100Hz */
83     0x0c, /* Expected 104Hz, ODR = 100Hz */
84 };
85 #endif /* LSM6DSM_I2C_MASTER_LSM303AGR */
86 
87 #endif /* __LSM6DSM_I2C_MASTER_LSM303AGR__ */
88