/***************************************************************************
usbHostSlaveTB.cpp - description
-------------------
begin : Mon Sep 25 2006
copyright : (C) 2006 by Steve Fielding
email : sfielding@base2designs.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "condCompileFlags.h"
#define SYSTEMC_TB
#ifdef SYSTEMC_TB
#include "systemc.h"
#include "transactor.h"
#else
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#ifdef UCLINUX_TB
#else
#include "altera_avalon_pio_regs.h"
#include "hw_address_defines.h"
#include "system.h"
#endif
#include "bus_if.h"
#endif
#include "usbHostSlaveTB.h"
#include "usbHostSlaveMemMap.h"
#include "usbSIEConstants.h"
#include "usbFifoConstants.h"
//#include "usbTransConstants.h"
//#define VERBOSE_PRINT
//#ifdef VERBOSE_PRINT
#define PRT(stuff...) cout << stuff
//#else
//#define PRT(stuff...) do{}while(0)
//#endif
/* ------------------------------- main ---------------------------------- */
void usbHostSlaveTB::main_host()
{
int i;
int tempDataFromHost;
int tempDataFromHost2;
int USBEndPoint;
int USBAddress;
int dataPacketSize;
int versionNum;
int fullSpeedRate;
int firstFrameNumMSB;
int firstFrameNumLSB;
int expectedFrameNum;
int bRequestType=0x80;
int bRequest=0x06;
int wValue=0x0002;//Device descriptor
int wIndex=0;
int wLength=0xff;
RequestInit();
cout << "-------- usbHostSlave TestBench ---------\n";
systemRstCtrl();
usbLineControl(SE0); //set default line state
waitUSBClockTicks(30); //allow time for reset to propagate, especially re-sync'd resets
hostBusRead(versionNum, RA_HOST_SLAVE_VERSION);
printf("Host Version number = %2d.%1d\n", (versionNum >> 4) & 0xf, versionNum & 0xf);
//
cout << "Host initializing...\n";
hostBusWrite(1, HOST_SLAVE_CONTROL_BASE); //set host mode
writeXCReg(HOST, 0xff, 9);
writeXCReg(HOST, 0x18, TX_LINE_CONTROL_REG); //full speed polarity and bit rate, direct control off, line state don't care
waitUSBClockTicks(20);
cout << "\nHost forcing reset...\n";
writeXCReg(HOST, 0x1c, TX_LINE_CONTROL_REG); //full speed polarity and bit rate, direct control on, line state SE0
waitUSBClockTicks(DISCONNECT_WAIT_TIME*4+100);
printf("SISR = 0x%0x, slave line state = 0x%0x \n", tempDataFromHost, tempDataFromHost2);
//host forces a reset
cout << "\nHost forcing reset...\n";
writeXCReg(HOST, 0x1c, TX_LINE_CONTROL_REG); //full speed polarity and bit rate, direct control on, line state SE0
waitUSBClockTicks(DISCONNECT_WAIT_TIME*4+100);
printf("SISR = 0x%0x, slave line state = 0x%0x \n", tempDataFromHost, tempDataFromHost2);
//re-connect at full speed
writeXCReg(HOST, 0x18, TX_LINE_CONTROL_REG); //full speed polarity and bit rate, direct control off, line state don't care
cout << "Reconnecting at full speed...\n";
// connection(ONE_ZERO, CONNECT_WAIT_TIME*4, 0, 1); //set default line state to ONE_ZERO, ie connect full speed
fullSpeedRate = 1;
//
writeXCReg(HOST,0xff,INTERRUPT_MASK_REG);
//Get first 8 bytes Device descriptor
GetDescriptorRequest(DeviceRequest.bRequestType,DeviceRequest.bRequest,DeviceRequest.wValue,DeviceRequest.wIndex,DeviceRequest.wLength);
waitUSBClockTicks(10000);
SendInToken();
waitUSBClockTicks(10000);
GetDescriptorRequest(DeviceRequest.bRequestType,DeviceRequest.bRequest,DeviceRequest.wValue,DeviceRequest.wIndex,DeviceRequest.wLength);
waitUSBClockTicks(10000);
SendInToken();
waitUSBClockTicks(10000);
SendInToken();
waitUSBClockTicks(10000);
SendInToken();
waitUSBClockTicks(10000);
SetAddr(2);
waitUSBClockTicks(10000);
cout<<"Host send configuration descriptor request...";
GetDescriptorRequest(ConfigurationRequest.bRequestType,ConfigurationRequest.bRequest,ConfigurationRequest.wValue,ConfigurationRequest.wIndex,ConfigurationRequest.wLength);
waitUSBClockTicks(10000);
SendInToken();
waitUSBClockTicks(10000);
SendInToken();
waitUSBClockTicks(10000);
cout<<"/nHost send all configuration descriptor request...";
GetDescriptorRequest(ConfigurationRequest.bRequestType,ConfigurationRequest.bRequest,ConfigurationRequest.wValue,ConfigurationRequest.wIndex,32);
waitUSBClockTicks(10000);
SendInToken();
waitUSBClockTicks(10000);
SendInToken();
waitUSBClockTicks(10000);
SendInToken();
waitUSBClockTicks(10000);
SendInToken();
waitUSBClockTicks(10000);
//Get string Type descriptor
cout<<"/nHost send string type descriptor request...";
GetDescriptorRequest(StringTypeRequest.bRequestType,StringTypeRequest.bRequest,StringTypeRequest.wValue,StringTypeRequest.wIndex,StringTypeRequest.wLength);
waitUSBClockTicks(10000);
SetConfig();
waitUSBClockTicks(10000);
sc_stop();
//
//setaddr
//host forces a reset
/*
cout << "\nHost forcing reset...\n";
writeXCReg(HOST, 0x1c, TX_LINE_CONTROL_REG); //full speed polarity and bit rate, direct control on, line state SE0
waitUSBClockTicks(2*DISCONNECT_WAIT_TIME*4+100);
printf("SISR = 0x%0x, slave line state = 0x%0x \n", tempDataFromHost, tempDataFromHost2);
waitUSBClockTicks(10000);
//Get all 18 bytes Device descriptor
GetDescriptorRequest(DeviceRequest.bRequestType,DeviceRequest.bRequest,DeviceRequest.wValue,DeviceRequest.wIndex,DeviceRequest.wLength);
cout<<"/nHost send device descriptor request...";
//Get Configuration descriptor
//Get all Configuration descriptor
GetDescriptorRequest(ConfigurationRequest.bRequestType,ConfigurationRequest.bRequest,ConfigurationRequest.wValue,ConfigurationRequest.wIndex,32);
cout<<"/nHost send all configuration descriptor request...";
//Get string Type descriptor
GetDescriptorRequest(StringTypeRequest.bRequestType,StringTypeRequest.bRequest,StringTypeRequest.wValue,StringTypeRequest.wIndex,StringTypeRequest.wLength);
cout<<"/nHost send string type descriptor request...";
*/
host_state=ST_SETUP;
waitUSBClockTicks(RESUME_WAIT_TIME*32+100);
}
void usbHostSlaveTB::host_trans_done_int()
{
int temp;
int rx_cnt;
while(1)
{
cout << "host trans done int\n";
cancelInterrupt(HOST, SC_TRANS_DONE_BIT);
//to check what kind of interrupt is available.
//check if we have received data.
hostBusRead(temp, HOST_RX_FIFO_BASE + FIFO_DATA_COUNT_MSB);
hostBusRead(rx_cnt, HOST_RX_FIFO_BASE + FIFO_DATA_COUNT_LSB);
rx_cnt+=temp*256;
printf("Host received %d data,they are:\n", rx_cnt);
for(int i=0;i<rx_cnt;i++)
{
hostBusRead(temp, HOST_RX_FIFO_BASE + FIFO_DATA_REG);
printf("%x,",temp);
}
/* cout << "Send in token\n";
if( host_state==ST_SETUP)
{
SendInToken() ;
host_state=ST_WAIT_DESC;
}
*/
wait();
}
}
/* ------------------------------- connection ---------------------------------- */
void usbHostSlaveTB::connection(int lineState, int waitTime, int hostInterruptExpected, int slaveInterruptExpected)
{
int tempDataFromHost;
int tempDataFromHost2;
int expectedData;
int expectedConnectState;
if (lineState == ZERO_ONE) {
expectedConnectState = LOW_SPEED_CONNECT;
writeXCReg(HOST, 0x00, TX_LINE_CONTROL_REG); //low speed polarity and bit rate, direct control off, line state