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

📄 usbdl_usbacm_adap.c

📁 MTK 平台启动源码
💻 C
字号:
/*****************************************************************************
*  Copyright Statement: 
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2005
*
*  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
*  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
*  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
*  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
*  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
*  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
*  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
*  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
*  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
*  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
*  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
*  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
*  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
*  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
*  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. 
*
*  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
*  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
*  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
*  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
*  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/

/*****************************************************************************
 *
 * Filename:
 * ---------
 *    usbacm_adap.c
 *
 * Project:
 * --------
 *   Maui_Software
 *
 * Description:
 * ------------
 *   This file implements usb adaption layer for UART API
 *
 * Author:
 * -------
 * -------
 *
 *============================================================================
 *             HISTORY
 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *------------------------------------------------------------------------------
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *------------------------------------------------------------------------------
 * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *============================================================================
 ****************************************************************************/

#ifdef  __USB_DOWNLOAD__
#include "drv_comm.h"
//#include "intrCtrl.h"
//#include "uart_sw.h"

#include "usb_comm.h"
//#include "usb_drv.h"
#include "usbdl_usb.h"
#include "usbdl_usbacm_drv.h"
#include "usbdl_usb_resource.h"
#include "usb_custom.h"

#include "reg_base.h"
//#include "drvpdn.h"
//#include "usb_drv_pri.h"
#include "kbd_table.h"
#include "keypad_sw.h"
#include "keypad_hw.h"
//#include "usb_hw.h"



extern void GPIO_init(void);
extern void DRV_RESET(void);

#ifdef __SECURE_COSIM__
#define Delay_Count      1080
#else
#define Delay_Count      324675
#endif //__SECURE_COSIM__

usbacm_buf_struct usbacm_tx_buf;
usbacm_buf_struct usbacm_rx_buf;

static const volatile kal_uint8  startcmd_len = 4;
static const kal_uint8  startcmd[4] = {
	0xa0,
	0x0a,
	0x50, 
	0x05
};

const keypad_customize_function_struct *keypadCustomFunc;
const keypad_struct *keypad_comm_def;
extern const keypad_customize_function_struct *keypad_GetFunc(void);
//extern USB_Drv_Info g_UsbDrvInfo;
extern kal_uint8 usbdlkey_position;

extern kal_bool USBDLKey_Pressed(void);
extern void USBDL_USBPowerON(void);

//extern void WDT_Enable(kal_bool en);
//extern void Drv_Customize_Init(void);
//extern kal_uint32 drv_get_current_time(void);
//extern kal_uint32 drv_get_duration_ms(kal_uint32 previous_time);


//-------------------------------------------------------------------------//
void keypad_customize_init(void)
{
   keypadCustomFunc=keypad_GetFunc();
   keypad_comm_def = keypadCustomFunc->keypad_Get_Data();
}   

kal_bool USBDLKey_Pressed(void)
{
	kal_uint32 index;
	kal_uint16 usbdlkey_reg = 0;
	kal_uint16 usbdlkey_bit = 0;
	kal_uint16 usbdlkey_status = 0;


	for(index = 0; index < kbd_allkeys; index++)
	{
		if(keypad_comm_def->keypad[index] == usbdlkey_position)
		{
			usbdlkey_reg = (index/16);         
			usbdlkey_bit = 0x0001 <<(index&0xf);
			break;
		}
	}
#if defined(DRV_KBD_MUX_WITH_GPIO)
	/* Need a delay to wait 32KHz sample for keypad scan. */
	for(index = 0; index < 1000000; index++)
	{}
#endif /* KBD_MUX_WITH_GPIO */
	switch(usbdlkey_reg)
	{
	case low_key:
		usbdlkey_status= DRV_Reg(KP_LOW_KEY);
		break;
#if defined(DRV_KBD_32KEYS_ABOVE)
	case medium_key:
		usbdlkey_status= DRV_Reg(KP_MID_KEY);               
		break;
#endif          
	case high_key:
		usbdlkey_status= DRV_Reg(KP_HI_KEY);
		break;
      default:
		ASSERT(0);
		break;     
	}

	if((DRV_Reg(KP_STS)&KP_STS_KEYPRESS)&&((~usbdlkey_status)&(usbdlkey_bit)))
		return KAL_TRUE;
	else
		return KAL_FALSE;    
}   

