hal_usb.lst

来自「非常全的nrf2401设计资料」· LST 代码 · 共 960 行 · 第 1/3 页

LST
960
字号
C51 COMPILER V7.50   HAL_USB                                                               04/09/2009 10:12:51 PAGE 6   

 298   1          if( i_usb.packetizer.data_size == 0 )
 299   1          {
 300   2              i_usb.map->in0bc = 0;
 301   2              USB_EP0_HSNAK();
 302   2      
 303   2              //USB_EP0_DSTALL();
 304   2              return;
 305   2          }
 306   1      
 307   1          size = MIN(i_usb.packetizer.data_size, i_usb.packetizer.pkt_size);
 308   1      
 309   1          // Copy data to the USB-controller buffer
 310   1          for( i = 0; i < size; i++ )
 311   1          {
 312   2              i_usb.map->in0buf[i] = i_usb.packetizer.data_ptr[i];
 313   2          }
 314   1      
 315   1          // Tell the USB-controller how many bytes to send
 316   1          // If a IN is received from host after this the USB-controller will send the data
 317   1          i_usb.map->in0bc = size;
 318   1      
 319   1          // Update the packetizer data
 320   1          i_usb.packetizer.data_ptr += size;
 321   1          i_usb.packetizer.data_size -= size;
 322   1      
 323   1          return;
 324   1      }
 325          
 326          /** This function processes the response from the callback */
 327          static void usb_process_dev_req_cb_response(hal_usb_dev_req_resp_t ret, hal_usb_device_req* req, uint8_t* 
             -data_ptr, uint16_t size)
 328          {
 329   1          switch( ret )
 330   1          {
 331   2          case STALL:
 332   2              USB_EP0_STALL();
 333   2              break;
 334   2          case DATA:
 335   2              packetize(data_ptr, MIN(LSB(req->wLength), size), g_hal_usb.descs.dev->bMaxPacketSize0);
 336   2              packetizer_isr_ep0_in();
 337   2              break;
 338   2          case NO_RESPONSE:
 339   2              break;
 340   2          case EMPTY_RESPONSE:
 341   2              USB_EP0_HSNAK();
 342   2              break;
 343   2          case NAK:
 344   2              USB_EP0_HSNAK();
 345   2              break;
 346   2          case ACK:
 347   2              i_usb.map->out0bc = 0xff;
 348   2              break;
 349   2          default:
 350   2              USB_EP0_STALL();
 351   2              break;
 352   2          }
 353   1      }
 354          
 355          static void usb_process_get_status(hal_usb_device_req* req)
 356          {
 357   1          uint8_t* ptr;
 358   1      
C51 COMPILER V7.50   HAL_USB                                                               04/09/2009 10:12:51 PAGE 7   

 359   1          if( g_hal_usb.state == ADDRESSED )
 360   1          {
 361   2              if( LSB(req->wIndex) != 0x00 )
 362   2              {
 363   3                  USB_EP0_STALL();
 364   3              }
 365   2              else
 366   2              {
 367   3                  i_usb.map->in0buf[0] = i_usb.map->in0buf[1] = 
 368   3                      ((g_hal_usb.descs.conf->conf.bmAttributes & 0x40 ) >> 6); // D0 - 0: bus powered, 1: self 
             -powered
 369   3                  i_usb.map->in0bc = 0x02;
 370   3              }
 371   2          }
 372   1          else if( g_hal_usb.state == CONFIGURED )
 373   1          {
 374   2              switch(req->bmRequestType)
 375   2              {
 376   3                  case 0x80: // Device
 377   3                      if( ( g_hal_usb.bm_state & USB_BM_STATE_ALLOW_REMOTE_WAKEUP ) == USB_BM_STATE_ALLOW_REMOTE
             -_WAKEUP )
 378   3                      {
 379   4                          i_usb.map->in0buf[0] = 0x02;
 380   4                      }
 381   3                      else
 382   3                      {
 383   4                          i_usb.map->in0buf[0] = 0x00;
 384   4                      }
 385   3      
 386   3                      i_usb.map->in0buf[0] |= ((g_hal_usb.descs.conf->conf.bmAttributes & 0x40 ) >> 6); // D0 - 
             -0: bus powered, 1: self powered
 387   3                      i_usb.map->in0buf[1] = 0x00; // Reserved (Reset to zero)
 388   3                      i_usb.map->in0bc = 0x02;
 389   3                      break;
 390   3                  case 0x81: // Interface
 391   3                      i_usb.map->in0buf[0] = i_usb.map->in0buf[1] = 0x00;
 392   3                      i_usb.map->in0bc = 0x02;
 393   3                      break;
 394   3                  case 0x82: // Endpoint
 395   3                      if( ( LSB(req->wIndex) & 0x80 ) == 0x80 ) // IN endpoints
 396   3                      {
 397   4                          ptr = CALCULATE_CS_IN_PTR(req->wIndex);
 398   4                      }
 399   3                      else
 400   3                      {
 401   4                          ptr = CALCULATE_CS_OUT_PTR(req->wIndex);
 402   4                      }
 403   3      
 404   3                      i_usb.map->in0buf[0] = *ptr & 0x01;
 405   3                      i_usb.map->in0buf[1] = 0x00;
 406   3                      i_usb.map->in0bc = 0x02;
 407   3                      break;
 408   3                  default:
 409   3                      USB_EP0_STALL();
 410   3                      break;
 411   3              } // switch(req.bmRequestType) --end--
 412   2          }
 413   1          else
 414   1          {
 415   2              // We should not be in this state
 416   2              USB_EP0_STALL();
 417   2          }
C51 COMPILER V7.50   HAL_USB                                                               04/09/2009 10:12:51 PAGE 8   

 418   1      }
 419          
 420          static void usb_process_get_descriptor(hal_usb_device_req* req)
 421          {
 422   1          hal_usb_dev_req_resp_t ret;
 423   1          uint8_t* data_ptr;
 424   1          uint16_t data_size;
 425   1      
 426   1          // Switch on descriptor type
 427   1          switch( MSB(req->wValue) )
 428   1          {
 429   2              case USB_DESC_DEVICE:
 430   2                  packetize((uint8_t*)g_hal_usb.descs.dev,
 431   2                  MIN(req->wLength, sizeof(hal_usb_dev_desc_t)),
 432   2                  g_hal_usb.descs.dev->bMaxPacketSize0);
 433   2                  packetizer_isr_ep0_in();
 434   2                  break;
 435   2              case USB_DESC_CONFIGURATION:
 436   2                  // For now we just support one configuration. The asked configuration is stored in LSB(wValue)
             -.
 437   2                  packetize((uint8_t*)g_hal_usb.descs.conf,
 438   2                      MIN(LSB(req->wLength), sizeof(usb_conf_desc_templ_t)),
 439   2                      g_hal_usb.descs.dev->bMaxPacketSize0);
 440   2                      packetizer_isr_ep0_in();
 441   2                  break;
 442   2              case USB_DESC_STRING:
 443   2                  // For now we just support english as string descriptor language.
 444   2                  if( LSB(req->wValue) == 0x00 )
 445   2                  {
 446   3                      packetize(g_hal_usb.descs.string_zero,
 447   3                          MIN(LSB(req->wLength), sizeof(g_hal_usb.descs.string_zero)),
 448   3                              g_hal_usb.descs.dev->bMaxPacketSize0);
 449   3                      packetizer_isr_ep0_in();
 450   3                  }
 451   2                  else
 452   2                  {
 453   3                      if( ( LSB(req->wValue) - 1 ) < USB_STRING_DESC_COUNT )
 454   3                      {
 455   4                          packetize((uint8_t*)(g_hal_usb.descs.string->idx[LSB(req->wValue)-1]),
 456   4                              MIN(LSB(req->wLength), g_hal_usb.descs.string->idx[LSB(req->wValue)-1][0]),
 457   4                              g_hal_usb.descs.dev->bMaxPacketSize0);
 458   4                          packetizer_isr_ep0_in();
 459   4                      }
 460   3                      else
 461   3                      {
 462   4                          USB_EP0_STALL();
 463   4                      }
 464   3                  }
 465   2                  break;
 466   2              case USB_DESC_INTERFACE:
 467   2              case USB_DESC_ENDPOINT:
 468   2              case USB_DESC_DEVICE_QUAL:
 469   2              case USB_DESC_OTHER_SPEED_CONF:
 470   2              case USB_DESC_INTERFACE_POWER:
 471   2                  USB_EP0_STALL();
 472   2                  break;
 473   2              default:
 474   2                  ret = g_hal_usb.device_req(req, &data_ptr, &data_size);
 475   2                  usb_process_dev_req_cb_response(ret, req, data_ptr, data_size);
 476   2                  break;
 477   2          }
 478   1      }
