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

📄 usb_drv.c

📁 atmel c5122 USB C51程序
💻 C
📖 第 1 页 / 共 2 页
字号:

/*F**************************************************************************
* NAME: usb_reset_endpoint
*----------------------------------------------------------------------------
* PARAMS:
* return:       none
*----------------------------------------------------------------------------
* PURPOSE: 
* This function resets the endpoint fifo. This should be performed before
* the first use of an endpoint and after a Clear Halt Feature for the 
* endpoint
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE: 
*----------------------------------------------------------------------------
* REQUIREMENTS: 
*****************************************************************************/
void usb_reset_endpoint (Uchar ep_num)
{
  UEPRST = 0x01 << ep_num ;
  UEPRST = 0x00;
}

/*F**************************************************************************
* NAME: configure_usb_clock
*----------------------------------------------------------------------------
* PARAMS:
* return:       none
* The possible value for FOSC are :
*              3000   ( 3MHz)
*              6000   ( 6MHz)
*              8000   ( 8MHz)
*             12000   (12MHz)
*             16000   (16MHz)
*             18000   (18MHz)
*             20000   (20MHz)
*             24000   (24MHz)
*             32000   (32MHz)
*             40000   (40MHz)
*             48000   (48MHz)
*              0000   (frequency auto-dectection)
*----------------------------------------------------------------------------
* PURPOSE: 
* This function configure the PLL to generate the 48MHz clock required by
* the USB controller, considering the FOSC defined in the "config.h" file.
*----------------------------------------------------------------------------
* EXAMPLE:
* #define FOSC   8000 // 8MHz
* configure_usb_clock();
*----------------------------------------------------------------------------
* NOTE: 
*----------------------------------------------------------------------------
* REQUIREMENTS: 
*****************************************************************************/
void configure_usb_clock(void)
{
#ifndef PLL_USB_OK
#if FOSC == 3000
  Pll_set_div(PLL_3MHz);
  Pll_enable();
#endif

#if FOSC == 4000
  Pll_set_div(PLL_4MHz);
  Pll_enable();
#endif

#if FOSC == 6000
  Pll_set_div(PLL_6MHz);
  Pll_enable();
#endif

#if FOSC == 8000
  Pll_set_div(PLL_8MHz);
  Pll_enable();
#endif

#if FOSC == 12000
  Pll_set_div(PLL_12MHz);
  Pll_enable();
#endif

#if FOSC == 16000
  Pll_set_div(PLL_16MHz);
  Pll_enable();
#endif

#if FOSC == 18000
  Pll_set_div(PLL_18MHz);
  Pll_enable();
#endif

#if FOSC == 20000
  Pll_set_div(PLL_20MHz);
  Pll_enable();
#endif

#if FOSC == 24000
  Pll_set_div(PLL_24MHz);
  Pll_enable();
#endif

#if FOSC == 32000
  Pll_set_div(PLL_32MHz);
  Pll_enable();
#endif

#if FOSC == 40000
  Pll_set_div(PLL_40MHz);
  Pll_enable();
#endif

#if FOSC == 48000
  Usb_set_EXT48();
#endif

#if FOSC == 0000            /* frequency Auto-detection */

Uchar i;
Uchar reload;
  
  PLLCON = 0x00;
  
  if (CKCON0 & MSK_X2)
  {
    CKCON0 |= MSK_T0X2;
    reload = 6; 
  }
  else
  {
    CKCON0 &= ~MSK_T0X2;
    reload = 9;
  }
  
  TMOD = 0x01;                            /* put Timer 0 in mode 1 */
  USBINT = 0x00;
  i=reload;
  
  while (!(USBINT & MSK_SOFINT))          /* Do until Start Of Frame detection */
  {
    if (i==9)
    {
      Pll_stop();                         /* external 48 MHz supposed */
      Usb_set_EXT48();
    }
    else
    {
      Usb_clear_EXT48();                  /* PLL output supposed */
      Pll_set_div(pll_value[i]);          /* configure PLL */
      Pll_enable();
    }
    TH0 = TH0_value[i];                   /* Run Timer 0 */
    TL0 = TL0_value[i];
    TCON |= 0x10;
    while(((TCON & 0x20) != 0x20));       /* Wait Timer 0 Overflow */
    TCON &= ~(0x30);
    if (i==0)
    {
      i=reload;
    }
    else
    {
      i--;
    }
  }

  TH0  = 0;                               /* Reset Timer 0 Registers */
  TL0  = 0;
  TCON = 0;
  TMOD = 0;
#endif
#endif
}



/*F**************************************************************************
* NAME: usb_init 
*----------------------------------------------------------------------------
* PARAMS:
* return:   none
*----------------------------------------------------------------------------
* PURPOSE: 
* This function initializes the USB controller (low-level)
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE: 
*----------------------------------------------------------------------------
* REQUIREMENTS: 
*****************************************************************************/
void usb_init (void)
{
#ifdef USB_FORCE_DETACH
register Byte   i,j;
#endif
configure_usb_clock();  // The PLL is already configured by the main task
Usb_enable();
#ifdef USB_FORCE_DETACH
Usb_detach();           // detach from USB interface if active connection set
for (i=0;--i;) for (j=(FOSC>>8);--j;); // Chap. 7 USB: wait 2.5us
#endif
}


/*F**************************************************************************
* NAME: Usb_clear_resume 
*----------------------------------------------------------------------------
* PARAMS:
* return:   none
*----------------------------------------------------------------------------
* PURPOSE: 
* This function clear the WUPCPU bit in USBINT register.
*
* WARNING :
* =========
* Bug#227 : USB Macro issue.
* For USB macro V1.8RevE2 (Hammer Shark), the WUPCPU bit may not be properly 
* cleared by the CPU if its clock frequency is greater or equal than 12 Mhz/X2
* WORKAROUND : 
* set the cpu clock to a frequency strictly lower than 12 Mhz/X2 before 
* clearing this bit. The cpu clock frequency can be restored to its initial 
* value after the bit is cleared 
*
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE: 
*----------------------------------------------------------------------------
* REQUIREMENTS: 
*****************************************************************************/

void Usb_clear_resume (void)
{
   #ifdef C5122_BUG227_USB_WAKEUP_INTERRUPT
		bit bEA;
   		data dCKSEL;
		data dCKCON;
			bEA = EA;
			dCKSEL = CKSEL;
        	dCKCON = CKCON0;
			EA = 0;
			Set_cpu_8mhz_osc();                       //force CPU frequency to 8 Mhz
			Usb_clear_wakeup_cpu();
			CKCON0 = dCKCON;
			CKSEL  = dCKSEL;
			EA = bEA;
   #else
       #ifdef C5131_BUG227_USB_WAKEUP_INTERRUPT
         
         #if FOSC >= 24000
            #error A CPU frequency >= 24 Mhz/X1 is not compatible with USB suspend/resume feature
         #else
            bit bEA;
            if(Mode_x2())
               {
               bEA = EA;
               EA=0;
               Set_x1_mode();
               Usb_clear_wakeup_cpu();
               Set_x2_mode();
               EA = bEA;
               }
            else {Usb_clear_wakeup_cpu();}
         #endif
       #else
         //Usb_clear_wakeup_cpu();
         if(EA) {EA=0;CKRL-=3;USBINT &= ~MSK_WUPCPU;CKRL+=3;EA=1;}
         else {CKRL-=3;USBINT &= ~MSK_WUPCPU;CKRL+=3;}
       #endif
   #endif
}      


⌨️ 快捷键说明

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