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

📄 usbdtask.c

📁 epson usb2.0 控制芯片 S1R72V05 固件程序。
💻 C
📖 第 1 页 / 共 5 页
字号:
// global		:
//=============================================================================
*/
LONG VBusChangeCallback(ULONG lParam0,ULONG lParam1,VOID *pParam)
{
	ULONG retValue;
	USBD_IF_USB_STATE usbState;
	UCHAR vbus,mode;
	PMSGUSBD msg;

	OS_GetMpf( MPFID_USBD_MSG, (VP)&msg );						/* Fixed length memory pool for message transmission */
	msg->msgHead.msgSndTskId = TSKID_USBD;						/* ID of USBD Task */
	msg->msgHead.useMpfId = MPFID_USBD_MSG;						/* ID of memory pool to be used */
	msg->msgHead.msgNo = MSG_NTFY_USBD_VBUS_STATUS;				/* Notification of status of VBUS */
	msg->msgHead.msgLength = sizeof (PARAM_MSG_NTFY_USBD_VBUS_STATUS);

	/* Get current status of VBUS */
	USBD_IFGetState(&usbState);
	vbus = usbState.VBUS;


	/* When this callback is called, vbus is being stable(after 15ms) */
	if (vbus == USBD_IF_VBUS_HIGH) {
		((PARAM_MSG_NTFY_USBD_VBUS_STATUS *)msg->msgData)->vbusStatus = USBD_VBUS_CONNECT;
	} else {
		((PARAM_MSG_NTFY_USBD_VBUS_STATUS *)msg->msgData)->vbusStatus = USBD_VBUS_DISCONNECT;
	}

	/*----------------------------------------------------------------------*/
	/* Transmission of message and notification of event				*/
	/*----------------------------------------------------------------------*/
	OS_SndMbx( MBXID_DEVICE, (T_MSG *)msg );						/* Send message */
	OS_SetFlg( FLGID_DEVICE, FLG_EVENT_MSG_DEVICE );					/* Notify event to DEVICE Task */

	/* Get state of power saving */
	PM_IFGetPMState(&mode);
	/* Do process according to current state */
	switch (UsbdTaskInfo.state) {
		case STATE_IDLE:										/* Idle */
			/* Hold the line */
			break;
		case STATE_WAIT_CONNECT:								/* Waiting Connect */
			if (vbus == USBD_IF_VBUS_HIGH) {
				/* Transit to state of Attach waiting */
				UsbdTaskInfo.state = STATE_WAIT_ATTACH;
			} else {
				/* Hold the line */
			}
			break;
		case STATE_WAIT_ATTACH:									/* Waiting Attach */
			if (vbus == USBD_IF_VBUS_LOW) {
				/* Transit to Connect waiting */
				UsbdTaskInfo.state = STATE_WAIT_CONNECT;
			} else {
				/* Hold the line */
			}
			break;
		case STATE_WAIT_DETACH:									/* Waiting Detach */
			if (vbus == USBD_IF_VBUS_HIGH) {
				/* Transit to state of Not Configured */
				if (mode != PM_IF_PMSTATE_ACT_DEVICE) {
					/* Send out the request of releasing from setting of power saving */
					ReqUsbdWakeupCallback(0L,0L,NULL);
				} else {
					retValue = USBD_FuncStart(USBD_FUNC_ATTACH_NORMAL);
					if (retValue == STATUS_SUCCESS) {
						/* State of Not Config */
						UsbdTaskInfo.state = STATE_NOT_CONFIG;
					} else {
						/* No change... */
					}
				}
			} else {
				/* No change... */
			}
			break;
		case STATE_NOT_CONFIG:									/* Disable state of Class */
		case STATE_STRG_CONFIG:									/* Enable state of Storage Class */
			if (vbus == USBD_IF_VBUS_LOW) {
				retValue = USBD_FuncStop();
				if (retValue == STATUS_SUCCESS) {
					/* Transit to state of Detach waiting */
					UsbdTaskInfo.state = STATE_WAIT_DETACH;
				} else {
					/* Hold the line */
				}
			} else {
				/* No change */
			}
			break;
		case STATE_NC_SUSPEND:									/* State of Suspend(Class is disabled) */
		case STATE_SC_SUSPEND:									/* State of Suspend(Storage Class is enabled) */
			if (vbus == USBD_IF_VBUS_LOW) {
				if (mode != PM_IF_PMSTATE_ACT_DEVICE) {
					/* Send out the request of releasing from setting of power saving */
					ReqUsbdWakeupCallback(0L,0L,NULL);			/* Request of Wakeup */
				} else {
					retValue = USBD_FuncStop();
					if (retValue == STATUS_SUCCESS) {
						/* Transit to state of Detach waiting */
						UsbdTaskInfo.state = STATE_WAIT_DETACH;
					} else {
						/* Hold the line */
					}
				}
			} else {
				/* No change */
			}
			break;
		default:
			break;
	}

	return STATUS_SUCCESS;
}
/*
//=============================================================================
// Function_Name: ReqUsbdPullupCtrl
// description	: Do Attach/Detach of USB
// argument		:
// return		:
// flag			:
// global		:
//=============================================================================
*/
static LONG ReqUsbdPullupCtrl(PMSGUSBD pMsg)
{
	LONG	retValue;
	UCHAR	pullupFlag;
	USBD_IF_USB_STATE usbState;

	PMSGUSBD msg;


	OS_GetMpf( MPFID_USBD_MSG, (VP)&msg );						/* Fixed length memory pool for message transmission */
	msg->msgHead.msgSndTskId = TSKID_USBD;						/* ID of USBD Task */
	msg->msgHead.useMpfId = MPFID_USBD_MSG;						/* ID of memory pool to be used */
	msg->msgHead.msgNo = MSG_NTFY_USBD_PULLUP_CTRL;				/* Notification for Attach/Detach of USB */
	msg->msgHead.msgLength = sizeof (PARAM_MSG_NTFY_USBD_PULLUP_CTRL);
	((PARAM_MSG_NTFY_USBD_PULLUP_CTRL *)msg->msgData)->result = USBD_PARAM_ERR;

	pullupFlag = ((PARAM_MSG_REQ_USBD_PULLUP_CTRL *)pMsg->msgData)->pullupFlag;
	if (pullupFlag == USBD_ATTACH) {
		retValue = USBD_FuncStart(USBD_FUNC_ATTACH_NORMAL);
		if (retValue == STATUS_SUCCESS) {
			/* Finished normally */
			UsbdTaskInfo.state = STATE_NOT_CONFIG;
			((PARAM_MSG_NTFY_USBD_PULLUP_CTRL *)msg->msgData)->result = USBD_REQ_OK;

		} else {
			/* Finished abnormally */
			((PARAM_MSG_NTFY_USBD_PULLUP_CTRL *)msg->msgData)->result = USBD_REQ_NG;
		}
#ifdef DEBUG_C
		LED_On(7);
#endif
	} else if (pullupFlag == USBD_DETACH) {
		retValue = USBD_FuncStop();
		if (retValue == STATUS_SUCCESS) {
			/* Finished normally */

			/* Get current status of VBUS */
			USBD_IFGetState(&usbState);
			if (usbState.VBUS == USBD_IF_VBUS_LOW) {
				UsbdTaskInfo.state = STATE_WAIT_CONNECT;
			} else {
				UsbdTaskInfo.state = STATE_WAIT_ATTACH;
			}

			((PARAM_MSG_NTFY_USBD_PULLUP_CTRL *)msg->msgData)->result = USBD_REQ_OK;
		} else {
			/* Finished abnormally */
			((PARAM_MSG_NTFY_USBD_PULLUP_CTRL *)msg->msgData)->result = USBD_REQ_NG;
		}
#ifdef DEBUG_C
		LED_Off(7);
#endif
	}

	/*----------------------------------------------------------------------*/
	/* Transmission of message and notification of event				*/
	/*----------------------------------------------------------------------*/
	OS_SndMbx( MBXID_DEVICE, (T_MSG *)msg );						/* Send message */
	OS_SetFlg( FLGID_DEVICE, FLG_EVENT_MSG_DEVICE );					/* Notify event to DEVICE Task */

	return STATUS_SUCCESS;
}
/*
//=============================================================================
// Function_Name: ReqUsbdWakeupCallback
// description	: The callback which is called when there is a request of Wakeup
// argument		:
// return		:
// flag			:
// global		:
//=============================================================================
*/
LONG ReqUsbdWakeupCallback(ULONG lParam0,ULONG lParam1,VOID *pParam)
{
	PMSGUSBD msg;
	USBD_IF_USB_STATE usbState;
	/* Get current status of VBUS */
	USBD_IFGetState(&usbState);
	if (!(UsbdTaskInfo.state == STATE_NC_SUSPEND || UsbdTaskInfo.state == STATE_SC_SUSPEND)) {
		UsbdTaskInfo.state = STATE_NC_SUSPEND;
	}
	if (usbState.VBUS == USBD_IF_VBUS_HIGH) {
		OS_GetMpf( MPFID_USBD_MSG, (VP)&msg );						/* Fixed length memory pool for message transmission */
		msg->msgHead.msgSndTskId = TSKID_USBD;						/* ID of USBD Task */
		msg->msgHead.useMpfId = MPFID_USBD_MSG;						/* ID of memory pool to be used */
		msg->msgHead.msgNo = MSG_REQ_USBD_WAKEUP;					/* Request of Wakeup */
		msg->msgHead.msgLength = 0;

		/*----------------------------------------------------------------------*/
		/* Transmission of message and notification of event				*/
		/*----------------------------------------------------------------------*/
		OS_SndMbx( MBXID_DEVICE, (T_MSG *)msg );						/* Send message */
		OS_SetFlg( FLGID_DEVICE, FLG_EVENT_MSG_DEVICE );					/* Notify event to DEVICE Task */
		UsbdTaskInfo.reqUsbdWakeup = 1;
	}
	return STATUS_SUCCESS;

}
/*
//=============================================================================
// Function_Name: NtfyUsbdWakeup
// description	: Notification of completion for process of Wakeup
// argument		:
// return		:
// flag			:
// global		:
//=============================================================================
*/
static LONG NtfyUsbdWakeup(PMSGUSBD pMsg)
{
	LONG retValue;
	USBD_IF_USB_STATE usbState;


	if (UsbdTaskInfo.reqUsbdWakeup == 1) {
		/* When request of Wakeup has been sent out from this task */
		UsbdTaskInfo.reqUsbdWakeup = 0;
		switch (UsbdTaskInfo.state) {
			case STATE_IDLE:									/* Idle */
			case STATE_WAIT_CONNECT:							/* Waiting Connect */
			case STATE_WAIT_ATTACH:								/* Waiting Attach */
				/* Do nothing */
				break;
			case STATE_WAIT_DETACH:								/* Waiting Detach */
				/* Get current status of VBUS */
				USBD_IFGetState(&usbState);
				if (usbState.VBUS == USBD_IF_VBUS_HIGH) {
					/* Connect automatically if VBUS has become High */
					retValue = USBD_FuncStart(USBD_FUNC_ATTACH_NORMAL);
					if (retValue == STATUS_SUCCESS) {
						/* State of Not Config */
						UsbdTaskInfo.state = STATE_NOT_CONFIG;
					} else {
						/* No change... */
					}
				} else {
					/* Do process of Detach, if VBUS is Low */
					retValue = USBD_FuncStop();
					if (retValue == STATUS_SUCCESS) {
						/* Transit to state of Detach waiting */
						UsbdTaskInfo.state = STATE_WAIT_DETACH;
					} else {
						/* Hold the line */
					}
				}
				break;
			case STATE_NOT_CONFIG:								/* Not Configured */
			case STATE_STRG_CONFIG:								/* Storage Configured */
				/* Do nothing */
				break;
			case STATE_NC_SUSPEND:								/* Suspend(Not Config) */
				USBD_FuncUsbBusWakeup();	/* USB Suspend -> USB Reset / USB Resume is done in this function */
				UsbdTaskInfo.state = STATE_NOT_CONFIG;
				break;
			case STATE_SC_SUSPEND:								/* Suspend(Storage Config) */
				USBD_FuncUsbBusWakeup();	/* USB Suspend -> USB Reset / USB Resume is done in this function */
				UsbdTaskInfo.state = STATE_STRG_CONFIG;
				break;
			default:
				/* ??? */
				break;
		}
	}

	return STATUS_SUCCESS;
}
/*
//=============================================================================
// Function_Name: ReqUsbdRemoteWakeup
// description	: Request of Remote Wakeup
// argument		:
// return		:
// flag			:
// global		:
//=============================================================================
*/
static LONG ReqUsbdRemoteWakeup(PMSGUSBD pMsg)
{
	LONG retValue;

	/* Send out the request of Remote Wakeup */
	retValue = USBD_ProtEnumWakeupA();
	if (retValue != STATUS_SUCCESS) {
		/* Call the callback here, if Remote Wakeup is unable to be done */
		NtfyUsbdRemoteWakeupCallback((ULONG)STATUS_UNSUCCESSFUL,0L,NULL);
	}
	return STATUS_SUCCESS;
}
/*
//=============================================================================
// Function_Name: NtfyUsbdRemoteWakeup
// description	: Notify result of Remote Wakeup
// argument		:
// return		:
// flag			:
// global		:
//=============================================================================
*/
static LONG NtfyUsbdRemoteWakeupCallback(ULONG lParam0,ULONG lParam1,VOID *pParam)
{
	PMSGUSBD msg;

	OS_GetMpf( MPFID_USBD_MSG, (VP)&msg );						/* Fixed length memory pool for message transmission */
	msg->msgHead.msgSndTskId = TSKID_USBD;						/* ID of USBD Task */
	msg->msgHead.useMpfId = MPFID_USBD_MSG;						/* ID of memory pool to be used */
	msg->msgHead.msgNo = MSG_NTFY_USBD_REMOTEWAKEUP;			/* Notification of Remote Wakeup */

	msg->msgHead.msgLength = 1;
	if (lParam0 == STATUS_SUCCESS) {
		((PARAM_MSG_NTFY_USBD_REMOTEWAKEUP *)msg->msgData)->result = USBD_REQ_OK;
	} else {
		((PARAM_MSG_NTFY_USBD_REMOTEWAKEUP *)msg->msgData)->result = USBD_REQ_NG;
	}

	/*----------------------------------------------------------------------*/
	/* Transmission of message and notification of event				*/
	/*----------------------------------------------------------------------*/
	OS_SndMbx( MBXID_DEVICE, (T_MSG *)msg );						/* Send message */
	OS_SetFlg( FLGID_DEVICE, FLG_EVENT_MSG_DEVICE );					/* Notify event to DEVICE Task */

	return STATUS_SUCCESS;
}
/*
//=============================================================================
// Function_Name: NtfyUsbdSuspendCallback
// description	: The callback which is called when USB Suspend has occurred
// argument		:
// return		:
// flag			:
// global		:
//=============================================================================
*/
LONG NtfyUsbdSuspendCallback(ULONG lParam0,ULONG lParam1,VOID *pParam)
{
	PMSGUSBD msg;


	OS_GetMpf( MPFID_USBD_MSG, (VP)&msg );						/* Fixed length memory pool for message transmission */
	msg->msgHead.msgSndTskId = TSKID_USBD;						/* ID of USBD Task */
	msg->msgHead.useMpfId = MPFID_USBD_MSG;						/* ID of memory pool to be used */
	msg->msgHead.msgNo = MSG_NTFY_USBD_SUSPEND;					/* Notification of USB suspend */
	msg->msgHead.msgLength = 0;

	if (UsbdTaskInfo.state == STATE_NOT_CONFIG) {
		UsbdTaskInfo.state = STATE_NC_SUSPEND;
	} else if (UsbdTaskInfo.state == STATE_STRG_CONFIG) {
		UsbdTaskInfo.state = STATE_SC_SUSPEND;
	} else {
		/* It doesn't come here */
		while (1)
			;
	}

⌨️ 快捷键说明

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