Implementation of serial communication under Linux based on arm11 processor S3C6410 kernel

Publisher:sclibinLatest update time:2018-12-18 Source: eefocusKeywords:arm11  S3C6410  kernel  linux Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/*Purpose of routine: By loading independent key module and serial communication module into the kernel and writing application program, when the key is pressed, arm can send data to the outside through serial port. The purpose of serial communication between arm and other devices has been achieved*/


1. Basic knowledge


1. Basic knowledge of serial port: Serial port: a very common device communication protocol on computers. Serial port communication: The serial port sends and receives bytes bit by bit.


2. Device driver: There are three types of devices in Linux: character device, block device, and network device. Here, the serial port and keyboard are both character devices. These devices are in the /dev directory. You can use ls -l to view the device type. [crw-rw---- 1 root dialout 4, 64 July 4 02:53 ttyS0] The first c indicates that it is a (char) character device.


2. Programming


1. Write the driver:


The serial port device driver is usually loaded in Linux and ARM. You can check the device name in /dev. The serial port name is usually ttys0-ttysn. Since this example also has an independent matrix keyboard driver, this needs to be loaded into the ARM kernel. Steps: (1) Find the matrix keyboard driver on the Internet or through other channels, then port it to your platform, and then write the makefile file. Here I once encountered a problem that the kernel specified directory was incorrect. At this time, you cannot compile it under Linux. Modify this path KERNELDIR := /SRC/kernel/linux-2.6.21_magic. (2) Make a .ko file to generate a loadable kernel module. Here I once encountered an error that the multi-threaded library could not be linked. The solution I took was to directly enter the command arm-linux-gcc keys.c -o key -lpthread. to dynamically add the library.


Keyboard driver download address


2. Write the application


Serial communication Open the serial port: fd = open(COM2, O_RDWR ); Close the serial port: ret = close(fd); Open the independent keyboard fs = open("/de/KEYS0",0)


Write to the serial port: write(fd,&c,n); represents the file descriptor, the data buffer storing the written data, and the number of bytes of written data. Read the serial port: read(fd,&c,n);


Reading the matrix keyboard returns 4>4 The keyboard code ret=read(fs,&keys_value,sizeof(keys_value)); I don’t know if the readers have realized it after reading this. The device operations under Linux are all based on files, so there are many similarities.


The following is the sending module of my program:


void* send(void * data)

{


        //int fd=-1;

        int ret,i;

        unsigned int keys_value=0;

       int c=0x01;

       printf("send data\n");

        while(STOP==FALSE)

       {

        fs = open("/dev/KEYS0",0);//open the button

        if(fd<0)

        {

                printf("open /dev/KEYS error!\n");

                //return -1;

        }


        for(;;)

        {

                ret=read(fs,&keys_value,sizeof(keys_value));

                if(ret<0)

                {

                        printf("read err!\n");

                        continue;

                }


               //write(fd,&c,1);

             //  while(STOP==FALSE)

             // {

                   switch(keys_value)

                {

                      case 0xEE://write(fd,&c,1);//Send hexadecimal 1 after button 1 is pressed

                                  write(fd,&c,1);

                                  usleep(100000);

                                  printf("S1 PRESS ,Send 1\n");

                                //  STOP=TRUE;


                                  break;

                      case 0xDE: c++; // Send hexadecimal 2 after button 2 is pressed

                                // write(fd,&c,1);

                                 printf("S2 PRESS,send 2\n");

                               // while(STOP==FALSE)

                               // STOP=TRUE;

                                 printf("send success\n");

                                      write(fd,&c,1);

                                  usleep(100000);

                                    break;


                  }

                keys_value=0;

                c=0x01 ;

}

}

}


Note: When conducting serial communication experiments, many people like to use the global variable fd to operate. There are two devices here, so if a global variable is set, the system will not know which device is being operated. This is a big mistake I made when conducting this experiment, so use global variables with caution.


3. Transplant the program to run on the arm platform


1. Establish nfs server, or other host machine to communicate with arm, copy the relevant files of this experiment to a readable and writable folder (such as yaffs)


2. Load the driver module into the kernel: insmod /yaffs/keys.ko; then create the device connection number mknod /dev/keys c 252 0


3. Execute the executable application ./buttonserial_test



Keywords:arm11  S3C6410  kernel  linux Reference address:Implementation of serial communication under Linux based on arm11 processor S3C6410 kernel

Previous article:STM32-IWDG watchdog
Next article:ARM11 learning based on S3C6410 (I) Basic knowledge

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号