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

📄 usb_main.lst

📁 利用增强型单片机C8051F340作USB开发时的程序。
💻 LST
📖 第 1 页 / 共 2 页
字号:
 162   2                      FLKEY   =       0xF1;
 163   2                      *pwrite =       *pread;                         //      Write data byte to flash
 164   2      
 165   2                      pread++;                                                //      Increment pointers
 166   2                      pwrite++;
 167   2              }
 168   1              PSCTL   =       0x00;                                   //      Disable flash writes
 169   1              EA      =       EA_Save;                                        //      Restore EA
 170   1      }
 171          
 172          void    State_Machine(void)                     
 173          {
 174   1              switch  (M_State)
 175   1              {
C51 COMPILER V8.02   USB_MAIN                                                              09/18/2007 16:20:59 PAGE 4   

 176   2                      case    ST_RX_SETUP:                    
 177   2                              Receive_Setup();                        //      Receive and decode host Setup Message
 178   2                              break;
 179   2                      case    ST_RX_FILE:
 180   2                              Receive_File();                         //      Receive File data from host
 181   2                              break;
 182   2                      case    ST_TX_ACK:
 183   2                              M_State =       ST_RX_FILE;             //      Ack Transmit complete, continue RX data
 184   2                              break;
 185   2                      case    ST_TX_FILE:                             //      Send file data to host
 186   2                              WriteStageLength = ((BytesToWrite - BytesWrote) > MAX_BLOCK_SIZE_WRITE)? MAX_BLOCK_SIZE_WRITE:(BytesToW
             -rite - BytesWrote);
 187   2                              BytesWrote      +=      Block_Write((BYTE*)(ReadIndex), WriteStageLength);
 188   2                              ReadIndex += WriteStageLength;
 189   2      
 190   2                              if      ((BlocksWrote%8) == 0)  Led2 = ~Led2;
 191   2                              if      (BytesWrote == NumBytes)        Led2 = 0;
 192   2                              break;
 193   2                      default:
 194   2                              break;
 195   2              }
 196   1      }
 197          
 198          
 199                  //      ISR for USB_API, run when API interrupts are enabled, and an interrupt is received
 200          void    USB_API_TEST_ISR(void)  interrupt       16
 201          {
 202   1              BYTE    INTVAL  =       Get_Interrupt_Source(); //      Determine type of API interrupts
 203   1              if      (INTVAL &       USB_RESET)                                      //      Bus Reset Event, go to Wait State
 204   1              {
 205   2                      M_State =       ST_WAIT_DEV;
 206   2              }
 207   1      
 208   1              if      (INTVAL &       DEVICE_OPEN)                            //      Device opened on host, go to Idle
 209   1              {
 210   2                      M_State =       ST_IDLE_DEV;
 211   2              }
 212   1      
 213   1              if      (INTVAL &       TX_COMPLETE)
 214   1              {
 215   2                      if      (M_State == ST_RX_FILE)                         //      Ack Transmit complete, go to RX state
 216   2                      {
 217   3                              M_State =       (ST_TX_ACK);
 218   3                      }
 219   2                      if      (M_State == ST_TX_FILE)                         //      File block transmit complete, go to TX state
 220   2                      {
 221   3                              M_State =       (BytesWrote == BytesToWrite) ? ST_IDLE_DEV :ST_TX_FILE; //      Go to Idle when done
 222   3                      }
 223   2              }
 224   1              if (INTVAL      &       RX_COMPLETE)                            //      RX Complete, go to RX Setup or RX file state
 225   1              {
 226   2                      M_State =       (M_State == ST_IDLE_DEV) ? ST_RX_SETUP : ST_RX_FILE;
 227   2              }
 228   1              if      (INTVAL &       DEVICE_CLOSE)                           //      Device closed, wait for re-open
 229   1              {
 230   2                      M_State =       ST_WAIT_DEV;
 231   2              }
 232   1              if      (INTVAL &       FIFO_PURGE)                                     //      Fifo purged, go to Idle State
 233   1              {
 234   2                      M_State =       ST_IDLE_DEV;
 235   2              }
 236   1      
C51 COMPILER V8.02   USB_MAIN                                                              09/18/2007 16:20:59 PAGE 5   

 237   1              State_Machine();                                                        //      Call state machine routine
 238   1      }
 239          
 240          void    Receive_Setup(void)
 241          {
 242   1              BytesRead       =       Block_Read(&Buffer,     3);             //      Read Setup Message
 243   1              
 244   1              if (Buffer[0]   ==      READ_MSG)                               //      Check See if Read File Setup
 245   1              {                       
 246   2                      PageIndex       =       0;                                              //      Reset Index
 247   2                      NumBlocks       =       LengthFile[2];                  //      Read NumBlocks from flash stg
 248   2                      NumBlocks       = (NumBlocks > MAX_NUM_BLOCKS)? MAX_NUM_BLOCKS: NumBlocks; // only write as many bytes 
 249   2                                                                                                      // as we have space available
 250   2                      Buffer[0]       =       SIZE_MSG;                               //      Send host size of transfer message
 251   2                      Buffer[1]       =       LengthFile[1];
 252   2                      Buffer[2]       =       LengthFile[0];
 253   2                      BytesToWrite    =       Buffer[1]       +       256*Buffer[2];  
 254   2                      BytesWrote      =       Block_Write((BYTE*)&Buffer,     3);
 255   2                      M_State =       ST_TX_FILE;                                     //      Go to TX data state
 256   2                      BytesWrote      =       0;
 257   2                      ReadIndex       =       PageIndices[0];
 258   2                      Led2    =       1;              
 259   2              }
 260   1              else    //      Otherwise assume Write Setup Packet
 261   1              {
 262   2                      BytesToRead     =       Buffer[1]       +       256*Buffer[2];  
 263   2                      NumBlocks       =       (BYTE)(BytesToRead/MAX_BLOCK_SIZE_READ);        //      Find NumBlocks
 264   2      
 265   2                      if (BytesToRead > MAX_NUM_BYTES)                                                //      State Error if transfer too big
 266   2                      {
 267   3                              M_State = ST_ERROR;
 268   3                      }
 269   2                      else
 270   2                      {
 271   3      
 272   3                              if      (BytesToRead%MAX_BLOCK_SIZE_READ)       NumBlocks++;    //      Increment NumBlocks for last partial block
 273   3      
 274   3                              TempStorage->Piece[0]   =       Buffer[2];
 275   3                              TempStorage->Piece[1]   =       Buffer[1];
 276   3                              TempStorage->Piece[2]   =       NumBlocks;
 277   3      
 278   3                              //      Write Values to Flash
 279   3                              Page_Erase(0x2000);                                                             //      Store file data to flash
 280   3                              Page_Write(0x2000);
 281   3      
 282   3                              PageIndex       =       0;                                                                      //      Reset Index 
 283   3                              BlockIndex      =       0;
 284   3                              BytesRead       =       0;
 285   3                              Led1    =       1;
 286   3                              M_State = ST_RX_FILE;                                                           //      Go to RX data state
 287   3                      }
 288   2              }
 289   1      }
 290          
 291          void    Receive_File(void)
 292          {
 293   1              ReadStageLength = ((BytesToRead - BytesRead) > MAX_BLOCK_SIZE_READ)? MAX_BLOCK_SIZE_READ:(BytesToRead - B
             -ytesRead);
 294   1              
 295   1              BytesRead       +=      Block_Read((BYTE*)(&TempStorage[BlockIndex]), ReadStageLength); //      Read Block
 296   1      
 297   1              BlockIndex++;
C51 COMPILER V8.02   USB_MAIN                                                              09/18/2007 16:20:59 PAGE 6   

 298   1              //      If device has received as many bytes as fit on one FLASH page, disable interrupts,
 299   1              //      write page to flash, reset packet index, enable interrupts
 300   1              //      Send handshake packet 0xFF to host after FLASH write
 301   1              if      ((BlockIndex    ==      (BLOCKS_PR_PAGE))       ||      (BytesRead      ==      BytesToRead))
 302   1              {
 303   2                      Page_Erase((BYTE*)(PageIndices[PageIndex]));
 304   2                      Page_Write((BYTE*)(PageIndices[PageIndex]));
 305   2                      PageIndex++;
 306   2                      Led1 = ~Led1;
 307   2                      BlockIndex      =       0;
 308   2                      Buffer[0]       =       0xFF;
 309   2                      Block_Write(Buffer,     1);                     //      Send handshake Acknowledge to host
 310   2              }
 311   1      
 312   1              //      Go to Idle state if last packet has been received
 313   1              if      (BytesRead      ==      BytesToRead)    {M_State        =       ST_IDLE_DEV;    Led1    =       0;}
 314   1      }
 315          
 316          


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    846    ----
   CONSTANT SIZE    =     60    ----
   XDATA SIZE       =    512    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =     65       3
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)

⌨️ 快捷键说明

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