MainEntry.cpp 33.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
//////////////////////////////////////////////////////////////////////////
//----------------------------------------------------------------------//
//-- Copyright 2001 VAutomation Inc. Nashua NH USA. All rights reserved.//
//-- This software is provided under license and contains proprietary   //
//-- and confidential material which is the property of VAutomation Inc.//
//-- HTTP://www.vautomation.com											//
//----------------------------------------------------------------------//
//////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////
// MainEntry.cpp 
/////////////////////////////////////////////////////////
 
/////////////////////////////////////////////////////////
// Common API headers
/////////////////////////////////////////////////////////
#include "USB1_Common.h"



//////////////////////////////////////////////////////////
// Non-API includes
//////////////////////////////////////////////////////////



//////////////////////////////////////////////////////////
// Global information shared with the IRQ function
//////////////////////////////////////////////////////////
BufferDescriptorTableType *g_BufferDescriptorTablePtr;
UnSignedLong		   g_CurrentTestProgress;
bool			   g_DoneWithDeviceTests = false;
bool                       g_DoneWithHostTests = false;
bool                       g_DoneWithOTGTests = false;
bool                       g_DoneWithLineStateTests = false;

/////////////////////////////////////////////////////////
// Global hardware information
//////////////////////////////////////////////////////////

CVirtualComponentInterface		*g_host_dma;
CVirtualComponentInterface		*g_usb_initiator;
CMessageIO						*g_message_interface;
CInterruptControllerInterface	*g_interrupt_cntrl;


/////////////////////////////////////////////////////////
// Local Functions
//////////////////////////////////////////////////////////
bool MainTestThread();

void InitBDTptrs(UnSignedLong Status, 
		 BufferDescriptor **currBDT_ptr,
		 BufferDescriptor **sendBDT_ptr,
		 BufferDescriptor **nxtRcvBDT_ptr); 


//////////////////////////////////////////////////////////
// IRQ callback functions
//
// when we get an interrupt read the interrupt status 
// register, clear the interrupt and pass along the cause 
// of the interrupt to the main code
//
//////////////////////////////////////////////////////////

