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

📄 dvb_usb.c

📁 DVB软件,基于CT216软件的开发源程序.
💻 C
📖 第 1 页 / 共 4 页
字号:
					}
				}
			}
		}

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

		_s8UsbSetCurrentLUN			  = _stUSBLUNInfo.s8CurrentLUN;
	}
	// status b: no USB device ...
	else
	{
		if(0 != _stPreUSBLUNInfo.u32LUNs)
		{
			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;

			_s8UsbSetCurrentLUN 		  = _stUSBLUNInfo.s8CurrentLUN;

			USB_DEBUG(("    [MESSAGE]: DVB_USB_DISCONNECT\n"));
			// send DVB_USB_DISCONNECT to dvb_usb_polling_task
			//stSendMsg.u8Cmd               = DVB_USB_DISCONNECT;
			//stSendMsg.unData.u32MsgData   = 0;
			//CT_OS_PutMsg(&DVB_USB_Request_MsgQueue, &stSendMsg, CTOS_NO_WAIT);
		}
	}
}

//--------------------------------------------------------------------
//
// The current interfaces is divided into two parts:
//
// FIRST PART:
//
// APIs is the main interface for file system operations.
//
// DVB_USB_Power(new added, dependent on user's GPIO defination)
// DVB_USB_Initial
// DVB_USB_SectorInfo
// DVB_USB_Status
// DVB_USB_Read
// DVB_USB_Write
//
//--------------------------------------------------------------------

#if 0
void DVB_USB_Power(bool8 b8Enable)
{
	EN_CT_GPIO_PORT		enUSB_Power_Port;
	EN_CT_GPIO_BIT		enUSB_Power_Bit;

	#ifdef CT956
	if(IOCFG_GetIOLocation(EN_USB_POWER, &enUSB_Power_Port, &enUSB_Power_Bit) == DRVERR_NOTOK)
	{
		USB_DEBUG((">> dvb_usb.c DVB_USB_Power() Get GPIO info not ok\n"));
		return;
	}

	CT_GPIO_BitSet(enUSB_Power_Port, enUSB_Power_Bit, b8Enable);
	#else
	if(DRV_OK == IOCFG_GetIOLocation(EN_5V_PWR, &enUSB_Power_Port, &enUSB_Power_Bit))
	{
		CT_GPIO_BitSet(enUSB_Power_Port, enUSB_Power_Bit, b8Enable);
	}
	else
	{
		USB_DEBUG((">> dvb_usb.c DVB_USB_Power() Get GPIO info not ok\n"));
	}
	#endif
}
#else
void DVB_USB_Power(bool8 b8Enable)
{
	EN_CT_GPIO_PORT		enUSB_Power_Port;
	EN_CT_GPIO_BIT		enUSB_Power_Bit;

	#ifdef CT956
	if(IOCFG_GetIOLocation(EN_USB_POWER, &enUSB_Power_Port, &enUSB_Power_Bit) == DRVERR_NOTOK)
	{
		USB_DEBUG((">> dvb_usb.c DVB_USB_Power() Get GPIO info not ok\n"));
		return;
	}

	CT_GPIO_BitSet(enUSB_Power_Port, enUSB_Power_Bit, b8Enable);
	#else
	if(DRV_OK == IOCFG_QueryIOFunction(EN_USB_POWER))
	{
		if(TRUE == b8Enable)	// turn on power
		{
			CT_GPIO_SetPFCR(EN_USB_POWER);
		}
		else 					// turn off power
		{
			CT_GPIO_ClrPFCR(EN_USB_POWER);
		}
	}
	else if(DRV_OK == IOCFG_GetIOLocation(EN_USB_POWER, &enUSB_Power_Port, &enUSB_Power_Bit))
	{
		CT_GPIO_BitSet(enUSB_Power_Port, enUSB_Power_Bit, b8Enable);
	}
	else
	{
		USB_DEBUG((">> dvb_usb.c DVB_USB_Power() Get GPIO info not ok\n"));
	}
	#endif
}
#endif
//--------------------------------------------------------------------