//static kal_bool JI_runtime_mode_control = KAL_FALSE;

kal_bool USBDL_Update_USB_Download_Mode(void)
{
	static kal_bool first = KAL_TRUE;

#if 1
	if(first)
	{
		first = KAL_FALSE;
		keypadCustomFunc=keypad_GetFunc();
		keypad_comm_def = keypadCustomFunc->keypad_Get_Data();
	}	 

	if(USBDLKey_Pressed() == KAL_TRUE)
	{
		if(gUsbDevice.nDevState == DEVSTATE_CONFIG)
			gUsbDevice.is_usb_download_mode = KAL_TRUE;
		else
			gUsbDevice.is_usb_download_mode = KAL_FALSE;

		return KAL_TRUE;
	}
	else
	{
		gUsbDevice.is_usb_download_mode = KAL_FALSE;
		return KAL_FALSE;	
	}
#else
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif
}


kal_bool USBDL_Is_USB_Download_Mode(void)
{
	return gUsbDevice.is_usb_download_mode;
}


/*
	Initialize power down fuction, DMA and USB comport
*/
void USBDL_hwInit(void)
{
	volatile kal_uint32 delay;


	// configure GPIO/GPO for D+
	// 1. GPIO/GPO is used as D+
	// 2. the reset value of GPIO/GPO is not GPIO/GPO mode and the level is high
	// 3. set the mode to GPIO/GPO mode and set the output to low level
	/* After GPIO_init, D+ will be pulled low */
	GPIO_init();
#ifdef __USB_AND_UART_WITH_ONE_GPIO__
	/* Set the switch to USB port */
	USB_Switch_Gpio_to_USB_And_PowerControl(KAL_FALSE);
#else
	USB_PowerControl(KAL_FALSE);
#endif
	/* turn on USB controller and UPLL, but not pull high D+!!*/
	USBDL_USBPowerON();	
	// delay for a while to make host identify the D+ low, or the host will ignore it.
	for(delay = 0; delay < 1000; delay++);
}

/* 
	1. initialize the cdc acm for usb download
	2. there is no USB task, NVRAM task and no interrupt
*/
void USBDL_ACMInit(void)
{

	/* get customize parameters*/
	gUsbDevice.device_param = USB_GetCustomFunc()->get_device_param_func();
	g_UsbACM.acm_param = USB_GetCustomFunc()->get_acm_param_func();

	USB_Register_CreateFunc("ACM DATA", USB_Acm_DataIf_Create,USB_Acm_DataIf_Reset);
	USB_Register_CreateFunc("ACM COMMU.", USB_Acm_CommIf_Create,USB_Acm_CommIf_Reset);
	USB_Register_Device_Code(USB_ACM_DEVICE_CODE, USB_ACM_SUBCLASS_CODE, 
								USB_ACM_PROTOCOL_CODE, g_UsbACM.acm_param->desc_product);

	USB_Init(USB_CDC_ACM, KAL_TRUE, KAL_FALSE);
}


