⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dvb_usb.c

📁 DVB软件,基于CT216软件的开发源程序.
💻 C
📖 第 1 页 / 共 4 页
字号:
												10))						// time interval
		{
			CT_OS_DeleteMsgQueue(&DVB_USB_Request_MsgQueue);
			CT_OS_DeleteMsgQueue(&DVB_USB_Reply_MsgQueue);
			CT_OS_DeleteSemaphore(&DVB_USB_API_Semaphore);
			CT_OS_DeleteTask(&DVB_USB_Polling_Task);

			USB_DEBUG(("    [ERROR]: %s - CreateTimer error\n", __FUNCTION__));
			return FALSE;
		}

		//_b8UsbStackInitial = TRUE;
	}

	// clear ST_USB_LUN_INFO _stUSBLUNInfo
	_stUSBLUNInfo.u32LUNs   	= 0;			// no media
	_stUSBLUNInfo.u8MaxLUN  	= 0;
	_stUSBLUNInfo.b8Connect 	= 0;			// no device connect
	_stUSBLUNInfo.s8CurrentLUN 	= USB_NO_MEDIA;	// effective media index 0~127

	// clear _u32USBErrCntInfo
	_u32USBErrCntInfo = 0;
	_u32USBMonitorCntInfo = 0;

	return TRUE;
}

//--------------------------------------------------------------------

bool8 DVB_USB_Change_LUN(u8 u8LUN)
{
	MSG_PARAMETER	stSendMsg, stRecvMsg;
	EN_CTOS_STATUS  enStatus;

	u8              u8CmdID = 0;
	u32             u32MsgData = 0;
	u32				u32RecvMsgLen = 0;

	u8 u8UserLUN = 0;
	bool8 b8State = FALSE;

#ifdef ENABLE_WASTE_TIME
	u32 u32T1=0, u32T2=0;
#endif

	if(FALSE==_b8UsbStackInitial || TRUE==_b8UsbStandby)
	{
		USB_DEBUG(("    [ERROR]: %s - DVB_USB not initial or standby\n", __FUNCTION__));
		return FALSE;
	}

	if(USB_LUN_MAX < u8LUN)	// our structure support only 32 LUNs(1~32 decimal)
	{
		USB_DEBUG(("    [ERROR]: %s - the LUN not support\n", __FUNCTION__));
		return FALSE;
	}

	USB_DEBUG(("    [STATUS]: %s - the LUN %d(1~32)\n", __FUNCTION__, u8LUN));
	u8UserLUN = u8LUN;	// 32 LUNs(0~31 decimal)

	if(0 == ((_stUSBLUNInfo.u32LUNs>>u8UserLUN)&0x01))	// check LUN plug-in & ready
	{
		USB_DEBUG(("    [ERROR]: %s - the LUN_%d not ready %08x %08x\n", __FUNCTION__, u8UserLUN, _stUSBLUNInfo.u32LUNs, (_stUSBLUNInfo.u32LUNs>>u8UserLUN)));
		return FALSE;
	}

	if(_s8UsbSetCurrentLUN == (s8)u8UserLUN)
	{
		return TRUE;
	}

	if(EN_CTOS_SUCCESS != CT_OS_WaitOnSemaphore(&DVB_USB_API_Semaphore, CTOS_WAIT))
	{
		USB_DEBUG(("    [ERROR]: %s - Wait semaphore\n", __FUNCTION__));
		return FALSE;
	}

	_s8UsbSetCurrentLUN = (s8)u8UserLUN;

	// send DVB_USB_CHANGE_LUN to dvb_usb_polling_task
	stSendMsg.u8Cmd               = DVB_USB_CHANGE_LUN;
	stSendMsg.unData.u32MsgData   = 0;
	CT_OS_PutMsg(&DVB_USB_Request_MsgQueue, &stSendMsg, CTOS_NO_WAIT);

	// wait and get DVB_USB_READ_DONE/DVB_USB_READ_ERROR from dvb_usb_polling_task
#ifdef ENABLE_WASTE_TIME
	CT_OS_MS_GetClock(&u32T1);
#endif
	enStatus = CT_OS_GetMsg(&DVB_USB_Reply_MsgQueue, &stRecvMsg, &u32RecvMsgLen, CTOS_WAIT);
#ifdef ENABLE_WASTE_TIME
	CT_OS_MS_GetClock(&u32T2);
#endif
	if(enStatus != EN_CTOS_SUCCESS)
	{
		CT_OS_FreeSemaphore(&DVB_USB_API_Semaphore);
		return FALSE;
	}

	u8CmdID		= stRecvMsg.u8Cmd;
	u32MsgData	= stRecvMsg.unData.u32MsgData;

	switch(u8CmdID)
	{
		case DVB_USB_CHANGE_LUN_DONE:
		{
			USB_DEBUG(("    [STATUS]: DVB_USB_Change_LUN: OK\n"));
			b8State = TRUE;
		}
		break;

		default:
		case DVB_USB_CHANGE_LUN_ERROR:
		{
			USB_DEBUG(("    [ERROR]: %s - can't change LUN\n", __FUNCTION__));
			b8State = FALSE;
		}
		break;
	}

	CT_OS_FreeSemaphore(&DVB_USB_API_Semaphore);

	return TRUE;
}