bool8 DVB_USB_ForceFullSpeed(u8 u8enable)
{
	if (u8enable)
		_b8ForceFullSpeedFlag = TRUE;
	else
		_b8ForceFullSpeedFlag = FALSE;

	return TRUE;
}

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

bool8 DVB_USB_Initial(void)
{
	if(FALSE == _b8UsbStackInitial)
	{
		#ifdef MEDIA_RESUME
		DVB_USB_Power(DVB_USB_DISABLE);
		#endif

		// init USB driver
		if(FALSE == CT_USB_Initial(USB_HOST_MODE, (u8 *)&_au8USBStack[0], USB_HOST_STACK_SIZE))
		{
			USB_DEBUG(("    [ERROR]: %s - USB driver can't initial\n", __FUNCTION__));
			_b8UsbStackInitial = FALSE;
			return FALSE;
		}

		// using USB Full Speed?
		if (TRUE == _b8ForceFullSpeedFlag)
		{
			CT_USB_ForceFullSpeed(1);
		}
		else
		{
			CT_USB_ForceFullSpeed(0);
		}

		// enable USB power
		DVB_USB_Power(DVB_USB_ENABLE);
		USB_DEBUG(("    [STATUS]: %s - USB PHY's power enabled\n", __FUNCTION__));
	}

	return TRUE;
}

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

void DVB_USB_SectorInfo(u32 *pu32TotalSectors, u32 *pu32SectorBytes)
{
	// get media infomation about sector number and sector size
	if(0 == CT_USB_GetTotalMedia())
	{
		*pu32TotalSectors = 0;
		*pu32SectorBytes  = 0;
	}
	else
	{
		CT_USB_GetSectorInfo((u8)CT_USB_GetCurrentActiveLun(), pu32TotalSectors, pu32SectorBytes);
	}
}

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

u32 DVB_USB_Status(void)
{
	ST_USB_LUN_INFO	 _stTmpInfo;
	u32  index;

	// device connect?
	_stTmpInfo.b8Connect		= CT_USB_CheckConnect();

	if(_stUSBLUNInfo.b8Connect != _stTmpInfo.b8Connect)
		_stUSBLUNInfo.b8Connect = _stTmpInfo.b8Connect;

	// total LUNs
	_stTmpInfo.u8MaxLUN		   = CT_USB_GetTotalDevice();
	if(_stUSBLUNInfo.u8MaxLUN != _stTmpInfo.u8MaxLUN)
	{
		_stUSBLUNInfo.u8MaxLUN = _stTmpInfo.u8MaxLUN;
	}

	// check any media?
	if(0 == CT_USB_GetTotalMedia())
	{
		_stUSBLUNInfo.u32LUNs		= 0;			// no media
		_stUSBLUNInfo.s8CurrentLUN 	= USB_NO_MEDIA;	// effective media index 0~127
	}
	else
	{
		for(_stTmpInfo.u32LUNs=0, index=0; index<_stTmpInfo.u8MaxLUN; index++)
		{
			// record LUN inserted or not
			if(TRUE == CT_USB_CheckMedia(index))
				_stTmpInfo.u32LUNs  |=  (1<<index);
			else
				_stTmpInfo.u32LUNs  &= ~(1<<index);
		}
		if(_stUSBLUNInfo.u32LUNs != _stTmpInfo.u32LUNs)
		{
			_stUSBLUNInfo.u32LUNs = _stTmpInfo.u32LUNs;
		}

		// current LUN
		_stTmpInfo.s8CurrentLUN	= CT_USB_GetCurrentActiveLun();
		if(_stUSBLUNInfo.s8CurrentLUN != _stTmpInfo.s8CurrentLUN)
		{
			_stUSBLUNInfo.s8CurrentLUN = _stTmpInfo.s8CurrentLUN;
		}
	}

	if(_stUSBLUNInfo.b8Connect && _stUSBLUNInfo.u32LUNs)
		return USB_STATUS_MASS_STORAGE_DEVICE;
	else if(0 == _stUSBLUNInfo.u32LUNs)
		return USB_STATUS_NO_DISC;
	else if(0 == _stUSBLUNInfo.b8Connect)
		return USB_STATUS_NO_DEVICE;
	else
		return USB_STATUS_UNKNOWN;
}

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

