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

📄 usb_main.lst

📁 一个使用c8051f320进行usb传输的例子
💻 LST
📖 第 1 页 / 共 2 页
字号:
 162   2                      *pwrite =       *pread;                         //      Write data byte to flash
 163   2      
 164   2                      pread++;                                                //      Increment pointers
 165   2                      pwrite++;
 166   2              }
 167   1              PSCTL   =       0x00;                                   //      Disable flash writes
 168   1              EA      =       EA_Save;                                        //      Restore EA
 169   1      }
 170          
 171          void    State_Machine(void)                     
 172          {
 173   1              switch  (M_State)
 174   1              {
 175   2                      case    ST_RX_SETUP:                    
C51 COMPILER V7.06   USB_MAIN                                                              05/08/2008 13:49:38 PAGE 4   

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

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

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


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 + -