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

📄 freescale

📁 Freescale 系列单片机常用模块与综合系统设计
💻
📖 第 1 页 / 共 2 页
字号:
/*************************************************************************
 * DISCLAIMER *
 * Services performed by FREESCALE in this matter are performed          *
 * AS IS and without any warranty. CUSTOMER retains the final decision   *
 * relative to the total design and functionality of the end product.    *
 * FREESCALE neither guarantees nor will be held liable by CUSTOMER      *
 * for the success of this project. FREESCALE disclaims all warranties,  *
 * express, implied or statutory including, but not limited to,          *
 * implied warranty of merchantability or fitness for a particular       *
 * purpose on any hardware, software ore advise supplied to the project  *
 * by FREESCALE, and or any product resulting from FREESCALE services.   *
 * In no event shall FREESCALE be liable for incidental or consequential *
 * damages arising out of this agreement. CUSTOMER agrees to hold        *
 * FREESCALE harmless against any and all claims demands or actions      *
 * by anyone on account of any damage, or injury, whether commercial,    *
 * contractual, or tortuous, rising directly or indirectly as a result   *
 * of the advise or assistance supplied CUSTOMER in connection with      *
 * product, services or goods supplied under this Agreement.             *
 *************************************************************************/
 /************************************************************************
 * File name   : Usb_Bdt.c
 * Description : This file provides the USB driver, all USB events will be 
 *               precessed
 *               
 *
 * History     :
 * 04/01/2007  : Initial Development
 * 
 *************************************************************************/
#include <MC9S08JM60.h>
#include <stdlib.h>
#include "typedef.h"
#include "Usb_Config.h"
#include "Usb_Drv.h"
#include "Usb_Bdt.h"
#include "Usb_Ep0_Handler.h"
#include "Usr_Ep_Handler.h"


/*Global variable definition*/

byte Usb_Device_State;          /* device states*/
USB_DEVICE_STATUS Usb_Stat;     /* global flags*/
byte Usb_Active_Cfg;            /* value of current configuration*/
byte Usb_Alt_Intf[MAX_NUM_INTF]; /* array to keep track of the current alternate*/
                                /* setting for each interface ID*/

/* buffer descriptor table entry*/
/* It locates the beginning of USb RAM,   0x1860 - 0x195F (256 bytes)*/
BDTMAP Bdtmap @0x1860;

/*endpoint 0 buffer definition*/
CTRL_TRANSFER_DATA  CtrlTrf_Data @0x1880;
CTRL_TRANSFER_SETUP Setup_Pkt    @0x1890;

void Check_USB_Status(void);

void USB_Suspend(void);
void USB_WakeFrom_Suspend(void);
unsigned char USB_WakeUp(void );

void USB_Bus_Reset_Handler(void);
void USB_SOF_Handler(void);
void USB_Stall_Handler(void);
void USB_Error_Handler(void);

void USB_Transaction_Handler(void);

void interrupt  USB_ISR();
void interrupt  IRQ_ISR();



/******************************************************************************
 * Function:        void Initialize_USBModule(void)
 * Input:           None
 * Output:          None
 * Overview:        initialize the USB module
 *                  enable regulator, PHY, pull-up resistor, BDT initialization
 *
 * Note:            None
 *****************************************************************************/
void  Initialize_USBModule()
{
  char i;
  unsigned char *pUsbMem = (unsigned char *) &Bdtmap;

  
  Usb_Device_State = POWERED_STATE;
  #ifdef SELF_POWER
    Usb_Stat._byte = 0x01;                /*SelfPower*/
  #else
    Usb_Stat._byte = 0x00;                /*Notice: Bus powered*/
  #endif
  Usb_Active_Cfg = 0x00;

  USBCTL0 = USB_RST;
  while(USBCTL0_USBRESET)
  {
    
  };
  
  
  for(i= 0; i < 0xFF; i++)
  {
    *pUsbMem = 0x00;
    pUsbMem ++; 
  }   /*the USB RAM initialization is just for test,it can be deleted*/
  
  
  /*Below is initialize the BDT*/
  Bdtmap.ep0Bo.Stat._byte = _SIE|_DATA0|_DTS;  /* endpoint 0 OUT initialization*/
  Bdtmap.ep0Bo.Cnt = EP0_BUFF_SIZE;              
  Bdtmap.ep0Bo.Addr = 0x0C;                       //((byte)(((byte*) &Setup_Pkt)-0x1860)  )>> 2;  //can calculate this value  

  Bdtmap.ep0Bi.Stat._byte = _CPU;                /* endpoint 0 IN buffer initialization*/
  Bdtmap.ep0Bi.Cnt = 0x00;                      
  Bdtmap.ep0Bi.Addr = 0x08;                     

  USBCTL0 = 0X00;
  
  EPCTL0 = 0x0D;    /*enable endpoint 0*/
  INTENB = 0x01;
}


/******************************************************************************
 * Function:        void Check_USB_Status(void)
 * Input:           None
 * Output:          None
 * Overview:        This function can be used to detect if the USB bus has attached
 *                  on USB bus, we can use a GPIO, or KBI interrupt, it is disable here
 *
 *****************************************************************************/
