usb_init.cpp 15.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
#include "DataTypes.h" 
#include "MessageIO.h"
#include "VirtualComponentInterface.h"
#include "SystemAccess.h"
#include "USB1_Common.h"


//////////////////////////////////////////////////////////
// Non-API includes
//////////////////////////////////////////////////////////
#include <memory.h>
#include <stdio.h>
//////////////////////////////////////////////////////////
// Structs
//////////////////////////////////////////////////////////
struct RegEntry
{
	char			*m_reg_name;
	AddressType		m_reg_address;
	UnSignedLong    m_reg_data;
};

//////////////////////////////////////////////////////////
// Global information
// these are the registers that are written to in the setup 
// routine.
//////////////////////////////////////////////////////////

const int g_total_reset_entries = 31;
static RegEntry g_RegResetEntryArray[g_total_reset_entries] =
{
  {"ID_FIELD",   0x00, 0x04},
  {"ID_COMP",    0x04, 0xFB},
  {"OTG_ICTRL",  0x14, 0x00},
  {"OTG_CTRL",   0x1C, 0x00},
  {"INT_ENABLE", 0x84, 0x00},
  {"ERR_STATUS", 0x88, 0x00},
  {"ERR_ENABLE", 0x8C, 0x00},
  {"ADDRESSREG", 0x98, 0x00},
  {"BDT_PAGE1",  0x9C, 0x00},
  {"FRAMENUMLOW",0xA0, 0x00},
  {"FRAMENUMHI", 0xA4, 0x00},
  {"TOKEN", 	 0xA8, 0x00},
  {"SOF_THDL",   0xAC, 0x00},
  {"BDT_PAGE2",  0xB0, 0x00},
  {"BDT_PAGE3",  0xB4, 0x00},
  {"ENDPCTL0",   0xC0, 0x00},
  {"ENDPCTL1",   0xC4, 0x00},
  {"ENDPCTL2",   0xC8, 0x00},
  {"ENDPCTL3",   0xCC, 0x00},
  {"ENDPCTL4",   0xD0, 0x00},
  {"ENDPCTL5",   0xD4, 0x00},
  {"ENDPCTL6",   0xD8, 0x00},
  {"ENDPCTL7",   0xDC, 0x00},
  {"ENDPCTL8",   0xE0, 0x00},
  {"ENDPCTL9",   0xE4, 0x00},
  {"ENDPCTL10",  0xE8, 0x00},
  {"ENDPCTL11",  0xEC, 0x00},
  {"ENDPCTL12",  0xF0, 0x00},
  {"ENDPCTL13",  0xF4, 0x00},
  {"ENDPCTL14",  0xF8, 0x00},
  {"ENDPCTL15",  0xFC, 0x00}
};

const int g_total_rdwr_entries = 9;
static RegEntry g_RegRdWrEntryArray[g_total_rdwr_entries] =
{
  {"ERR_ENABLE", 0x8C, 0xff},
  {"ADDRESSREG", 0x98, 0xef},
  {"BDT_PAGE1",  0x9C, 0xff},
  {"BDT_PAGE2",  0xB0, 0xff},
  {"BDT_PAGE3",  0xB4, 0xff},
  {"ENDPCTL0",   0xC0, 0x1f},
  {"ENDPCTL1",   0xC4, 0x1f},
  {"ENDPCTL2",   0xC8, 0x1f},
  {"ENDPCTL3",   0xCC, 0x1f}
};

const int g_total_data_checks = 4;
static UnSignedLong g_RdWrDataArray[g_total_data_checks] =
{
  0xff,
  0xaa,
  0x55,
  0x00
};

const int g_total_setup_regs = 8;
static RegEntry g_SetupRegEntryArray[g_total_setup_regs] = 
{	
    {"OTG_ICTRL",  0x14, 0x00 },
    {"OTG_CTRL",   0x1c, 0x00 },
	{"CONTROL"	 , 0x94, 0x00 },
	{"INT_ENABLE", 0x84, 0x00 },
	{"ERR_ENABLE", 0x8C, 0x00 },
	{"ADDRESSREG", 0x98, 0x00 },
	{"FRAMENUMLOW",0xA0, 0x00 },
	{"FRAMENUMHI", 0xA4, 0x00 }
};

