📄 chap_9.lst
字号:
291 2 ENABLE;
292 2 init_unconfig();
293 2 } else if (ControlData.DeviceRequest.wValue == 1) {
294 2 /* Configure device */
295 2 single_transmit(0, 0);
296 2
297 2 init_unconfig();
298 2 init_config();
299 2
300 2 DISABLE;
301 2 bEPPflags.bits.configuration = 1;
302 2 ENABLE;
303 2 } else
C51 COMPILER V7.06 CHAP_9 07/02/2003 10:38:56 PAGE 6
304 1 stall_ep0();
305 1 }
306
307 void get_interface(void)
308 {
309 1 unsigned char txdat = 0; /* Only/Current interface = 0 */
310 1 single_transmit(&txdat, 1);
311 1 }
312
313 void set_interface(void)
314 {
315 1 if (ControlData.DeviceRequest.wValue == 0 && ControlData.DeviceRequest.wIndex == 0)
316 1 single_transmit(0, 0);
317 1 else
318 1 stall_ep0();
319 1 }
320
321
322
323
324
325
326 //Move by Liu Ying Bin
327
328
329 void init_unconfig(void)
330 {
331 1 //unsigned char i;
332 1
333 1 D12_SetEndpointEnable(0); /* Disable all endpoints but EPP0. */
334 1 }
335
336 void init_config(void)
337 {
338 1 D12_SetEndpointEnable(1); /* Enable generic/iso endpoints. */
339 1 }
340
341 void single_transmit(unsigned char * buf, unsigned char len)
342 {
343 1 if( len <= EP0_PACKET_SIZE) {
344 2 D12_WriteEndpoint(1, len, buf);
345 2 }
346 1 }
347
348 void code_transmit(unsigned char code * pRomData, unsigned short len)
349 {
350 1 ControlData.wCount = 0;
351 1 if(ControlData.wLength > len)
352 1 ControlData.wLength = len;
353 1
354 1 ControlData.pData = pRomData;
355 1 if( ControlData.wLength >= EP0_PACKET_SIZE) {
356 2 D12_WriteEndpoint(1, EP0_PACKET_SIZE, ControlData.pData);
357 2 ControlData.wCount += EP0_PACKET_SIZE;
358 2
359 2 DISABLE;
360 2 bEPPflags.bits.control_state = USB_TRANSMIT;
361 2 ENABLE;
362 2 }
363 1 else {
364 2 D12_WriteEndpoint(1, ControlData.wLength, pRomData);
365 2 ControlData.wCount += ControlData.wLength;
C51 COMPILER V7.06 CHAP_9 07/02/2003 10:38:56 PAGE 7
366 2 DISABLE;
367 2 bEPPflags.bits.control_state = USB_IDLE;
368 2 ENABLE;
369 2 }
370 1 }
371
372 void control_handler()
373 {
374 1 unsigned char type, req;
375 1
376 1 type = ControlData.DeviceRequest.bmRequestType & USB_REQUEST_TYPE_MASK;
377 1 req = ControlData.DeviceRequest.bRequest & USB_REQUEST_MASK;
378 1
379 1 if (type == USB_STANDARD_REQUEST)
380 1 (*StandardDeviceRequest[req])();
381 1 else if (type == USB_VENDOR_REQUEST)
382 1 (*VendorDeviceRequest[req])();
383 1 else
384 1 stall_ep0();
385 1 }
386
387
388 void fn_usb_isr()
389 {
390 1 unsigned int i_st;
391 1 bEPPflags.bits.in_isr = 1;
392 1 i_st = D12_ReadInterruptRegister();
393 1
394 1 if(i_st != 0) {
395 2 if(i_st & D12_INT_BUSRESET) {
396 3 bus_reset();
397 3 bEPPflags.bits.bus_reset = 1;
398 3 }
399 2
400 2 if(i_st & D12_INT_EOT)
401 2 dma_eot();
402 2
403 2 if(i_st & D12_INT_SUSPENDCHANGE)
404 2 bEPPflags.bits.suspend = 1;
405 2
406 2 if(i_st & D12_INT_ENDP0IN)
407 2 ep0_txdone();
408 2 if(i_st & D12_INT_ENDP0OUT)
409 2 ep0_rxdone();
410 2 if(i_st & D12_INT_ENDP1IN)
411 2 ep1_txdone();
412 2 if(i_st & D12_INT_ENDP1OUT)
413 2 ep1_rxdone();
414 2 if(i_st & D12_INT_ENDP2IN)
415 2 ep2_txdone();
416 2 if(i_st & D12_INT_ENDP2OUT)
417 2 ep2_rxdone();
418 2 }
419 1
420 1 bEPPflags.bits.in_isr = 0;
421 1 }
422
423 void stall_ep0(void)
424 {
425 1 D12_SetEndpointStatus(0, 1);
426 1 D12_SetEndpointStatus(1, 1);
427 1 }
C51 COMPILER V7.06 CHAP_9 07/02/2003 10:38:56 PAGE 8
428
429 void ep0_rxdone(void)
430 {
431 1 unsigned char ep_last, i;
432 1
433 1 ep_last = D12_ReadLastTransactionStatus(0); // Clear interrupt flag
434 1
435 1 if (ep_last & D12_SETUPPACKET) {
436 2
437 2 ControlData.wLength = 0;
438 2 ControlData.wCount = 0;
439 2
440 2 if( D12_ReadEndpoint(0, sizeof(ControlData.DeviceRequest),
441 2 (unsigned char *)(&(ControlData.DeviceRequest))) != sizeof(DEVICE_REQUEST) ) {
442 3
443 3 D12_SetEndpointStatus(0, 1);
444 3 D12_SetEndpointStatus(1, 1);
445 3 bEPPflags.bits.control_state = USB_IDLE;
446 3
447 3 return;
448 3 }
449 2
450 2 ControlData.DeviceRequest.wValue = SWAP(ControlData.DeviceRequest.wValue);
451 2 ControlData.DeviceRequest.wIndex = SWAP(ControlData.DeviceRequest.wIndex);
452 2 ControlData.DeviceRequest.wLength = SWAP(ControlData.DeviceRequest.wLength);
453 2
454 2 // Acknowledge setup here to unlock in/out endp
455 2 D12_AcknowledgeEndpoint(0);
456 2 D12_AcknowledgeEndpoint(1);
457 2
458 2 ControlData.wLength = ControlData.DeviceRequest.wLength;
459 2 ControlData.wCount = 0;
460 2
461 2 if (ControlData.DeviceRequest.bmRequestType & (unsigned char)USB_ENDPOINT_DIRECTION_MASK) {
462 3 bEPPflags.bits.setup_packet = 1;
463 3 bEPPflags.bits.control_state = USB_TRANSMIT; /* get command */
464 3 }
465 2 else {
466 3 if (ControlData.DeviceRequest.wLength == 0) {
467 4 bEPPflags.bits.setup_packet = 1;
468 4 bEPPflags.bits.control_state = USB_IDLE; /* set command */
469 4 }
470 3 else {
471 4 if(ControlData.DeviceRequest.wLength > MAX_CONTROLDATA_SIZE) {
472 5 bEPPflags.bits.control_state = USB_IDLE;
473 5 D12_SetEndpointStatus(0, 1);
474 5 D12_SetEndpointStatus(1, 1);
475 5 }
476 4 else {
477 5 bEPPflags.bits.control_state = USB_RECEIVE; /* set command with OUT token */
478 5 }
479 4 } // set command with data
480 3 } // else set command
481 2 } // if setup packet
482 1
483 1 else if (bEPPflags.bits.control_state == USB_RECEIVE) {
484 2 i = D12_ReadEndpoint(0, EP0_PACKET_SIZE,
485 2 ControlData.dataBuffer + ControlData.wCount);
486 2
487 2 ControlData.wCount += i;
488 2 if( i != EP0_PACKET_SIZE || ControlData.wCount >= ControlData.wLength) {
489 3 bEPPflags.bits.setup_packet = 1;
C51 COMPILER V7.06 CHAP_9 07/02/2003 10:38:56 PAGE 9
490 3 bEPPflags.bits.control_state = USB_IDLE;
491 3 }
492 2 }
493 1
494 1 else {
495 2 bEPPflags.bits.control_state = USB_IDLE;
496 2 }
497 1 }
498
499 void ep0_txdone(void)
500 {
501 1 short i = ControlData.wLength - ControlData.wCount;
502 1
503 1 D12_ReadLastTransactionStatus(1); // Clear interrupt flag
504 1
505 1 if (bEPPflags.bits.control_state != USB_TRANSMIT)
506 1 {
507 2 single_transmit(0, 0);
508 2 return;
509 2 }
510 1
511 1 if( i >= EP0_PACKET_SIZE) {
512 2 D12_WriteEndpoint(1, EP0_PACKET_SIZE, ControlData.pData + ControlData.wCount);
513 2 ControlData.wCount += EP0_PACKET_SIZE;
514 2
515 2 bEPPflags.bits.control_state = USB_TRANSMIT;
516 2 }
517 1 else if( i != 0) {
518 2 D12_WriteEndpoint(1, i, ControlData.pData + ControlData.wCount);
519 2 ControlData.wCount += i;
520 2
521 2 bEPPflags.bits.control_state = USB_IDLE;
522 2 }
523 1 else if (i == 0){
524 2 D12_WriteEndpoint(1, 0, 0); // Send zero packet at the end ???
525 2
526 2 bEPPflags.bits.control_state = USB_IDLE;
527 2 }
528 1 }
529
530
531 void usbserve(void)
532 {
533 1 if (bEPPflags.bits.bus_reset) {
534 2 DISABLE;
535 2 bEPPflags.bits.bus_reset = 0;
536 2 ENABLE;
537 2 // Release D12's SUSPEND pin after bus reset
538 2 // Enable 74HCT123 pulse generation before disconnect
539 2 //D12SUSPD = 1;
540 2 } // if bus reset
541 1
542 1 if (bEPPflags.bits.suspend) {
543 2 DISABLE;
544 2 bEPPflags.bits.suspend= 0;
545 2 ENABLE;
546 2 /*
547 2 if(D12SUSPD == 1) {
548 2 D12SUSPD = 0;
549 2 P0 = 0xFF;
550 2 P1 = 0xFF;
551 2 P2 = 0xFF;
C51 COMPILER V7.06 CHAP_9 07/02/2003 10:38:56 PAGE 10
552 2 P3 = 0xFF;
553 2 D12_SetDMA(0x0);
554 2 D12SUSPD = 1;
555 2 PCON |= 0x02;
556 2 while (1);
557 2 }*/
558 2
559 2 } // if suspend change
560 1
561 1 if (bEPPflags.bits.setup_packet){
562 2 DISABLE;
563 2 bEPPflags.bits.setup_packet = 0;
564 2 control_handler();
565 2 ENABLE;
566 2 //D12SUSPD = 1;
567 2 } // if setup_packet
568 1 }
569
570
571
572
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 1574 ----
CONSTANT SIZE = 112 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 28
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 + -