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

📄 s3c-spi.c

📁 基于wince5.0的GSPI8686 wifi驱动源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "s3c-spi.h"

extern int		g_spi_dummy_clk_reg;
extern int		g_spi_dummy_clk_data;

#define IRQ_WLAN IRQ_EINT11

DWORD wlan_isr(SPI_DEVICE_HANDLE hDevice)
{
	SPI_DEVICE_CONTEXT *pDeviceContext = SPIHANDLE_TO_DEVICE(hDevice);
	DWORD dwStatus;
	BOOL result;
	
   RETAILMSG(1, (L"Running: wlan_isr\r\n"));
	 
	pDeviceContext->exit_isr_wlan = FALSE;
	
	while(pDeviceContext->exit_isr_wlan == FALSE)
	{
		dwStatus = WaitForSingleObject(pDeviceContext->event_wlan, INFINITE);

		ResetEvent(pDeviceContext->event_wlan);

		//call back the ist function outside
		result = pDeviceContext->IsrCallBack(pDeviceContext->isrContext);

		InterruptDone(pDeviceContext->sysintr_wlan);
	}

	return TRUE;
}

BOOL p_s3c_map_register(SPI_DEVICE_HANDLE hDevice)
{
	SPI_DEVICE_CONTEXT *pDeviceContext = SPIHANDLE_TO_DEVICE(hDevice);
	
	/*
	* Alloc register memory
	*/ 
 	//allocate GPIO address
    pDeviceContext->gpio = (volatile S3C2440A_IOPORT_REG *) VirtualAlloc(0,sizeof(S3C2440A_IOPORT_REG), MEM_RESERVE, PAGE_NOACCESS);
    if(pDeviceContext->gpio == NULL)
	{
        RETAILMSG(1, (L"ERROR: s3c_spi_init: "
            L"Failed VirtualAlloc gpio device structure\r\n" ));
		   goto error;
	 }
    else 
	{
	    if (!VirtualCopy((PVOID)pDeviceContext->gpio, (PVOID)(S3C2440A_BASE_REG_PA_IOPORT >> 8), sizeof(S3C2440A_IOPORT_REG), PAGE_PHYSICAL | PAGE_READWRITE | PAGE_NOCACHE)) 		
		{
        	RETAILMSG(1, (L"ERROR: s3c_spi_init: "
           	 L"Failed VirtualCopy gpio device structure\r\n" ));
    		goto error;
	    }
	 }
   
	//allocate SPI address
    pDeviceContext->spi = (volatile S3C2440A_SPI_REG *) VirtualAlloc(0,sizeof(S3C2440A_SPI_REG), MEM_RESERVE, PAGE_NOACCESS);
    if(pDeviceContext->spi == NULL)
	{
        RETAILMSG(1, (L"ERROR: s3c_spi_init: "
            L"Failed VirtualAlloc spi device structure\r\n" ));
		   goto error;
	 }
    else 
	{
	    if (!VirtualCopy((PVOID)pDeviceContext->spi, (PVOID)(S3C2440A_BASE_REG_PA_SPI>> 8), sizeof(S3C2440A_SPI_REG), PAGE_PHYSICAL | PAGE_READWRITE | PAGE_NOCACHE)) 		
		{
        	RETAILMSG(1, (L"ERROR: s3c_spi_init: "
           	 L"Failed VirtualCopy spi device structure\r\n" ));
    		goto error;
	    }
	 }
		
	//allocate DMA address
    pDeviceContext->dma = (volatile S3C2440A_DMA_REG *) VirtualAlloc(0,sizeof(S3C2440A_DMA_REG), MEM_RESERVE, PAGE_NOACCESS);
    if(pDeviceContext->dma == NULL)
	{
        RETAILMSG(1, (L"ERROR: s3c_spi_init: "
            L"Failed VirtualAlloc dma device structure\r\n" ));
		   goto error;
	 }
    else 
	{
	    if (!VirtualCopy((PVOID)pDeviceContext->dma, (PVOID)(S3C2440A_BASE_REG_PA_DMA>> 8), sizeof(S3C2440A_DMA_REG), PAGE_PHYSICAL | PAGE_READWRITE | PAGE_NOCACHE)) 		
		{
        	RETAILMSG(1, (L"ERROR: s3c_spi_init: "
           	 L"Failed VirtualCopy dma device structure\r\n" ));
    		goto error;
	    }
	 }

	//allocate clkpwr address
    pDeviceContext->clkpwr = (volatile S3C2440A_CLKPWR_REG *) VirtualAlloc(0,sizeof(S3C2440A_CLKPWR_REG), MEM_RESERVE, PAGE_NOACCESS);
    if(pDeviceContext->clkpwr == NULL)
	{
        RETAILMSG(1, (L"ERROR: s3c_spi_init: "
            L"Failed VirtualAlloc clkpwr device structure\r\n" ));
		   goto error;
	 }
    else 
	{
	    if (!VirtualCopy((PVOID)pDeviceContext->clkpwr, (PVOID)(S3C2440A_BASE_REG_PA_CLOCK_POWER>> 8), sizeof(S3C2440A_CLKPWR_REG), PAGE_PHYSICAL | PAGE_READWRITE | PAGE_NOCACHE)) 		
		{
        	RETAILMSG(1, (L"ERROR: s3c_spi_init: "
           	 L"Failed VirtualCopy clkpwr device structure\r\n" ));
    		goto error;
	    }
	 }
		
	return TRUE;
	
error:
	return FALSE;
	
}