const int g_total_enable_regs = 5;

static RegEntry g_EnableRegEntryArray[g_total_enable_regs] = 
{	
	{"INT_STATUS", 0x80, 0xFF },
	{"INT_ENABLE", 0x84, 0x01 },
	{"CONTROL"	 , 0x94, 0x02 },
	{"CONTROL"	 , 0x94, 0x01 },
    {"OTG_CTRL",   0x1c, 0x80 }

};

///////////////////////////////////////////////////////////////////////////
// Function Defines
//
// SetMemoryForTable     -- Allocates meory and initializes the BDT
// SetupTableAddressRegs -- Write the address of the BDT into USB regs.
///////////////////////////////////////////////////////////////////////////
void register_test(CVirtualComponentInterface *usb_initiator);

bool SetupTableAddressRegs(CVirtualComponentInterface *usb_initiator, 
						   CMessageIO *message_interface, 
						   BufferDescriptorTableType *bd_table_ptr);


/////////////////////////////////////////////////////////////////////////////
// DevUsbInit procedure
// allocates and initializes memory for buffer descriptors
// sets up USB 1.1 registers for a device then reads them back
// allocates and initializes the USB receive and transmit buffers
//////////////////////////////////////////////////////////////////////////////

bool DevUsbInit( CVirtualComponentInterface *usb_initiator, 
		 CMessageIO *message_interface, 
		 BufferDescriptorTableType *bd_table_ptr)
{
  

  register_test(usb_initiator);
	
  // writes the page regs.
  if( SetupTableAddressRegs( usb_initiator, message_interface, bd_table_ptr ) == false ) 
    {
      return(false);
    }

  // initializes the setup registers
  int i=0;
  for( i=0; i<g_total_setup_regs; i++ )
    {
      if( usb_initiator->WriteBuffer( (AddressType)g_SetupRegEntryArray[i].m_reg_address, (AddressType)(&(g_SetupRegEntryArray[i].m_reg_data)), sizeof(UnSignedLong), false) == false )
	{
	  SystemAbort("Error During WriteBuffer!", 7);
	}
    }


  // Disable Endpoint control registers
  //  message_interface->PrintStringStdioLF( "Disable  Endpoint control");
  for( i=0; i<g_MAX_ENDPOINTS; i++ )
    {
      UnSignedLong value = 0x00;
      if( usb_initiator->WriteBuffer( end_ctl0_addr + (i*4), (AddressType)(&(value)), sizeof(UnSignedLong), false) == false )
	{
	  SystemAbort("Error During WriteBuffer!", 7);
	}
    }

  // Enable Endpoint control registers
  for( i=0; i<g_MAX_ENDPOINTS; i++ )
    {
      UnSignedLong value = 0x0D;
      if( usb_initiator->WriteBuffer( end_ctl0_addr + (i*4), (AddressType)(&(value)), sizeof(UnSignedLong), false) == false )
	{
	  SystemAbort("Error During WriteBuffer!", 7);
	}
    }

  // now clear interrupt, enable interface and enable interrupts 
  for( i=0; i<g_total_enable_regs; i++ )
    {
      if( usb_initiator->WriteBuffer( (AddressType)g_EnableRegEntryArray[i].m_reg_address, (AddressType)(&(g_EnableRegEntryArray[i].m_reg_data)), sizeof(UnSignedLong), false) == false )
	{
	  SystemAbort("Error During WriteBuffer!", 7);
	}
    }
	
  return(true);
}

/////////////////////////////////////////////////////////////////////////////
// HostUsbInit procedure
// sets up USB 1.1 registers for a host
// initializes the USB receive transmit buffer
//////////////////////////////////////////////////////////////////////////////
//AddressType     host_pkt_addr;
//AddressType     host_rcv_addr;

