/*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
Previous article:STM32-IWDG watchdog
Next article:ARM11 learning based on S3C6410 (I) Basic knowledge
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Sandia Labs develops battery failure early warning technology to detect battery failures faster
- The Pinouts Book: A Free eBook Every Electronics Engineer Should Have
- Please tell me the principle of this filter circuit
- Answer the questions to win prizes! ADI Application Tour - Battery Management and Smart Energy Storage
- Huawei HR complained in real name: R&D brother worked overtime for 160 hours a month but was said to be not working hard
- LPS22HH has problems setting Autozero
- [Analysis of the topic of the college electronic competition] - 2015 National Competition Topic E "80MHz~100MHz Spectrum Analyzer"
- EEWORLD University - Teach you how to learn STM32-M4 series video
- "Playboard" + STM32F030Disco drives LCD5110 display
- [SC8905 EVM Review] + Unboxing and Power-on Test
- 【TI mmWave Radar Evaluation】+mmWave_Demo_Visualizer Usage