void USBDL_UART_Init(void)
{
	gUsbDevice.nDevState = DEVSTATE_DEFAULT;
	gUsbDevice.is_usb_download_mode = KAL_FALSE;
	USBDL_hwInit();
	USBDL_ACMInit();

	/* wait PC set_config or DL key is un-pressed */
	while((USBDL_Update_USB_Download_Mode() == KAL_TRUE) && (gUsbDevice.nDevState != DEVSTATE_CONFIG))
	{

		USB_HISR();
	}
	
	g_UsbACM.line_coding.dwDTERate = 115200;
	g_UsbACM.line_coding.bCharFormat = 0;
	g_UsbACM.line_coding.bParityType = 0;	
	g_UsbACM.line_coding.bDataBits = 8;
	   	
	USB2UARTPort.initialized = KAL_TRUE;	   	
	
	USB_TxEPEn(g_UsbACM.txpipe->byEP, KAL_FALSE, KAL_TRUE);

	USB_RxEPEn(g_UsbACM.rxpipe->byEP, KAL_FALSE, KAL_TRUE);
		
}


void USBDL_PutUARTByte(kal_uint8 data)
{
	kal_uint8 	ep_num;


	if(gUsbDevice.nDevState == DEVSTATE_CONFIG)
	{
		usbacm_tx_buf.data[usbacm_tx_buf.len] = data;
		usbacm_tx_buf.len++;

		if(usbacm_tx_buf.len != USBDL_EP_BULK_MAXP)
		{
			/* gather the data until one USB MAX packet size */
			return;
		}
		else
		{
			ep_num = g_UsbACM.txpipe->byEP;
			gUSBAcm_IsInEPComplete = KAL_FALSE;		
			USB_EPFIFOWrite(ep_num, USBDL_EP_BULK_MAXP, usbacm_tx_buf.data);
			USB_EP_Bulk_Tx_Ready(ep_num);

			while(gUSBAcm_IsInEPComplete == KAL_FALSE)
			{
				USB_HISR();
			}
			usbacm_tx_buf.len = 0;
		}
	}	
}

/*
	It is called by bootloader to flush the tx buffer into USB FIFO
*/
void USBDL_PutUARTByte_Complete()
{
	kal_uint8 	ep_num;


	if(gUsbDevice.nDevState == DEVSTATE_CONFIG)
	{
		ep_num = g_UsbACM.txpipe->byEP;
		gUSBAcm_IsInEPComplete = KAL_FALSE;		
		USB_EPFIFOWrite(ep_num, (kal_uint16)usbacm_tx_buf.len, usbacm_tx_buf.data);
		USB_EP_Bulk_Tx_Ready(ep_num);

		while(gUSBAcm_IsInEPComplete == KAL_FALSE)
		{
			USB_HISR();
		}
		usbacm_tx_buf.len = 0;
	}
}

#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#else
kal_uint8 USBDL_GetUARTByte(void)
{
	static kal_uint32 index = 0;
	 
 
	if(index == usbacm_rx_buf.len)
	{
		/* All data have been read */
		usbacm_rx_buf.len = 0;
		index = 0;
	}

	/* Get data from FIFO only when local buffer is empty  */
	while(usbacm_rx_buf.len == 0)
	{
		USB_HISR();
	}
	
	return usbacm_rx_buf.data[index++];
}
#endif

kal_uint8 USBDL_CheckStartCmd(void)
{
	kal_uint8  startcmd_index = 0;
	kal_uint32 count = Delay_Count;  /*300ms for 13M*/
	kal_uint8  data;
	kal_uint8  GetStartCmd = KAL_FALSE;
     

	while(count)
	{	    
		data = USBDL_GetUARTByte() ;
		USBDL_PutUARTByte(~data);
		USBDL_PutUARTByte_Complete();

		if (data == startcmd[startcmd_index])
		{
			count = Delay_Count;
			startcmd_index++;

			if (startcmd_index == startcmd_len)
			{
				GetStartCmd = KAL_TRUE;
				break;
			}
		}
		else
		{
			startcmd_index = 0;
		}
		count--;
	}
	return GetStartCmd;
}

// this can be omited for USB COM
void USBDL_CheckUARTSendEnd(void)
{
}

#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
		#if 1
/* under construction !*/
/* under construction !*/
		#else
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
		#endif
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif

#endif  /* __USB_DOWNLOAD__ */

⌨️ 快捷键说明

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