📄 usb2ser.lst
字号:
299 3 sendToUSB = 1; // let everyone know that we'll be sending a packet to USB.
300 3 }
301 2
302 2 if (rxOutPtr != rxInPtr) // if we have data to send...
303 2 {
C51 COMPILER V6.03b USB2SER 08/16/2001 12:50:55 PAGE 6
304 3 do
305 3 {
306 4 IN1BUF[Ep1Index] = rxBuffer[rxOutPtr];
307 4 rxOutPtr = (rxOutPtr + 1) % RX_BUFFER_SIZE;
308 4 Ep1Index++;
309 4 } while ( (Ep1Index < TOTAL_IN_LENGTH) && (rxOutPtr != rxInPtr) );
310 3
311 3 sendToUSB = 1; // let everyone know that we'll be sending a packet to USB.
312 3 }
313 2
314 2 if (sendToUSB) // if we have something to send...
315 2 {
316 3 IN1BUF[0] = bitTemp;
317 3 IN1BUF[1] = Ep1Index - 2;
318 3
319 3 IN1BC = Ep1Index;
320 3 }
321 2 }
322 1
323 1 //
324 1 // FROM USB
325 1 //
326 1
327 1 if ( !(OUT2CS & bmEPBUSY) ) // If data's waiting in EP2OUT
328 1 {
329 2 BYTE outLength;
330 2
331 2 #if (DEBUG)
332 2 OUTA ^= 0x04;
333 2 #endif
334 2
335 2 #if (PASS_DTR)
336 2 if (OUT2BUF[0] & bmBIT5) DTRon;
337 2 else DTRoff;
338 2 #endif
339 2
340 2 if (OUT2BUF[0] & bmBIT4) hostRTS = 1;
341 2 else hostRTS = 0;
342 2
343 2 outLength = OUT2BUF[1]+2;
344 2
345 2 while ( (Ep2Index != outLength)
346 2 && (((txInPtr + 1) % TX_BUFFER_SIZE) != txOutPtr) )
347 2 {
348 3 #if (DEBUG)
349 3 OUTA ^= 0x20;
350 3 #endif
351 3
352 3 txBuffer[txInPtr] = OUT2BUF[Ep2Index];
353 3 txInPtr = (txInPtr + 1) % TX_BUFFER_SIZE;
354 3 Ep2Index++;
355 3 }
356 2
357 2 if (Ep2Index == outLength)
358 2 {
359 3 Ep2Index = 2;
360 3 OUT2BC = 0;
361 3 }
362 2 }
363 1
364 1 //
365 1 // TO RS-232
C51 COMPILER V6.03b USB2SER 08/16/2001 12:50:55 PAGE 7
366 1 //
367 1
368 1 if (TI)
369 1 {
370 2 #if (DEBUG)
371 2 OUTA ^= 0x08;
372 2 #endif
373 2
374 2 #if (USE_DSR)
375 2 if (DSRisOn)
376 2 #endif
377 2 {
378 3 if (txOutPtr != txInPtr) // If we have data in txBuffer[]
379 3 {
380 4 #if (DEBUG)
381 4 OUTA ^= 0x40;
382 4 #endif
383 4
384 4 #if (RTS_MODE == 1)
385 4 if (RTSisOn)
386 4 #endif
387 4 {
388 5 #if (USE_CTS)
if (CTSisOn)
#endif
391 5 {
392 6 TI = 0;
393 6 SBUF0 = txBuffer[txOutPtr];
394 6 txOutPtr = (txOutPtr + 1) % TX_BUFFER_SIZE;
395 6 }
396 5 }
397 4 #if (RTS_MODE == 1)
398 4 else
399 4 {
400 5 #if (STRICT_RTS_SPEC)
if (!CTSisOn)
#endif
403 5 {
404 6 localRTS = 1;
405 6 }
406 5 }
407 4 #endif
408 4 }
409 3 else // No data in txBuffer[]
410 3 {
411 4 #if (DEBUG)
412 4 OUTA ^= 0x80;
413 4 #endif
414 4
415 4 #if (RTS_MODE == 1)
416 4 localRTS = 0;
417 4 #endif
418 4 }
419 3 }
420 2 }
421 1
422 1 //
423 1 // FROM RS-232
424 1 //
425 1
426 1 if (RI)
427 1 {
C51 COMPILER V6.03b USB2SER 08/16/2001 12:50:55 PAGE 8
428 2 #if (DEBUG)
429 2 OUTA ^= 0x10;
430 2 #endif
431 2
432 2 nextRxInPtr = (rxInPtr + 1) % RX_BUFFER_SIZE;
433 2
434 2 if (nextRxInPtr == rxOutPtr) // If rxBuffer[] full
435 2 {
436 3 #if (RTS_MODE == 2)
localRTS = 0;
#endif
439 3 }
440 2 else
441 2 {
442 3 #if (STRICT_RTS_SPEC)
if (!CTSisOn)
#endif
445 3 {
446 4 #if (RTS_MODE == 2)
localRTS = 1;
#endif
449 4 }
450 3
451 3 #if (RTS_MODE == 2)
if (RTSisOn)
#endif
454 3 {
455 4 rxBuffer[rxInPtr] = SBUF0;
456 4 rxInPtr = nextRxInPtr;
457 4
458 4 RI = 0;
459 4 }
460 3 }
461 2 }
462 1 }
463
464 //////////////////////////////
465 // //
466 // Setup the UART Baud Rate //
467 // //
468 //////////////////////////////
469
470 void setup_uart(BYTE b[])
471 {
472 1 BYTE thi, tlo;
473 1
474 1 // setup UART registers
475 1 // using timer2 for minimal baud rate error
476 1
477 1 thi = 0xFF; // Assume baudrate > 2400, so thi = 0xFF.
478 1
479 1 switch(b[1]) // Set tlo (and thi, for baud rates <= 2400)
480 1 {
481 2 case BAUDRATE_57600:
482 2 tlo = 0xF3;
483 2 break;
484 2
485 2 case BAUDRATE_38400:
486 2 tlo = 0xEC;
487 2 break;
488 2
489 2 case BAUDRATE_28800:
C51 COMPILER V6.03b USB2SER 08/16/2001 12:50:55 PAGE 9
490 2 tlo = 0xE6;
491 2 break;
492 2
493 2 case BAUDRATE_19200:
494 2 tlo = 0xD9;
495 2 break;
496 2
497 2 case BAUDRATE_9600:
498 2 tlo = 0xB2;
499 2 break;
500 2
501 2 case BAUDRATE_4800:
502 2 tlo = 0x64;
503 2 break;
504 2
505 2 case BAUDRATE_2400:
506 2 thi = 0xFE;
507 2 tlo = 0xC8;
508 2 break;
509 2
510 2 case BAUDRATE_1200:
511 2 thi = 0xFD;
512 2 tlo = 0x8F;
513 2 break;
514 2
515 2 case BAUDRATE_300:
516 2 thi = 0xF6;
517 2 tlo = 0x3C;
518 2 break;
519 2
520 2 default:
521 2 tlo = 0xB2; // 9600 baud
522 2 break;
523 2 }
524 1
525 1 RCAP2H = thi; // auto-reload, baudrate determined by set_report class request
526 1 RCAP2L = tlo; // auto-reload, baudrate determined by set_report class request
527 1 T2CON = 0x34; // overflow clk, start timer
528 1
529 1 #if (PARITY)
SCON0 |=0xD0; // mode 3 (parity)
#else
532 1 SCON0 |=0x50; // mode 1 (no parity)
533 1 #endif
534 1 }
535
536 //
537 // This function is called before the device goes into suspend mode.
538 //
539
540 BOOL TD_Suspend(void)
541 {
542 1 // Turn off the breakpoint light before entering suspend
543 1
544 1 USBBAV |= bmBREAK; // Clear the breakpoint
545 1 return(TRUE);
546 1 }
547
548 //
549 // This function is called after the device resumes from suspend mode.
550 //
551
C51 COMPILER V6.03b USB2SER 08/16/2001 12:50:55 PAGE 10
552 BOOL TD_Resume(void)
553 {
554 1 return(TRUE);
555 1 }
556
557 //-----------------------------------------------------------------------------
558 // Device Request hooks
559 // The following hooks are called by the end point 0 device request parser.
560 //-----------------------------------------------------------------------------
561
562 BOOL DR_ClassRequest(void)
563 {
564 1 BYTE i;
565 1
566 1 BYTE cntl_report[5]; // buffer to contain EP0 data
567 1
568 1 BYTE baudrate[4]; // buffer to contain baud rate information
569 1 BYTE out_cntl_flags;
570 1 BYTE bc; // EP0 byte count
571 1
572 1 switch(SETUPDAT[1]) // check to see what class request is being handled
573 1 {
574 2 case CR_SET_REPORT: // set_report request
575 2 OUT0BC = 0; // arm endpoint zero to handle data phase
576 2
577 2 while(EP0CS & 0x08); // wait for busy bit to be cleared
578 2
579 2 bc = OUT0BC; // Get the new bytecount
580 2
581 2 for(i=0; i<bc; i++)
582 2 {
583 3 cntl_report[i] = OUT0BUF[i]; // get configuration data from host
584 3 }
585 2
586 2 baudrate[1] = cntl_report[1]; // gets high byte of baudrate and will use this to determine
587 2 baudrate[0] = cntl_report[0]; // timer-reload value
588 2 setup_uart(baudrate); // setup UART baud rate
589 2 out_cntl_flags = cntl_report[4]; // set flags in the control OUT transfer
590 2
591 2 return (FALSE);
592 2 break;
593 2
594 2 case CR_GET_REPORT: // get_report request
595 2 for(i=0; i<5; i++)
596 2 {
597 3 IN0BUF[i] = cntl_report[i]; // transmits configuration data to host
598 3 }
599 2 IN0BC = 5;
600 2 return (FALSE);
601 2 break;
602 2
603 2 default:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -