1 /*!
2  * \file      sx1509.c
3  *
4  * \brief     SX1509 IO expander 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 "sx1509.h"
27 
28 extern I2c_t I2c;
29 
30 static uint8_t I2cDeviceAddr = 0;
31 
32 static bool SX1509Initialized = false;
33 
SX1509Init(void)34 void SX1509Init( void )
35 {
36     if( SX1509Initialized == false )
37     {
38         SX1509SetDeviceAddr( SX1509_I2C_ADDRESS );
39         SX1509Initialized = true;
40 
41         SX1509Reset( );
42     }
43 }
44 
SX1509Reset()45 LmnStatus_t SX1509Reset( )
46 {
47     if( SX1509Write( RegReset, 0x12 ) == LMN_STATUS_OK )
48     {
49         if( SX1509Write( RegReset, 0x34 ) == LMN_STATUS_OK )
50         {
51             return LMN_STATUS_OK;
52         }
53     }
54     return LMN_STATUS_ERROR;
55 }
56 
SX1509Write(uint8_t addr,uint8_t data)57 LmnStatus_t SX1509Write( uint8_t addr, uint8_t data )
58 {
59     return SX1509WriteBuffer( addr, &data, 1 );
60 }
61 
SX1509WriteBuffer(uint8_t addr,uint8_t * data,uint8_t size)62 LmnStatus_t SX1509WriteBuffer( uint8_t addr, uint8_t *data, uint8_t size )
63 {
64     return I2cWriteMemBuffer( &I2c, I2cDeviceAddr << 1, addr, data, size );
65 }
66 
SX1509Read(uint8_t addr,uint8_t * data)67 LmnStatus_t SX1509Read( uint8_t addr, uint8_t *data )
68 {
69     return SX1509ReadBuffer( addr, data, 1 );
70 }
71 
SX1509ReadBuffer(uint8_t addr,uint8_t * data,uint8_t size)72 LmnStatus_t SX1509ReadBuffer( uint8_t addr, uint8_t *data, uint8_t size )
73 {
74     return I2cReadMemBuffer( &I2c, I2cDeviceAddr << 1, addr, data, size );
75 }
76 
SX1509SetDeviceAddr(uint8_t addr)77 void SX1509SetDeviceAddr( uint8_t addr )
78 {
79     I2cDeviceAddr = addr;
80 }
81 
SX1509GetDeviceAddr(void)82 uint8_t SX1509GetDeviceAddr( void )
83 {
84     return I2cDeviceAddr;
85 }
86