void p_s3c_unmap_register(SPI_DEVICE_HANDLE hDevice)
{
	SPI_DEVICE_CONTEXT *pDeviceContext = SPIHANDLE_TO_DEVICE(hDevice);
	
	/*
	* Un Alloc register memory
	*/ 
	if( pDeviceContext->gpio)
   	{
        	VirtualFree((void*) pDeviceContext->gpio, 0, MEM_RELEASE);
        	 pDeviceContext->gpio = NULL;
    }
	
	if( pDeviceContext->spi)
   	{
        	VirtualFree((void*) pDeviceContext->spi, 0, MEM_RELEASE);
        	 pDeviceContext->spi = NULL;
    }

	if( pDeviceContext->dma)
   	{
        	VirtualFree((void*) pDeviceContext->dma, 0, MEM_RELEASE);
        	 pDeviceContext->dma = NULL;
    }

	if( pDeviceContext->clkpwr)
   	{
        	VirtualFree((void*) pDeviceContext->clkpwr, 0, MEM_RELEASE);
        	 pDeviceContext->clkpwr = NULL;
    }
	
}

void p_s3c_spi_read_short(SPI_DEVICE_HANDLE hDevice, unsigned short *pData)
{
	SPI_DEVICE_CONTEXT *pDeviceContext = SPIHANDLE_TO_DEVICE(hDevice);

	// wait while busy
	while((pDeviceContext->spi->SPSTA0 & (0x1<<0))==0);	
	// write 0xff to make a exchange
	pDeviceContext->spi->SPTDAT0 = (UINT8)0xFF;
	// wait while busy
   while((pDeviceContext->spi->SPSTA0 & (0x1<<0))==0);	
	// read byte
   *pData = (UINT8)(pDeviceContext->spi->SPRDAT0<<8);

	// wait while busy
	while((pDeviceContext->spi->SPSTA0 & (0x1<<0))==0);	
	// write 0xff to make a exchange
	pDeviceContext->spi->SPTDAT0 = (UINT8)0xFF;
	// wait while busy
   while((pDeviceContext->spi->SPSTA0 & (0x1<<0))==0);	
	// read byte
   *pData |= (UINT8)(pDeviceContext->spi->SPRDAT0);
	
}

void p_s3c_spi_write_short(SPI_DEVICE_HANDLE hDevice, unsigned short Data)
{
	SPI_DEVICE_CONTEXT *pDeviceContext = SPIHANDLE_TO_DEVICE(hDevice);

	// wait while busy
	while((pDeviceContext->spi->SPSTA0 & (0x1<<0))==0);	
	// write high byte	
	pDeviceContext->spi->SPTDAT0 = (UINT8)((Data & 0xFF00)>>8);
	// wait while busy
   while((pDeviceContext->spi->SPSTA0 & (0x1<<0))==0);	
 	// write low byte	
	pDeviceContext->spi->SPTDAT0 = (UINT8)(Data & 0x00FF);
	
}

void p_s3c_spi_exchange_short(SPI_DEVICE_HANDLE hDevice, unsigned short Data, unsigned short *pData)
{
	SPI_DEVICE_CONTEXT *pDeviceContext = SPIHANDLE_TO_DEVICE(hDevice);

	// wait while busy
	while((pDeviceContext->spi->SPSTA0 & (0x1<<0))==0);	
	// write high byte
	pDeviceContext->spi->SPTDAT0 = (UINT8)((Data & 0xFF00)>>8);
	// wait while busy
   while((pDeviceContext->spi->SPSTA0 & (0x1<<0))==0);	
	// read byte
   *pData = (UINT8)(pDeviceContext->spi->SPRDAT0<<8);

	// wait while busy
	while((pDeviceContext->spi->SPSTA0 & (0x1<<0))==0);	
	// write 0xff to make a exchange
	pDeviceContext->spi->SPTDAT0 = (UINT8)(Data & 0x00FF);
	// wait while busy
   while((pDeviceContext->spi->SPSTA0 & (0x1<<0))==0);	
	// read byte
   *pData |= (UINT8)(pDeviceContext->spi->SPRDAT0);
	
}