void InterruptCallbackIRQ_5( void )
{
  BufferDescriptor *currBDT_ptr=NULL;
  BufferDescriptor *sendBDT_ptr=NULL;
  BufferDescriptor *nxtRcvBDT_ptr=NULL;
  UnSignedLong IntStatus_write = 0x0;
  UnSignedLong bDirection;
  UnSignedLong *rcv_pkt_ptr=NULL;

  // g_message_interface->PrintStringStdioLF( "InterruptCallback Called for USB");
  UnSignedLong USB_IntStatus;
  UnSignedLong USB_IntEnable;
  UnSignedLong USB_ErrStatus;
  UnSignedLong USB_ErrEnable;
  UnSignedLong USB_Status;
  UnSignedLong USB_IntMasked;
  UnSignedLong USB_OTG_IStat;
  UnSignedLong USB_OTG_Stat;
  UnSignedLong USB_Ctrl;


  g_CurrentTestProgress++;	

  // Save values of some USB registers
  if( g_usb_initiator->ReadBuffer( (AddressType)(&USB_IntStatus),
				   int_stat_addr, 
				   sizeof(UnSignedLong), false ) == false )
  {
    SystemAbort("Error ReadBuffer", (g_CurrentTestProgress<<4) );
  }

  if( g_usb_initiator->ReadBuffer( (AddressType)(&USB_IntEnable),
				   int_enbl_addr, 
				   sizeof(UnSignedLong), false ) == false )
  {
    SystemAbort("Error ReadBuffer", (g_CurrentTestProgress<<4) );
  }

  if( g_usb_initiator->ReadBuffer( (AddressType)(&USB_ErrStatus),
				   err_stat_addr, 
				   sizeof(UnSignedLong), false ) == false )
  {
    SystemAbort("Error ReadBuffer", (g_CurrentTestProgress<<4) );
     
  }

  if( g_usb_initiator->ReadBuffer( (AddressType)(&USB_ErrEnable),
				   err_enbl_addr, 
				   sizeof(UnSignedLong), false ) == false )
  {
    SystemAbort("Error ReadBuffer", (g_CurrentTestProgress<<4) );
  }

  if( g_usb_initiator->ReadBuffer( (AddressType)(&USB_Status),
				   usb_stat_addr, 
				   sizeof(UnSignedLong), false ) == false )
  {
    SystemAbort("Error ReadBuffer", (g_CurrentTestProgress<<4) );
  }

  // read otg interrupt status
  if( g_usb_initiator->ReadBuffer( (AddressType)(&USB_OTG_IStat),
				   otg_istat_addr, 
				   sizeof(UnSignedLong), false ) == false )
  {
    SystemAbort("Error ReadBuffer", (g_CurrentTestProgress<<4) );
  }

  USB_IntMasked = USB_IntStatus & USB_IntEnable;
  bDirection = (USB_Status >> 3) & 0x01;

  InitBDTptrs(USB_Status, &currBDT_ptr, &sendBDT_ptr, &nxtRcvBDT_ptr); 

  switch (g_CurrentTestProgress) 
    {
    case 1:
      
      // A reset interrupt
      if (USB_IntMasked == 0x01) 
	{ 	// Reset Interrupt

	  // Set the value to clear the interrupt
	  IntStatus_write = 0x01;
		
	  // at end of USB Reset set endpoint 0 control to enable RXD TXD and Setup tokens
	  UnSignedLong endpt0_ctrl = 0x0D;

	  if( g_usb_initiator->WriteBuffer( end_ctl0_addr, 
					    (AddressType)(&endpt0_ctrl), 
					    sizeof(UnSignedLong), false ) == false )
	    {
	      SystemAbort("Error WriteBuffer", (g_CurrentTestProgress<<4) | 2 );
	    }
			
	  // at end of USB Reset enable interrupts
	  UnSignedLong int_enb      = 0xB9;
	  if( g_usb_initiator->WriteBuffer( int_enbl_addr, 
					    (AddressType)(&int_enb), 
					    sizeof(UnSignedLong), false ) == false ) 
	    {
	      SystemAbort("Error Write Buffer", (g_CurrentTestProgress<<4) | 3 );
	    }

	}
      else 
	{
	  SystemAbort("Error - Did not get reset interrupt", (g_CurrentTestProgress<<4) | 1 );
      
	}

      break;

    case 2:
    case 4:
    case 6:
    case 8:
    
      // A token Done Interrupt for a setup packet
      if (USB_IntMasked == 0x08 && bDirection == 0) 
	{

	  // Make sure the packet is a setup packet
	  if (currBDT_ptr->pid == 0xd) 
	    {
	      // Value to clear interrupt
	      IntStatus_write = USB_IntMasked;

	      // Write the packet length
	      sendBDT_ptr->bytecnt = currBDT_ptr->bytecnt;

	      // Set the pid
	      sendBDT_ptr->pid = 0;

	      // Set the own bit on the transmit packet
	      sendBDT_ptr->own = 1;
	      
	      // clear txd suspend
	      UnSignedLong txd_suspend_clr = 0x01;
	      if( g_usb_initiator->WriteBuffer( usb_ctrl_addr, 
						(AddressType)(&txd_suspend_clr), 
						sizeof(UnSignedLong), false ) == false )
		{
		  SystemAbort("Error WriteBuffer", (g_CurrentTestProgress<<4) | 3 );
		}
	    }
	  else 
	    {
	      SystemAbort("Error - Not a setup packet", (g_CurrentTestProgress<<4) | 2 );
	    }
	}
      else 
	{
	  SystemAbort("Error - No token done interrupt for RX packet", (g_CurrentTestProgress<<4) | 1 );
	}	
      break;
      
      
    case 3:
    case 5:
    case 7:
    case 9:

      // Now there should be a token done interrupt caused by the respond data going out
      if (USB_IntMasked == 0x8 && bDirection == 1) // token done, tx packet
	{ 
	  // Value to clear interrupt
	  IntStatus_write = USB_IntStatus;

	  // Set the max packet size
	  nxtRcvBDT_ptr->bytecnt = 8;

	  // Set the pid
	  nxtRcvBDT_ptr->pid = 0;

	  // Set the own bit on the next RX packet
	  nxtRcvBDT_ptr->own = 1;
	}
      else
	{
	  SystemAbort("Error - No token done interrupt for TX packet", (g_CurrentTestProgress<<4) | 1);
	}
      break;

    case 10:

      // A packet should be received... and we should switch into host mode
      if (USB_IntMasked == 0x8 && bDirection == 0) // token done, rx packet
      {
	  // Check if the packet is a null data1 packet
	  if (currBDT_ptr->pid == 0x1 && currBDT_ptr->data0_1 == 0 && 
	      currBDT_ptr->bytecnt == 0)
	    {
	      IntStatus_write = USB_IntStatus;
	    }
	  else 
	    {
	      SystemAbort("Error - Packet not a NULL data1 packet", (g_CurrentTestProgress<<4) | 2 );
	    }
      	UnSignedLong turn_all_off      = 0x00;
		if( g_usb_initiator->WriteBuffer( int_enbl_addr, 
							(AddressType)(&turn_all_off), 
							sizeof(UnSignedLong), false ) == false )
		{
		  SystemAbort("Error WriteBuffer", (g_CurrentTestProgress<<4) | 2 );
		}
      }
      else 
	    {
	    SystemAbort("Error - No token done interrupt for RX packet", (g_CurrentTestProgress<<4) | 1 );	
	    }	
      break;

	case 11:

	  // Value to clear interrupt
	  IntStatus_write = USB_IntStatus;

      // this should be a line state change interrupt -- we should be in SE0 State
      if  (USB_OTG_IStat & 0x20) 

        {
         if( g_usb_initiator->ReadBuffer( (AddressType)(&USB_Ctrl),
		    		   usb_ctrl_addr, 
				   sizeof(UnSignedLong), false ) == false )
         {
          SystemAbort("Error ReadBuffer", (g_CurrentTestProgress<<4) );
         }
         if ((USB_Ctrl & 0x40) == 0)
           {
           SystemAbort("Error Not in SE0 State -- Linestate Testing", (g_CurrentTestProgress<<4) );
           }
         if( g_usb_initiator->ReadBuffer( (AddressType)(&USB_OTG_Stat),
		  		   usb_ctrl_addr, 
				   sizeof(UnSignedLong), false ) == false )
         {
           SystemAbort("Error ReadBuffer", (g_CurrentTestProgress<<4) );
         }
         if( g_usb_initiator->ReadBuffer( (AddressType)(&USB_OTG_Stat),
		    		   otg_stat_addr, 
				   sizeof(UnSignedLong), false ) == false )
         {
          SystemAbort("Error ReadBuffer", (g_CurrentTestProgress<<4) );
         }
         if ((USB_OTG_Stat & 0x20) == 0)
           {
           SystemAbort("Error Linestate Stable Not Set -- Linestate Testing", (g_CurrentTestProgress<<4) );
           }

      }
      else 
	    {
	    SystemAbort("Error - Didn't Receive Line State Change Interrupt", (g_CurrentTestProgress<<4) | 1 );	
	    }	
      break;

    case 12:

	  // Value to clear interrupt
	  IntStatus_write = USB_IntStatus;

      // this should be a line state change interrupt -- we should be in J State
      if  (USB_OTG_IStat & 0x20) 

        {
         if( g_usb_initiator->ReadBuffer( (AddressType)(&USB_Ctrl),
		    		   usb_ctrl_addr, 
				   sizeof(UnSignedLong), false ) == false )
         {
          SystemAbort("Error ReadBuffer", (g_CurrentTestProgress<<4) );
         }
         if ((USB_Ctrl & 0x80) == 0)
           {
           SystemAbort("Error Not in J State -- Linestate Testing", (g_CurrentTestProgress<<4) );
           }
         if( g_usb_initiator->ReadBuffer( (AddressType)(&USB_OTG_Stat),
		  		   usb_ctrl_addr, 
				   sizeof(UnSignedLong), false ) == false )
         {
           SystemAbort("Error ReadBuffer", (g_CurrentTestProgress<<4) );
         }
         if( g_usb_initiator->ReadBuffer( (AddressType)(&USB_OTG_Stat),
		    		   otg_stat_addr, 
				   sizeof(UnSignedLong), false ) == false )
         {
          SystemAbort("Error ReadBuffer", (g_CurrentTestProgress<<4) );
         }
         if ((USB_OTG_Stat & 0x20) == 0)
           {
           SystemAbort("Error Linestate Stable Not Set -- Linestate Testing", (g_CurrentTestProgress<<4) );
           }

         // turn on resume interrupt
	     UnSignedLong int_enb      = 0x20;
		 if( g_usb_initiator->WriteBuffer( int_enbl_addr, 
					    (AddressType)(&int_enb), 
					    sizeof(UnSignedLong), false ) == false ) 
		 {
	      SystemAbort("Error Write Buffer", (g_CurrentTestProgress<<4) | 3 );
		 }   

      }
      else 
	    {
	    SystemAbort("Error - Didn't Receive Line State Change Interrupt", (g_CurrentTestProgress<<4) | 1 );	
	    }	
      break;

    case 13:

       // this should be a resume interrupt -- we should be in k State

      // this should be a line state change interrupt -- we should be in J State
      if  (USB_IntStatus & 0x20) 

        {
         if( g_usb_initiator->ReadBuffer( (AddressType)(&USB_Ctrl),
		    		   usb_ctrl_addr, 
				   sizeof(UnSignedLong), false ) == false )
         {
          SystemAbort("Error ReadBuffer", (g_CurrentTestProgress<<4) );
         }
         if ((USB_Ctrl & 0x80) | (USB_Ctrl & 0x40))
           {
           SystemAbort("Error Not in K State -- Linestate Testing", (g_CurrentTestProgress<<4) );
           }
         if( g_usb_initiator->ReadBuffer( (AddressType)(&USB_OTG_Stat),
		  		   usb_ctrl_addr, 
				   sizeof(UnSignedLong), false ) == false )
         {
           SystemAbort("Error ReadBuffer", (g_CurrentTestProgress<<4) );
         }
         if (USB_OTG_Stat & 0x20)
           {
           SystemAbort("Error Linestate Stable Still Set -- Linestate Testing", (g_CurrentTestProgress<<4) );
           }

         // turn off resume interrupt
	     UnSignedLong int_dis      = 0x00;
	     if( g_usb_initiator->WriteBuffer( int_enbl_addr, 
					    (AddressType)(&int_dis), 
					    sizeof(UnSignedLong), false ) == false ) 
	    {
	      SystemAbort("Error Write Buffer", (g_CurrentTestProgress<<4) | 3 );
	    }
	    if( g_usb_initiator->WriteBuffer( otg_ctrl_addr, 
					    (AddressType)(&int_dis), 
					    sizeof(UnSignedLong), false ) == false ) 
	    {
	      SystemAbort("Error Write Buffer", (g_CurrentTestProgress<<4) | 3 );
	    }   
     }
      else 
	    {
	    SystemAbort("Error - Didn't receive Resume Interrupt", (g_CurrentTestProgress<<4) | 1 );	
	    }	
      break;

    case 14:
		 
		 // this should be an otg ID interrupt checking id pin
		if (USB_OTG_IStat & 0x80) 
		{
			UnSignedLong turn_all_off      = 0x00;
			if( g_usb_initiator->WriteBuffer( otg_ctrl_addr, 
							(AddressType)(&turn_all_off), 
							sizeof(UnSignedLong), false ) == false )
			{
			  SystemAbort("Error WriteBuffer", (g_CurrentTestProgress<<4) | 2 );
			}

		    // read otg status
			if( g_usb_initiator->ReadBuffer( (AddressType)(&USB_OTG_Stat),
						   otg_stat_addr, 
						   sizeof(UnSignedLong), false ) == false )
			{
				SystemAbort("Error ReadBuffer", (g_CurrentTestProgress<<4) );
			}

			if( !(USB_OTG_Stat & 0x80 ) )
			{
				SystemAbort("Error - !(USB_OTG_Stat & 0x80 ) == true, ( IDStatus not set ) not expected result for this interrupt.", (g_CurrentTestProgress<<4) | 1 );
			}
		}
		else 
		{
		  SystemAbort("Error - Not an OTG ID Interrupt as expected.", (g_CurrentTestProgress<<4) | 1 );
		}

     break;

	 case 15:
         //OTG disconnect
		 // this should be an otg ID interrupt
		if (USB_OTG_IStat & 0x80) 
		{
			UnSignedLong vbus_charge_on      = 0x02;
			if( g_usb_initiator->WriteBuffer( otg_ctrl_addr, 
							(AddressType)(&vbus_charge_on), 
							sizeof(UnSignedLong), false ) == false )
			{
			  SystemAbort("Error WriteBuffer", (g_CurrentTestProgress<<4) | 2 );
			}

            //turn on session valid interript enable
			UnSignedLong enable_sess_vld_int      = 0x08;
			if( g_usb_initiator->WriteBuffer( otg_ictrl_addr, 
							(AddressType)(&enable_sess_vld_int), 
							sizeof(UnSignedLong), false ) == false )
			{
			  SystemAbort("Error WriteBuffer", (g_CurrentTestProgress<<4) | 2 );
			}

			// read otg status
			if( g_usb_initiator->ReadBuffer( (AddressType)(&USB_OTG_Stat),
						   otg_stat_addr, 
						   sizeof(UnSignedLong), false ) == false )
			{
				SystemAbort("Error ReadBuffer", (g_CurrentTestProgress<<4) );
			}

			if( (USB_OTG_Stat & 0x80 ) )
			{
				SystemAbort("Error - (USB_OTG_Stat & 0x80 ) == true,  ( IDStatus not clear ) not expected result for this interrupt.", (g_CurrentTestProgress<<4) | 1 );
			}
		}
		else 
		{
		  SystemAbort("Error - Not an OTG ID Interrupt as expected.", (g_CurrentTestProgress<<4) | 1 );
		}


     break;

	 case 16:
		 // this should be an otg session valid interrupt
		if (USB_OTG_IStat & 0x08) 
		{
			UnSignedLong all_off      = 0x00;
			if( g_usb_initiator->WriteBuffer( otg_ctrl_addr, 
							(AddressType)(&all_off), 
							sizeof(UnSignedLong), false ) == false )
			{
			  SystemAbort("Error WriteBuffer", (g_CurrentTestProgress<<4) | 2 );
			}

			// read otg status
			if( g_usb_initiator->ReadBuffer( (AddressType)(&USB_OTG_Stat),
						   otg_stat_addr, 
						   sizeof(UnSignedLong), false ) == false )
			{
				SystemAbort("Error ReadBuffer", (g_CurrentTestProgress<<4) );
			}

			if( !(USB_OTG_Stat & 0x08 ) )
			{
				SystemAbort("Error - !(USB_OTG_Stat & 0x08 ) == true, ( session valid not set ) not expected result for this interrupt.", (g_CurrentTestProgress<<4) | 1 );
			}
		}
		else 
		{
		  SystemAbort("Error - Not an otg session valid interrupt, as expected.", (g_CurrentTestProgress<<4) | 1 );
		}

     break;

	 case 17:
         
		 // this should be an otg end of session valid interrupt
		if (USB_OTG_IStat & 0x08) 
		{
			UnSignedLong vbus_discharge_on      = 0x01;
			if( g_usb_initiator->WriteBuffer( otg_ctrl_addr, 
							(AddressType)(&vbus_discharge_on), 
							sizeof(UnSignedLong), false ) == false )
			{
			  SystemAbort("Error WriteBuffer", (g_CurrentTestProgress<<4) | 2 );
			}

            //turn on vbus valid interrupt enable
			UnSignedLong enable_vbus_vld_int      = 0x01;
			if( g_usb_initiator->WriteBuffer( otg_ictrl_addr, 
							(AddressType)(&enable_vbus_vld_int), 
							sizeof(UnSignedLong), false ) == false )
			{
			  SystemAbort("Error WriteBuffer", (g_CurrentTestProgress<<4) | 2 );
			}

            // read otg status
			if( g_usb_initiator->ReadBuffer( (AddressType)(&USB_OTG_Stat),
						   otg_stat_addr, 
						   sizeof(UnSignedLong), false ) == false )
			{
				SystemAbort("Error ReadBuffer", (g_CurrentTestProgress<<4) );
			}

			if( (USB_OTG_Stat & 0x08 ) )
			{
				SystemAbort("Error - (USB_OTG_Stat & 0x08 ) == true, ( session valid not cleared ) not expected result for this interrupt.", (g_CurrentTestProgress<<4) | 1 );
			}
		}
		else 
		{
		  SystemAbort("Error - Not an otg end of session valid interrupt, as expected.", (g_CurrentTestProgress<<4) | 1 );
		}

     break;

	 case 18:
         
		 // this should be an otg vbus valid interrupt
		if (USB_OTG_IStat & 0x01) 
		{
			UnSignedLong turn_all_off      = 0x00;
			if( g_usb_initiator->WriteBuffer( otg_ctrl_addr, 
							(AddressType)(&turn_all_off), 
							sizeof(UnSignedLong), false ) == false )
			{
			  SystemAbort("Error WriteBuffer", (g_CurrentTestProgress<<4) | 2 );
			}

			// read otg status
			if( g_usb_initiator->ReadBuffer( (AddressType)(&USB_OTG_Stat),
						   otg_stat_addr, 
						   sizeof(UnSignedLong), false ) == false )
			{
				SystemAbort("Error ReadBuffer", (g_CurrentTestProgress<<4) );
			}

			if( !(USB_OTG_Stat & 0x01 ) )
			{
				SystemAbort("Error - !(USB_OTG_Stat & 0x01 ) == true,  ( vbus valid not set ) not expected result for this interrupt.", (g_CurrentTestProgress<<4) | 1 );
			}
		}
		else 
		{
		  SystemAbort("Error - Not an otg vbus valid interrupt, as expected.", (g_CurrentTestProgress<<4) | 1 );
		}

     break;

	 case 19:
         
		 // this should be an otg end of vbus valid interrupt
		if (USB_OTG_IStat & 0x01) 
		{
			UnSignedLong turn_vbus_on_and_charge_on = 0x0A;
			if( g_usb_initiator->WriteBuffer( otg_ctrl_addr, 
							(AddressType)(&turn_vbus_on_and_charge_on), 
							sizeof(UnSignedLong), false ) == false )
			{
			  SystemAbort("Error WriteBuffer", (g_CurrentTestProgress<<4) | 2 );
			}

            //turn on session end interrupt enable
			UnSignedLong enable_sess_end_int      = 0x04;
			if( g_usb_initiator->WriteBuffer( otg_ictrl_addr, 
							(AddressType)(&enable_sess_end_int), 
							sizeof(UnSignedLong), false ) == false )
			{
			  SystemAbort("Error WriteBuffer", (g_CurrentTestProgress<<4) | 2 );
			}

			// read otg status
			if( g_usb_initiator->ReadBuffer( (AddressType)(&USB_OTG_Stat),
						   otg_stat_addr, 
						   sizeof(UnSignedLong), false ) == false )
			{
				SystemAbort("Error ReadBuffer", (g_CurrentTestProgress<<4) );
			}

			if( (USB_OTG_Stat & 0x01 ) )
			{
				SystemAbort("Error - (USB_OTG_Stat & 0x01 ) == true, ( vbus valid not cleared ) not expected result for this interrupt.", (g_CurrentTestProgress<<4) | 1 );
			}
		}
		else 
		{
		  SystemAbort("Error - Not an otg end of vbus valid interrupt, as expected.", (g_CurrentTestProgress<<4) | 1 );
		}

     break;

	 case 20:
         
		 // this should be an otg b session end interrupt
		if (USB_OTG_IStat & 0x04) 
		{
			UnSignedLong turn_all_off = 0x00;
			if( g_usb_initiator->WriteBuffer( otg_ctrl_addr, 
							(AddressType)(&turn_all_off), 
							sizeof(UnSignedLong), false ) == false )
			{
			  SystemAbort("Error WriteBuffer", (g_CurrentTestProgress<<4) | 2 );
			}

			// read otg status
			if( g_usb_initiator->ReadBuffer( (AddressType)(&USB_OTG_Stat),
						   otg_stat_addr, 
						   sizeof(UnSignedLong), false ) == false )
			{
				SystemAbort("Error ReadBuffer", (g_CurrentTestProgress<<4) );
			}

			if( !(USB_OTG_Stat & 0x04 ) )
			{
				SystemAbort("Error - !(USB_OTG_Stat & 0x04 ) == true, ( b session end not set ) not expected result for this interrupt.", (g_CurrentTestProgress<<4) | 1 );
			}
		}
		else 
		{
		  SystemAbort("Error - Not an otg b session end interrupt, as expected.", (g_CurrentTestProgress<<4) | 1 );
		}

     break;

	 case 21:
         
		 // this should be an otg end of b session valid interrupt
		if ( (USB_OTG_IStat & 0x04) ) 
		{

			// read otg status
			if( g_usb_initiator->ReadBuffer( (AddressType)(&USB_OTG_Stat),
					   otg_stat_addr, 
					   sizeof(UnSignedLong), false ) == false )
				{
				SystemAbort("Error ReadBuffer", (g_CurrentTestProgress<<4) );
				}

			if( (USB_OTG_Stat & 0x04 ) )
				{
				SystemAbort("Error - (USB_OTG_Stat & 0x04 ) == true, ( b session end not cleared ) not expected result for this interrupt.", (g_CurrentTestProgress<<4) | 1 );
				}

			// turn off OTG interrupt enables
			UnSignedLong otg_int_dis      = 0x00;
			if( g_usb_initiator->WriteBuffer( otg_ictrl_addr, 
					    (AddressType)(&otg_int_dis), 
					    sizeof(UnSignedLong), false ) == false ) 
				{
					SystemAbort("Error Write Buffer", (g_CurrentTestProgress<<4) | 3 );
				}
		}
		else 
		{
		  SystemAbort("Error - Not an otg end of b session valid interrupt, as expected.", (g_CurrentTestProgress<<4) | 1 );
		}

     break;

    case 22:

      // Should now be in host mode... this should be an SOF interrupt
      if (USB_IntMasked == 0x04) 
	{
	  IntStatus_write = 0x04;
	  UnSignedLong pid_out      = 0x10;
	  if( g_usb_initiator->WriteBuffer( hst_tokn_addr, 
					    (AddressType)(&pid_out), 
					    sizeof(UnSignedLong), false ) == false )
	    {
	      SystemAbort("Error WriteBuffer", (g_CurrentTestProgress<<4) | 2 );
	    }
	}
      else 
	{
	  SystemAbort("Error - No SOF interrupt in host mode", (g_CurrentTestProgress<<4) | 1 );
	}

      break;	
    
    case 23:

      // Token Done interrupt for TX packet
      if (USB_IntMasked == 0x8 && bDirection == 1) // token done, tx packet
	{
	  IntStatus_write = 0x08;

	  // Now send an IN token
	  UnSignedLong pid_in      = 0x90;
	  if( g_usb_initiator->WriteBuffer( hst_tokn_addr, 
					    (AddressType)(&pid_in), 
					    sizeof(UnSignedLong), false ) == false )
	    {
	      SystemAbort("Error WriteBuffer", (g_CurrentTestProgress<<4) | 2 );
	    }
	}
      else
	{
	  SystemAbort("Error - No token done interrupt for TX packet", (g_CurrentTestProgress<<4) | 1 );
	}
      break;
    
    case 24:

      // Token Done interrupt for RX packet
      if (USB_IntMasked == 0x8 && bDirection == 0) // token done, rx packet
	{
	  IntStatus_write = 0x08;
	  // Now check the returned packet
	  rcv_pkt_ptr = (UnSignedLong *) currBDT_ptr->buffer_addr;

	  for (int i=0; i<16; i++) 
	  {
	    if ( rcv_pkt_ptr[i] !=  host_pkt[i])
		{
			SystemAbort("Error Receive Packet Check", (g_CurrentTestProgress<<4 | 0x02));
		}
	  }

      // turns off host mode
	  UnSignedLong usb_ctrl = 0x00;
	  if( g_usb_initiator->WriteBuffer( usb_ctrl_addr, 
					    (AddressType)(&usb_ctrl), 
					    sizeof(UnSignedLong), false ) == false )
	    {
	      SystemAbort("Error WriteBuffer", (g_CurrentTestProgress<<4) | 0x03 );
	    }
	  UnSignedLong int_enb      = 0x00;
	  if( g_usb_initiator->WriteBuffer( int_enbl_addr, 
					    (AddressType)(&int_enb), 
					    sizeof(UnSignedLong), false ) == false ) 
	    {
	      SystemAbort("Error Write Buffer", (g_CurrentTestProgress<<4) | 3 );
	    }
	}
      else
	{
	  SystemAbort("Error - No token done interrupt for RX packet", (g_CurrentTestProgress<<4) | 1 );
		
	}
      break;


    default:
      break;
    }

	// clear the interrupt 
	if( g_usb_initiator->WriteBuffer( int_stat_addr, 
					(AddressType)(&IntStatus_write), 
					sizeof(UnSignedLong), false ) == false )
	{
		SystemAbort("Error WriteBuffer", (g_CurrentTestProgress<<4) | 15 );
	}


	// clear the interrupt 
	if( g_usb_initiator->WriteBuffer( otg_istat_addr, 
					(AddressType)(&USB_OTG_IStat), 
					sizeof(UnSignedLong), false ) == false )
	{
		SystemAbort("Error WriteBuffer", (g_CurrentTestProgress<<4) | 11 );
	}

	// read back the otg clear register
	UnSignedLong tmp;
	if( g_usb_initiator->ReadBuffer( (AddressType)(&tmp), 
									 otg_istat_addr, 
									 sizeof(UnSignedLong), false ) == false )
	{
		SystemAbort("Error ReadBuffer", (g_CurrentTestProgress<<4) | 11 );
	}



	if (g_CurrentTestProgress == 10)
	{
		g_DoneWithDeviceTests = true;
	}
	else
	if (g_CurrentTestProgress == 13)
	{
		g_DoneWithLineStateTests = true;
	}
	else
	if (g_CurrentTestProgress == 21)
	{
		//SystemAbort("OTG ID_PIN Tested!");
		g_DoneWithOTGTests = true;
	}
	else
	if (g_CurrentTestProgress >= 24)
	{
		//SystemAbort("Tests Completed!");
		g_DoneWithHostTests = true;
  
	}
    
}