//--------------------------------------------------------------------

void DVB_USB_Get_LUN(u8 u8Port, ST_USB_LUN_INFO *pstUSBLUNInfo)
{
	// the input paramenter: u8Port not used.
	// it's used for ct216u

	if(pstUSBLUNInfo == NULL)
		return;

	if(EN_CTOS_SUCCESS != CT_OS_WaitOnSemaphore(&DVB_USB_API_Semaphore, CTOS_WAIT))
	{
		USB_DEBUG(("    [ERROR]: %s - Wait semaphore\n", __FUNCTION__));
		return;
	}

	memcpy(pstUSBLUNInfo, &_stUSBLUNInfo, sizeof(ST_USB_LUN_INFO));

	CT_OS_FreeSemaphore(&DVB_USB_API_Semaphore);
}

//--------------------------------------------------------------------

bool8 DVB_USB_Ready(u8 u8LUN)
{
	if(EN_CTOS_SUCCESS != CT_OS_WaitOnSemaphore(&DVB_USB_API_Semaphore, CTOS_WAIT))
	{
		USB_DEBUG(("    [ERROR]: %s - Wait semaphore\n", __FUNCTION__));
		return FALSE;
	}

	if( TRUE  == _b8UsbStackInitial      &&  	// 1. usb initial ok
	    FALSE == _b8UsbStandby           && 	// 2. usb is not standby
	    TRUE  == CT_USB_CheckMedia(u8LUN) )		// 3. usb has meida plug-in
	{
		_b8UsbReady = TRUE;
	}
	else
	{
		_b8UsbReady = FALSE;
	}

	CT_OS_FreeSemaphore(&DVB_USB_API_Semaphore);

	return _b8UsbReady;
}

//--------------------------------------------------------------------

bool8 DVB_USB_Reset(void)
{
	MSG_PARAMETER	stSendMsg;

	if(FALSE == _b8UsbStandby)	// have not done that standby usb before
	{
		return TRUE;
	}

	if(EN_CTOS_SUCCESS != CT_OS_WaitOnSemaphore(&DVB_USB_API_Semaphore, CTOS_WAIT))
	{
		USB_DEBUG(("    [ERROR]: %s - semaphore error\n", __FUNCTION__));
		return FALSE;
	}

	// send DVB_USB_RESET to dvb_usb_polling_task
	stSendMsg.u8Cmd               = DVB_USB_RESET;
	stSendMsg.unData.u32MsgData   = 0;
	CT_OS_PutMsg(&DVB_USB_Request_MsgQueue, &stSendMsg, CTOS_NO_WAIT);

	_b8UsbStandby = FALSE;

	CT_OS_FreeSemaphore(&DVB_USB_API_Semaphore);

	return TRUE;
}

//--------------------------------------------------------------------

