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

📄 main.c

📁 DK3200 USB DEMO for KEIL C
💻 C
📖 第 1 页 / 共 3 页
字号:
  counter = 0;

           // We are not currently transmitting or receiving feature/input reports
  rcvIndex = CMD_SIZE;
  txIndex = CMD_SIZE;

                                             // Disable watchdog
  WDKEY=WD_OFF;

  initXREG();                                // init extended regs in xdata
  initLCD();
  UsbInitialize();                           // Initialize USB,



/* ONLY USB must have the highest priority to assure good respose
  and proper processing of all requests.
  */
  IP  = 0;                               // USB must have the highest priority !!!
  IPA = 1;                               // USB must have the highest priority !!!
  EA  = 0;

  IEA |= 0x80;                      // Enable DDC Interrupt - Priority is user TBD

  EA  = 1;                                   // Enable INTs

  printfLCD("\r*USB DEMO V.2.1*\n");         //dislay the string


  #ifdef DisconnectOnDemand
  ReConnectUSB();         // Enable when DisconnectOnDemand feature is implemented
  #endif

  LCD_delay_ms(1000);                        //wait


  while (1)
   {
               // Indicate which flash we are running out of: main (M) or boot (B)
    c = ((UPSD_xreg.VM == 0x92) ? 'B' : 'M');
    printfLCD("\r%B %x %x%c %W\r", g_debugTimer2_INT_CNT, USB_ISR_FLAGS, currentCmd.u.cmd,c, g_debugUSB_INT_CNT);

   // Enable the following lines if you plan to use the DisconnectOnDemand feature
         // and if you have the HW modifications on your DK3200 kit (see appnote).

    #ifdef DisconnectOnDemand
                                             // DisconnectOnDemand feature
    if ((UPSD_xreg.DATAIN_B & 0x04)==0)      // check Swith ONE
     {
      ReConnectUSB();                        // Disconnect USB when pressed
     }
    #endif

   }
 }





















static void UsbIsr() interrupt 6 using 3
/******************************************************************************
 Function   : static void UsbIsr()
 Parameters : none
 Description: USB interrupt service routine.
 Note:        Do not modify this routine !!!
 ******************************************************************************/
 {








  data uchar cb;
  data uchar i;









  if (RXD0F)                                 // If data received on EP0 OUT ...
   {
                 // do not change this SETUP packet processing part up to else !!!
    if (USTA & uSETUP)                       // If it's a SETUP packet ...
     {

      if (UCON0 & uSTALL0)                   // try to fix bug in uPSD
       {
        #pragma asm
              anl    UCON0,#255-uSTALL0        ;clear STALL0 bit when it hangs
        #pragma endasm
       }

      if  (ReadSetupPacket())                // Read the SETUP packet
       {
                                             //tady to nepomaha
        if (!HandleReport())                 // Test and handle HID type packets
         {
   // If this is not a HID report ... pass it on to the basic SETUP packet handler
          OnSetupPacket();
         }
        else
         {
         }
       }
      else
       {
//No Setup packet with DATA length = 8, STALLed automatically in ReadSetupPacket()
       }

      RXD0F = 0;            // Clear interrupt flag so next packet can come in now
     }

    else
           /*======== No SETUP packet, normal data packets ========*/
     {
                                     // If in the middle of receiving a report ...
      if ((USTA & 0x0F) && (rcvIndex < OUTPUT_REPORT_SIZE))
       {
        cb = (USTA & 0x0F);                  // Read the next segment
        if (cb > EP0_SIZE)
         {
          cb = EP0_SIZE;                     // fix bug
         }
        for (i = 0; i < cb; i++)
         {
          rcvReport.u.buffer[rcvIndex + i] = UDR0;            //read received data
         }
        #pragma asm
              xrl    USTA,#uRSEQ;                       // Toggle data toggle bit
        #pragma endasm
                              // Handle report as it comes in and/or when all done
        OnReportSegmentReceived(cb);

        if ((rcvIndex += cb) >= OUTPUT_REPORT_SIZE)
         {
          OnReportReceived();

          #pragma asm                        //send a zero length packet

              anl    UCON0,#uTSEQ0+uRX0E
              orl    UCON0,#uTX0E                              ;enable trasmit
          #pragma endasm

         }
        RXD0F = 0;          // Clear interrupt flag so next packet can come in now
       }
      else
       {
                    // Got 0 length ACK data packet; expecting a SETUP packet now.
        USTA ^= uRSEQ;                       // Toggle data toggle bit
        RXD0F = 0;          // Clear interrupt flag so next packet can come in now
       }
     }
   }





  if (TXD0F)
   {

    TXD0F = 0;                               // Clear the interrupt flag
                           // Do not change this part up to call of BaseEp0Handler
                                  // If in the middle of transmitting a report ...
    if (txIndex < FEATURE_REPORT_SIZE)
     {
                                   // Transmit the next segment of outgoing report
      cb = min(FEATURE_REPORT_SIZE - txIndex, EP0_SIZE);
      TransmitDataEP0(txReport.u.buffer + txIndex, cb);
      if ((txIndex += cb) >= FEATURE_REPORT_SIZE)
       {
        OnReportTransmitted();
       }
      else
       {
                      // Prepare the next segment while that last one is going out
        PrepareTransmitSegment(txIndex);
       }
     }
    else
     {
                             // This part can changed (or BaseEp0Handler directly)
      BaseEp0TxHandler();                    // Handle standard control requests
     }
   }



  if (SUSPND)                                // Handle suspend interrupt
   {
    SUSPND = 0;                              // keep USB logic functional
    GoOnSuspend = 1;                         // set global flag
    UIEN &= ~uSUSPNDIE;                      // disable INT
    DDCCON = 0x2;                            // generate DDC interrupt
    DDCCONintc++;                            // aux. DDC cnt
   }



  if (TXD1F)                        // If data successfully transmitted on EP1 ...
   {
    UIEN &= ~uTXD1IE;                   // disable EP1,2 INT, serviced in OnIdle()
    DDCCON = 0x2;                            // generate DDC interrupt
    DDCCONintc++;                            // aux. DDC cnt
   }


  if (RSTF)              // Handle USB bus reset, it must be at the end of USB ISR
   {
    USB_ISR_FLAGS = 0;
    OnUSBReset();                            //resets all the flags automatically
    DDCCON = 0x2;                            // generate DDC interrupt
    DDCCONintc++;                            // aux. DDC cnt
   }
  g_debugUSB_INT_CNT++;                      // increment USB ISR counter



 }