// Get a pointer to the current BDT
void InitBDTptrs(UnSignedLong Status, 
		 BufferDescriptor **currBDT_ptr,
		 BufferDescriptor **sendBDT_ptr,
		 BufferDescriptor **nxtRcvBDT_ptr) 
{

  unsigned char *tmp = (unsigned char *)g_BufferDescriptorTablePtr; //Assign two times make compile warning go away
  BufferDescriptor *BDT_ptr = (BufferDescriptor *)tmp;

  *currBDT_ptr = &(BDT_ptr[Status>>2]);

  *sendBDT_ptr = &(BDT_ptr[(Status>>2)^0x2]);

  *nxtRcvBDT_ptr = &(BDT_ptr[(Status>>2)^0x3]);

}

///////////////////////////////////////////////////////////////////////////
// Main Hardware Create
///////////////////////////////////////////////////////////////////////////

bool AddAllIRQCallbacks()
{
  //  g_message_interface->PrintStringStdioLF( "Add the IRQ0-IRQ7 callback!");

  for( IRQNumberType i=IRQ_0; i<=IRQ_7; i = (IRQNumberType)(i + 1) )
  {
	if( g_interrupt_cntrl->AddIRQ(InterruptCallbackIRQ_5,i, 0 ) == false )
	{
	  SystemAbort("Error: Adding Interrupt for Controller Device", 0);
	}
  }

  return( true );
}


