-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathI2C_Interface.c
339 lines (294 loc) · 14.6 KB
/
I2C_Interface.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*!*****************************************************************************
* @file I2C_Interface.h
* @author Fabien 'Emandhal' MAILLY
* @version 1.1.1
* @date 02/10/2021
* @brief I2C interface for drivers
* @details This I2C interface that can be used to communicate with devices
* for all the https://github.com/Emandhal drivers and developments.
* This implements I2C communication with Arduino, STM32cubeIDE
******************************************************************************/
/* Revision history:
* 1.1.1 Add STM32cubeIDE
* 1.1.0 Add Arduino
* 1.0.0 Release version
*****************************************************************************/
//-----------------------------------------------------------------------------
#include "I2C_Interface.h"
#include "ErrorsDef.h"
//-----------------------------------------------------------------------------
#ifdef __cplusplus
extern "C" {
#endif
//-----------------------------------------------------------------------------
#ifdef USE_HAL_DRIVER // STM32cubeIDE
# include <Main.h> // To get the MCU general defines
#endif
//-----------------------------------------------------------------------------
//********************************************************************************************************************
// I2C Interface initialization implementations
//********************************************************************************************************************
#ifdef ARDUINO
//=============================================================================
// Function for I2C driver initialization with Arduino
//=============================================================================
eERRORRESULT Interface_I2Cinit(I2C_Interface* pIntDev, const uint32_t sclFreq)
{
#ifdef CHECK_NULL_PARAM
if (pIntDev == NULL) return ERR__I2C_PARAMETER_ERROR;
#endif
return ERR_NONE;
}
#endif // #ifdef ARDUINO
//-----------------------------------------------------------------------------
#if defined(USE_HAL_DRIVER) || defined(USE_FULL_LL_DRIVER) // STM32cubeIDE
//=============================================================================
// Function for I2C driver initialization with STM32cubeIDE
//=============================================================================
eERRORRESULT Interface_I2Cinit(I2C_Interface* pIntDev, const uint32_t sclFreq)
{
#ifdef CHECK_NULL_PARAM
if (pIntDev == NULL) return ERR__I2C_PARAMETER_ERROR;
#endif
(void)pIntDev;
(void)sclFreq;
return ERR_NONE;
}
#endif // #if defined(USE_HAL_DRIVER) || defined(USE_FULL_LL_DRIVER) // STM32cubeIDE
//-----------------------------------------------------------------------------
//********************************************************************************************************************
// I2C Interface transfer implementations
//********************************************************************************************************************
#ifdef ARDUINO
//=============================================================================
// Function for I2C transfer with Arduino
//=============================================================================
eERRORRESULT Interface_I2Ctransfer(I2C_Interface *pIntDev, I2CInterface_Packet* const pPacketDesc)
{
#ifdef CHECK_NULL_PARAM
if (pIntDev == NULL) return ERR__I2C_PARAMETER_ERROR;
#endif
return ERR_NONE;
}
#endif // #ifdef ARDUINO
//-----------------------------------------------------------------------------
#if defined(USE_HAL_DRIVER) && defined(STM32G4xx_HAL_I2C_H) // STM32cubeIDE with HAL
//=============================================================================
// [STATIC] Wait for a proper I2C status
//=============================================================================
static eERRORRESULT __Interface_I2Cstatus(I2C_HandleTypeDef *hi2c)
{
HAL_I2C_StateTypeDef I2Cstate;
do
{
I2Cstate = HAL_I2C_GetState(hi2c);
switch (I2Cstate)
{
case HAL_I2C_STATE_BUSY :
case HAL_I2C_STATE_BUSY_TX :
case HAL_I2C_STATE_BUSY_RX :
case HAL_I2C_STATE_READY : break;
default:
case HAL_I2C_STATE_ERROR :
case HAL_I2C_STATE_ABORT :
case HAL_I2C_STATE_RESET : return ERR__I2C_COMM_ERROR;
case HAL_I2C_STATE_LISTEN :
case HAL_I2C_STATE_BUSY_TX_LISTEN:
case HAL_I2C_STATE_BUSY_RX_LISTEN: return ERR__I2C_OTHER_BUSY;
case HAL_I2C_STATE_TIMEOUT : return ERR__I2C_TIMEOUT;
}
}
while (I2Cstate != HAL_I2C_STATE_READY);
return ERR_NONE;
}
//=============================================================================
// [STATIC] Convert I2C error code to eERRORRESULT
//=============================================================================
static eERRORRESULT __I2CerrorCodeToERRORRESULT(uint32_t errorCode)
{
switch (errorCode)
{
case HAL_I2C_ERROR_NONE : break;
case HAL_I2C_ERROR_DMA : return ERR__DMA_ERROR;
case HAL_I2C_ERROR_TIMEOUT : return ERR__I2C_TIMEOUT;
case HAL_I2C_ERROR_SIZE : return ERR__I2C_CONFIG_ERROR;
case HAL_I2C_ERROR_DMA_PARAM: return ERR__DMA_PARAMETER_ERROR;
default:
case HAL_I2C_ERROR_BERR :
case HAL_I2C_ERROR_ARLO :
case HAL_I2C_ERROR_AF :
case HAL_I2C_ERROR_OVR : return ERR__I2C_COMM_ERROR;
#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
case HAL_I2C_ERROR_INVALID_CALLBACK:
#endif // USE_HAL_I2C_REGISTER_CALLBACKS
case HAL_I2C_ERROR_INVALID_PARAM: return ERR__I2C_PARAMETER_ERROR;
}
return ERR_NONE;
}
//=============================================================================
// [STATIC] Convert HAL_StatusTypeDef to eERRORRESULT
//=============================================================================
static eERRORRESULT __I2CHALstatusToERRORRESULT(HAL_StatusTypeDef halError)
{
switch (halError)
{
case HAL_OK : break;
default:
case HAL_ERROR : return ERR__I2C_COMM_ERROR;
case HAL_BUSY : return ERR__I2C_NACK;
case HAL_TIMEOUT: return ERR__I2C_TIMEOUT;
}
return ERR_NONE;
}
//=============================================================================
// Function for I2C transfer with STM32cubeIDE and HAL driver
//=============================================================================
eERRORRESULT Interface_I2Ctransfer(I2C_Interface *pIntDev, I2CInterface_Packet* const pPacketDesc)
{
#ifdef CHECK_NULL_PARAM
if (pIntDev == NULL) return ERR__I2C_PARAMETER_ERROR;
#endif
const bool DeviceWrite = ((pPacketDesc->ChipAddr & 0x01) == 0);
const uint16_t ChipAddr = (pPacketDesc->ChipAddr & I2C_ONLY_ADDR_Mask);
const uint32_t XferOption = (pPacketDesc->Stop ? I2C_AUTOEND_MODE : I2C_SOFTEND_MODE);
HAL_StatusTypeDef HALerror;
eERRORRESULT Error;
//--- Device polling? ---
if ((pPacketDesc->pBuffer == NULL) || (pPacketDesc->BufferSize <= 0)) // Device polling only
{
HALerror = HAL_I2C_IsDeviceReady(pIntDev->pHI2C, ChipAddr, 1, 2);
switch (HALerror)
{
case HAL_OK : break;
default:
case HAL_ERROR : return ERR__I2C_COMM_ERROR;
case HAL_BUSY : return ERR__I2C_NACK;
case HAL_TIMEOUT: return ERR__I2C_TIMEOUT;
}
return ERR_NONE;
}
//--- Transfer data ---
Error = __Interface_I2Cstatus(pIntDev->pHI2C); // Wait for a I2C ready
if (Error != ERR_NONE) return Error; // If the status of the I2C is not ready, return the error
if (DeviceWrite)
{
Error = __I2CHALstatusToERRORRESULT(HAL_I2C_Master_Seq_Transmit_IT(pIntDev->pHI2C, ChipAddr, pPacketDesc->pBuffer, pPacketDesc->BufferSize, XferOption));
if (Error != ERR_NONE) return Error; // If there is an error while calling HAL_I2C_Master_Seq_Transmit_IT() then return the error
}
else
{
Error = __I2CHALstatusToERRORRESULT(HAL_I2C_Master_Seq_Receive_IT(pIntDev->pHI2C, ChipAddr, pPacketDesc->pBuffer, pPacketDesc->BufferSize, XferOption));
if (Error != ERR_NONE) return Error; // If there is an error while calling HAL_I2C_Master_Seq_Receive_IT() then return the error
}
return __I2CerrorCodeToERRORRESULT(HAL_I2C_GetError(pIntDev->pHI2C));
}
#endif // #if defined(USE_HAL_DRIVER) && defined(STM32G4xx_HAL_I2C_H) // STM32cubeIDE with HAL
#if defined(USE_FULL_LL_DRIVER) && defined(STM32G4xx_LL_I2C_H) // STM32cubeIDE with LL
//=============================================================================
// Function for I2C transfer with STM32cubeIDE and Low Level driver
//=============================================================================
eERRORRESULT Interface_I2Ctransfer(I2C_Interface *pIntDev, I2CInterface_Packet* const pPacketDesc)
{
#ifdef CHECK_NULL_PARAM
if (pIntDev == NULL) return ERR__I2C_PARAMETER_ERROR;
#endif
const bool DeviceWrite = ((pPacketDesc->ChipAddr & 0x01) == 0);
const uint16_t ChipAddr = (pPacketDesc->ChipAddr & (I2C_IS_10BITS_ADDRESS(pPacketDesc->ChipAddr) ? I2C_ONLY_ADDR10_Mask : I2C_ONLY_ADDR8_Mask));
const uint32_t ChipAddrSize = (I2C_IS_10BITS_ADDRESS(pPacketDesc->ChipAddr) ? LL_I2C_ADDRSLAVE_10BIT : LL_I2C_ADDRSLAVE_7BIT);
const uint32_t EndMode = (pPacketDesc->Stop ? LL_I2C_MODE_SOFTEND : LL_I2C_MODE_RELOAD);
size_t RemainingBytes = pPacketDesc->BufferSize;
uint32_t Timeout = pIntDev->I2Ctimeout;
//--- Device polling? ---
if ((pPacketDesc->pBuffer == NULL) || (pPacketDesc->BufferSize <= 0)) // Device polling only
{
Timeout = pIntDev->I2Ctimeout;
LL_I2C_HandleTransfer(pIntDev->pHI2C, ChipAddr, ChipAddrSize, 0, LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_WRITE);
while (true) // Wait the polling to finish
{
if (LL_I2C_IsActiveFlag_NACK(pIntDev->pHI2C) > 0) return ERR__I2C_NACK; // If NACK received, return the error
if (LL_I2C_IsActiveFlag_STOP(pIntDev->pHI2C) > 0) break; // Wait STOP condition detected
if (Timeout == 0) return ERR__I2C_TIMEOUT; // Timeout? return an error
--Timeout;
}
return ERR_NONE;
}
//--- Endianness configuration for data striding ---
const eI2C_EndianTransform EndianTransform = I2C_ENDIAN_TRANSFORM_GET(pPacketDesc->Config.Value); // Only the endianness configuration is important for this driver
const size_t BlockSize = (EndianTransform == I2C_NO_ENDIAN_CHANGE ? 1 : (size_t)EndianTransform); // Get block size. No endian change = 8-bits data
if ((pPacketDesc->BufferSize % BlockSize) > 0) return ERR__DATA_MODULO; // Data block size shall be a multiple of data size
size_t CurrentBlockPos = BlockSize;
//--- Transfer data ---
uint8_t* pBuffer = &pPacketDesc->pBuffer[BlockSize - 1]; // Adjust the start of data for endianness
if (DeviceWrite) // Device write
{
const uint32_t RequestMode = (pPacketDesc->Start ? LL_I2C_GENERATE_START_WRITE : LL_I2C_GENERATE_NOSTARTSTOP);
LL_I2C_HandleTransfer(pIntDev->pHI2C, ChipAddr, ChipAddrSize, RemainingBytes, EndMode, RequestMode);
while (true)
{
if (LL_I2C_IsActiveFlag_NACK(pIntDev->pHI2C) > 0) return ERR__I2C_NACK_DATA; // If NACK received, return the error
if (LL_I2C_IsActiveFlag_STOP(pIntDev->pHI2C) > 0) break; // STOP condition detected? break
if (Timeout == 0) return ERR__I2C_TIMEOUT; // Timeout? return an error
--Timeout;
if (LL_I2C_IsActiveFlag_TXE(pIntDev->pHI2C) == 0) continue; // TX not empty? Do another loop
Timeout = pIntDev->I2Ctimeout; // Reset timeout
if (RemainingBytes == 0) break; // No data remaining to send, then break the loop
LL_I2C_TransmitData8(pIntDev->pHI2C, *pBuffer); // Send next data byte
--RemainingBytes;
//--- Adjust buffer address with data striding ---
--CurrentBlockPos;
if (CurrentBlockPos == 0)
{
pBuffer += (2 * BlockSize) - 1;
CurrentBlockPos = BlockSize;
}
else --pBuffer;
}
}
else // Device read
{
const uint32_t RequestMode = (pPacketDesc->Start ? LL_I2C_GENERATE_START_READ : LL_I2C_GENERATE_NOSTARTSTOP);
LL_I2C_HandleTransfer(pIntDev->pHI2C, ChipAddr, ChipAddrSize, RemainingBytes, EndMode, RequestMode);
while (RemainingBytes > 0)
{
Timeout = pIntDev->I2Ctimeout; // Reset timeout
while (true)
{
if (LL_I2C_IsActiveFlag_RXNE(pIntDev->pHI2C) > 0) break; // Data received
if (Timeout == 0) return ERR__I2C_TIMEOUT; // Timeout? return an error
--Timeout;
}
*pBuffer = LL_I2C_ReceiveData8(pIntDev->pHI2C); // Get next data byte
--RemainingBytes;
//--- Adjust buffer address with data striding ---
--CurrentBlockPos;
if (CurrentBlockPos == 0)
{
pBuffer += (2 * BlockSize) - 1;
CurrentBlockPos = BlockSize;
}
else --pBuffer;
}
}
if (pPacketDesc->Stop)
{
Timeout = pIntDev->I2Ctimeout; // Reset timeout
LL_I2C_HandleTransfer(pIntDev->pHI2C, ChipAddr, ChipAddrSize, 0, LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_STOP); // Send a stop
while (true) // Wait the stop to finish
{
if (LL_I2C_IsActiveFlag_STOP(pIntDev->pHI2C) > 0) break; // Wait STOP condition detected
if (Timeout == 0) return ERR__I2C_TIMEOUT; // Timeout? return an error
--Timeout;
}
LL_I2C_ClearFlag_STOP(pIntDev->pHI2C); // Clear STOP flag
}
//--- Endianness result ---
pPacketDesc->Config.Value &= ~I2C_ENDIAN_RESULT_Mask;
pPacketDesc->Config.Value |= I2C_ENDIAN_RESULT_SET(EndianTransform); // Indicate that the endian transform have been processed
return ERR_NONE;
}
#endif // #if defined(USE_FULL_LL_DRIVER) && defined(STM32G4xx_LL_I2C_H) // STM32cubeIDE with LL
//-----------------------------------------------------------------------------
#ifdef __cplusplus
}
#endif
//-----------------------------------------------------------------------------