BOOL p_s3c_wlan_isr_init(SPI_DEVICE_HANDLE hDevice)
{
	SPI_DEVICE_CONTEXT *pDeviceContext = SPIHANDLE_TO_DEVICE(hDevice);
	DWORD irq;
	
	//creat wlan isr
	pDeviceContext->event_wlan = CreateEvent( NULL, FALSE, FALSE, NULL );
	if(!pDeviceContext->event_wlan)
	{
		RETAILMSG(1, (L"ERROR: p_s3c_wlan_isr_init: "
            L"Failed  creat event for wlan \r\n" ));
        goto error;	
	}

	pDeviceContext->event_wlan_exit = CreateEvent( NULL, FALSE, FALSE, NULL );
	if(!pDeviceContext->event_wlan_exit)
	{
		RETAILMSG(1, (L"ERROR: p_s3c_wlan_isr_init: "
            L"Failed  creat event for wlan thread exit \r\n" ));
        goto error;	
	}
	
    // Bind system interrupt to event
    irq = IRQ_WLAN;  
    if(!KernelIoControl(IOCTL_HAL_REQUEST_SYSINTR, &irq, sizeof(UINT32), &(pDeviceContext->sysintr_wlan), sizeof(UINT32), NULL))
    {
		RETAILMSG(1, (L"ERROR: p_s3c_wlan_isr_init: "
            L"Failed  request sysintr for wlan \r\n" ));
        goto error;	
    }
	RETAILMSG(1, (TEXT(" Mapped Irq 0x%x to SysIntr 0x%x.\r\n"), irq, pDeviceContext->sysintr_wlan));
	
    if(!InterruptInitialize(pDeviceContext->sysintr_wlan, pDeviceContext->event_wlan, NULL, 0))
    {
		RETAILMSG(1, (L"ERROR: p_s3c_wlan_isr_init: "
            L"Failed  bind sysintr for wlan event \r\n" ));
        goto error;	    
	}

	pDeviceContext->thread_wlan = CreateThread(NULL, 0, wlan_isr, hDevice, 0, NULL);
	if(pDeviceContext->thread_wlan == NULL)
	{
		RETAILMSG(1, (L"ERROR: p_s3c_isr_init: "
            L"Failed  creat thread for wlan \r\n" ));
        goto error;	   
	}

   //CeSetThreadPriority(pDeviceContext->thread_wlan, I2C_THREAD_PRIORITY);

	return TRUE;
	
error:
	
	return FALSE;
	
}


BOOL s3c_spi_init(SPI_DEVICE_HANDLE *phDevice)
{
    SPI_DEVICE_CONTEXT *pDeviceContext = NULL;
    SPI_DEVICE_HANDLE    hDevice;
	
	/*
     *  Get ourselves a device context.
     */
    pDeviceContext = (SPI_DEVICE_CONTEXT *)LocalAlloc(LPTR, sizeof(SPI_DEVICE_CONTEXT));
    if (pDeviceContext == NULL)
    {
        RETAILMSG(1, (L"ERROR: s3c_spi_init: "
            L"Failed allocate SPI_DEVICE_CONTEXT device structure\r\n" ));
        goto error;
    }
	memset(pDeviceContext,0,sizeof(SPI_DEVICE_CONTEXT)); 
	pDeviceContext->sysintr_wlan = SYSINTR_UNDEFINED;
	
	
    /*
     * Create our device handle.
     */    
    hDevice = SPIDEVICE_TO_HANDLE( pDeviceContext );

	/*
	* Map s3c register
	*/
	if(p_s3c_map_register( hDevice) == FALSE)
	{
        RETAILMSG(1, (L"ERROR: s3c_spi_init: "
            L"Failed  s3c_map_register \r\n" ));
        goto error;		
	}

	/*
	* map wlan intr 
	*/
	if(p_s3c_wlan_isr_init( hDevice) == FALSE)
	{
        RETAILMSG(1, (L"ERROR: s3c_spi_init: "

⌨️ 快捷键说明

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