bool RemoveAllIRQCallbacks()
{
  //  g_message_interface->PrintStringStdioLF( "Add the IRQ0-IRQ7 callback!");

  for( IRQNumberType i=IRQ_0; i<=IRQ_7; i = (IRQNumberType)(i + 1) )
  {
      if( g_interrupt_cntrl->RemoveIRQ(InterruptCallbackIRQ_5,i, 0 ) == false )
	  {
		SystemAbort("Error: Adding Interrupt for Controller Device", 0);
	  }
  }

  return( true );
}

bool HardwareCreate()
{
  g_message_interface = new CMessageIO();
  g_host_dma = new CVirtualComponentInterface();
  g_usb_initiator = new CVirtualComponentInterface();
  g_interrupt_cntrl = new CInterruptControllerInterface();

  //--------------------------------------------------
  //Create a device for the tests
  //--------------------------------------------------
  //g_message_interface->PrintStringStdioLF( "Create a device for tests.");

  if( g_usb_initiator->Create("device_1_", BVCI_INITIATOR) == false )
    {
      SystemAbort("Error: Creating BVCI_INITIATOR Device", 0);
    }

  if( g_host_dma->Create("device_1_", BVCI_TARGET_CONNECTED_TO_HOST_MEMORY_DEVICE) == false )
    {
      SystemAbort("Error: Creating BVCI_TARGET_CONNECTED_TO_HOST_MEMORY_DEVICE Device", 0);
    }

  if( g_interrupt_cntrl->Create( "pirq_1_" ) == false )
    {
      SystemAbort("Error: Creating Interrupt Controller Device", 0);
    }

  SystemCreateThread( MainTestThread ); 

  return(true);
}

