1 /*!
2  * \file      i2c.c
3  *
4  * \brief     I2C 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-board.h"
26 
27 /*!
28  * Flag to indicates if the I2C is initialized
29  */
30 static bool I2cInitialized = false;
31 
I2cInit(I2c_t * obj,I2cId_t i2cId,PinNames scl,PinNames sda)32 void I2cInit( I2c_t *obj, I2cId_t i2cId, PinNames scl, PinNames sda )
33 {
34     if( I2cInitialized == false )
35     {
36         I2cInitialized = true;
37 
38         I2cMcuInit( obj, i2cId, scl, sda );
39         I2cMcuFormat( obj, MODE_I2C, I2C_DUTY_CYCLE_2, true, I2C_ACK_ADD_7_BIT, 400000 );
40     }
41 }
42 
I2cDeInit(I2c_t * obj)43 void I2cDeInit( I2c_t *obj )
44 {
45     I2cInitialized = false;
46     I2cMcuDeInit( obj );
47 }
48 
I2cResetBus(I2c_t * obj)49 void I2cResetBus( I2c_t *obj )
50 {
51     I2cMcuResetBus( obj );
52 }
53 
I2cWrite(I2c_t * obj,uint8_t deviceAddr,uint8_t data)54 LmnStatus_t I2cWrite( I2c_t *obj, uint8_t deviceAddr, uint8_t data )
55 {
56     if( I2cInitialized == true )
57     {
58         if( I2cMcuWriteBuffer( obj, deviceAddr, &data, 1 ) == LMN_STATUS_ERROR )
59         {
60             // if first attempt fails due to an IRQ, try a second time
61             if( I2cMcuWriteBuffer( obj, deviceAddr, &data, 1 ) == LMN_STATUS_ERROR )
62             {
63                 return LMN_STATUS_ERROR;
64             }
65             else
66             {
67                 return LMN_STATUS_OK;
68             }
69         }
70         else
71         {
72             return LMN_STATUS_OK;
73         }
74     }
75     else
76     {
77         return LMN_STATUS_ERROR;
78     }
79 }
80 
I2cWriteBuffer(I2c_t * obj,uint8_t deviceAddr,uint8_t * buffer,uint16_t size)81 LmnStatus_t I2cWriteBuffer( I2c_t *obj, uint8_t deviceAddr, uint8_t *buffer, uint16_t size )
82 {
83     if( I2cInitialized == true )
84     {
85         if( I2cMcuWriteBuffer( obj, deviceAddr, buffer, size ) == LMN_STATUS_ERROR )
86         {
87             // if first attempt fails due to an IRQ, try a second time
88             if( I2cMcuWriteBuffer( obj, deviceAddr, buffer, size ) == LMN_STATUS_ERROR )
89             {
90                 return LMN_STATUS_ERROR;
91             }
92             else
93             {
94                 return LMN_STATUS_OK;
95             }
96         }
97         else
98         {
99             return LMN_STATUS_OK;
100         }
101     }
102     else
103     {
104         return LMN_STATUS_ERROR;
105     }
106 }
107 
I2cWriteMem(I2c_t * obj,uint8_t deviceAddr,uint16_t addr,uint8_t data)108 LmnStatus_t I2cWriteMem( I2c_t *obj, uint8_t deviceAddr, uint16_t addr, uint8_t data )
109 {
110     if( I2cInitialized == true )
111     {
112         if( I2cMcuWriteMemBuffer( obj, deviceAddr, addr, &data, 1 ) == LMN_STATUS_ERROR )
113         {
114             // if first attempt fails due to an IRQ, try a second time
115             if( I2cMcuWriteMemBuffer( obj, deviceAddr, addr, &data, 1 ) == LMN_STATUS_ERROR )
116             {
117                 return LMN_STATUS_ERROR;
118             }
119             else
120             {
121                 return LMN_STATUS_OK;
122             }
123         }
124         else
125         {
126             return LMN_STATUS_OK;
127         }
128     }
129     else
130     {
131         return LMN_STATUS_ERROR;
132     }
133 }
134 
I2cWriteMemBuffer(I2c_t * obj,uint8_t deviceAddr,uint16_t addr,uint8_t * buffer,uint16_t size)135 LmnStatus_t I2cWriteMemBuffer( I2c_t *obj, uint8_t deviceAddr, uint16_t addr, uint8_t *buffer, uint16_t size )
136 {
137     if( I2cInitialized == true )
138     {
139         if( I2cMcuWriteMemBuffer( obj, deviceAddr, addr, buffer, size ) == LMN_STATUS_ERROR )
140         {
141             // if first attempt fails due to an IRQ, try a second time
142             if( I2cMcuWriteMemBuffer( obj, deviceAddr, addr, buffer, size ) == LMN_STATUS_ERROR )
143             {
144                 return LMN_STATUS_ERROR;
145             }
146             else
147             {
148                 return LMN_STATUS_OK;
149             }
150         }
151         else
152         {
153             return LMN_STATUS_OK;
154         }
155     }
156     else
157     {
158         return LMN_STATUS_ERROR;
159     }
160 }
161 
I2cRead(I2c_t * obj,uint8_t deviceAddr,uint8_t * data)162 LmnStatus_t I2cRead( I2c_t *obj, uint8_t deviceAddr, uint8_t *data )
163 {
164     if( I2cInitialized == true )
165     {
166         return( I2cMcuReadBuffer( obj, deviceAddr, data, 1 ) );
167     }
168     else
169     {
170         return LMN_STATUS_ERROR;
171     }
172 }
173 
I2cReadBuffer(I2c_t * obj,uint8_t deviceAddr,uint8_t * buffer,uint16_t size)174 LmnStatus_t I2cReadBuffer( I2c_t *obj, uint8_t deviceAddr, uint8_t *buffer, uint16_t size )
175 {
176     if( I2cInitialized == true )
177     {
178         return( I2cMcuReadBuffer( obj, deviceAddr, buffer, size ) );
179     }
180     else
181     {
182         return LMN_STATUS_ERROR;
183     }
184 }
185 
I2cReadMem(I2c_t * obj,uint8_t deviceAddr,uint16_t addr,uint8_t * data)186 LmnStatus_t I2cReadMem( I2c_t *obj, uint8_t deviceAddr, uint16_t addr, uint8_t *data )
187 {
188     if( I2cInitialized == true )
189     {
190         return( I2cMcuReadMemBuffer( obj, deviceAddr, addr, data, 1 ) );
191     }
192     else
193     {
194         return LMN_STATUS_ERROR;
195     }
196 }
197 
I2cReadMemBuffer(I2c_t * obj,uint8_t deviceAddr,uint16_t addr,uint8_t * buffer,uint16_t size)198 LmnStatus_t I2cReadMemBuffer( I2c_t *obj, uint8_t deviceAddr, uint16_t addr, uint8_t *buffer, uint16_t size )
199 {
200     if( I2cInitialized == true )
201     {
202         return( I2cMcuReadMemBuffer( obj, deviceAddr, addr, buffer, size ) );
203     }
204     else
205     {
206         return LMN_STATUS_ERROR;
207     }
208 }
209