u8 au8UsbBuff[512];

bool8 DVB_USB_Read(u32 u32SectorIndex, u16 u16SectorLengths, u8 *pu8Buffer)
{
	MSG_PARAMETER	stSendMsg, stRecvMsg;
	EN_CTOS_STATUS  enStatus;

	u8              u8CmdID = 0;
	u32             u32MsgData = 0;
	u32				u32RecvMsgLen = 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 (FALSE == CT_USB_CheckConnect())
	{
		USB_DEBUG(("    [ERROR]: %s - device connection error\n", __FUNCTION__));
		return FALSE;
	}

	if (FALSE == CT_USB_CheckMedia(_s8UsbSetCurrentLUN))
	{
		USB_DEBUG(("    [ERROR]: %s - device media error\n", __FUNCTION__));
		return FALSE;
	}

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

	_u32UsbStartSect = u32SectorIndex;
	if (NULL == pu8Buffer)	// for debug
	{
		_u32UsbSectNum   = 1;
		_pu8UsbReadBuf	 = &au8UsbBuff[0];
	}
	else					// normal operation
	{
		_u32UsbSectNum   = u16SectorLengths;
		_pu8UsbReadBuf   = pu8Buffer;
	}

	USB_DEBUG(("    [STATUS]: %s - LUN_%d start %x, length %x %08x\n", __FUNCTION__, CT_USB_GetCurrentActiveLun(), u32SectorIndex, u16SectorLengths, pu8Buffer));

	// send DVB_USB_READ_ENABLE to dvb_usb_polling_task
	stSendMsg.u8Cmd               = DVB_USB_READ_ENABLE;
	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)
	{
		USB_DEBUG(("    [ERROR]: %s - wait message error\n", __FUNCTION__));
		goto exit1;
	}

	u8CmdID		= stRecvMsg.u8Cmd;
	u32MsgData	= stRecvMsg.unData.u32MsgData;
	switch(u8CmdID)
	{
		case DVB_USB_READ_DONE:
		{
			USB_DEBUG(("    [STATUS]: %s - OK\n", __FUNCTION__));

			// for debug
			//if(NULL == pu8Buffer)	if_dump_hex_data(_pu8UsbReadBuf, 512);

			b8State = TRUE;
		}
		break;

		case DVB_USB_READ_ERROR:
		{
			USB_DEBUG(("    [STATUS]: %s - READ ERROR\n", __FUNCTION__));
			_u32USBErrCntInfo++;

			b8State = FALSE;
		}
		break;
		default :
		break;
	}

exit1:
	CT_OS_FreeSemaphore(&DVB_USB_API_Semaphore);

	return b8State;
}

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

bool8 DVB_USB_Write(u32 u32SectorIndex, u16 u16SectorLengths, u8 *pu8Buffer)
{
	MSG_PARAMETER	stSendMsg, stRecvMsg;
	EN_CTOS_STATUS  enStatus;

	u8              u8CmdID = 0;
	u32             u32MsgData = 0;
	u32				u32RecvMsgLen = 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 (FALSE == CT_USB_CheckConnect())
	{
		USB_DEBUG(("    [ERROR]: %s - device connection error\n", __FUNCTION__));
		return FALSE;
	}

	if (FALSE == CT_USB_CheckMedia(_s8UsbSetCurrentLUN))
	{
		USB_DEBUG(("    [ERROR]: %s - device media error\n", __FUNCTION__));
		return FALSE;
	}

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

	_u32UsbStartSect = u32SectorIndex;
	if (NULL == pu8Buffer)	// for debug
	{
		_u32UsbSectNum   = 1;
		_pu8UsbWriteBuf  = &au8UsbBuff[0];
	}
	else					// for normal
	{
		_u32UsbSectNum   = u16SectorLengths;
		_pu8UsbWriteBuf  = pu8Buffer;
	}

	USB_DEBUG(("    [STATUS]: %s - LUN_%d start %x, length %x %08x\n", __FUNCTION__, CT_USB_GetCurrentActiveLun(), u32SectorIndex, u16SectorLengths, pu8Buffer));

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

	// wait and get DVB_USB_WRITE_DONE/DVB_USB_WRITE_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)
	{
		goto exit1;
	}

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

	switch(u8CmdID)
	{
		case DVB_USB_WRITE_DONE:
		{
			USB_DEBUG(("    [STATUS]: %s - OK\n", __FUNCTION__));
			b8State = TRUE;
		}
		break;
		case DVB_USB_WRITE_ERROR:
		{
			USB_DEBUG(("    [STATUS]: %s - READ ERROR\n", __FUNCTION__));
			b8State = FALSE;
		}
		break;
		default:
		break;
	}

exit1:
	CT_OS_FreeSemaphore(&DVB_USB_API_Semaphore);

	return b8State;
}

