1 /* 2 * Copyright (C) 2010 NXP Semiconductors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* 18 * \file phLlcNfc_DataTypes.h 19 * \brief Contains the structure information. 20 * 21 * Project: NFC-FRI-1.1 22 * 23 * $Date: Fri Apr 30 10:03:36 2010 $ 24 * $Author: ing02260 $ 25 * $Revision: 1.43 $ 26 * $Aliases: NFC_FRI1.1_WK1017_R34_1,NFC_FRI1.1_WK1017_R34_2,NFC_FRI1.1_WK1023_R35_1 $ 27 * 28 */ 29 30 #ifndef PHLLCNFC_DATATYPES_H 31 #define PHLLCNFC_DATATYPES_H 32 33 /** 34 * \name LLC NFC frame creation, deletion and processing 35 * 36 * File: \ref phLlcNfc_DataTypes.h 37 * 38 */ 39 /*@{*/ 40 #define PH_LLCNFC_DATATYPES_FILEREVISION "$Revision: 1.43 $" /**< \ingroup grp_hal_nfc_llc */ 41 #define PH_LLCNFC_DATATYPES_FILEALIASES "$Aliases: NFC_FRI1.1_WK1017_R34_1,NFC_FRI1.1_WK1017_R34_2,NFC_FRI1.1_WK1023_R35_1 $" /**< \ingroup grp_hal_nfc_llc */ 42 /*@}*/ 43 /*************************** Includes *******************************/ 44 #include <phNfcCompId.h> 45 /*********************** End of includes ****************************/ 46 /***************************** Macros *******************************/ 47 48 /* Trace buffer declaration */ 49 #if defined (LLC_TRACE) 50 #include <phOsalNfc.h> 51 #include <stdio.h> 52 53 extern char phOsalNfc_DbgTraceBuffer[]; 54 #define trace_buffer phOsalNfc_DbgTraceBuffer 55 56 #define MAX_TRACE_BUFFER 150 57 #define PH_LLCNFC_PRINT( str ) phOsalNfc_DbgString(str) 58 #define PH_LLCNFC_PRINT_DATA(buf,len) 59 #define PH_LLCNFC_STRING( str ) 60 #define PH_LLCNFC_DEBUG(str, arg) \ 61 { \ 62 snprintf(trace_buffer,MAX_TRACE_BUFFER,str,arg); \ 63 phOsalNfc_DbgString(trace_buffer); \ 64 } 65 #define PH_LLCNFC_PRINT_BUFFER(buf,len) \ 66 { \ 67 /* uint16_t i = 0; \ 68 char trace_buffer[MAX_TRACE_BUFFER]; \ 69 snprintf(trace_buffer,MAX_TRACE_BUFFER,"\n\t %s:",msg); \ 70 phOsalNfc_DbgString(trace); */\ 71 phOsalNfc_DbgTrace(buf,len); \ 72 phOsalNfc_DbgString("\r"); \ 73 } 74 #endif /* #if defined (LLC_TRACE) */ 75 76 77 #if (!defined (LLC_TRACE) && defined (LLC_DATA_BYTES)) 78 #include <phOsalNfc.h> 79 80 extern char phOsalNfc_DbgTraceBuffer[]; 81 #define trace_buffer phOsalNfc_DbgTraceBuffer 82 83 #define PH_LLCNFC_PRINT( str ) 84 #define PH_LLCNFC_PRINT_BUFFER(buf, len) 85 #define PH_LLCNFC_DEBUG(str, arg1) 86 #define PH_LLCNFC_STRING( str ) phOsalNfc_DbgString(str) 87 #define PH_LLCNFC_PRINT_DATA(buf,len) \ 88 { \ 89 /* uint16_t i = 0; \ 90 char trace_buffer[MAX_TRACE_BUFFER]; \ 91 snprintf(trace_buffer,MAX_TRACE_BUFFER,"\n\t %s:",msg); \ 92 phOsalNfc_DbgString(trace_buffer); */\ 93 phOsalNfc_DbgTrace(buf,len); \ 94 } 95 #endif /* #if (!defined (LLC_TRACE) && defined (LLC_DATA_BYTES)) */ 96 97 98 #if (!defined (LLC_TRACE) && !defined (LLC_DATA_BYTES)) 99 /** To disable prints */ 100 #define PH_LLCNFC_PRINT(str) 101 #define PH_LLCNFC_PRINT_BUFFER(buf, len) 102 #define PH_LLCNFC_DEBUG(str, arg1) 103 #define PH_LLCNFC_PRINT_DATA(buf,len) 104 #define PH_LLCNFC_STRING( str ) 105 #endif /* #if (!defined (LLC_TRACE) && !defined (LLC_DATA_BYTES)) */ 106 107 108 /* If the below MACRO (RECV_NR_CHECK_ENABLE) is 109 ENABLED : then check for the NR frame received from PN544 in the I frame is 110 added. This shall be greater than sent NS from the HOST. 111 This is used to stop the timer 112 DISABLED : dont check the N9R) frame received from the PN544 113 */ 114 /* #define RECV_NR_CHECK_ENABLE */ 115 116 /* If the below MACRO (LLC_UPP_LAYER_NTFY_WRITE_RSP_CB) is 117 ENABLED : then if an I frame is received and the 118 upper layer response callback (before another READ is pended) is called 119 only after sending S frame and wait for the callback and then notify the 120 upper layer 121 DISABLED : then if an I frame is received and the 122 upper layer response callback (before another READ is pended) is called 123 immediately after sending S frame (not waiting for the sent S frame 124 callback) 125 */ 126 #define LLC_UPP_LAYER_NTFY_WRITE_RSP_CB 127 128 /* PN544 continuously sends an incorrect I frames to the HOST, 129 even after the REJ frame from HOST to PN544 130 If the below MACRO (LLC_RR_INSTEAD_OF_REJ) is 131 ENABLED : then if the received NS = (expected NR - 1) then instead of REJ 132 RR frame is sent 133 DISABLED : then REJ frame is sent 134 */ 135 #define LLC_RR_INSTEAD_OF_REJ 136 137 #define SEND_UFRAME 138 139 /* If the below MACRO (CTRL_WIN_SIZE_COUNT) is 140 ENABLED : then window size will be maximum 141 DISABLED : then window size is 1 142 */ 143 #define CTRL_WIN_SIZE_COUNT 144 145 /* 146 If the below MACRO (LLC_URSET_NO_DELAY) is 147 ENABLED : then after receiving the UA frame, then immediately this will be 148 notified or further operation will be carried on. 149 DISABLED : then after receiving the UA frame, then a timer is started, to 150 delay the notifiation or to carry on the next operation 151 */ 152 #define LLC_URSET_NO_DELAY 153 154 #ifdef LLC_UPP_LAYER_NTFY_WRITE_RSP_CB 155 156 /* NO definition is required */ 157 158 #else 159 160 /* 161 This macro is useful only if the LLC_UPP_LAYER_NTFY_WRITE_RSP_CB is DISABLED 162 If the below MACRO (LLC_RELEASE_FLAG) is 163 ENABLED : then whenever LLC release is called the g_release_flag variable 164 will be made TRUE. Also, NO notification is allowed to the 165 upper layer. 166 DISABLED : g_release_flag is not declared and not used 167 */ 168 #define LLC_RELEASE_FLAG 169 170 #endif /* #ifdef LLC_UPP_LAYER_NTFY_WRITE_RSP_CB */ 171 172 /* 173 Actually, there is a send and receive error count, if either of them reaches 174 limit, then exception is raised. 175 If the below MACRO (LLC_RSET_INSTEAD_OF_EXCEPTION) is 176 ENABLED : then exception is not raised, instead a U RSET command is sent. 177 DISABLED : then exception is raised 178 */ 179 /* #define LLC_RSET_INSTEAD_OF_EXCEPTION */ 180 181 #define LLC_SEND_ERROR_COUNT 182 183 #define RECV_ERROR_FRAME_COUNT (0x50U) 184 #define SENT_ERROR_FRAME_COUNT (0x50U) 185 186 /** Initial bytes to read */ 187 #define PH_LLCNFC_BYTES_INIT_READ (1) 188 /** Maximum buffer that I frame can send */ 189 #define PH_LLCNFC_MAX_IFRAME_BUFLEN (29) 190 #define PH_LLCNFC_MAX_UFRAME_BUFLEN (4) 191 #define PH_LLCNFC_CRC_LENGTH (2) 192 #define PH_LLCNFC_MAX_LLC_PAYLOAD ((PH_LLCNFC_MAX_IFRAME_BUFLEN) + (4)) 193 /** Maximum timer used in the Llc */ 194 #define PH_LLCNFC_MAX_TIMER_USED (2) 195 /** Maximum timer used in the Llc */ 196 #define PH_LLCNFC_MAX_ACK_GUARD_TIMER (4) 197 /** Maximum I frame that can be stored */ 198 #define PH_LLCNFC_MAX_I_FRAME_STORE (8) 199 200 /** Read pending for one byte */ 201 #define PH_LLCNFC_READPEND_ONE_BYTE (0x01U) 202 /** Read pending for remaining byte */ 203 #define PH_LLCNFC_READPEND_REMAIN_BYTE (0x02U) 204 /** Read pending not done */ 205 #define PH_LLCNFC_READPEND_FLAG_OFF FALSE 206 #define PH_LLCNFC_MAX_REJ_RETRY_COUNT (10) 207 208 209 /**** Macros for state machine ****/ 210 211 typedef enum phLlcNfc_State 212 { 213 /** This specifies that LLC is in uninitialise state */ 214 phLlcNfc_Uninitialise_State = 0x00, 215 /** This specifies that LLC initialise is in progress */ 216 phLlcNfc_Initialising_State = 0x01, 217 /** This specifies that LLC is in initialise is complete */ 218 phLlcNfc_Initialised_State = 0x02, 219 /** This specifies that LLC is send with the 220 lower layer */ 221 phLlcNfc_Sending_State = 0x03, 222 /** This specifies that LLC is receive with the 223 lower layer */ 224 phLlcNfc_Receiving_State = 0x04, 225 /** This specifies that LLC is receive wait with the 226 lower layer */ 227 phLlcNfc_ReceiveWait_State = 0x05, 228 /** This specifies that LLC is resending the I frames */ 229 phLlcNfc_Resend_State = 0x06 230 }phLlcNfc_State_t; 231 /**** Macros for state machine end ****/ 232 233 /************************ End of macros *****************************/ 234 235 /********************** Callback functions **************************/ 236 237 /******************* End of Callback functions **********************/ 238 239 /********************* Structures and enums *************************/ 240 /** 241 * \ingroup grp_hal_nfc_llc 242 * \brief Enum to get the baud rate 243 * 244 * This enum contains the baud rate information. 245 * 246 */ 247 /*@{*/ 248 typedef enum phLlcNfc_LlcBaudRate 249 { 250 /** Baud rate = 9600 */ 251 phLlcNfc_e_9600 = 0x00, 252 /** Baud rate = 19200 */ 253 phLlcNfc_e_19200 = 0x01, 254 /** Baud rate = 28800 */ 255 phLlcNfc_e_28800 = 0x02, 256 /** Baud rate = 38400 */ 257 phLlcNfc_e_38400 = 0x03, 258 /** Baud rate = 57600 */ 259 phLlcNfc_e_57600 = 0x04, 260 /** Baud rate = 115200 */ 261 phLlcNfc_e_115200 = 0x05, 262 /** Baud rate = 23400 */ 263 phLlcNfc_e_234000 = 0x06, 264 /** Baud rate = 46800 */ 265 phLlcNfc_e_460800 = 0x07, 266 /** Baud rate = 921600 */ 267 phLlcNfc_e_921600 = 0x08, 268 /** Baud rate = 1228000 */ 269 phLlcNfc_e_1228000 = 0x09, 270 /** Baud rate error */ 271 phLlcNfc_e_bdrate_err = 0xFF 272 }phLlcNfc_LlcBaudRate_t; 273 /*@}*/ 274 275 /** 276 * \ingroup grp_hal_nfc_llc 277 * \brief Enum to select the U or I or S frame 278 * 279 * This enum is to set the frames. 280 * 281 */ 282 /*@{*/ 283 typedef enum phLlcNfc_LlcCmd 284 { 285 /** This command is for I frame (no command) */ 286 phLlcNfc_e_no_cmd = 0xFF, 287 /** This command is for U frame */ 288 phLlcNfc_e_rset = 0x19, 289 /** This command is for U frame */ 290 phLlcNfc_e_ua = 0x06, 291 /** This is RR command for S frame */ 292 phLlcNfc_e_rr = 0x00, 293 /** This is REJ command for S frame */ 294 phLlcNfc_e_rej = 0x08, 295 /** This is RNR command for S frame */ 296 phLlcNfc_e_rnr = 0x10, 297 /** This is SREJ command for S frame */ 298 phLlcNfc_e_srej = 0x18, 299 /** Error command */ 300 phLlcNfc_e_error = 0xFE 301 }phLlcNfc_LlcCmd_t; 302 /*@}*/ 303 304 /** 305 * \ingroup grp_hal_nfc_llc 306 * \brief Enum to select the U or I or S frame 307 * 308 * This enum is to set the frames. 309 * 310 */ 311 /*@{*/ 312 typedef enum phLlcNfc_FrameType 313 { 314 /** U frame type */ 315 phLlcNfc_eU_frame = 0x00, 316 /** I frame type */ 317 phLlcNfc_eI_frame = 0x01, 318 /** S frame type */ 319 phLlcNfc_eS_frame = 0x02, 320 /** Error frame type */ 321 phLlcNfc_eErr_frame = 0x03 322 }phLlcNfc_FrameType_t; 323 /*@}*/ 324 325 /** 326 * \ingroup grp_hal_nfc_llc 327 * \brief LLC sent frame type 328 * 329 * This enum values defines what are the frames sent to the PN544 330 * 331 */ 332 /*@{*/ 333 334 typedef enum phLlcNfc_eSentFrameType 335 { 336 invalid_frame, 337 /* During initialisation the U RSET is sent to PN544 */ 338 init_u_rset_frame, 339 /* During initialisation the UA is sent to PN544 */ 340 init_u_a_frame, 341 /* After unsuccessful retries of sending I frame to PN544, 342 URSET is sent */ 343 u_rset_frame, 344 /* If PN544 sends the URSET frame in between any transaction, then 345 the UA response shall be sent */ 346 u_a_frame, 347 /* S frame is sent to PN544, this will be sent only if an I frame 348 is received from PN544 */ 349 s_frame, 350 /* User has sent an I frame, for that a write response callback 351 shall be called */ 352 user_i_frame, 353 /* LLC, internally (means stored non acknowledged frames) has sent 354 an I frame as it doesnt get a proper acknowledgement */ 355 resend_i_frame, 356 /* LLC, internally (means stored non acknowledged frames) has sent 357 an I frame as it doesnt get a reject as acknowledgement */ 358 rejected_i_frame, 359 /* LLC has received a I frame for the re-sent I frames, so an S 360 frame is sent */ 361 resend_s_frame, 362 /* LLC has received a I frame for the re-sent I frames, so an S 363 frame is sent */ 364 resend_rej_s_frame, 365 /* PN544 has sent an I frame, which is wrong, so send a reject S 366 frame */ 367 reject_s_frame, 368 #ifdef LLC_RR_INSTEAD_OF_REJ 369 370 /* RR is sent instead of REJECT */ 371 rej_rr_s_frame, 372 373 #endif /* #ifdef LLC_RR_INSTEAD_OF_REJ */ 374 /* For any of the above sent frames, the response shall be received */ 375 write_resp_received 376 }phLlcNfc_eSentFrameType_t; 377 378 /*@}*/ 379 380 /** 381 * \ingroup grp_hal_nfc_llc 382 * \brief LLC payload 383 * 384 * This structure contains both the header information and 385 * the exact length of the buffer. 386 * 387 */ 388 /*@{*/ 389 typedef struct phLlcNfc_Payload 390 { 391 /** Llc header information */ 392 uint8_t llcheader; 393 394 /** User or received buffer */ 395 uint8_t llcpayload[PH_LLCNFC_MAX_LLC_PAYLOAD]; 396 }phLlcNfc_Payload_t; 397 /*@}*/ 398 399 /** 400 * \ingroup grp_hal_nfc_llc 401 * \brief Llc buffer 402 * 403 * This structure contains the information of the LLC length byte 404 * and payload. 405 * 406 */ 407 /*@{*/ 408 typedef struct phLlcNfc_Buffer 409 { 410 /** Llc length */ 411 uint8_t llc_length_byte; 412 413 /** LLC data including the LLC header and CRC */ 414 phLlcNfc_Payload_t sllcpayload; 415 }phLlcNfc_Buffer_t; 416 /*@}*/ 417 418 /** 419 * \ingroup grp_hal_nfc_llc 420 * \brief Packet information 421 * 422 * This structure contains the length and buffer of the packet. 423 * 424 */ 425 /*@{*/ 426 typedef struct phLlcNfc_LlcPacket 427 { 428 /** Complete LLC buffer */ 429 phLlcNfc_Buffer_t s_llcbuf; 430 431 /** LLC buffer length */ 432 uint8_t llcbuf_len; 433 434 /** Stored frame needs completion callback, to be sent to HCI */ 435 phLlcNfc_eSentFrameType_t frame_to_send; 436 437 }phLlcNfc_LlcPacket_t; 438 /*@}*/ 439 440 /** 441 * \ingroup grp_hal_nfc_llc_helper 442 * \brief I frame details 443 * 444 * This structure stores the information of the I frame 445 * (to support sliding window). 446 * 447 */ 448 /*@{*/ 449 typedef struct phLlcNfc_StoreIFrame 450 { 451 /** Complete LLC packet */ 452 phLlcNfc_LlcPacket_t s_llcpacket[PH_LLCNFC_MAX_I_FRAME_STORE]; 453 454 /** Window size count */ 455 uint8_t winsize_cnt; 456 457 /** Start position */ 458 uint8_t start_pos; 459 460 }phLlcNfc_StoreIFrame_t; 461 /*@}*/ 462 463 /** 464 * \ingroup grp_hal_nfc_llc 465 * \brief LLC timer information 466 * 467 * This structure contains the timer related information 468 * 469 */ 470 /*@{*/ 471 typedef struct phLlcNfc_Timerinfo 472 { 473 /** Store the timer id for each timer create */ 474 uint32_t timer_id[PH_LLCNFC_MAX_TIMER_USED]; 475 476 /** This will store the connection time out value */ 477 uint16_t con_to_value; 478 479 /** This will store the guard time out values */ 480 uint16_t guard_to_value[PH_LLCNFC_MAX_ACK_GUARD_TIMER]; 481 482 /** This will store the guard time out values */ 483 uint16_t iframe_send_count[PH_LLCNFC_MAX_ACK_GUARD_TIMER]; 484 485 /** This will store ns value for the sent N(S) */ 486 uint8_t timer_ns_value[PH_LLCNFC_MAX_ACK_GUARD_TIMER]; 487 488 /** Each frame stored needs to be */ 489 uint8_t frame_type[PH_LLCNFC_MAX_ACK_GUARD_TIMER]; 490 491 /** Index to re-send */ 492 uint8_t index_to_send; 493 494 /** This is a count for gaurd time out */ 495 uint8_t guard_to_count; 496 497 #ifdef PIGGY_BACK 498 /** This will store the ack time out values */ 499 uint16_t ack_to_value[PH_LLCNFC_MAX_ACK_GUARD_TIMER]; 500 #endif /* #ifdef PIGGY_BACK */ 501 502 /** This is a timer flag 503 Bit 0 = 1 means connection time out started else stopped 504 Bit 1 = 1 means guard time out started else stopped 505 Bit 2 = 1 means ack time out started else stopped 506 */ 507 uint8_t timer_flag; 508 }phLlcNfc_Timerinfo_t; 509 /*@}*/ 510 511 /** 512 * \ingroup grp_hal_nfc_llc 513 * \brief LLC frame information 514 * 515 * This structure contains the information of the LLC frame. 516 * 517 */ 518 /*@{*/ 519 typedef struct phLlcNfc_Frame 520 { 521 /** N(S) - Number of information frame */ 522 uint8_t n_s; 523 524 /** N(R) - Number of next information frame to receive */ 525 uint8_t n_r; 526 527 /** Store the window size */ 528 uint8_t window_size; 529 530 /** SREJ is optional, so store the flag whether it is set or not */ 531 uint8_t srej_on_off; 532 533 /** Store the baud rate */ 534 uint8_t baud_rate; 535 536 /** Flag to find the rset_recvd */ 537 uint8_t rset_recvd; 538 539 /** Complete LLC packet information */ 540 phLlcNfc_LlcPacket_t s_llcpacket; 541 542 /** Store the I frames, that has been sent, Storage will be till 543 the window size */ 544 phLlcNfc_StoreIFrame_t s_send_store; 545 546 #ifdef PIGGY_BACK 547 /** Store the I frames, that has been received, Storage will be 548 till the window size */ 549 phLlcNfc_StoreIFrame_t s_recv_store; 550 #endif /* #ifdef PIGGY_BACK */ 551 552 /** To receive the packet sent by below layer */ 553 phLlcNfc_LlcPacket_t s_recvpacket; 554 555 /** Number of window I frames has to be sent again */ 556 uint8_t rejected_ns; 557 558 /** To store the count received error frames like 559 wrong CRC, REJ and RNR frames */ 560 uint8_t recv_error_count; 561 562 /** Sending error frames like REJ frames to the PN544 */ 563 uint8_t send_error_count; 564 565 /** Send U frame count */ 566 uint8_t retry_cnt; 567 568 /** Read pending flag, to know that read is already pended 569 or not. Use the below macros to ON and OFF the flag 570 PH_LLCNFC_READPEND_FLAG_OFF 571 PH_LLCNFC_READPEND_ONE_BYTE 572 PH_LLCNFC_READPEND_REMAIN_BYTE 573 */ 574 uint8_t read_pending; 575 576 /** Write pending */ 577 uint8_t write_pending; 578 579 /** Sent frame type */ 580 phLlcNfc_eSentFrameType_t sent_frame_type; 581 582 /** upper receive called */ 583 uint8_t upper_recv_call; 584 585 /** Status returned during DAL write */ 586 NFCSTATUS write_status; 587 588 /** Depending on the "write_status", write call has to be called */ 589 phLlcNfc_eSentFrameType_t write_wait_call; 590 }phLlcNfc_Frame_t; 591 /*@}*/ 592 593 /** 594 * \ingroup grp_hal_nfc_llc 595 * \brief LLC Component Context Structure 596 * 597 * This structure is used to store the current context information 598 * of the instance. 599 * 600 */ 601 /*@{*/ 602 typedef struct phLlcNfc_Context 603 { 604 /** Information regarding all the LLC frame */ 605 phLlcNfc_Frame_t s_frameinfo; 606 607 /** Local send and receive */ 608 phNfc_sLowerIF_t lower_if; 609 610 /** Register attention, send and receive callback from the 611 register functions of the upper layer */ 612 phNfcIF_sCallBack_t cb_for_if; 613 614 /** Store the length, which shall be sent later through the 615 "send complete" callback */ 616 uint32_t send_cb_len; 617 618 /** Receive buffer provided by the upper layer */ 619 uint8_t precv_buf[PH_LLCNFC_MAX_LLC_PAYLOAD]; 620 621 /** Receive length provided by the upper layer */ 622 uint32_t recvbuf_length; 623 624 /** Llc state */ 625 phLlcNfc_State_t state; 626 627 /** Hardware information */ 628 void *phwinfo; 629 630 /** Timer information */ 631 phLlcNfc_Timerinfo_t s_timerinfo; 632 }phLlcNfc_Context_t; 633 /*@}*/ 634 /****************** End of structures and enums *********************/ 635 636 /******************** Function declarations *************************/ 637 638 /******************** Function declarations *************************/ 639 #endif /* PHLLCNFC_DATATYPES_H */ 640 641 642