S3C2440 spi driver simple test

Publisher:HarmonyInLifeLatest update time:2024-07-09 Source: elecfansKeywords:S3C2440  test Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

In the past two days, I referred to the information on the Internet and wrote an SPI driver myself, which passed the actual test.


Hardware platform: mini2440 uses SPI1 of S3C2440 (there are 2 SPI modules in total)


Operating system: linux-2.6.32.2


Test method: Short-circuit the MISO and MOSI pins of the SPI, so that when reading data, the first dummy byte sent is the received byte.


Below is the source code of the driver (mini2440_spi.c):


/************************************************ **/

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

int loopChar=0x88;

module_param(loopChar,int,S_IRUGO);

static int spi_major = 55;

#define spi_name "mini2440_spi"

struct cdev spiCdev;

/************************************************ ****/

static int spi_open(struct inode *,struct file *);

static int spi_release(struct inode *,struct file *);

static ssize_t spi_write(struct file *filp,const char *buf,size_t count,loff_t *f_ops);

static ssize_t spi_read(struct file *filp,char *buf,size_t count,loff_t *f_ops);

static ssize_t spi_ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long data);

volatile int *spi_gpfcon=NULL;//GPF Part define

volatile int *spi_gpfdat=NULL;

volatile int *spi_gpfup=NULL;

volatile int *spi_gpgcon=NULL;//GPG Part define

volatile int *spi_gpgdat=NULL;

volatile int *spi_gpgup=NULL;

volatile int *s3c2440_clkcon;

volatile int *spi_spcon1;//SPI Part define

volatile int *spi_spsta1;

volatile int *spi_sppin1;

volatile int *spi_sppre1;

volatile int *spi_sptdat1;

volatile int *spi_sprdat1;

#define SPI_TXRX_READY (((*spi_spsta1)&0x1) == 0x1)

/************************************************ *********/

static const struct file_operations spi_fops =

{

.owner=THIS_MODULE,

.open=spi_open,

.read=spi_read,

.ioctl=spi_ioctl,

.release=spi_release,

.write=spi_write,

};

/************************************************ *******/

static int spi_open(struct inode *inode,struct file *filp)

{

filp->private_data =&spiCdev;

/**********************************************

*PCLK

************************************************/

/*control PCLK into spi block*/

*s3c2440_clkcon |= 0x40000;

printk("s3c2440_clkcon=%08Xn",*s3c2440_clkcon);

/**********************************************

*GPG PORTS

************************************************/

/*config SCK1,MOSI1,MISO1 = 11*/

*spi_gpgcon |= 0x0000FC00;

/*poll up MISO1 MOSI1,SCK1*/

*spi_gpgup &=0xFF1F;

*spi_gpgup |= 0x0060;

/**********************************************

*GPF PORTS

************************************************/

*spi_gpfcon &= 0xFCF3;

*spi_gpfcon |= 0x0108;

*spi_gpfup &= 0xED;

*spi_gpfdat |= 0x10;

/**********************************************

*SPI REGS

************************************************/

//SPI Baud Rate Prescaler Register,Baud Rate=PCLK/2/(Prescaler value+1)

*spi_sppre1=0x18; //freq = 1M

printk("spi_sppre1=%02Xn",*spi_sppre1);

//polling,en-sck,master,low,format A,nomal = 0 | TAGD = 1

*spi_spcon1=(0<<6)|(0<<5)|(1<<4)|(1<<3)|(0<<2)|(0<<1)|(0<<0 );

printk("spi_spcon1=%02Xn",*spi_spcon1);

//Multi Master error detect disable,reserved,rech=*spi_sprdat0;lease

*spi_sppin1=(0<<2)|(0<<0);

printk("spi_sppin1=%02Xn",*spi_sppin1);

return 0;

}

static int spi_release(struct inode *inode,struct file *filp)

{

//free irq

free_irq(IRQ_EINT1, NULL);

printk("<1>releasen");

return 0;

}

static void writeByte(const char c)

{

int j = 0;

*spi_sptdat1 = c;

for(j=0;j<0xFF;j++);

while(!SPI_TXRX_READY)

for(j=0;j<0xFF;j++);

}

static char readByte(void)

{

int j = 0;

char ch = 0;

*spi_sptdat1 = (char)loopChar;

for(j=0;j<0xFF;j++);

while(!SPI_TXRX_READY)

for(j=0;j<0xFF;j++);

ch=*spi_sprdat1;

return ch;

}

static ssize_t spi_read(struct file *filp,char __user *buf,size_t count,loff_t *f_ops)

{

//int len=0;

char ch;

printk("<1>spi read!n");

ch=readByte();

copy_to_user(buf,&ch,1);

return 1;

}

static ssize_t spi_write(struct file *filp,const char __user *buf,size_t count,loff_t *f_ops)

{

int i;

char *kbuf;

printk("<1>spi write!,count=%dn",count);

kbuf=kmalloc(count,GFP_KERNEL);

if(copy_from_user(kbuf,buf,count))

{

printk("no enough memory!n");

return -1;

}

for(i=0;i

{

writeByte(*kbuf);

printk("write 0x%02X!n",*kbuf);

kbuf++;

}

return count;

}

static ssize_t spi_ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long data)

{

return 0;

}

static int __init spi_init(void)

{

int result;

dev_t devno = MKDEV(spi_major, 0);

/**/

if (spi_major)

result = register_chrdev_region(devno, 1, spi_name);

else /**/

{

result = alloc_chrdev_region(&devno, 0, 1, spi_name);

spi_major = MAJOR(devno);

}

if (result < 0)

return result;

cdev_init(&spiCdev, &spi_fops);

spiCdev.owner = THIS_MODULE;

spiCdev.ops = &spi_fops;

if (cdev_add(&spiCdev, devno, 1))

printk(KERN_NOTICE "Error adding spi %d", 0);

s3c2440_clkcon = (int *)ioremap(0x4C00000c,3);

spi_gpgcon = (int *)ioremap (0x56000060,4);

spi_gpgdat = (int *)ioremap (0x56000064,2);

spi_gpgup = (int *)ioremap (0x56000068,2);

spi_gpfcon = (int *)ioremap (0x56000050,2);

spi_gpfdat = (int *)ioremap (0x56000054,1);

spi_gpfup = (int *)ioremap (0x56000058,1);

spi_spcon1 = (int *)ioremap(0x59000020,1);

spi_spsta1 = (int *)ioremap(0x59000024,1);

spi_sppin1 = (int *)ioremap(0x59000028,1);

spi_sppre1 = (int *)ioremap(0x5900002c,1);

spi_sptdat1 = (int *)ioremap(0x59000030,1);

spi_sprdat1 = (int *)ioremap(0x59000034,1);

printk("Init spi success!n");

return result;

}

static void __exit spi_exit(void)

{

cdev_del(&spiCdev);

unregister_chrdev_region(MKDEV(spi_major, 0), 1);

printk("<1>spi_exit!n");

}

module_init(spi_init);

module_exit(spi_exit);

MODULE_LICENSE("GPL");

MODULE_AUTHOR("nkzc");

MODULE_DESCRIPTION("SPI driver for S3C2440");

A few points to note:


1. At first, the void unregister_chrdev(unsigned int major, const char *name) function was used in the spi_exit() function to unregister the device. However, when the driver was insmoded again, it prompted "Device or resource busy". After changing to unregister_chrdev_region(), everything was normal. This shows that even if only one device is registered, register_chrdev_region() and unregister_chrdev_region() must be used together.


2. When defining register variables such as spi_spcon1, add the volatile keyword in front of it, so that each time the variable is accessed, the CPU will read the value from the actual memory instead of using the value in the register. Especially the spi_spsta1 variable, its lowest bit represents whether the spi is ready to send or receive. If there is no volatile, it may cause an infinite loop in the readByte() or writeByte() function.


3. The module_param() macro is used to pass parameters to the driver. Here, an int type loopChar parameter is defined. When loading the module, insmod mini2440_spi.ko loopChar=123 is used to set the value of loopChar.



Test program: spi_test.c


#include

#include

#include

#include

#include

#include

#include

int main(int argc, char **argv)

{

int fd;

int count=0;

char buf[]={0x11,0x22,0x33,0x44,0x55};

fd = open("/dev/mini2440_spi", O_RDWR);

if (fd < 0) {

perror("open device spi");

exit(1);

}

count=write(fd,buf,sizeof(buf)/sizeof(buf[0]));

read(fd,buf,1);

printf("read byte is: 0x%02Xn",buf[0]);

close(fd);

return 0;

}

This is a very simple program that calls the open, write, read, and close functions respectively. You can observe the output results to verify whether the driver is correct. The output of read is the value of loopChar.


Note: When opening, pay attention to the second parameter flag. Only when flag is O_RDWR, the corresponding spi_read and spi_write functions in the driver will be called.


Keywords:S3C2440  test Reference address:S3C2440 spi driver simple test

Previous article:linux-2.6.32 porting I2C-EEPROM driver on mini2440 development board
Next article:ARM9 S3C2440 Timer Interrupt

Recommended ReadingLatest update time:2024-11-16 09:45

ARM Learning S3C2440 bootloader code analysis (1)
;=========================================== ; NAME: OPTION.A ; DESC: Configuration options for .S files ; HISTORY: ; 02.28.2002: ver 0.0 ; 03.11.2003: ver 0.0    attached for 2440. ; jan E, 2004: ver0.03  modified for 2440A01. ;=========================================== ;This Option.inc file is mainly for setting th
[Microcontroller]
S3C2440 transplants the linux3.4.2 kernel and kernel tailoring
Why prune the kernel? Because the kernel partition of mtd is only 2M, and the actual kernel is 2.37MB, it needs to be cut to less than 2M (or the mtd partition value is modified) First cut the irrelevant CPU/board files in the kernel Through vi .config, and then search for 2440, as shown below: Then refer to the p
[Microcontroller]
S3C2440 transplants the linux3.4.2 kernel and kernel tailoring
S3C2440 development board practice (1): Burning program (pure Ubuntu environment)
      For the convenience of learning, I bought Mr. Wei's JZ2440 development board, because I have done projects related to STM32 and 51 single-chip microcomputers, and participated in three electronic design competitions. But I feel that I have only learned the basics (after learning embedded systems, I feel that I h
[Microcontroller]
S3c2440 bare metal realizes picture display
Function    LCD display Chinese characters, characters and pictures illustrate    Chinese characters, characters and pictures need to use the corresponding module software to get the corresponding c file, and then include it in the project Main code 1) Draw the background void Brush_ U32  c) {     int x,y ;     f
[Microcontroller]
S3c2440 bare metal realizes picture display
STM32 SPI2 read W25Q128 driver
//SPI1 reads and writes a byte //TxData: Bytes to be written //Return value: the bytes read u8 SPI2_ReadWriteByte(u8 TxData) {     while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET){}//Wait for the send area to be empty   SPI_I2S_SendData(SPI2, TxData); //Send a byte of data through peripheral SPIx   while
[Microcontroller]
PWM application in s3c2440
There are 5 16-bit timers in the s3c2440 chip, 4 of which (timer 0 to timer 3) have pulse width modulation function, so PWM function can be easily implemented with s3c2440. The following is a detailed introduction on how to implement PWM function. 1. PWM is output through pins TOUT0 to TOUT3, and these 4 pins
[Microcontroller]
Analysis of LCD driver of S3C2440 built-in Linux kernel
Let's first look at how applications operate the screen: Linux works in protected mode, so user-mode processes cannot use the interrupt calls provided in the graphics card BIOS to directly write to the screen like DOS does. Linux abstracts the FrameBuffer device for user-mode processes to write to the screen directl
[Microcontroller]
Design of network coordinator based on CC2420 radio frequency chip and S3C2440 chip
1 Overview There are various wireless communication methods. Compared with Bluetooth, Wi-Fi, and GSM mobile communication methods, the ZigBee method developed by the ZigBee Alliance has the advantages of low power consumption, reliable data transmission, good compatibility, low implementation cost, and convenient netw
[Microcontroller]
Design of network coordinator based on CC2420 radio frequency chip and S3C2440 chip
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号