bool HostUsbInit( CVirtualComponentInterface *usb_initiator, 
				  CMessageIO *message_interface, 
				  BufferDescriptorTableType *bd_table_ptr,
				  CMemorySegment& host_packet_memory_segment,
				  CMemorySegment& host_rcv_memory_segment )
{
  const int end_point_buffer_size = 64;

  //Allocate the memory for host packets.
  if( host_packet_memory_segment.Create( end_point_buffer_size, MAX_CACHE_SIZE_IN_BYTES, 0 ) == false )
  {
	  SystemAbort("Error: CMemorySegment::Create failed host_packet_memory_segment in HostUsbInit!", 0);
	  return( false );
  }

  // Setup the host packet to go out, using the allocated data segment in memory
  volatile UnSignedLong* host_pkt_ptr = (volatile UnSignedLong*) host_packet_memory_segment.GetWorkingAddress();


  //Copy all the data from the static host packet to the new allocated packet.
  for (int i=0; i<16; i++)
  {
    host_pkt_ptr[i] = host_pkt[i];
  }


  // Setup the BDT for the host packet and assign the pointer to the new packet working address.
  (*bd_table_ptr)[0][1][0].rsrvd1_0 = 0;
  (*bd_table_ptr)[0][1][0].buffer_addr = (unsigned int)host_pkt_ptr;
  (*bd_table_ptr)[0][1][0].rsrvd15_8 = 0;
  (*bd_table_ptr)[0][1][0].rsrvd31_26 = 0;
  (*bd_table_ptr)[0][1][0].bytecnt = 64;
  (*bd_table_ptr)[0][1][0].data0_1 = 0;
  (*bd_table_ptr)[0][1][0].pid = 0;
  (*bd_table_ptr)[0][1][0].own = 1;



  //Allocate the memory for host packets.
  if( host_rcv_memory_segment.Create( end_point_buffer_size, MAX_CACHE_SIZE_IN_BYTES, 0 ) == false )
  {
	  SystemAbort("Error: CMemorySegment::Create host_rcv_memory_segment failed in HostUsbInit!", 0);
	  return( false );
  }

  // Setup the BDT for the host receive packet and assign the pointer to the new packet working address.
  (*bd_table_ptr)[0][0][0].rsrvd1_0 = 0;
  (*bd_table_ptr)[0][0][0].buffer_addr = (volatile unsigned int) host_rcv_memory_segment.GetWorkingAddress();
  (*bd_table_ptr)[0][0][0].rsrvd15_8 = 0;
  (*bd_table_ptr)[0][0][0].rsrvd31_26 = 0;
  (*bd_table_ptr)[0][0][0].bytecnt = 64;
  (*bd_table_ptr)[0][0][0].data0_1 = 0;
  (*bd_table_ptr)[0][0][0].pid = 0;
  (*bd_table_ptr)[0][0][0].own = 1;

  // Clear the Error register
  UnSignedLong usb_clear_error = 0xff;
  if( usb_initiator->WriteBuffer( err_stat_addr, 
				  (AddressType)(&usb_clear_error), 
				  sizeof(UnSignedLong), false ) == false )
    {
      SystemAbort("Error Write buffer!", 0);
    }

  // clear any interrupts that have occured
  UnSignedLong usb_int_clear = 0x0E;
  if( usb_initiator->WriteBuffer( int_stat_addr, 
				  (AddressType)(&usb_int_clear), 
				  sizeof(UnSignedLong), false ) == false )
    {
      SystemAbort("Error Write buffer!", 0);
    }


  // turn off reset interrupts
  UnSignedLong usb_rst_int_dis = 0x0E;
  if( usb_initiator->WriteBuffer( int_enbl_addr, 
				  (AddressType)(&usb_rst_int_dis), 
				  sizeof(UnSignedLong), false ) == false )
    {
      SystemAbort("Error Write buffer!", 0);
    }

  // turn off the pull up and turn on the pull downs
  UnSignedLong otg_ctrl_host = 0x30;
  if( usb_initiator->WriteBuffer( otg_ctrl_addr, 
				  (AddressType)(&otg_ctrl_host), 
				  sizeof(UnSignedLong), false ) == false )
    {
      SystemAbort("Error Write buffer!", 0);
    }

  // enable host mode and do a reset
  UnSignedLong host_mode_en_rst = 0x1B;
  if( usb_initiator->WriteBuffer( usb_ctrl_addr, 
				  (AddressType)(&host_mode_en_rst), 
				  sizeof(UnSignedLong), false ) == false )
    {
      SystemAbort("Error Write buffer!", 0);
    }

  //Burn time, this should burn at least 4us at 48mhz. ( a reset needs 2us )
  if( SystemWait( 100) == false )
  {
		SystemAbort("SystemWait failed during wait for reset to complete.", 0);
  }
  
  // turn off reset
  UnSignedLong host_mode_en = 0x09;
  if( usb_initiator->WriteBuffer( usb_ctrl_addr, 
				  (AddressType)(&host_mode_en), 
				  sizeof(UnSignedLong), false ) == false )
    {
      SystemAbort("Error write buffer!", 0);
    }
    
  return(true);
}

