⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scib_lib.c

📁 atmel c5122 USB C51程序
💻 C
📖 第 1 页 / 共 4 页
字号:
/*C**************************************************************************
* NAME:         scib_lib.c
*----------------------------------------------------------------------------
* Copyright (c) 2003 Atmel.
*----------------------------------------------------------------------------
* RELEASE:      c5122-scib-usb-hid-2_4_0      
* REVISION:     1.13     
*----------------------------------------------------------------------------
* PURPOSE: 
* This file contains SCIB library functions:
* init, receive and send functions...
*****************************************************************************/

/*_____ I N C L U D E S ____________________________________________________*/
#include "config.h"
#include "scib_lib.h"


/*_____ M A C R O S ________________________________________________________*/

#define Scib_hw_get_clear_sciir()       (Scib_hw_clear_SCRS(),  SCIIR)
#define Scib_hw_get_scisr()             (Scib_hw_clear_SCRS(),  SCISR)
#define Scib_hw_get_scier()             (Scib_hw_set_SCRS(),    SCIER) 
#define Scib_hw_set_or_scier(c)         (SCIER|=(c)) 
#define Scib_hw_set_and_scier(c)        (SCIER&=(c)) 
#define Scib_hw_set_status_error(c)     (uc_error |= (c))
#define Scib_hw_get_status_error()      (uc_error)
#define Scib_hw_clear_status_error(c)   (uc_error &= ~(c))
#define Scib_hw_set_uc_transmit(c)      (uc_transmit=(c))


/*_____ D E F I N I T I O N S ______________________________________________*/
extern Uchar uc_error;
extern Uchar uc_transmit;


extern Uchar uc_tab_index_rec;
extern Uchar uc_tab_index_trans;

/*extern api fct*/
extern void scib_api_card_in(void);
extern void scib_api_card_out(void) ;
extern void scib_api_card_mvt(void);
extern void scib_api_store_rx_datas(void);
extern void scib_api_send_tx(void);
extern volatile bit b_first_byte_ATR ;
extern volatile Uchar * puc_buffer_tx;
extern volatile Uint16 us_size_tx;

/*_____ D E C L A R A T I O N S ____________________________________________*/
code Uint16 aus_Fi[] ={372,372,558,744,1116,1488,1860,0,0,
512,768,1024,1536,2048,0,0};

code Uchar auc_Di[] ={0,1,2,4,8,16,32,0,12,20,0,0,0,0,0,0};

void empty_fct(void) {}     /* empty function*/

code st_set_parameter_T0 parameter_T0_default = {
      0x11,        //Default TA1 = 11
      0,           //Default Direct convention
      0x0,         //Default Gard Time 12 etu
      10,          //Default WI 
      0            //Clock stopping not allowed
      }; 

#ifdef SCIB_USE_TIMER1
Union16 u16_timer_last_count;
Uint16  us_timer_count;
#endif

/*F**************************************************************************
* NAME: scib_it_int1 
*----------------------------------------------------------------------------
* PARAMS: none
* return: none
*----------------------------------------------------------------------------
* PURPOSE: 
* external interrupt 1 function
* This function manage by interrupt all mouvement card
* If pull up on CPRES:
* CPLEV = 0 (detection of falling edge on CPRES, reset value)
* insertion of the smart card, an interrupt appears
* CPLEV = 1 (detection of rising edge on CPRES)
* extraction of the smart card, an interrupt appears
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE: This function set the counter debounce uc_slot_debounce at the max 
* value and set bit mouvement card in slot data SCIB hard
*----------------------------------------------------------------------------
* REQUIREMENTS: is necessarry to check and decrement uc_slot_debounce in 
* the main loop
*****************************************************************************/
void scib_it_int1(void) interrupt IRQ_INT1
{
  if(MSK_ISEL_PRESIT == (ISEL & MSK_ISEL_PRESIT))
  {
    if(MSK_ISEL_CPLEV == (ISEL & MSK_ISEL_CPLEV))
    {
      /*set CPLEV*/
      ISEL &= (~MSK_ISEL_CPLEV);
      
      scib_api_card_out();  

      Scib_hw_set_status_error(MSK_SCISR_CARDIN );
      //scib_card_power_off (&ast_slot[0]);
         
      /*CARD EXTRACTION*/
    }
    else
    {
      /*reset CPLEV*/
      ISEL |= MSK_ISEL_CPLEV;      

      scib_api_card_in();
      /*CARD INSERTION*/
    }
    //ast_slot[0].uc_smart_card |= MASK_CARD_MVT;
    //uc_slot_debounce = MAX_DEBOUNCE;        
    scib_api_card_mvt();
    
    ISEL &= (~MSK_ISEL_PRESIT);    
  }
}


/*F**************************************************************************
* NAME: scib_it_sci 
*----------------------------------------------------------------------------
* PARAMS: none
* return: none
*----------------------------------------------------------------------------
* PURPOSE: 
* SCIB hard interrupt management 
* This function manage the uart SCIB hard for the Tx, Rx carracter and 
* the timeout 
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE: this function use the global variable uc_error,puc_buffer_rx
* puc_buffer_tx, uc_size_tx, uc_size_rx, us_size_buffer_rx_max
*----------------------------------------------------------------------------
* REQUIREMENTS: 
*****************************************************************************/

