1 /*!
2 * \file sx9500.c
3 *
4 * \brief SX9500 proximity sensor 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 "sx9500.h"
27
28 extern I2c_t I2c;
29
30 static uint8_t I2cDeviceAddr = 0;
31
32 static bool SX9500Initialized = false;
33
SX9500Init(void)34 LmnStatus_t SX9500Init( void )
35 {
36 uint8_t regVal = 0;
37
38 SX9500SetDeviceAddr( SX9500_I2C_ADDRESS );
39
40 if( SX9500Initialized == false )
41 {
42 SX9500Initialized = true;
43
44 SX9500Read( SX9500_REG_PROXCTRL0, ®Val );
45 if( regVal != 0x0F )
46 {
47 return LMN_STATUS_ERROR;
48 }
49
50 SX9500Reset( );
51 }
52 return LMN_STATUS_OK;
53 }
54
SX9500Reset()55 LmnStatus_t SX9500Reset( )
56 {
57 if( SX9500Write( SX9500_REG_RESET, SX9500_RESET_CMD ) == LMN_STATUS_OK )
58 {
59 return LMN_STATUS_OK;
60 }
61 return LMN_STATUS_ERROR;
62 }
63
SX9500Write(uint8_t addr,uint8_t data)64 LmnStatus_t SX9500Write( uint8_t addr, uint8_t data )
65 {
66 return SX9500WriteBuffer( addr, &data, 1 );
67 }
68
SX9500WriteBuffer(uint8_t addr,uint8_t * data,uint8_t size)69 LmnStatus_t SX9500WriteBuffer( uint8_t addr, uint8_t *data, uint8_t size )
70 {
71 return I2cWriteMemBuffer( &I2c, I2cDeviceAddr << 1, addr, data, size );
72 }
73
SX9500Read(uint8_t addr,uint8_t * data)74 LmnStatus_t SX9500Read( uint8_t addr, uint8_t *data )
75 {
76 return SX9500ReadBuffer( addr, data, 1 );
77 }
78
SX9500ReadBuffer(uint8_t addr,uint8_t * data,uint8_t size)79 LmnStatus_t SX9500ReadBuffer( uint8_t addr, uint8_t *data, uint8_t size )
80 {
81 return I2cReadMemBuffer( &I2c, I2cDeviceAddr << 1, addr, data, size );
82 }
83
SX9500SetDeviceAddr(uint8_t addr)84 void SX9500SetDeviceAddr( uint8_t addr )
85 {
86 I2cDeviceAddr = addr;
87 }
88
SX9500GetDeviceAddr(void)89 uint8_t SX9500GetDeviceAddr( void )
90 {
91 return I2cDeviceAddr;
92 }
93
SX9500LockUntilDetection(void)94 void SX9500LockUntilDetection( void )
95 {
96 uint8_t val = 0;
97
98 SX9500Write( SX9500_REG_RESET, SX9500_RESET_CMD );
99 SX9500Read( SX9500_REG_IRQSRC, &val );
100 SX9500Read( SX9500_REG_STAT, &val );
101
102 SX9500Write( SX9500_REG_PROXCTRL0, 0x0F );
103 SX9500Write( SX9500_REG_PROXCTRL1, 0x43 );
104 SX9500Write( SX9500_REG_PROXCTRL2, 0x77 );
105 SX9500Write( SX9500_REG_PROXCTRL3, 0x01 );
106 SX9500Write( SX9500_REG_PROXCTRL4, 0x30 );
107 SX9500Write( SX9500_REG_PROXCTRL5, 0x0F );
108 SX9500Write( SX9500_REG_PROXCTRL6, 0x04 );
109 SX9500Write( SX9500_REG_PROXCTRL7, 0x40 );
110 SX9500Write( SX9500_REG_PROXCTRL8, 0x00 );
111 SX9500Write( SX9500_REG_IRQMSK, 0x60 );
112
113 val = 0;
114
115 while( ( val & 0xF0 ) == 0x00 )
116 {
117 SX9500Read( SX9500_REG_STAT, &val );
118 }
119
120 SX9500Read( SX9500_REG_STAT, &val );
121 SX9500Read( SX9500_REG_IRQSRC, &val );
122 }
123