//--------------------------------------------------------------------
// SECOND PART:
//
// APIs is the main interface for user UI system operations,
// include a internal task which process user UI commands
//
// DVB_USB_InitPolling
// DVB_USB_Change_LUN
// DVB_USB_Get_LUN
// DVB_USB_Ready
// DVB_USB_Standby
// DVB_USB_Reset
// DVB_USB_DebugInfo
//
//--------------------------------------------------------------------

bool8 DVB_USB_InitPolling(void)
{
    if(FALSE == _b8UsbStackInitial)
	{
		//// init usb software stack
		//if(FALSE == DVB_USB_Initial())
		//{
		//	USB_DEBUG(("    [ERROR]: %s -USBInitial error\n", __FUNCTION__));
		//	return FALSE;
		//}

		if(EN_CTOS_SUCCESS != CT_OS_CreateMsgQueue(&DVB_USB_Request_MsgQueue, "USBQ", &u32UsbRequestQueueMsg[0], USB_QUEUE_SIZE, EN_CTOS_SUSPEND_FIFO))
		{
			USB_DEBUG(("    [ERROR]: %s - CreateRequestMsgQueue error\n", __FUNCTION__));
			return FALSE;
		}

		if(EN_CTOS_SUCCESS != CT_OS_CreateMsgQueue(&DVB_USB_Reply_MsgQueue, "USBQ", &u32UsbReplyQueueMsg[0], USB_QUEUE_SIZE, EN_CTOS_SUSPEND_FIFO))
		{
			CT_OS_DeleteMsgQueue(&DVB_USB_Request_MsgQueue);

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

		if(EN_CTOS_SUCCESS != CT_OS_CreateSemaphore(&DVB_USB_API_Semaphore, "DVBUsbSem", 1, EN_CTOS_SUSPEND_FIFO))
		{
			CT_OS_DeleteMsgQueue(&DVB_USB_Request_MsgQueue);
			CT_OS_DeleteMsgQueue(&DVB_USB_Reply_MsgQueue);

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

		if(EN_CTOS_SUCCESS != CT_OS_CreateTask(&DVB_USB_Polling_Task,
											   "UsbTask",
											   (CTOS_TASK_ENTRY_FN)dvb_usb_polling_task,
											   &_au8USBPollingStack[0],
											   DVB_USB_TASK_STACK_SIZE,
											   DVB_USB_TASK_PRIORITY,
											   DVB_USB_TIME_SLICE))
		{
			CT_OS_DeleteMsgQueue(&DVB_USB_Request_MsgQueue);
			CT_OS_DeleteMsgQueue(&DVB_USB_Reply_MsgQueue);
			CT_OS_DeleteSemaphore(&DVB_USB_API_Semaphore);

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

		if(EN_CTOS_SUCCESS != CT_OS_ResumeTask(&DVB_USB_Polling_Task))
		{
			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 - ResumeTask error\n", __FUNCTION__));
			return FALSE;
		}

		if(EN_CTOS_SUCCESS != CT_OS_CreateTimer(&DVB_USB_MonitorTimer,
												"UsbTimer",
												EN_CTOS_TIMER_CONTINUOUS,
												dvb_usb_monitor_timer,		// handler
												0,

⌨️ 快捷键说明

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