///////////////////////////////////////////////////////////////////////////
// Main Hardware Destroy
///////////////////////////////////////////////////////////////////////////
bool HardwareDestroy()
{
 delete g_host_dma;
 delete g_usb_initiator;
 delete g_message_interface;
 delete g_interrupt_cntrl;

  return(true);
}



///////////////////////////////////////////////////////////////////////////
// Main Software Test 
///////////////////////////////////////////////////////////////////////////
bool MainTestThread()
{	

	//Memory segments for access by the core.
	CMemorySegment host_packet_memory_segment;
	CMemorySegment host_rcv_memory_segment;
	CMemorySegment setup_address1_segment;
	CMemorySegment setup_address2_segment;

	//--------------------------------------------------
	// Test Progress Variable
	//--------------------------------------------------
	g_CurrentTestProgress = 0;
	if( SystemWait(50) == false )
	{
		return( false );
	}
	//--------------------------------------------------
	// Create a BDT in memory for the device to access
	//--------------------------------------------------
	CMemorySegment bd_table_memory_segment;

	if( bd_table_memory_segment.Create( sizeof(BufferDescriptorTableType), 512, 0 ) == false )
	{
		SystemAbort("Error: CMemorySegment::Create failed in MainTestThread!", 0);
		return( false );
	}

	//--------------------------------------------------
	//Grab the working address and assign it to the BD table pointer.
	//--------------------------------------------------
	g_BufferDescriptorTablePtr = (BufferDescriptorTableType *)bd_table_memory_segment.GetWorkingAddress();

	//--------------------------------------------------
	//Add the callback so that the interrupts will enter the function above.
	//--------------------------------------------------
	AddAllIRQCallbacks();

	//--------------------------------------------------
	//fills in the bdt memory
	//--------------------------------------------------
	if( SetMemoryForTable( g_BufferDescriptorTablePtr, setup_address1_segment, setup_address2_segment ) == false )
	{
		return( false );
	}

	//if( bd_table_memory_segment.PrintWorkingDataBytes( *g_message_interface ) == false )
	//{
	//	return( false );
	//}

	//--------------------------------------------------
	// call usb init to set up registers
	//--------------------------------------------------
	if( DevUsbInit( g_usb_initiator, g_message_interface, g_BufferDescriptorTablePtr ) == false )
	{	
		SystemAbort("Error: DEVICE Init USB!", 0);
	}

	//--------------------------------------------------
	// Wait for simple device tests to finish
	 //--------------------------------------------------
	while (g_DoneWithDeviceTests == false)
    {
		if( SystemWait(1) == false )
		{
			return( false );
		}
    }

	//--------------------------------------------------
	// now check if the hardware has a host
	//--------------------------------------------------
	UnSignedLong USB_AddInfo;

	if( g_usb_initiator->ReadBuffer( (AddressType)(&USB_AddInfo),
				   add_info_addr, 
				   sizeof(UnSignedLong), false ) == false )
	{
		SystemAbort("Error ReadBuffer", (g_CurrentTestProgress<<4) );
	}

	if (USB_AddInfo & 0x01) // host enabled
	{

	    //--------------------------------------------------
        // start otg tests
        // turn on host mode
	    //--------------------------------------------------
        UnSignedLong en_host_mode = 0x08;
        if( g_usb_initiator->WriteBuffer( usb_ctrl_addr, 
				  (AddressType)(&en_host_mode), 
				  sizeof(UnSignedLong), false ) == false )
        {
           SystemAbort("Error write buffer!", 0);
        }

       //
       UnSignedLong en_linestate_int = 0x20;
       if( g_usb_initiator->WriteBuffer( otg_ictrl_addr, 
				  (AddressType)(&en_linestate_int), 
				  sizeof(UnSignedLong), false ) == false )
       {
           SystemAbort("Error write buffer!", 0);
       }


	//--------------------------------------------------
        // Wait for linestate test to finish
        //--------------------------------------------------
	while (g_DoneWithLineStateTests == false)
    {
		if( SystemWait(1) == false )
		{
			return( false );
		}
    }

       //--------------------------------------------------
       // turn on the id_pin interrupt
       //--------------------------------------------------
       UnSignedLong en_id_int = 0x80;
       if( g_usb_initiator->WriteBuffer( otg_ictrl_addr, 
				  (AddressType)(&en_id_int), 
				  sizeof(UnSignedLong), false ) == false )
       {
			SystemAbort("Error write buffer!", 0);
       }

	   //--------------------------------------------------
       // turn on the vbus signal which loopbacks to id
	   //--------------------------------------------------
        UnSignedLong vbus_on = 0x08;
        if( g_usb_initiator->WriteBuffer( otg_ctrl_addr, 
				  (AddressType)(&vbus_on), 
				  sizeof(UnSignedLong), false ) == false )
       {
                SystemAbort("Error write buffer!", 0);
       }

		//--------------------------------------------------
        // now wait for interrupt
		//--------------------------------------------------
        while (g_DoneWithOTGTests == false)
		{
			  if( SystemWait (1) == false )
			  {
				  return( false );
			  }
		}

	    //--------------------------------------------------
        // turn off host mode to clear SOF counter
	    //--------------------------------------------------
        UnSignedLong dis_host_mode = 0x00;
        if( g_usb_initiator->WriteBuffer( usb_ctrl_addr, 
				  (AddressType)(&dis_host_mode), 
				  sizeof(UnSignedLong), false ) == false )
        {
           SystemAbort("Error write buffer!", 0);
        }

		//--------------------------------------------------
        // call host usb init to set up registers
		//--------------------------------------------------
		if( HostUsbInit( g_usb_initiator, g_message_interface, g_BufferDescriptorTablePtr, host_packet_memory_segment, host_rcv_memory_segment ) == false )
		{	
			SystemAbort("Error: HOST Init USB!", 0);
		}


        //--------------------------------------------------
		// Wait for Host test to finish
		//--------------------------------------------------
		while (g_DoneWithHostTests == false)
		{
			  if( SystemWait (1) == false )
			  {
				  return( false );
			  }
		}

        // now test the otg interrupts

 
	}


	RemoveAllIRQCallbacks();

   
	return true;
}