bool8 DVB_USB_Standby(void)
{
	u32  u32Index;

	if(FALSE == _b8UsbStackInitial)
	{
		return FALSE;
	}

	if(TRUE == _b8UsbStandby)
	{
		return TRUE;
	}

	if(EN_CTOS_SUCCESS != CT_OS_WaitOnSemaphore(&DVB_USB_API_Semaphore, CTOS_WAIT))
	{
		USB_DEBUG(("    [ERROR]: %s - Semaphore\n", __FUNCTION__));
		return FALSE;
	}

	// stop timer monitor
	CT_OS_ControlTimer(&DVB_USB_MonitorTimer, EN_CTOS_TIMER_DISABLE);

	for(u32Index=0; u32Index<USB_LUN_MAX; u32Index++) // current usb system support 32 LUNs
	{
		if(_stPreUSBLUNInfo.u32LUNs&(1<<u32Index))	// release exist fs
		{
			// release all fs, DVB_LIST_Delete ST_PARTITION list
			{
			DVB_DEVCTRL_UnRegisterDevice(EN_DEV_USB_LUN0+u32Index);
			USB_DEBUG(("    [STATUS]: USB LUN_%d plug out\n", u32Index));
			}
		}
	}

	// update LUN state
	_stPreUSBLUNInfo.u32LUNs      = _stUSBLUNInfo.u32LUNs		= 0;
	_stPreUSBLUNInfo.u8MaxLUN     = _stUSBLUNInfo.u8MaxLUN		= 0;
	_stPreUSBLUNInfo.b8Connect    = _stUSBLUNInfo.b8Connect		= 0;
	_stPreUSBLUNInfo.s8CurrentLUN = _stUSBLUNInfo.s8CurrentLUN	= USB_NO_MEDIA;

	CT_USB_Suspend();
	CT_SYS_PowerDown_UPLL(TRUE);

	_b8UsbStandby = TRUE;

	CT_OS_FreeSemaphore(&DVB_USB_API_Semaphore);

	return TRUE;
}

//--------------------------------------------------------------------

bool8 DVB_USB_Recovery(void)
{
	if(FALSE == _b8UsbStackInitial)
	{
		return FALSE;
	}

	if(FALSE == _b8UsbStandby)
	{
		return TRUE;
	}

	if(EN_CTOS_SUCCESS != CT_OS_WaitOnSemaphore(&DVB_USB_API_Semaphore, CTOS_WAIT))
	{
		USB_DEBUG(("    [ERROR]: %s - wait semaphore error\n", __FUNCTION__));
		return FALSE;
	}

	CT_SYS_PowerDown_UPLL(FALSE);
	CT_USB_Resume();

	_b8UsbStandby = FALSE;

	// start timer monitor
	CT_OS_ControlTimer(&DVB_USB_MonitorTimer, EN_CTOS_TIMER_ENABLE);

	CT_OS_FreeSemaphore(&DVB_USB_API_Semaphore);

	return TRUE;
}

//--------------------------------------------------------------------

bool8 DVB_USB_SetChunkSize(u32 u32chunksize)
{
	if(0 == u32chunksize)
		return FALSE;

	_u32UsbChunkSize = u32chunksize;

	return TRUE;
}

bool8 DVB_USB_GetChunkSize(void)
{
	USB_MSG(("    [STATUS]: USB chunk size %ld\n", _u32UsbChunkSize));
	return TRUE;
}

//--------------------------------------------------------------------

//u32 DVB_USB_GetInternalTaskPriority(u32 index)
//{
//	u32 u32Prio;
//
//	u32Prio = CT_USB_GetTaskPriority(index);
//	USB_MSG(("    [STATUS]: JUSB(%ld) get task priority %ld\n", index, u32Prio));
//
//	return u32Prio;
//}

//void DVB_USB_SetInternalTaskPriority(u32 index, u32 priority)
//{
//	CT_USB_SetTaskPriority(index, priority);
//	USB_MSG(("    [STATUS]: JUSB(%ld) set task priority %ld\n", index, priority));
//}