static data char bufIndex = 0;               // Current position in LCD buffer
static data char txBuf[8];                   // Buffer to send back to PC



static void OnTransmitEP1()
/******************************************************************************
 Function   : static void OnTransmitEP1()
 Parameters : none
 Description: Transmits data to Endpoint1
 ******************************************************************************/
 {
  data unsigned char i,nBytes;               // Num bytes of LCD data

                 // Store current index into LCD buffer in first byte of tx buffer
  if (bufIndex >= LCD_BUFFER_SIZE - 1)       //one zero only, save packet size
   {
    bufIndex = 0;      // Wrap around to start of LCD buffer for next transmission
   }

  nBytes = LCD_BUFFER_SIZE - bufIndex - 1;
  if (nBytes>7)
   {
    nBytes = 7;                              //max. 1 info byte + 7 characters
   }

  txBuf[0] = bufIndex;                       //save position at the first byte

  i = 1;
  while (i<=nBytes)
   {
    txBuf[i++] = LCD_buffer[bufIndex++];     //fill buffer
   }

  TransmitDataEPx(1, txBuf, nBytes+1);       // Transmit input report to host
 }










static void DDC_isr (void) interrupt 7 using 2
/******************************************************************************
 Function   : static void UsbIsr()
 Parameters : none
 Description: This is the rest of USB ISR. You can use other INT (except USB)
              if needed. Only USB must have the highest priority ...
 Remark:      This part can be deleted if you do not use suspend mode or EP1/2.
              All the time-consuming operations (>200us) must be here to avoid
              possible problems with very long USB ISR.
*******************************************************************************/
 {
  data uchar ddcint;

  EA = 0;                                    // disable int
  DDCCON = 0;                                // Clear DDCCON
  ddcint = S1STA;                    // Dummy Read DDC I2C S1STA to clear Interupt
  S1CON = 0;                                 // Clear DDC I2C S1CON
  EA = 1;                                    // enable int

  while (DDCCONintc>0)                       // multiple DDC calls
   {
    DDCCONintc--;                            // aux. DDC cnt



    if (TXD1F)                               //Service USB INT EP1
     {
      TXD1F = 0;
      OnTransmitEP1();                       //service EP1
     }

       /*====== All the time-consuming operations (>100us) must be here.
         Place your longer code (>450us) instead of CMD_ERASE. ======*/
    switch (currentCmd.u.cmd)
     {
      case CMD_ERASE:

      if (currentCmd.u.erase.flash == PRIMARY_FLASH)
       {
        status.u.status.ret = flash_erase_sector(
        (volatile uchar xdata*) currentCmd.u.erase.address);
       }
      else
       {
        status.u.status.ret = flash_boot_erase_sector(
        (volatile uchar xdata*) currentCmd.u.erase.address);
       }
      currentCmd.u.cmd = 0;                  // Done
      break;
      default:
      break;
     }


// The following 3 lines can be deleted if you don't plan to use Suspend
    if (GoOnSuspend)                         //Service USB INT Suspend
     {
      OnUSBSuspend();
     }

    g_debugTimer2_INT_CNT++;                 // increment USB T2 counter

   }
 }


/* *************************************************************************
  ***                                                                      ***
  **                         *** End of File ***                            **
  ***                                                                      ***
  *************************************************************************
  */

⌨️ 快捷键说明

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