void scib_it_sci(void) interrupt IRQ_SCIB
{
   register data Uchar uc_sciir;

   Uchar uc_old_scrs;
 //  Scib_hw_save_context_scrs(save_scrs);                                



   uc_old_scrs = SCSR;
   /*SCIIR is reseted after a the first read operation*/ 
   uc_sciir = Scib_hw_get_clear_sciir();

   /* IF IT transmit buffer empty */
    if (MSK_SCIIR_SCTBI == (uc_sciir & MSK_SCIIR_SCTBI))      
    {
      us_size_tx --;
      /* if last byte */
      if (us_size_tx==0)
      {
        /* disable IT transmit buffer */
        Scib_set_SCRS();
        Scib_disable_it_empty_buffer(); //SCIER &= ~ MSK_SCIER_ESCTBI;
    
        /* enable IT Transmit and BWT Rx*/
        Scib_enable_it_transmit_and_bwt_rx();
    
        /*start counter WTEN=1 */
    //    scib_en_wait_time_count();
      } 
      Scib_clear_SCRS();
      Scib_putchar (Scib_dma_read()); //SCTBUF = * puc_buffer_tx; 
      puc_buffer_tx ++;       
    } 

   /* IF received */
   if (MSK_SCIIR_SCRI == (uc_sciir & MSK_SCIIR_SCRI))         
   {   
      Scib_hw_set_SCRS();
      scib_api_store_rx_datas();  
   }

    /* IF timeout */
    if (MSK_SCIIR_SCWTI == (uc_sciir & MSK_SCIIR_SCWTI))      
    {
      /* disable IT RX,TX,Timeout*/
      Scib_hw_set_SCRS();
      SCIER &= ~ (MSK_SCIER_ESCTI | MSK_SCIER_ESCTBI | MSK_SCIER_ESCWT | MSK_SCIER_ESCRI) ;
      Scib_hw_set_status_error(MSK_SCIIR_SCWTI);
    }

  

    /* IF IT transmit byte */  
    if (MSK_SCIIR_SCTI == (uc_sciir & MSK_SCIIR_SCTI))      
    {
     /*start counter WTEN=1 */
      scib_en_wait_time_count();

      /* disable IT transmit */
      Scib_hw_set_SCRS();
      Scib_hw_disable_it_transmit_buffer(); 
      
      Scib_hw_set_uc_transmit(1);
    }  

   /* IF other error IT */   
    if (uc_sciir & (MSK_SCIIR_ICARDERR | MSK_SCIIR_VCARDERR | MSK_SCIIR_SCPI))    
    {
      // for first byte ATR TS is in inverse convention
      if (b_first_byte_ATR)     
      {
          // update convention 
        scib_inverse_conv();  
        b_first_byte_ATR = 0;
      }
      else      
      {
        Scib_hw_set_status_error((uc_sciir & (MSK_SCIIR_ICARDERR | MSK_SCIIR_VCARDERR | MSK_SCIIR_SCPI)));
        
        Scib_hw_set_SCRS();
        SCIER &= ~ (MSK_SCIER_ESCTI | MSK_SCIER_ESCTBI | MSK_SCIER_ESCPI | MSK_SCIER_ESCWT) ;
      }
    }
//    Scib_hw_restore_context_scrs(save_scrs);

    Scib_clear_SCRS();
    SCSR |=  (uc_old_scrs & MSK_SCSR_SCRS);
}

 








/*F**************************************************************************
* NAME: scib_hw_init 
*----------------------------------------------------------------------------
* PARAMS:
* return:   none
*----------------------------------------------------------------------------
* PURPOSE: 
* reset/unreset if the VCC appears
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE: 
*----------------------------------------------------------------------------
* REQUIREMENTS: 
*****************************************************************************/
void scib_hw_init(void)
{
  Scib_hw_set_SCRS();
  SCICR |= MSK_SCICR_RESET;     /* Reset UART configuration */
  SCICR &=(~MSK_SCICR_RESET);   /* End of UART Reset */

}


/*F**************************************************************************
* NAME: scib_hw_activ_card 
*----------------------------------------------------------------------------
* PARAMS: none
* return: none
*----------------------------------------------------------------------------
* PURPOSE: 
* start the activation sequence in line smart card SCIB hard (IO,CLK,VCC)
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE: 
*----------------------------------------------------------------------------
* REQUIREMENTS: 
*****************************************************************************/
void scib_hw_activ_card(void)
{
  /*select UART mode*/
  Scib_set_SCRS();
  SCICR |= MSK_SCICR_UART;

  scib_cardio_on(); 

  scib_cardclk_on();
}


/*F**************************************************************************
* NAME: scib_hw_send_byte 
*----------------------------------------------------------------------------
* PARAMS: uc_data_to_send: byte to send
* return: none
*----------------------------------------------------------------------------
* PURPOSE: 
* Sends one byte on CIO pin
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE: 
*----------------------------------------------------------------------------
* REQUIREMENTS: 
*****************************************************************************/
void scib_hw_send_byte(Uchar uc_data_to_send)
{
  Scib_set_SCRS();
  SCIER |= MSK_SCIER_ESCTI;

  Scib_clear_SCRS();
  SCTBUF = uc_data_to_send;
}


/*F**************************************************************************
* NAME: scib_hw_clk_config  
*----------------------------------------------------------------------------
*----------------------------------------------------------------------------
* PARAMS: none
* return: none   
*----------------------------------------------------------------------------
* PURPOSE: 
* configuration of the smard card clock
*****************************************************************************
* NOTE: 
*****************************************************************************/
void  scib_hw_clk_config(Uchar clk)
{
  /*SELECT BANK*/
  Scib_set_SCRS();

#ifdef _FPGA_ 

  /*CPU_CLOCK = 7.37MHz, ISO_CLOCK = 3.68MHz*/
  SCICLK = 0x2D;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -