OPTIGA™ Trust M
Host Library Documentation
ifx_i2c_config.h
Go to the documentation of this file.
1 
38 #ifndef _IFX_I2C_CONFIG_H_
39 #define _IFX_I2C_CONFIG_H_
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 #ifdef IFX_I2C_CONFIG_EXTERNAL
46  #include "ifx_i2c_config_external.h"
47 #else
48 // Protocol Stack Includes
49 #include "optiga/pal/pal_i2c.h"
50 #include "optiga/pal/pal_gpio.h"
55 
57 #define IFX_I2C_BASE_ADDR (0x30)
58 
60 #define PL_POLLING_INVERVAL_US (1000U)
61 
62 #define PL_POLLING_MAX_CNT (200U)
63 
64 #define PL_DATA_POLLING_INVERVAL_US (5000U)
65 
66 #define PL_GUARD_TIME_INTERVAL_US (50U)
67 
71 #ifdef IFX_I2C_FRAME_SIZE
72  #if (IFX_I2C_FRAME_SIZE > 277) || (IFX_I2C_FRAME_SIZE < 16)
73  #error "Unsupported value for IFX_I2C_FRAME_SIZE"
74  #endif
75 #else
76  //Setting of frame size more than 277 and less than 16 bytes cause unexpected behaviour.
77  #define IFX_I2C_FRAME_SIZE (277U)
78 #endif
79 
81 #define TL_HEADER_SIZE (1U)
82 
83 #define DL_HEADER_SIZE (5U)
84 
85 #define DL_TRANS_REPEAT (3U)
86 
87 #define PL_TRANS_TIMEOUT_MS (10U)
88 
90 #define TL_MAX_EXIT_TIMEOUT (180U)
91 
93 #define RESET_LOW_TIME_MSEC (2000U)
94 
95 #define STARTUP_TIME_MSEC (12000U)
96 
98 #define IFX_I2C_STACK_SUCCESS (0x0000)
99 
100 #define IFX_I2C_STACK_BUSY (0x0001)
101 
102 #define IFX_I2C_STACK_ERROR (0x0102)
103 
104 #define IFX_I2C_STACK_MEM_ERROR (0x0104)
105 
106 #define IFX_I2C_FATAL_ERROR (0x0106)
107 
108 #define IFX_I2C_HANDSHAKE_ERROR (0x0107)
109 
110 #define IFX_I2C_SESSION_ERROR (0x0108)
111 
113 #define IFX_I2C_DL_HEADER_OFFSET (0U)
114 
115 #define IFX_I2C_TL_HEADER_OFFSET (IFX_I2C_DL_HEADER_OFFSET + 3)
116 
117 #define IFX_I2C_LOG_PL (0U)
118 
119 #define IFX_I2C_LOG_DL (0U)
120 
121 #define IFX_I2C_LOG_TL (0U)
122 
123 #define IFX_I2C_LOG_PRL (0U)
124 
126 #define IFX_I2C_LOG_ID_PL (0x00)
127 
128 #define IFX_I2C_LOG_ID_DL (0x01)
129 
130 #define IFX_I2C_LOG_ID_TL (0x02)
131 
132 #define IFX_I2C_LOG_ID_PRL (0x03)
133 
134 #define IFX_I2C_LOG_ID_PAL (0x04)
135 
136 #if defined OPTIGA_COMMS_SHIELDED_CONNECTION
137  #define IFX_I2C_TL_ENABLE (1U)
138  #define IFX_I2C_PRL_ENABLED (1U)
139 
140  #define IFX_I2C_PRESENCE_BIT (0x08)
141  #define IFX_I2C_PRL_MAC_SIZE (0x08)
142  #define IFX_I2C_PRL_HEADER_SIZE (0x05)
143  #define IFX_I2C_PRL_OVERHEAD_SIZE (IFX_I2C_PRL_HEADER_SIZE + IFX_I2C_PRL_MAC_SIZE)
145  #define IFX_I2C_DATA_OFFSET (IFX_I2C_PRL_HEADER_SIZE)
147  #define IFX_I2C_PRESENCE_BIT_CHECK (0x08)
148 #else
149  #define IFX_I2C_PRESENCE_BIT (0x00)
150  #define IFX_I2C_PRL_MAC_SIZE (0x00)
151  #define IFX_I2C_PRL_HEADER_SIZE (0x00)
152  #define IFX_I2C_PRL_OVERHEAD_SIZE (IFX_I2C_PRL_HEADER_SIZE + IFX_I2C_PRL_MAC_SIZE)
154  #define IFX_I2C_DATA_OFFSET (IFX_I2C_PRL_HEADER_SIZE)
156  #define IFX_I2C_PRESENCE_BIT_CHECK (0x00)
157 #endif
158 
160 #define PROTOCOL_VERSION_PRE_SHARED_SECRET (0x01)
161 
162 #define IFX_I2C_SESSION_CONTEXT_RESTORE (0x11)
163 
164 #define IFX_I2C_SESSION_CONTEXT_SAVE (0x22)
165 
166 #define IFX_I2C_SESSION_CONTEXT_NONE (0x33)
167 
169 #define NO_PROTECTION (0x00)
170 
171 #define MASTER_PROTECTION (0x01)
172 
173 #define SLAVE_PROTECTION (0x02)
174 
175 #define FULL_PROTECTION (0x03)
176 
177 #define RE_ESTABLISH (0x80)
178 
180 #define IFX_I2C_SESSION_KEY_BUFFER_SIZE (0x28)
181 
182 
183 typedef struct ifx_i2c_context ifx_i2c_context_t;
184 
186 typedef void ( * ifx_i2c_event_handler_t)(struct ifx_i2c_context * p_ctx,
187  optiga_lib_status_t event,
188  const uint8_t * data,
189  uint16_t data_len);
190 
192 typedef struct ifx_i2c_pl
193 {
194  // Physical Layer low level interface variables
195 
199  uint16_t buffer_tx_len;
201  uint16_t buffer_rx_len;
205  uint8_t i2c_cmd;
207  uint16_t retry_counter;
208 
209  // Physical Layer high level interface variables
210 
212  uint8_t frame_action;
214  uint8_t frame_state ;
216  uint8_t * p_tx_frame;
218  uint16_t tx_frame_len;
219  // Upper layer handler
221 
222  // Physical Layer negotiation/soft reset variables
223 
229 
231 typedef struct ifx_i2c_dl
232 {
233  // Data Link layer internal state variables
234 
236  uint8_t state;
238  uint8_t tx_seq_nr;
239  // Rx sequence number
240  uint8_t rx_seq_nr;
242  uint8_t action_rx_only;
246  uint8_t error;
248  uint8_t resynced;
252  uint16_t tx_buffer_size;
254  uint16_t rx_buffer_size;
256  uint8_t * p_tx_frame_buffer;
258  uint8_t * p_rx_frame_buffer;
261  // Upper layer Event handler
264 
266 typedef struct ifx_i2c_tl
267 {
268  // Transport Layer state and buffer
269 
271  uint8_t state;
273  uint8_t * p_actual_packet;
279  uint16_t packet_offset;
287  uint32_t api_start_time;
299  uint8_t payload_offset;
304 
308 
309 #if defined OPTIGA_COMMS_SHIELDED_CONNECTION
310 
312 typedef struct ifx_i2c_prl_manage_context
313 {
315  uint8_t session_key[IFX_I2C_SESSION_KEY_BUFFER_SIZE];
317  uint8_t decryption_failure_counter;
319  uint8_t data_retransmit_counter;
321  uint8_t negotiation_state;
323  uint8_t stored_context_flag;
325  uint32_t master_sequence_number;
327  uint32_t save_slave_sequence_number;
328 }ifx_i2c_prl_manage_context_t;
329 
331 typedef struct ifx_i2c_datastore_config
332 {
334  uint8_t protocol_version;
336  uint16_t datastore_shared_secret_id;
338  uint16_t datastore_manage_context_id;
340  uint16_t shared_secret_length;
341 } ifx_i2c_datastore_config_t;
342 
343 
345 typedef struct ifx_i2c_prl
346 {
347  // Presentation layer state
348  uint8_t state;
349  // Handshake state
350  uint8_t hs_state;
352  uint8_t negotiation_state;
354  uint8_t mc_state;
356  uint32_t master_sequence_number;
358  uint32_t slave_sequence_number;
360  uint32_t save_slave_sequence_number;
362  uint8_t * p_actual_payload;
364  uint16_t actual_payload_length;
366  uint8_t * p_recv_payload_buffer;
368  uint16_t * p_recv_payload_buffer_length;
370  uint8_t sctr;
372  uint8_t saved_sctr;
374  uint8_t alert_type;
376  uint8_t prl_header_offset;
378  uint8_t session_key[IFX_I2C_SESSION_KEY_BUFFER_SIZE];
380  uint8_t random[32];
382  uint8_t prl_txrx_buffer[58];
384  uint16_t prl_txrx_receive_length;
386  uint8_t associate_data[8];
388  uint16_t prl_receive_length;
390  uint8_t decryption_failure_counter;
392  uint8_t data_retransmit_counter;
394  optiga_lib_status_t return_status;
396  uint8_t restore_context_flag;
397  //Context to be stored
398  ifx_i2c_prl_manage_context_t prl_saved_ctx;
399  // Upper layer Event handler
400  ifx_i2c_event_handler_t upper_layer_event_handler;
401  // Trans repeat status
402  uint8_t trans_repeat_status;
403 }ifx_i2c_prl_t;
404 #endif
405 
407 typedef struct ifx_i2c_context
408 {
410  uint8_t slave_address;
412  uint16_t frequency;
414  uint16_t frame_size;
421 #if defined OPTIGA_COMMS_SHIELDED_CONNECTION
422  ifx_i2c_datastore_config_t * ifx_i2c_datastore_config;
424 #endif
433 
436  uint8_t state;
438  uint8_t status;
440  uint8_t reset_state;
444  uint8_t reset_type;
446  uint8_t do_pal_init;
447 #ifdef OPTIGA_COMMS_SHIELDED_CONNECTION
448  // protection level:
449  //0(master unprotected and slave unprotected),1(master protected and slave unprotected),
450  //2(master unprotected and slave protected),3(master protected and slave protected),
451  //255(re-negotiate)
452  uint8_t protection_level;
454  uint8_t protocol_version;
456  uint8_t manage_context_operation;
457 #endif
464 #ifdef OPTIGA_COMMS_SHIELDED_CONNECTION
465  ifx_i2c_prl_t prl;
467 #endif
473 
475 
478 
479 #endif
480 
481 #ifdef __cplusplus
482 }
483 #endif
484 #endif /* _IFX_I2C_CONFIG_H_ */
485 
ifx_i2c_dl::upper_layer_event_handler
ifx_i2c_event_handler_t upper_layer_event_handler
Definition: ifx_i2c_config.h:262
optiga_lib_config.h
This file is defines the compilation switches to build code with required features.
ifx_i2c_context::p_upper_layer_rx_buffer_len
uint16_t * p_upper_layer_rx_buffer_len
Pointer to length of upper layer rx buffer.
Definition: ifx_i2c_config.h:432
IFX_I2C_SESSION_KEY_BUFFER_SIZE
#define IFX_I2C_SESSION_KEY_BUFFER_SIZE
Session key buffer size.
Definition: ifx_i2c_config.h:180
ifx_i2c_tl::actual_packet_length
uint16_t actual_packet_length
Actual length of user provided packet.
Definition: ifx_i2c_config.h:277
upper_layer_callback_t
void(* upper_layer_callback_t)(void *upper_layer_ctx, optiga_lib_status_t event)
typedef for application event handler
Definition: optiga_lib_types.h:96
ifx_i2c_pl::register_action
uint8_t register_action
Action on register, read/write.
Definition: ifx_i2c_config.h:203
pal_i2c.h
This file provides the prototype declarations of PAL I2C.
ifx_i2c_context::upper_layer_event_handler
upper_layer_callback_t upper_layer_event_handler
Upper layer event handler.
Definition: ifx_i2c_config.h:426
ifx_i2c_tl::packet_offset
uint16_t packet_offset
Offset till which data is sent from p_actual_packet.
Definition: ifx_i2c_config.h:279
ifx_i2c_pl::buffer_tx_len
uint16_t buffer_tx_len
Tx length.
Definition: ifx_i2c_config.h:199
IFX_I2C_FRAME_SIZE
#define IFX_I2C_FRAME_SIZE
Data link layer: frame size (max supported is 277 in OPTIGA ).
Definition: ifx_i2c_config.h:77
ifx_i2c_tl::payload_offset
uint8_t payload_offset
Tl rx payload copy offset.
Definition: ifx_i2c_config.h:299
ifx_i2c_dl::tx_seq_nr
uint8_t tx_seq_nr
Tx sequence number.
Definition: ifx_i2c_config.h:238
ifx_i2c_pl::retry_counter
uint16_t retry_counter
Retry counter.
Definition: ifx_i2c_config.h:207
ifx_i2c_dl::frame_start_time
uint32_t frame_start_time
Start time of sending frame.
Definition: ifx_i2c_config.h:260
ifx_i2c_tl_t
struct ifx_i2c_tl ifx_i2c_tl_t
Transport layer structure.
ifx_i2c_pl
Physical layer structure.
Definition: ifx_i2c_config.h:193
ifx_i2c_context::frame_size
uint16_t frame_size
Data link layer frame size.
Definition: ifx_i2c_config.h:414
ifx_i2c_dl::data_poll_timeout
uint32_t data_poll_timeout
Timeout value.
Definition: ifx_i2c_config.h:250
ifx_i2c_dl::retransmit_counter
uint8_t retransmit_counter
Retransmit counter.
Definition: ifx_i2c_config.h:244
ifx_i2c_context::p_upper_layer_ctx
void * p_upper_layer_ctx
Upper layer context.
Definition: ifx_i2c_config.h:428
ifx_i2c_event_handler_t
void(* ifx_i2c_event_handler_t)(struct ifx_i2c_context *p_ctx, optiga_lib_status_t event, const uint8_t *data, uint16_t data_len)
Event handler function prototype.
Definition: ifx_i2c_config.h:186
ifx_i2c_dl::p_rx_frame_buffer
uint8_t * p_rx_frame_buffer
Pointer to main receive buffers.
Definition: ifx_i2c_config.h:258
ifx_i2c_context::p_slave_reset_pin
pal_gpio_t * p_slave_reset_pin
Pointer to pal gpio context for reset.
Definition: ifx_i2c_config.h:418
ifx_i2c_dl::tx_buffer_size
uint16_t tx_buffer_size
Transmit buffer size.
Definition: ifx_i2c_config.h:252
ifx_i2c_context::close_state
optiga_lib_status_t close_state
Close states.
Definition: ifx_i2c_config.h:442
ifx_i2c_dl::p_tx_frame_buffer
uint8_t * p_tx_frame_buffer
Pointer to main transmit buffers.
Definition: ifx_i2c_config.h:256
pal_os_datastore.h
This file provides API prototypes of platform abstraction layer for datastore operations.
ifx_i2c_pl::frame_state
uint8_t frame_state
Frame state.
Definition: ifx_i2c_config.h:214
ifx_i2c_context::p_upper_layer_rx_buffer
uint8_t * p_upper_layer_rx_buffer
Pointer to upper layer rx buffer.
Definition: ifx_i2c_config.h:430
ifx_i2c_context::p_slave_vdd_pin
pal_gpio_t * p_slave_vdd_pin
Pointer to pal gpio context for vdd.
Definition: ifx_i2c_config.h:416
ifx_i2c_pl::request_soft_reset
uint8_t request_soft_reset
Soft reset requested.
Definition: ifx_i2c_config.h:227
pal_i2c
PAL I2C context structure.
Definition: pal_i2c.h:57
ifx_i2c_tl
Transport layer structure.
Definition: ifx_i2c_config.h:267
ifx_i2c_dl
Datalink layer structure.
Definition: ifx_i2c_config.h:232
ifx_i2c_context_0
ifx_i2c_context_t ifx_i2c_context_0
IFX I2C Instance.
ifx_i2c_tl::master_chaining_error_count
uint8_t master_chaining_error_count
Chaining error count for master.
Definition: ifx_i2c_config.h:291
ifx_i2c_context::tx_frame_buffer
uint8_t tx_frame_buffer[IFX_I2C_FRAME_SIZE+1]
IFX I2C tx frame of max length.
Definition: ifx_i2c_config.h:469
ifx_i2c_tl::p_actual_packet
uint8_t * p_actual_packet
Pointer to packet provided by user.
Definition: ifx_i2c_config.h:273
ifx_i2c_pl::frame_action
uint8_t frame_action
Action of frame. Tx/Rx.
Definition: ifx_i2c_config.h:212
pal_os_timer.h
This file provides the prototype declarations of PAL OS timer functionalities.
pal_gpio.h
This file provides the prototype declarations of PAL GPIO.
ifx_i2c_dl::state
uint8_t state
Datalink layer state.
Definition: ifx_i2c_config.h:236
ifx_i2c_tl::upper_layer_event_handler
ifx_i2c_event_handler_t upper_layer_event_handler
Upper layer event handler.
Definition: ifx_i2c_config.h:306
ifx_i2c_tl::p_recv_packet_buffer_length
uint16_t * p_recv_packet_buffer_length
Length of receive buffer.
Definition: ifx_i2c_config.h:285
ifx_i2c_tl::error_event
optiga_lib_status_t error_event
Error event state.
Definition: ifx_i2c_config.h:297
ifx_i2c_context::state
uint8_t state
Definition: ifx_i2c_config.h:436
ifx_i2c_pl_t
struct ifx_i2c_pl ifx_i2c_pl_t
Physical layer structure.
ifx_i2c_context::reset_state
uint8_t reset_state
reset states
Definition: ifx_i2c_config.h:440
ifx_i2c_context::tl
ifx_i2c_tl_t tl
Transport layer context.
Definition: ifx_i2c_config.h:459
optiga_lib_logger.h
This file provides the prototypes for the OPTIGA library logger.
ifx_i2c_context::status
uint8_t status
ifx i2c wrapper api status
Definition: ifx_i2c_config.h:438
ifx_i2c_context::pal_os_event_ctx
void * pal_os_event_ctx
Definition: ifx_i2c_config.h:472
ifx_i2c_dl::rx_buffer_size
uint16_t rx_buffer_size
Receive buffer size.
Definition: ifx_i2c_config.h:254
ifx_i2c_context::reset_type
uint8_t reset_type
type of reset
Definition: ifx_i2c_config.h:444
ifx_i2c_tl::p_recv_packet_buffer
uint8_t * p_recv_packet_buffer
Pointer to user provided receive buffer.
Definition: ifx_i2c_config.h:283
ifx_i2c_dl::action_rx_only
uint8_t action_rx_only
Indicate only Rx required.
Definition: ifx_i2c_config.h:242
ifx_i2c_dl_t
struct ifx_i2c_dl ifx_i2c_dl_t
Datalink layer structure.
ifx_i2c_context::dl
ifx_i2c_dl_t dl
Datalink layer context.
Definition: ifx_i2c_config.h:461
ifx_i2c_context::do_pal_init
uint8_t do_pal_init
init pal
Definition: ifx_i2c_config.h:446
ifx_i2c_pl::negotiate_state
uint8_t negotiate_state
Negotiation state.
Definition: ifx_i2c_config.h:225
ifx_i2c_tl::total_recv_length
uint16_t total_recv_length
Total received data.
Definition: ifx_i2c_config.h:275
ifx_i2c_tl::max_packet_length
uint16_t max_packet_length
Maximum length of packet at transport layer.
Definition: ifx_i2c_config.h:281
ifx_i2c_context::p_pal_i2c_ctx
pal_i2c_t * p_pal_i2c_ctx
Pointer to pal i2c context.
Definition: ifx_i2c_config.h:420
ifx_i2c_pl::i2c_cmd
uint8_t i2c_cmd
i2c read/i2c write
Definition: ifx_i2c_config.h:205
ifx_i2c_tl::state
uint8_t state
Transport layer state.
Definition: ifx_i2c_config.h:271
ifx_i2c_context::frequency
uint16_t frequency
Frequency of i2c master.
Definition: ifx_i2c_config.h:412
ifx_i2c_context::rx_frame_buffer
uint8_t rx_frame_buffer[IFX_I2C_FRAME_SIZE+1]
IFX I2C rx frame of max length.
Definition: ifx_i2c_config.h:471
ifx_i2c_context
IFX I2C context structure.
Definition: ifx_i2c_config.h:408
ifx_i2c_pl::tx_frame_len
uint16_t tx_frame_len
Length of data to be sent.
Definition: ifx_i2c_config.h:218
ifx_i2c_dl::error
uint8_t error
Error occured.
Definition: ifx_i2c_config.h:246
ifx_i2c_tl::tx_payload_offset
uint8_t tx_payload_offset
Tl tx payload copy offset.
Definition: ifx_i2c_config.h:301
ifx_i2c_pl::buffer_rx_len
uint16_t buffer_rx_len
Rx length.
Definition: ifx_i2c_config.h:201
pal_gpio
Structure defines the PAL GPIO configuration.
Definition: pal_gpio.h:52
ifx_i2c_pl::upper_layer_event_handler
ifx_i2c_event_handler_t upper_layer_event_handler
Definition: ifx_i2c_config.h:220
ifx_i2c_pl::p_tx_frame
uint8_t * p_tx_frame
Pointer to data to be sent.
Definition: ifx_i2c_config.h:216
ifx_i2c_pl::buffer
uint8_t buffer[IFX_I2C_FRAME_SIZE+1]
Physical layer buffer.
Definition: ifx_i2c_config.h:197
ifx_i2c_context_t
struct ifx_i2c_context ifx_i2c_context_t
IFX I2C context structure.
Definition: ifx_i2c_config.h:183
ifx_i2c_tl::chaining_error_count
uint8_t chaining_error_count
Chaining error count from slave.
Definition: ifx_i2c_config.h:289
ifx_i2c_dl::rx_seq_nr
uint8_t rx_seq_nr
Definition: ifx_i2c_config.h:240
ifx_i2c_context::slave_address
uint8_t slave_address
I2C Slave address.
Definition: ifx_i2c_config.h:410
ifx_i2c_tl::previous_chaining
uint8_t previous_chaining
State to check last chaining state.
Definition: ifx_i2c_config.h:293
ifx_i2c_tl::api_start_time
uint32_t api_start_time
Start time of the transport layer API.
Definition: ifx_i2c_config.h:287
optiga_lib_status_t
uint16_t optiga_lib_status_t
typedef for OPTIGA host library status
Definition: optiga_lib_types.h:90
ifx_i2c_tl::transmission_completed
uint8_t transmission_completed
transmission done
Definition: ifx_i2c_config.h:295
ifx_i2c_tl::initialization_state
uint8_t initialization_state
Initial state check.
Definition: ifx_i2c_config.h:303
ifx_i2c_context::pl
ifx_i2c_pl_t pl
Physical layer context.
Definition: ifx_i2c_config.h:463
ifx_i2c_dl::resynced
uint8_t resynced
Resynced.
Definition: ifx_i2c_config.h:248