bool8 DVB_USB_GetDescriptorInfo(void)
{
	ST_USB_DEVICE_INFO  stdevice;
	ST_USB_MEDIA_INFO  stmedia;

	if (TRUE == CT_USB_GetDevcieInfo(&stdevice))
	{
		USB_MSG(("    [INFO]:   subclass: %d, devtype: %d, lun: %d\n",
							stdevice.subclass, stdevice.devtype, stdevice.lun));
		USB_MSG(("              vendor: %s, idVendor: %04x\n",
							stdevice.vendor, stdevice.idVendor));
		USB_MSG(("              product: %s, idProduct: %04x\n",
							stdevice.product, stdevice.idProduct));
		USB_MSG(("              iManufacturer: %x\n", stdevice.iManufacturer));
	}

	if (TRUE == CT_USB_GetMediaInfo(&stmedia))
	{
		USB_MSG(("    [INFO]:   sector_count: %ld, sector_size: %ld\n",
							stmedia.sector_count, stmedia.sector_size));
		USB_MSG(("              medium_type: %d, write_protect: %d\n",
							stmedia.medium_type, stmedia.write_protect));
		USB_MSG(("              heads: %d, sectors: %d, cylinders:%ld\n",
							stmedia.heads, stmedia.sectors, stmedia.cylinders));
	}

	return TRUE;
}

//--------------------------------------------------------------------