void Check_USB_Status(void)
{
    #ifdef SELF_POWER
    if(PTGD_PTGD0 == USB_BUS_ATTACHED)         /* Is JM60 attached on USB bus? */
     {  
    #endif
        if(CTL_USBEN == 0)                     /* JM60 module off ? */
         {
            EPCTL0 = 0x0D;
            INTSTAT = 0xBF;
            CTL = 0x00;
            INTENB = 0x00;                      /* disable USB interrupt*/
            CTL_USBEN = 0x01;                   /* enable module */
            USBCTL0 = UCFG_VAL;                 /* attach JM60 to USB bus*/

            Usb_Device_State = ATTACHED_STATE;      
         }
  
   #ifdef SELF_POEWER
     }
    else
    {
        if(CTL_USBEN == 1)                      /*  JM60 USB on ?*/
         {
             CTL = 0x00;                        /* disable module */
             INTENB = 0x00;                     /* mask USB interrupt*/
             Usb_Device_State = POWERED_STATE;      
          }
    }
   #endif
   
     if(Usb_Device_State == ATTACHED_STATE)
      {
         INTSTAT = 0xBF;                        /*clear USB interrupts*/
         INTENB = 0xBF;                            
      }


    if(Usb_Device_State == USB_ENTER_SUSPEND)
    {
        USB_Suspend();
    }
}



/******************************************************************************
 * Function:        void USB_Soft_Detach(void)
 * Input:           None
 * Output:          None
 * Overview:        USB_Soft_Detach disconnects the device from USB bus. 
 *                  This is done by stop supplying Vusb voltage to
 *                  pull-up resistor. The pull-down resistors on the host
 *                  side will pull both differential signal lines low and
 *                  the host registers the event as a disconnect.
 *
 *                  Since the USB cable is not physically disconnected, the
 *                  power supply through the cable can still be sensed by
 *                  the device. The next time Check_USBBus_Status() function
 *                  is called, it will reconnect the device back to the bus.
 *****************************************************************************/
void USB_Soft_Detach(void)
{
   CTL = 0x00;                             /* disable module */
   INTENB = 0x00;                          /* mask USB interrupt*/
   Usb_Device_State = POWERED_STATE;      
   return;
}





/******************************************************************************
 * Function:        void USB_Suspend(void)
 * PreCondition:    None
 * Input:           None
 * Output:          None
 * Side Effects:    None
 * Overview:        After 3ms of USB bus idle status, the SLEEPF will be set,
 *                  The firmware should enable the resume function in 7ms before 
 *                  the USB enters into suspend state.       
 * Note:            None
 *****************************************************************************/
void USB_Suspend(void)
{
    unsigned char Result;
   
    //INTENB_RESUME = 1;   
    USBCTL0_USBRESMEN = 1;
                     
    INTSTAT_SLEEPF = 1;   
    #ifndef SELF_POWERED                 
      Clear_Int();          /*disable the int which will wake the MCU from stop3*/
    #endif

   
    Usb_Device_State = USB_SUSPEND;
    do
      {
        Result = USB_WakeUp() ;
      }while(!Result);
      
    USBCTL0_USBRESMEN = 0;  
    
    if(Result == 2)
        USB_Remote_Wakeup();
    
    #ifndef SELF_POWERED
      Recover_Int();    /*recover the diabled int*/
    #endif                  
  	return;
}

/******************************************************************************
 * Function:        void USB_WakeFrom_Suspend(void)
 * Input:           None
 * Output:          None
 * Overview:        
 *****************************************************************************/
void USB_WakeFrom_Suspend(void)
{
    INTSTAT_RESUMEF = 1;  /*write 1 to clear*/
    INTENB_RESUME = 0;
    CTL_TSUSPEND = 0;	    /*enable the transmit or receive of packet */

}
/******************************************************************************
 * Function:        void USB_Remote_Wakeup(void)
 * Input:           None
 * Output:          None
 * Overview:        This function is used to send wake-up signal from device to
 *                  host or hub
 *
 * Note:           According to USB 2.0 specification section 7.1.7.7,
 *                  "The remote wakeup device must hold the resume signaling
 *                  for at lest 1 ms but for no more than 15 ms."
 *****************************************************************************/
void USB_Remote_Wakeup(void)
{
    static word delay_count;

    USB_WakeFrom_Suspend();   
     
    CTL_CRESUME = 1;          /* Start RESUME signaling*/
       
    delay_count = 5000;       /* Set RESUME line for 1-15 ms*/
     do
     {
       delay_count--;
       __RESET_WATCHDOG();
     }while(delay_count);        
        

    CTL_CRESUME = 0;
}

/******************************************************************************
 * Function:        void USB_SOF_Handler(void)
 * Input:           None
 *
 * Output:          None
 * Overview:        The USB host sends out a SOF packet to full-speed devices
 *                  every 1 ms. This interrupt may be useful for isochronous
 *                  pipes. 
 *****************************************************************************/
void USB_SOF_Handler(void)
{
    /*can add some code if isochronous transfer*/

⌨️ 快捷键说明

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