C51 COMPILER V7.50   HAL_USB                                                               04/09/2009 10:12:51 PAGE 9   

 479          
 480          static void isr_sudav()
 481          {
 482   1          // Parse data in setupbuf
 483   1          hal_usb_dev_req_resp_t ret;
 484   1          uint8_t *data_ptr;
 485   1          uint16_t data_size;
 486   1      
 487   1           // Parsing the request into request structure
 488   1          req.bmRequestType = i_usb.map->setupbuf[0];
 489   1          req.bRequest = i_usb.map->setupbuf[1];
 490   1          req.wValue = i_usb.map->setupbuf[2] + (i_usb.map->setupbuf[3] << 8);
 491   1          req.wIndex = i_usb.map->setupbuf[4] + (i_usb.map->setupbuf[5] << 8);
 492   1          req.wLength = i_usb.map->setupbuf[6] + (i_usb.map->setupbuf[7] << 8);
 493   1          req.wLength = req.wLength > 0xff ? 0xff : LSB(req.wLength); // We truncate packets requests longer the
             -n 255 bytes
 494   1      
 495   1      #if 0
                  uart0_putstring("\r\n--\r\nbmRequestType: "); hex_out_byte(req.bmRequestType);
                  uart0_putstring("\r\nbRequest     : "); hex_out_byte(req.bRequest);
                  uart0_putstring("\r\nwValue [H][L]: "); hex_out_byte(MSB(req.wValue)); hex_out_byte(LSB(req.wValue));
                  uart0_putstring("\r\nwIndex [H][L]: "); hex_out_byte(MSB(req.wIndex)); hex_out_byte(LSB(req.wIndex));
                  uart0_putstring("\r\nwLength[H][L]: "); hex_out_byte(MSB(req.wLength)); hex_out_byte(LSB(req.wLength))
             -;
              #endif
 502   1      
 503   1           // bmRequestType = 0 00 xxxxx : Data transfer direction: Host-to-device Type: Standard
 504   1          if( ( req.bmRequestType & 0x60 ) == 0x00 )
 505   1          {
 506   2              switch(req.bRequest)
 507   2              {
 508   3                 case USB_REQ_GET_DESCRIPTOR:
 509   3                     usb_process_get_descriptor(&req);
 510   3                     break;
 511   3                 case USB_REQ_GET_STATUS:
 512   3                     usb_process_get_status(&req); 
 513   3                     break; // case USB_REQ_GET_STATUS --end--
 514   3                 case USB_REQ_CLEAR_FEATURE: 
 515   3                    switch(req.bmRequestType)
 516   3                    {
 517   4                        case 0x00: // Device
 518   4                            if( LSB(req.wValue) == USB_DEVICE_REMOTE_WAKEUP )
 519   4                            {
 520   5                                g_hal_usb.bm_state &= ~(USB_BM_STATE_ALLOW_REMOTE_WAKEUP);
 521   5                                USB_EP0_HSNAK();
 522   5                            }
 523   4                            else USB_EP0_STALL();
 524   4                            break;
 525   4                        case 0x01: // Interface
 526   4                            USB_EP0_STALL();
 527   4                            break;
 528   4                        case 0x02: // Endpoint
 529   4                            if( LSB(req.wValue) == USB_ENDPOINT_HALT )
 530   4                            {
 531   5                                hal_usb_endpoint_stall(LSB(req.wIndex), false);
 532   5                                USB_EP0_HSNAK();
 533   5                            }
 534   4                            else USB_EP0_STALL();
 535   4                            break;
 536   4                        default:
 537   4                            USB_EP0_STALL();
 538   4                            break;
C51 COMPILER V7.50   HAL_USB                                                               04/09/2009 10:12:51 PAGE 10  

 539   4                    }
 540   3                    break;
 541   3                 case USB_REQ_SET_FEATURE: 
 542   3                     switch(req.bmRequestType)
 543   3                     {
 544   4                         case 0x00: // Device
 545   4                             if( LSB(req.wValue) == USB_DEVICE_REMOTE_WAKEUP )
 546   4                             {
 547   5                                 g_hal_usb.bm_state |= USB_BM_STATE_ALLOW_REMOTE_WAKEUP;
 548   5                                 USB_EP0_HSNAK();
 549   5                             }
 550   4                             else USB_EP0_STALL();
 551   4                             break;
 552   4                         case 0x01: // Interface
 553   4                             USB_EP0_STALL();
 554   4                             break;
 555   4                         case 0x02: // Endpoint - TODO: check for valid endpoints here
 556   4                             if( LSB(req.wValue) == USB_ENDPOINT_HALT )
 557   4                             {
 558   5                                  hal_usb_endpoint_stall(LSB(req.wIndex), true);
 559   5                                  USB_EP0_HSNAK();
 560   5                             }
 561   4                             else
 562   4                             {
 563   5                                  USB_EP0_STALL();
 564   5                             }
 565   4                             break;
 566   4                         default:
 567   4                             USB_EP0_STALL();
 568   4                             break;
 569   4                     }
 570   3                     break;
 571   3              case USB_REQ_SET_ADDRESS:
 572   3                 g_hal_usb.state = ADDRESSED;
 573   3                 g_hal_usb.current_config = 0x00;
 574   3                 break;
 575   3              case USB_REQ_SET_DESCRIPTOR:
 576   3                 USB_EP0_STALL();
 577   3                 break;
 578   3              case USB_REQ_GET_CONFIGURATION:
 579   3                 switch( g_hal_usb.state )
 580   3                 {
 581   4                     case ADDRESSED:
 582   4                         i_usb.map->in0buf[0] = 0x00;
 583   4                         i_usb.map->in0bc = 0x01;
 584   4                         break;
 585   4                     case CONFIGURED:
 586   4                         i_usb.map->in0buf[0] = g_hal_usb.current_config;
 587   4                         i_usb.map->in0bc = 0x01;
 588   4                         break;
 589   4                     default:
 590   4                         USB_EP0_STALL();
 591   4                         break;
 592   4                 }
 593   3                 break;
 594   3              case USB_REQ_SET_CONFIGURATION:
 595   3                 switch(LSB(req.wValue)) {
 596   4                     case 0x00:
 597   4                         g_hal_usb.state = ADDRESSED;
 598   4                         g_hal_usb.current_config = 0x00;
 599   4                         USB_EP0_HSNAK();
 600   4                         break;

⌨️ 快捷键说明

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