void DVB_USB_DebugInfo(u8 *pu8PreBuffer, u8 *pu8TstBuffer)
{
	u32 u32TotalSectors, u32SectorBytes;
	u32 u32Index;

	USB_MSG(("    [CMD]: This command will only test the first active LUN of USB device\n"));
	USB_MSG(("           and needs two pieces of memory(size 32KB) for IO&bitrate testing\n"));
	USB_MSG(("    =====================================================================\n"));

	for(u32Index=0; u32Index<32; u32Index++)
	{
		if( ((_stUSBLUNInfo.u32LUNs>>u32Index)&0x01) )
		{
			DVB_USB_Change_LUN(u32Index);
			break;
		}
	}

	DVB_USB_Status();

	{ // general status info
	USB_MSG(("    [STATUS]: USB %s   USB %s\n",
							TRUE  == _b8UsbStackInitial ? "INITIALED" : "NOT INITIAL",
							FALSE == _b8UsbStandby ? "NOT STANDBY" : "STANDBY"));
	USB_MSG(("              Connected: %s, TotalDevice: %d, InsertedMedia: %08lx\n",
							TRUE == _stUSBLUNInfo.b8Connect ? "TRUE" : "FALSE",
							_stUSBLUNInfo.u8MaxLUN,
							_stUSBLUNInfo.u32LUNs));

	DVB_USB_SectorInfo(&u32TotalSectors, &u32SectorBytes);
	USB_MSG(("              CurrentLUN: %d, TotalSector: %ld, SectorBytes: %ld\n",
							_stUSBLUNInfo.s8CurrentLUN,
							u32TotalSectors,
							u32SectorBytes));

	USB_MSG(("              USB chunk size: %ld\n", _u32UsbChunkSize));
	USB_MSG(("\n"));
	} // general status info end

	if(USB_NO_MEDIA != _stUSBLUNInfo.s8CurrentLUN)
	{
#if 1 // descriptor info ============================================
		{
		DVB_USB_GetDescriptorInfo();
		USB_MSG(("\n"));
		}
#endif // descriptor info end =======================================

#if 1 // test bitrate and IO ========================================
		{
		u32 u32T1=0, u32T2=0;
		u32 u32BitRate = 0;
		u8  u8TestStatus = 0;
		u16 u16secnum = 0;

		if(NULL == pu8PreBuffer)
		{
			return;
		}
		if(NULL == pu8TstBuffer)
		{
			return;
		}

		u16secnum = (512*64)/u32SectorBytes;

		// backup the original data of USB device
		DVB_USB_Read((u32TotalSectors-128), u16secnum, pu8PreBuffer);

		// prepare the test pattern
		for(u32Index=0; u32Index<(512*64); u32Index++)
		{
			pu8TstBuffer[u32Index] = (u8)(u32Index % 512);
		}

		// write the test pattern to USB device and measure bitrate
		CT_OS_MS_GetClock(&u32T1);
		DVB_USB_Write((u32TotalSectors-128), u16secnum, pu8TstBuffer);
		CT_OS_MS_GetClock(&u32T2);
		u32BitRate = ( (8 * (512*64)) / (u32T2-u32T1) );
		USB_MSG(("    [TEST]: USB write bit rate: %08ld bps(32KB / %ldms)\n", u32BitRate*1000, u32T2-u32T1));

		for(u32Index=0; u32Index<(512*64); u32Index++)
		{
			pu8TstBuffer[u32Index] = 0;
		}

		// read the test pattern from USB device and measure bitrate
		CT_OS_MS_GetClock(&u32T1);
		DVB_USB_Read((u32TotalSectors-128), u16secnum, pu8TstBuffer);
		CT_OS_MS_GetClock(&u32T2);
		u32BitRate = ((8 * (512*64)) / (u32T2-u32T1));
		USB_MSG(("            USB read bit rate:  %08ld bps(32KB / %ldms)\n", u32BitRate*1000, u32T2-u32T1));

		USB_MSG(("\n"));

		for(u32Index=0; u32Index<(512*64); u32Index++)
		{
			if(pu8TstBuffer[u32Index] != (u8)(u32Index%512))
			{
				USB_MSG(("    [TEST]: USB Write IO testing :  %08ld'nbyte [%02x][%02x] error\n", u32Index, pu8TstBuffer[u32Index], (u8)(u32Index%512)));
				u8TestStatus++;
				break;
			}
		}

		// write the backup data to USB device and measure bitrate
		CT_OS_MS_GetClock(&u32T1);
		DVB_USB_Write((u32TotalSectors-128), u16secnum, pu8PreBuffer);
		CT_OS_MS_GetClock(&u32T2);
		u32BitRate = ( (8 * (512*64)) / (u32T2-u32T1) );
		USB_MSG(("    [TEST]: USB write bit rate: %08ld bps(32KB / %ldms)\n", u32BitRate*1000, u32T2-u32T1));

		for(u32Index=0; u32Index<(512*64); u32Index++)
		{
			pu8TstBuffer[u32Index] = 0;
		}

		// read the backup data from USB device and measure bitrate
		CT_OS_MS_GetClock(&u32T1);
		DVB_USB_Read((u32TotalSectors-128), u16secnum, pu8TstBuffer);
		CT_OS_MS_GetClock(&u32T2);
		u32BitRate = ((8 * (512*64)) / (u32T2-u32T1));
		USB_MSG(("            USB read bit rate:  %08ld bps(32KB / %ldms)\n", u32BitRate*1000, u32T2-u32T1));

		USB_MSG(("\n"));

		for(u32Index=0; u32Index<(512*64); u32Index++)
		{
			if(pu8TstBuffer[u32Index] != pu8PreBuffer[u32Index])
			{
				USB_MSG(("    [TEST]: USB Read IO testing :  %08ld'nbyte [%02x][%02x] error\n", u32Index, pu8TstBuffer[u32Index], pu8PreBuffer[u32Index]));
				u8TestStatus++;
				break;
			}
		}

		if(u8TestStatus)
		{
			USB_MSG(("    [TEST]: USB Read/Write IO ERROR\n"));
		}
		else
		{
			USB_MSG(("    [TEST]: USB Read/Write IO OK\n"));
		}

		USB_MSG(("\n"));
		}
#endif // test bitrate and IO end ===================================
	}
}

//--------------------------------------------------------------------

void DVB_USB_TestReadWrite(u32 u32TestCnt)
{
	u8 au8FileName[]  = "TEST.MP3";
	bool8 b8FileExist = FALSE;

	u32 u32FileHandle;
	u32 u32FileSize;

	u8 *pu8OriginBuffer  = NULL;
	u8 *pu8CompareBuffer  = NULL;

	u32 u32Index, i;
	u32 u32CompareCnt = 0;

	bool8 b8CompareFlag;

	/* Test file exist or not? */
	b8FileExist = DVB_FILECTRL_FileExist(EN_USB_DEVICE, au8FileName);
	if (FALSE == b8FileExist)
	{
		printf("[%s]    File Not Found\n", __FILE__);
		return;
	}
	/* Open test file */
	u32FileHandle = DVB_FILECTRL_FileOpen(EN_USB_DEVICE, au8FileName, 'r');
	/* Get test file size */
	u32FileSize = DVB_FILECTRL_GetFileSize(EN_USB_DEVICE, u32FileHandle);


	/* allocate two pieces of memory for reading test file data */
	/* 1. the memory of origin data */
	pu8OriginBuffer = DVB_MemoryAllocate(u32FileSize);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -