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

📄 spi.h

📁 2410 spi 外接2515 can转换 驱动代码
💻 H
字号:
#ifndef _LINUX_SPI_H#define _LINUX_SPI_H#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

#include <linux/miscdevice.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/spinlock.h>
#include <linux/irq.h>
#include <linux/delay.h>
#include <linux/vmalloc.h>#include <linux/Def.h>#include <linux/2410.h>#include <linux/ioctl.h>#include <asm/hardware.h>
#include <asm/arch/cpu_s3c2410.h>

U8 SPI_WaitTxRxReady(void)
{
	int waitCount;
	//unsigned char status;
	//----- 1. Wait until the controller is ready to transfer ----- 
	waitCount = 1000;
	//status = rSPSTA0;
	while(!(SPSTA0 & 0x01))
	{
		if(--waitCount == 0)
		{			
			return FALSE;
		}
	}
	
	return TRUE;
	
}

void SPI_Init(void)
{
	int index;
	
	//1. IMPORTANT: By default, the internal clock is disabled.  To configure the controller 
	//					  we must first enable it.
	
	CLKCON |= 0x40000 ;  // Enable the CPU clock to the SPI controller
	
	SPPRE0 = 0x05;  //设置 baud rate= pclk/2/(sppre value + 1) pclk = 25.35M  baud rate = 3168750
	SPCON0 = 0x18;  // en-SCK,master
        SPPIN0 = 0x02;
    

	//----- 2. Configure the GPIO pins for SPI mode -----
	//
	//		   nSPICS3#  (chip select)		= GPC6
	//         nSPICS4#  (chip select)		= GPC7
	//		   SPICLK0  (SPI clock)			= GPE13
	//		   SPIMOSI0 (SPI output data)	= GPE12
	//         SPIMISO0 (SPI input data)	= GPE11
	
	GPECON |= (1<<27) | (1<<25) | (1<<23);
        GPECON &= 0xfabfffff;
    
	//----- 3. Configure the SPI controller with reasonable default values -----
	
	SPCON0 |= 0x18;  // en-SCK,master,high,A,normal
        SPCON0 &= 0x1f;  //polling mode
    
	for( index = 0; index < 20; index++)   
	
	SPTDAT0 =  0xFF;  //初始化MMC和SD卡

    //StopSPIClock();
}

U8 SPI_SendByte(U8 bData)
{
    //----- 0. Start clock
    //StartSPIClock();

	//----- 1. Chip select the slave device (active low) -----
	//rGPCDAT &= 0xffbf;  //选择3号can
	//rGPFDAT &= 0xfb;
    //rGPCDAT &= 0xff7f;  //选择4号can
    
	//----- 2. Wait until the controller is ready to transfer -----
	if(SPI_WaitTxRxReady()==FALSE) return FALSE;

	//----- 3. Put the byte out onto the SPI bus -----
	SPTDAT0 =  bData;
	
	//----- 4. Delay a little bit so the byte finishes clocking out before the chip select line is deasserted -----
	if(SPI_WaitTxRxReady()==FALSE) return FALSE;

	//*pData = rSPRDAT0;

	//----- 5. Deselect the slave device (active low) -----
	//rGPCDAT |= (1<<6);
	//rGPFDAT |= 0x04;

   // StopSPIClock();

	return TRUE;
}



U8 SPI_ReadByte(void)
{
    U8 pData=0;
    //int    bRet = TRUE;
    
    //----- 0. Start clock
    //StartSPIClock();
    //pData = 0x00;
	//----- 1. Chip select the slave device (active low) -----
	//GPCDAT &= 0xffbf;  //选择3号can
	//GPFDAT &= 0xfb;
    //GPCDAT &= 0xff7f;  //选择4号can
        SPTDAT0 = 0xff;  //在normal模式下,读数据前要写入数据0xff

	//----- 2. Wait until the controller is ready to transfer -----
	if(SPI_WaitTxRxReady()==FALSE) return FALSE;

	//3. Put the byte out onto the SPI bus
	
	pData = SPRDAT0;
	
	// 4. Delay a little bit so the byte finishes clocking out before the chip select line is deasserted 
	if(SPI_WaitTxRxReady()==FALSE)         return FALSE;

	//5. Deselect the slave device (active low) 
	//rGPCDAT |= (1<<6);
	//rGPFDAT |= 0x04;

   // StopSPIClock();
    
	return pData;	
}

#endif /* _LINUX_SPI_H */

⌨️ 快捷键说明

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