//////////////////////////////////////////////////////////////////////
//
// SetMemoryForTable
//
// allocates memory for the Buffer Descriptor Table and initializes
// the table
// 
//////////////////////////////////////////////////////////////////////

bool SetMemoryForTable( BufferDescriptorTableType *bd_table_ptr , 
						CMemorySegment& setup_address1_segment,
						CMemorySegment& setup_address2_segment)
{
  const int end_point_buffer_size = 16;

  if( setup_address1_segment.Create( end_point_buffer_size, MAX_CACHE_SIZE_IN_BYTES, 0 ) == false )
  {
	  SystemAbort("Error: CMemorySegment::Create failed setup_address1_segment in SetMemoryForTable!", 0);
	  return( false );
  }

  if( setup_address2_segment.Create( end_point_buffer_size, MAX_CACHE_SIZE_IN_BYTES, 0 ) == false )
  {
	  SystemAbort("Error: CMemorySegment::Create failed setup_address2_segment in SetMemoryForTable!", 0);
	  return( false );
  }
 
  // BDT for the first recieved packet
  (*bd_table_ptr)[0][0][0].rsrvd1_0 = 0;
  (*bd_table_ptr)[0][0][0].buffer_addr = setup_address1_segment.GetWorkingAddress();
  (*bd_table_ptr)[0][0][0].rsrvd15_8 = 0;
  (*bd_table_ptr)[0][0][0].rsrvd31_26 = 0;
  (*bd_table_ptr)[0][0][0].bytecnt = 8;
  (*bd_table_ptr)[0][0][0].data0_1 = 0;
  (*bd_table_ptr)[0][0][0].pid = 0;
  (*bd_table_ptr)[0][0][0].own = 1;

  // BDT for the second recieved packet
  (*bd_table_ptr)[0][0][1].rsrvd1_0 = 0;
  (*bd_table_ptr)[0][0][1].buffer_addr = setup_address2_segment.GetWorkingAddress();
  (*bd_table_ptr)[0][0][1].rsrvd15_8 = 0;
  (*bd_table_ptr)[0][0][1].rsrvd31_26 = 0;
  (*bd_table_ptr)[0][0][1].bytecnt = 8;
  (*bd_table_ptr)[0][0][1].data0_1 = 1;
  (*bd_table_ptr)[0][0][1].pid = 0;
  (*bd_table_ptr)[0][0][1].own = 0;

  // BDT for the first transmit packet, point the buffer to
  // the first received buffer because we will echo that packet
  (*bd_table_ptr)[0][1][0].rsrvd1_0 = 0;
  (*bd_table_ptr)[0][1][0].buffer_addr = setup_address1_segment.GetWorkingAddress();
  (*bd_table_ptr)[0][1][0].rsrvd15_8 = 0;
  (*bd_table_ptr)[0][1][0].rsrvd31_26 = 0;
  (*bd_table_ptr)[0][1][0].bytecnt = 8;
  (*bd_table_ptr)[0][1][0].data0_1 = 0;
  (*bd_table_ptr)[0][1][0].pid = 0;
  (*bd_table_ptr)[0][1][0].own = 0;

  // BDT for the second transmit packet, point the buffer to
  // the second received buffer because we will echo that packet
  (*bd_table_ptr)[0][1][1].rsrvd1_0 = 0;
  (*bd_table_ptr)[0][1][1].buffer_addr = setup_address2_segment.GetWorkingAddress();
  (*bd_table_ptr)[0][1][1].rsrvd15_8 = 0;
  (*bd_table_ptr)[0][1][1].rsrvd31_26 = 0;
  (*bd_table_ptr)[0][1][1].bytecnt = 8;
  (*bd_table_ptr)[0][1][1].data0_1 = 1;
  (*bd_table_ptr)[0][1][1].pid = 0;
  (*bd_table_ptr)[0][1][1].own = 0;

  return( true );

}

