Changeset 1286


Ignore:
Timestamp:
01/19/10 12:28:04 (11 years ago)
Author:
dingo35
Message:

Move T1 timing aspects to pps.c and fix selected_protocol error

Location:
trunk/csctapi
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/csctapi/pps.c

    r1285 r1286  
    3939#define PPS_DEFAULT_PROTOCOL    0x00
    4040#define PROTOCOL_T0_DEFAULT_WI 10
     41
     42#define PROTOCOL_T1_DEFAULT_IFSC        32
     43#define PROTOCOL_T1_DEFAULT_IFSD        32
     44#define PROTOCOL_T1_MAX_IFSC            251  /* Cannot send > 255 buffer */
     45#define PROTOCOL_T1_DEFAULT_CWI         13
     46#define PROTOCOL_T1_DEFAULT_BWI         4
     47#define PROTOCOL_T1_EDC_LRC             0
     48#define PROTOCOL_T1_EDC_CRC             1
    4149/*
    4250 * Not exported macros definition
     
    486494}
    487495
     496int Protocol_T1_Init ()
     497{
     498    BYTE ta, tb, tc, cwi, bwi;
     499    unsigned long baudrate;
     500    double work_etu;
     501    int i;
     502
     503    // Set IFSC
     504    if (ATR_GetInterfaceByte (atr, 3, ATR_INTERFACE_BYTE_TA, &ta) == ATR_NOT_FOUND)
     505        ifsc = PROTOCOL_T1_DEFAULT_IFSC;
     506    else if ((ta != 0x00) && (ta != 0xFF))
     507        ifsc = ta;
     508    else
     509        ifsc = PROTOCOL_T1_DEFAULT_IFSC;
     510
     511    // Towitoko does not allow IFSC > 251 //FIXME not sure whether this limitation still exists
     512    ifsc = MIN (ifsc, PROTOCOL_T1_MAX_IFSC);
     513
     514    // Set IFSD
     515    ifsd = PROTOCOL_T1_DEFAULT_IFSD;
     516
     517#ifndef PROTOCOL_T1_USE_DEFAULT_TIMINGS
     518    // Calculate CWI and BWI
     519    if (ATR_GetInterfaceByte (atr, 3, ATR_INTERFACE_BYTE_TB, &tb) == ATR_NOT_FOUND)
     520        {
     521#endif
     522            cwi = PROTOCOL_T1_DEFAULT_CWI;
     523            bwi = PROTOCOL_T1_DEFAULT_BWI;
     524#ifndef PROTOCOL_T1_USE_DEFAULT_TIMINGS
     525        }
     526    else
     527        {
     528            cwi = tb & 0x0F;
     529            bwi = (tb & 0xF0) >> 4;
     530        }
     531#endif
     532   
     533    // Work etu = (1000 / baudrate) milliseconds
     534    ICC_Async_GetBaudrate (&baudrate);
     535    work_etu = 1000 / (double)baudrate;
     536
     537    // Set CWT = (2^CWI + 11) work etu
     538    cwt = 1;
     539
     540    for (i = 0; i < cwi ; i++)
     541        cwt *= 2;
     542
     543    cwt = (unsigned short) ((cwt + 11) * work_etu);
     544
     545    // Set BWT = (2^BWI * 960 + 11) work etu
     546    bwt = 1;
     547    for (i = 0; i < bwi; i++)
     548        bwt *= 2;
     549
     550    bwt = (unsigned short) ((bwt * 960 + 11) * work_etu);
     551
     552    // Set BGT = 22 * work etu
     553    bgt = (unsigned short) (22 * work_etu);
     554
     555    // Set the error detection code type
     556    if (ATR_GetInterfaceByte (atr, 3, ATR_INTERFACE_BYTE_TC, &tc) == ATR_NOT_FOUND)
     557        edc = PROTOCOL_T1_EDC_LRC;
     558    else
     559        edc = tc & 0x01;
     560
     561    // Set initial send sequence (NS)
     562    ns = 1;
     563   
     564    // Set timings
     565    icc_timings.block_timeout = bwt;
     566    icc_timings.char_timeout = cwt;
     567    icc_timings.block_delay = bgt;
     568    ICC_Async_SetTimings ();
     569
     570#ifdef DEBUG_PROTOCOL
     571    printf ("Protocol: T=1: IFSC=%d, IFSD=%d, CWT=%d, BWT=%d, BGT=%d, EDC=%s\n",
     572                    ifsc, ifsd, cwt, bwt, t1->bgt,
     573                    (edc == PROTOCOL_T1_EDC_LRC) ? "LRC" : "CRC");
     574#endif
     575
     576    return PROTOCOL_T1_OK;
     577}
     578
    488579static int PPS_InitProtocol ()
    489580{
    490     int ret;
    491    
    492     if ((parameters.t == ATR_PROTOCOL_TYPE_T0) || (parameters.t == ATR_PROTOCOL_TYPE_T14))
    493     {
    494         BYTE wi;
    495        
    496         /* Integer value WI  = TC2, by default 10 */
     581    switch (parameters.t) {
     582        case ATR_PROTOCOL_TYPE_T0:
     583        case ATR_PROTOCOL_TYPE_T14:
     584            {
     585            BYTE wi;
     586            /* Integer value WI = TC2, by default 10 */
    497587#ifndef PROTOCOL_T0_USE_DEFAULT_TIMINGS
    498         if (ATR_GetInterfaceByte (atr, 2, ATR_INTERFACE_BYTE_TC, &(wi)) != ATR_OK)
    499 #endif
    500         wi = PROTOCOL_T0_DEFAULT_WI;
    501        
    502         /* WWT = 960 * WI * (Fi / f) * 1000 milliseconds */
    503         double F =  (double) atr_f_table[parameters.FI];
    504         unsigned long wwt = (long unsigned int) (960 * wi * (F / ICC_Async_GetClockRate ()) * 1000);
    505         if (parameters.t == 14)
    506             wwt >>= 1; //is this correct?
    507        
    508         /* Set timings */
    509         icc_timings.block_timeout = wwt;
    510         icc_timings.char_timeout = wwt;
    511         ICC_Async_SetTimings ();
    512        
    513 #ifdef DEBUG_PROTOCOL
    514         printf ("Protocol: T=%i: WWT=%d, Clockrate=%lu\n", params->t, (int)(wwt),ICC_Async_GetClockRate());
    515 #endif
    516         return PPS_OK;
    517     }
    518     else if (parameters.t == ATR_PROTOCOL_TYPE_T1)
    519     {
    520             ret = Protocol_T1_Init ();
     588            if (ATR_GetInterfaceByte (atr, 2, ATR_INTERFACE_BYTE_TC, &(wi)) != ATR_OK)
     589#endif
     590            wi = PROTOCOL_T0_DEFAULT_WI;
     591
     592            /* WWT = 960 * WI * (Fi / f) * 1000 milliseconds */
     593            double F =  (double) atr_f_table[parameters.FI];
     594            unsigned long wwt = (long unsigned int) (960 * wi * (F / ICC_Async_GetClockRate ()) * 1000);
     595            if (parameters.t == 14)
     596                wwt >>= 1; //is this correct?
    521597           
    522             if (ret != PROTOCOL_T1_OK)
    523             {
    524                 protocol = NULL;
    525                 return PPS_PROTOCOL_ERROR;
    526             }
    527            
    528             return PPS_OK;
    529     }
    530     else
    531     {
    532         protocol = NULL;
    533     }
    534    
    535     return PPS_PROTOCOL_ERROR;
     598            /* Set timings */
     599            icc_timings.block_timeout = wwt;
     600            icc_timings.char_timeout = wwt;
     601            ICC_Async_SetTimings ();
     602#ifdef DEBUG_PROTOCOL
     603            printf ("Protocol: T=%i: WWT=%d, Clockrate=%lu\n", params->t, (int)(wwt),ICC_Async_GetClockRate());
     604#endif
     605            }
     606            break;
     607     case ATR_PROTOCOL_TYPE_T1:
     608            Protocol_T1_Init ();//always returns ok
     609            break;
     610     default:
     611            protocol = NULL;
     612            return PPS_PROTOCOL_ERROR;
     613            break;
     614    }
     615    return PPS_OK;
    536616}
    537617
  • trunk/csctapi/protocol_t1.c

    r1283 r1286  
    3131
    3232/*
    33  * Not exported constants definition
    34  */
    35 #define PROTOCOL_T1_DEFAULT_IFSC        32
    36 #define PROTOCOL_T1_DEFAULT_IFSD        32
    37 #define PROTOCOL_T1_MAX_IFSC            251  /* Cannot send > 255 buffer */
    38 #define PROTOCOL_T1_DEFAULT_CWI         13
    39 #define PROTOCOL_T1_DEFAULT_BWI         4
    40 #define PROTOCOL_T1_EDC_LRC             0
    41 #define PROTOCOL_T1_EDC_CRC             1
    42 
    43 /*
    4433 * Not exported functions declaration
    4534 */
     
    5746 * Exproted funtions definition
    5847 */
    59 
    60 int
    61 Protocol_T1_Init (int selected_protocol)
    62 {
    63   BYTE ta, tb, tc, cwi, bwi;
    64   unsigned long baudrate;
    65   double work_etu;
    66   int i;
    67 
    68   /* Set IFSC */
    69   if (ATR_GetInterfaceByte (atr, selected_protocol, ATR_INTERFACE_BYTE_TA, &ta) == ATR_NOT_FOUND)
    70     ifsc = PROTOCOL_T1_DEFAULT_IFSC;
    71   else if ((ta != 0x00) && (ta != 0xFF))
    72     ifsc = ta;
    73   else
    74     ifsc = PROTOCOL_T1_DEFAULT_IFSC;
    75 
    76   /* Towitoko does not allow IFSC > 251 */
    77   ifsc = MIN (ifsc, PROTOCOL_T1_MAX_IFSC);
    78 
    79   /* Set IFSD */
    80   ifsd = PROTOCOL_T1_DEFAULT_IFSD;
    81 
    82 #ifndef PROTOCOL_T1_USE_DEFAULT_TIMINGS
    83   /* Calculate CWI and BWI */
    84   if (ATR_GetInterfaceByte (atr, selected_protocol, ATR_INTERFACE_BYTE_TB, &tb) == ATR_NOT_FOUND)
    85     {
    86 #endif
    87       cwi  = PROTOCOL_T1_DEFAULT_CWI;
    88       bwi = PROTOCOL_T1_DEFAULT_BWI;
    89 #ifndef PROTOCOL_T1_USE_DEFAULT_TIMINGS
    90     }
    91   else
    92     {
    93       cwi  = tb & 0x0F;
    94       bwi = (tb & 0xF0) >> 4;
    95     }
    96 #endif
    97  
    98   /* Work etu  = (1000 / baudrate) milliseconds */
    99   ICC_Async_GetBaudrate (&baudrate);
    100   work_etu = 1000 / (double)baudrate;
    101 
    102   /* Set CWT = (2^CWI + 11) work etu */
    103   cwt = 1;
    104 
    105   for (i = 0; i < cwi ; i++)
    106     cwt *= 2;
    107 
    108   cwt = (unsigned short) ((cwt + 11) * work_etu);
    109 
    110   /* Set BWT = (2^BWI * 960 + 11) work etu */
    111   bwt = 1;
    112   for (i = 0; i < bwi; i++)
    113     bwt *= 2;
    114 
    115   bwt = (unsigned short) ((bwt * 960 + 11) * work_etu);
    116 
    117   /* Set BGT = 22 * work etu */
    118   bgt = (unsigned short) (22 * work_etu);
    119 
    120   /* Set the error detection code type */
    121   if (ATR_GetInterfaceByte (atr, selected_protocol, ATR_INTERFACE_BYTE_TC, &tc) == ATR_NOT_FOUND)
    122     edc = PROTOCOL_T1_EDC_LRC;
    123   else
    124     edc = tc & 0x01;
    125 
    126   /* Set initial send sequence (NS) */
    127   ns = 1;
    128  
    129   /* Set timings */
    130   icc_timings.block_timeout = bwt;
    131   icc_timings.char_timeout = cwt;
    132   icc_timings.block_delay = bgt;
    133     ICC_Async_SetTimings ();
    134 
    135 #ifdef DEBUG_PROTOCOL
    136   printf ("Protocol: T=1: IFSC=%d, IFSD=%d, CWT=%d, BWT=%d, BGT=%d, EDC=%s\n",
    137           ifsc, ifsd, cwt, bwt, t1->bgt,
    138           (edc == PROTOCOL_T1_EDC_LRC) ? "LRC" : "CRC");
    139 #endif
    140 
    141   return PROTOCOL_T1_OK;
    142 }
    14348
    14449int
Note: See TracChangeset for help on using the changeset viewer.