SPI driver code in S3C2410 + LINUX, open source to the end

Source: Internet
Author: User

Static int spi_open (struct inode * inode, struct file * file)
{
Unsigned long temp1, temp2, temp4;
Int I;
// Printk ("sppre = % lD, spcon = % lD, sppin = % LD \ n", rsppre0, rspcon0, rsppin0 );
// Printk ("clkcon = 0x % x \ n", rclkcon); // For debug
Temp4 = _ raw_readl (s3c2410_gpeup );
// Temp4 & = ~ (0x3800 );
// Temp4 | = 0x2000;
Temp4 | = 0x1800;
_ Raw_writel (temp4, s3c2410_gpeup );
Printk ("s3c2410_gpeup value 0x % x \ n" ,__ raw_readl (s3c2410_gpeup); // For debug
Temp1 = _ raw_readl (s3c2410_gpecon );
Temp1 & = 0xf03fffff;
Temp1 | = 0xa800000;
_ Raw_writel (temp1, s3c2410_gpecon );
Printk ("s3c2410_gpecon value 0x % x \ n" ,__ raw_readl (s3c2410_gpecon); // For debug
/*
Temp3 = _ raw_readl (s3c2410_gpgup );
Temp3 | = 0x04;
_ Raw_writel (temp3, s3c2410_gpgup); // spi0 CS0 poll enable
*/
Printk ("s3c2410_gpgup value 0x % x \ n" ,__ raw_readl (s3c2410_gpgup); // For debug
Temp2 = _ raw_readl (s3c2410_gpgcon );
Temp2 & = 0 xfffffdcf;
Temp2 | = 0x50;
_ Raw_writel (temp2, s3c2410_gpgcon); // For spi0 CS0

Printk ("s3c2410_gpgcon value 0x % x \ n" ,__ raw_readl (s3c2410_gpgcon); // For debug
Rclkcon | = 0x40000; // key. You must first set pclk to SPI clock; otherwise, the SPI-related registers cannot be set !!!
Rsppre0 = 0x01; // you can specify the SPI baud rate.
Rspcon0 = 0x19; // set to query mode
Rsppin0 = 0x02;
For (I = 0; I <10; I ++)
{
Rsptdat0 = 0xff;
} // Initialize the device
Return 0;
}
Static int spi_release (struct inode * inode, struct file * file)
{
Return 0;
}
Static ssize_t spi_read (struct file * file, char _ User * buffer, Long Count, loff_t * PPOs)
{
Int I, J;
Unsigned long csval, temp1;
Unsigned char * kbuf = kmalloc (count, gfp_kernel), * PTR;
If (! Kbuf)
Return-enomem;

If (copy_from_user (kbuf, buffer, count ))
Return-efault;
PTR = kbuf;

Csval = _ raw_readl (s3c2410_gpgdat); // set Cs Low
Temp1 = csval & 0 xfffffffb;
_ Raw_writel (temp1, s3c2410_gpgdat );
// Printk ("s3c2410_gpgdat value 0x % x \ n" ,__ raw_readl (s3c2410_gpgdat); // For debug

For (I = 0; I <4; I ++)
{
// Printk ("TX times: 0x % x, txdata: 0x % 2x \ n", I, PTR );
If (rspsta0 & 0x01 ))
Rsptdat0 = PTR;
For (j = 0; j <50; j ++ );
}

For (I = 3; I <count + 1; I ++) // the first character received is 0xff, Which is manually removed.
{
// Rsptdat0 = 0xff;
If (rspsta0 & 0x01 ))//{
PTR = rsprdat0;

For (j = 0; j <50; j ++ );
// Printk ("RX times: 0x % x, rxdata: 0x % 2x \ n", I, PTR );}
}

Csval = _ raw_readl (s3c2410_gpgdat); // set CS high
Temp1 = csval | 0x04;
_ Raw_writel (temp1, s3c2410_gpgdat );
// Printk ("s3c2410_gpgdat value 0x % x \ n" ,__ raw_readl (s3c2410_gpgdat); // For debug

Copy_to_user (buffer, kbuf, count );
Kfree (kbuf );
Return count;
}
Static ssize_t spi_write (struct file * file, const char _ User * buffer, Long Count, loff_t * PPOs)
{
Int I, J;
Unsigned long csval, temp1;
Unsigned char * kbuf = kmalloc (count, gfp_kernel), * PTR;
If (! Kbuf)
Return-enomem;
If (copy_from_user (kbuf, buffer, count ))
Return-efault;
PTR = kbuf;

Csval = _ raw_readl (s3c2410_gpgdat); // set Cs Low
Temp1 = csval & 0 xfffffffb;
_ Raw_writel (temp1, s3c2410_gpgdat );
// Printk ("s3c2410_gpgdat value 0x % x \ n" ,__ raw_readl (s3c2410_gpgdat); // For debug

For (I = 0; I <count; I ++)
{
// Printk ("txdata: 0x % 2x \ n", PTR );
If (rspsta0 & 0x01 ))
Rsptdat0 = PTR;

For (j = 0; j <50; j ++ );

}
// Printk ("txdata: 0x % 02x 0x % 02x 0x % 02x 0x % 02x \ n", PTR [0], PTR [1], PTR [2], PTR [3]);
// Printk ("/*******************/\ n ");
// Printk ("SPI write debug \ n ");
Csval = _ raw_readl (s3c2410_gpgdat); // set CS high
Temp1 = csval | 0x04;
_ Raw_writel (temp1, s3c2410_gpgdat );
// Printk ("s3c2410_gpgdat value 0x % x \ n" ,__ raw_readl (s3c2410_gpgdat); // For debug

Kfree (kbuf );
Return count;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.