1 /*!
2  * \file      mag3110.c
3  *
4  * \brief     MAG3110 Magnetometer driver implementation
5  *
6  * \copyright Revised BSD License, see section \ref LICENSE.
7  *
8  * \code
9  *                ______                              _
10  *               / _____)             _              | |
11  *              ( (____  _____ ____ _| |_ _____  ____| |__
12  *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
13  *               _____) ) ____| | | || |_| ____( (___| | | |
14  *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
15  *              (C)2013-2017 Semtech
16  *
17  * \endcode
18  *
19  * \author    Miguel Luis ( Semtech )
20  *
21  * \author    Gregory Cristian ( Semtech )
22  */
23 #include <stdbool.h>
24 #include "utilities.h"
25 #include "i2c.h"
26 #include "mag3110.h"
27 
28 extern I2c_t I2c;
29 
30 static uint8_t I2cDeviceAddr = 0;
31 static bool MAG3110Initialized = false;
32 
MAG3110Init(void)33 LmnStatus_t MAG3110Init( void )
34 {
35     uint8_t regVal = 0;
36 
37     MAG3110SetDeviceAddr( MAG3110_I2C_ADDRESS );
38 
39     if( MAG3110Initialized == false )
40     {
41         MAG3110Initialized = true;
42 
43         MAG3110Read( MAG3110_ID, &regVal );
44         if( regVal != 0xC4 )   // Fixed Device ID Number = 0xC4
45         {
46             return LMN_STATUS_ERROR;
47         }
48 
49         MAG3110Reset( );
50     }
51     return LMN_STATUS_OK;
52 }
53 
MAG3110Reset(void)54 LmnStatus_t MAG3110Reset( void )
55 {
56     if( MAG3110Write( 0x11, 0x10 ) == LMN_STATUS_OK ) // Reset the MAG3110 with CTRL_REG2
57     {
58         return LMN_STATUS_OK;
59     }
60     return LMN_STATUS_ERROR;
61 }
62 
MAG3110Write(uint8_t addr,uint8_t data)63 LmnStatus_t MAG3110Write( uint8_t addr, uint8_t data )
64 {
65     return MAG3110WriteBuffer( addr, &data, 1 );
66 }
67 
MAG3110WriteBuffer(uint8_t addr,uint8_t * data,uint8_t size)68 LmnStatus_t MAG3110WriteBuffer( uint8_t addr, uint8_t *data, uint8_t size )
69 {
70     return I2cWriteMemBuffer( &I2c, I2cDeviceAddr << 1, addr, data, size );
71 }
72 
MAG3110Read(uint8_t addr,uint8_t * data)73 LmnStatus_t MAG3110Read( uint8_t addr, uint8_t *data )
74 {
75     return MAG3110ReadBuffer( addr, data, 1 );
76 }
77 
MAG3110ReadBuffer(uint8_t addr,uint8_t * data,uint8_t size)78 LmnStatus_t MAG3110ReadBuffer( uint8_t addr, uint8_t *data, uint8_t size )
79 {
80     return I2cReadMemBuffer( &I2c, I2cDeviceAddr << 1, addr, data, size );
81 }
82 
MAG3110SetDeviceAddr(uint8_t addr)83 void MAG3110SetDeviceAddr( uint8_t addr )
84 {
85     I2cDeviceAddr = addr;
86 }
87 
MAG3110GetDeviceAddr(void)88 uint8_t MAG3110GetDeviceAddr( void )
89 {
90     return I2cDeviceAddr;
91 }
92