/
pal_i2c.c
324 lines (277 loc) · 9.95 KB
/
pal_i2c.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
/**
* \copyright
* MIT License
*
* Copyright (c) 2018 Infineon Technologies AG
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE
*
* \endcopyright
*
* \author Infineon Technologies AG
*
* \file pal_i2c.c
*
* \brief This file implements the platform abstraction layer(pal) APIs for I2C.
*
* \ingroup grPAL
* @{
*/
#include <linux/i2c-dev.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include "optiga/pal/pal_i2c.h"
#include "pal_linux.h"
#if IFX_I2C_LOG_HAL == 1
#define LOG_HAL IFX_I2C_LOG
#else
#include<stdio.h>
#define LOG_HAL(...) //printf(__VA_ARGS__)
#endif
/// I2C device
char * i2c_if = "/dev/i2c-1";
// Slave address not initialization
#define IFXI2C_SLAVE_ADDRESS_INIT 0xFFFF
#define PAL_I2C_MASTER_MAX_BITRATE 100
#define WAIT_500_MS (500)
/// @cond hidden
void i2c_master_end_of_transmit_callback(void);
void i2c_master_end_of_receive_callback(void);
void invoke_upper_layer_callback (const pal_i2c_t* p_pal_i2c_ctx, optiga_lib_status_t event);
uint16_t usb_i2c_poll_operation_result(pal_i2c_t* p_i2c_context);
/* Varibale to indicate the re-entrant count of the i2c bus acquire function*/
static volatile uint32_t g_entry_count = 0;
/* Pointer to the current pal i2c context*/
static pal_i2c_t * gp_pal_i2c_current_ctx;
//lint --e{715} suppress the unused p_i2c_context variable lint error , since this is kept for future enhancements
static pal_status_t pal_i2c_acquire(const void * p_i2c_context)
{
if (0 == g_entry_count)
{
g_entry_count++;
if (1 == g_entry_count)
{
return PAL_STATUS_SUCCESS;
}
}
return PAL_STATUS_FAILURE;
}
// I2C release bus function
//lint --e{715} suppress the unused p_i2c_context variable lint, since this is kept for future enhancements
static void pal_i2c_release(const void* p_i2c_context)
{
g_entry_count = 0;
}
/// @endcond
void invoke_upper_layer_callback (const pal_i2c_t * p_pal_i2c_ctx, optiga_lib_status_t event)
{
upper_layer_callback_t upper_layer_handler;
//lint --e{611} suppress "void* function pointer is type casted to upper_layer_callback_t type"
upper_layer_handler = (upper_layer_callback_t )p_pal_i2c_ctx->upper_layer_event_handler;
upper_layer_handler(p_pal_i2c_ctx->p_upper_layer_ctx , event);
//Release I2C Bus
pal_i2c_release(p_pal_i2c_ctx->p_upper_layer_ctx );
}
/// @cond hidden
// I2C driver callback function when the transmit is completed successfully
void i2c_master_end_of_transmit_callback(void)
{
invoke_upper_layer_callback(gp_pal_i2c_current_ctx, PAL_I2C_EVENT_SUCCESS);
}
// I2C driver callback function when the receive is completed successfully
void i2c_master_end_of_receive_callback(void)
{
invoke_upper_layer_callback(gp_pal_i2c_current_ctx, PAL_I2C_EVENT_SUCCESS);
}
// I2C error callback function
void i2c_master_error_detected_callback(void)
{
//I2C_MASTER_t *p_i2c_master;
//
//p_i2c_master = gp_pal_i2c_current_ctx->p_i2c_hw_config;
//if (I2C_MASTER_IsTxBusy(p_i2c_master))
//{
// //lint --e{534} suppress "Return value is not required to be checked"
// I2C_MASTER_AbortTransmit(p_i2c_master);
// while (I2C_MASTER_IsTxBusy(p_i2c_master)){}
//}
//if (I2C_MASTER_IsRxBusy(p_i2c_master))
//{
// //lint --e{534} suppress "Return value is not required to be checked"
// I2C_MASTER_AbortReceive(p_i2c_master);
// while (I2C_MASTER_IsRxBusy(p_i2c_master)){}
//}
invoke_upper_layer_callback(gp_pal_i2c_current_ctx, PAL_I2C_EVENT_ERROR);
}
void i2c_master_nack_received_callback(void)
{
i2c_master_error_detected_callback();
}
void i2c_master_arbitration_lost_callback(void)
{
i2c_master_error_detected_callback();
}
/// @endcond
pal_status_t pal_i2c_init(const pal_i2c_t* p_i2c_context)
{
int32_t ret = PAL_I2C_EVENT_ERROR;
pal_linux_t *pal_linux;
do
{
pal_linux = (pal_linux_t*) p_i2c_context->p_i2c_hw_config;
pal_linux->i2c_handle = open(i2c_if, O_RDWR);
LOG_HAL("IFX OPTIGA TRUST X Logs \n");
// Assign the slave address
ret = ioctl(pal_linux->i2c_handle, I2C_SLAVE, p_i2c_context->slave_address);
if(PAL_STATUS_SUCCESS != ret)
{
LOG_HAL((uint32_t)pal_linux->i2c_handle, "ioctl returned an error = ", ret);
break;
}
//start_transceive_thread();
}while(0);
return ret;
}
pal_status_t pal_i2c_deinit(const pal_i2c_t* p_i2c_context)
{
LOG_HAL("pal_i2c_deinit\n. ");
return PAL_STATUS_SUCCESS;
}
pal_status_t pal_i2c_write(const pal_i2c_t* p_i2c_context,uint8_t* p_data , uint16_t length)
{
pal_status_t status = PAL_STATUS_FAILURE;
int32_t i2c_write_status;
pal_linux_t *pal_linux;
pal_linux = (pal_linux_t*) p_i2c_context->p_i2c_hw_config;
LOG_HAL("[IFX-HAL]: I2C TX (%d): ", length);
#if 1
for (int i = 0; i < length; i++)
{
LOG_HAL("%02X ", p_data[i]);
}
#endif
LOG_HAL("\n");
if(PAL_STATUS_SUCCESS == pal_i2c_acquire(p_i2c_context))
{
gp_pal_i2c_current_ctx = p_i2c_context;
//Invoke the low level i2c master driver API to write to the bus
i2c_write_status = write(pal_linux->i2c_handle, p_data, length);
if (0 > i2c_write_status)
{
//If I2C Master fails to invoke the write operation, invoke upper layer event handler with error.
//lint --e{611} suppress "void* function pointer is type casted to upper_layer_callback_t type"
((upper_layer_callback_t )(p_i2c_context->upper_layer_event_handler))
(p_i2c_context->p_upper_layer_ctx , PAL_I2C_EVENT_ERROR);
//Release I2C Bus
pal_i2c_release((void *)p_i2c_context);
}
else
{
i2c_master_end_of_transmit_callback();
status = PAL_STATUS_SUCCESS;
//transmission_completed = true;
}
}
else
{
status = PAL_STATUS_I2C_BUSY;
//lint --e{611} suppress "void* function pointer is type casted to upper_layer_callback_t type"
((upper_layer_callback_t )(p_i2c_context->upper_layer_event_handler))
(p_i2c_context->p_upper_layer_ctx , PAL_I2C_EVENT_BUSY);
}
return status;
}
pal_status_t pal_i2c_read(const pal_i2c_t* p_i2c_context , uint8_t* p_data , uint16_t length)
{
int32_t i2c_read_status = PAL_STATUS_FAILURE;
pal_linux_t *pal_linux;
pal_linux = (pal_linux_t*) p_i2c_context->p_i2c_hw_config;
//Acquire the I2C bus before read/write
if (PAL_STATUS_SUCCESS == pal_i2c_acquire(p_i2c_context))
{
gp_pal_i2c_current_ctx = p_i2c_context;
i2c_read_status = read(pal_linux->i2c_handle,p_data, length);
if (0 > i2c_read_status)
{
LOG_HAL("[IFX-HAL]: libusb_interrupt_transfer ERROR %d\n.", i2c_read_status);
//lint --e{611} suppress "void* function pointer is type casted to upper_layer_callback_t type"
((upper_layer_callback_t )(p_i2c_context->upper_layer_event_handler))
(p_i2c_context->p_upper_layer_ctx , PAL_I2C_EVENT_ERROR);
//Release I2C Bus
pal_i2c_release((void *)p_i2c_context);
return i2c_read_status;
}
else
{
LOG_HAL("[IFX-HAL]: I2C RX (%d)\n", length);
#if 1
for (int i = 0; i < length; i++)
{
LOG_HAL("%02X ", p_data[i]);
}
#endif
i2c_master_end_of_receive_callback();
i2c_read_status = PAL_STATUS_SUCCESS;
//reception_started = true;
}
}
else
{
i2c_read_status = PAL_STATUS_I2C_BUSY;
//lint --e{611} suppress "void* function pointer is type casted to upper_layer_callback_t type"
((upper_layer_callback_t )(p_i2c_context->upper_layer_event_handler))
(p_i2c_context->p_upper_layer_ctx , PAL_I2C_EVENT_BUSY);
}
return i2c_read_status;
}
pal_status_t pal_i2c_set_bitrate(const pal_i2c_t* p_i2c_context , uint16_t bitrate)
{
pal_status_t return_status = PAL_STATUS_FAILURE;
optiga_lib_status_t event = PAL_I2C_EVENT_ERROR;
LOG_HAL("pal_i2c_set_bitrate\n. ");
//Acquire the I2C bus before setting the bitrate
if (PAL_STATUS_SUCCESS == pal_i2c_acquire(p_i2c_context))
{
// If the user provided bitrate is greater than the I2C master hardware maximum supported value,
// set the I2C master to its maximum supported value.
if (bitrate > PAL_I2C_MASTER_MAX_BITRATE)
{
bitrate = PAL_I2C_MASTER_MAX_BITRATE;
}
return_status = PAL_STATUS_SUCCESS;
event = PAL_I2C_EVENT_SUCCESS;
}
else
{
return_status = PAL_STATUS_I2C_BUSY;
event = PAL_I2C_EVENT_BUSY;
}
if (0 != p_i2c_context->upper_layer_event_handler)
{
//lint --e{611} suppress "void* function pointer is type casted to upper_layer_callback_t type"
((upper_layer_callback_t)(p_i2c_context->upper_layer_event_handler))(p_i2c_context->p_upper_layer_ctx , event);
}
//Release I2C Bus
pal_i2c_release((void *)p_i2c_context);
return return_status;
}
/**
* @}
*/