////////////////////////////////////////////////////////////////////////
//
// SetupTableAddressRegs
//
// This procedure takes the address of the buffer descriptor table and
// splits it into the three bytes needed by the USB 1.1 page registers and
// then writes them into the USB 1.1 page registers
//
////////////////////////////////////////////////////////////////////////

bool SetupTableAddressRegs(CVirtualComponentInterface *usb_initiator, 
			   CMessageIO *message_interface, 
			   BufferDescriptorTableType *bd_table_ptr)
{
  AddressType bd_table_reg_address_0 = bdt_pag0_addr;
  AddressType bd_table_reg_address_1 = bdt_pag1_addr;
  AddressType bd_table_reg_address_2 = bdt_pag2_addr;

  UnSignedLong bd_table_reg_data_0 = (((AddressType)bd_table_ptr) & 0x0000FF00) >> 8;
  UnSignedLong bd_table_reg_data_1 = (((AddressType)bd_table_ptr) & 0x00FF0000) >> 16;
  UnSignedLong bd_table_reg_data_2 = (((AddressType)bd_table_ptr) & 0xFF000000) >> 24;
	

  if( usb_initiator->WriteBuffer( bd_table_reg_address_0, 
				  (AddressType)(&bd_table_reg_data_0), 
				  sizeof(AddressType), false) == false )
    {
      SystemAbort("Error During BDT Page Register 0 Write!", 6);
    }

  if( usb_initiator->WriteBuffer( bd_table_reg_address_1, (AddressType)(&bd_table_reg_data_1), sizeof(AddressType), false) == false )
    {
      SystemAbort("Error During BDT Page Register 1 Write!", 6);
    }

  if( usb_initiator->WriteBuffer( bd_table_reg_address_2, (AddressType)(&bd_table_reg_data_2), sizeof(AddressType), false) == false )
    {
      SystemAbort("Error During BDT Page Register 2 Write!", 6);
    }

  return(true);
}

void register_test(CVirtualComponentInterface *usb_initiator)
{

  UnSignedLong rd_data, wr_data;

  // Check the reset values of the registers
  int i=0;
  for ( i=0; i<g_total_reset_entries; i++)
    {
      if( usb_initiator->ReadBuffer( (AddressType)&rd_data, (AddressType)(g_RegResetEntryArray[i].m_reg_address), sizeof(UnSignedLong), false) == false )
	{
	  SystemAbort("Error ReadBuffer", 1 );
	}
      if ( g_RegResetEntryArray[i].m_reg_data != rd_data )
	{
	  SystemAbort("Error register reset test", 1 );
	}
    }

  // Now check the registers which are read/writeable
  int j=0;
  for ( i=0; i<g_total_data_checks; i++)
    {
      for ( j=0; j<g_total_rdwr_entries; j++)
	{
	  wr_data = g_RdWrDataArray[i] & g_RegRdWrEntryArray[j].m_reg_data;;
	  if( usb_initiator->WriteBuffer( g_RegRdWrEntryArray[j].m_reg_address, (AddressType)&wr_data, sizeof(AddressType), false) == false )
	      {
		SystemAbort("Error WriteBuffer", i+2 );
	      }
	}
      for ( j=0; j<g_total_rdwr_entries; j++)
	{
	  wr_data = g_RdWrDataArray[i] & g_RegRdWrEntryArray[j].m_reg_data;;
	  if( usb_initiator->ReadBuffer( (AddressType)&rd_data, (AddressType)(g_RegRdWrEntryArray[j].m_reg_address), sizeof(UnSignedLong), false) == false )
	    {
	      SystemAbort("Error ReadBuffer", i+2 );
	    }
	  if ( wr_data != rd_data ) 
	    {
	      SystemAbort("Error Register read write test", i+2 );
	    }
	}
    }
}