Implementation of USB bidirectional communication

2022-08-18
  • Detail

Implementation of USB bidirectional communication in embedded operating system

Abstract: This paper introduces the design and implementation of USB device firmware program, driver program and application program based on s1c33l11 chip, which uses the synchronization mechanism of embedded operating system to realize USB bidirectional communication through the operation of circular queue and user-defined control package

key words: USB bidirectional communication; S1C33L11; Embedded operating system; Circular queue

design and implementation of USB bidirectional C communication

in the embedded operating system

deng binwei1, he huiqin2, ZHU Shaowen1

(stitute of Electronic and Computer, Central China Normal University,

Wu Han, 430079,China;

partment of Electronic and Information Engineering, Institute of physics,

Ce ntral China Normal University,430079,China)

Abstract:Based on the S1C33L11 chip, Discussions creates an unparalleled appearance ed the realizati on of USB bidirectional communication by synchronization mechanism, circular queue and userdefined control packet in the embedded operating system ・ and introduced their hardware program, driver program, application and implementation

keywords:usb bidirectional communication; S1C33L11; embedded operating system; As a serial communication bus, circular queue

USB is widely used in the communication between embedded system and PC. Because USB bus is a master-slave structure, the master-slave communication mode is usually adopted. The disadvantage of this method is that the slave device can only passively respond to the request from the master device, and cannot actively initiate the request, so it is limited in interactivity. However, with the development of embedded system technology, the requirements for interactive operation are becoming more and more urgent, and the use of USB two-way communication can well solve the above problems. This paper introduces a method of two-way communication based on s1c33l11 chip by using the synchronization mechanism of embedded operating system through the operation of circular queue and user-defined control packet

1 the overall hierarchical structure of USB two-way communication system in embedded operating system

the overall hierarchical structure of USB two-way communication system in embedded operating system is shown in Figure 1

2 hardware system

2.1 introduction to s1c33l11 and its USB block

s1c33l11 is a 32-bit high-speed, low-power, low-voltage MCU of Epson company. With C33 STD 32-bit RISC CPU as the core, it has powerful functions. In addition to general peripheral devices, it has LCD controller, camera interface, JPEG coding, USB1.1 function controller, MAC (SPI mode) interface, SmartMedia interface, as well as 3 oscillation circuits and 2 phase-locked loops (PLLs), with 16KB RAM built-in and no ROM

s1c33l11 has built-in full speed mode supporting USB1.1 protocol. It supports four transmission modes: control, block, synchronization and interrupt, supports four general channels (EPR (r=a, B, C, d)) and one control channel (endpoint0), and provides 1 KB FIFO for each channel (endpoint)

2.2 introduction to s1c33l11dmt01 development board

s1c33l11dmt01 development board uses s1c33l11f00a1 chip as the core, external 2 Mb ram, 32 MB flash, and STN TFT dual screen color LCD. This hardware environment is suitable for the operation of various embedded operating systems and the development of multimedia, PDA and other products

3 design and implementation of USB bidirectional communication

in this paper, USB bidirectional communication adopts USB block transmission in the basic transmission mode [1]. It consists of USB initialization, USB interrupt processing, control transmission and block transmission [2]. In terms of realizing two-way communication, the specific communication mechanism is: the embedded application communicates with the out and in FIFO in the USB hardware module through reading and writing circular queue and semaphore status, while the reading and writing communication between the USB lower computer and the upper computer (PC) is realized through the upper computer reading and writing the control package, and finally achieves the purpose of USB two-way communication through the combination of circular queue, semaphore and control package

3.1 design and implementation of USB two-way communication firmware program

(1) circular queue

uses in to transmit a circular queue, out to transmit a circular queue (hereinafter referred to as queue), and each queue is dynamically allocated 32 KB. The out queue is used as the secondary buffer during out transmission, that is, the FIFO data during out transmission must be put into the out queue before it can be read and written by the embedded operating system; The in queue is used as the secondary buffer during in transmission, that is, the FIFO data during in transmission must come from the in queue; The embedded operating system only reads and writes the secondary buffer, and the operating system uses the semaphore notification mechanism to manage the queue

(2) control package

in order to realize two-way communication, a control package format is specified. The reading of the control package is customized with a gas output of 220ml/g outside the USB protocol

the control packet is fixed to 5 bytes. The first byte from left to right is the status word, and the remaining 4 bytes transmit the number of data bytes to be sent and received. When the control package is sent by the upper computer, there are three kinds of status words: 0x4f: the upper computer requests out transmission, 0x49: the upper computer requests in transmission, 0x52: the upper computer requests to read the status of the lower computer; When the upper computer receives the control packet, the status word specifies five types: 0 X00: USB idle state, 0x01: the out cycle queue of the lower computer is full (i.e. out timeout), 0x02: the in cycle queue of the lower computer is empty (i.e. in timeout), 0x04: out transmission is successful, 0x08: in transmission is successful

(3) embedded operating system end application reading and writing USB process

read function: void readusb (unsigned char * readbuffer, DWORD size) function:

function: embedded system application reads the data of upper computer (PC) through USB interface

Parameter Description: unsigned char*readbuffer pointer to store data, DWORD size is the size of the data to be read (unit: b)

implementation process: first, judge whether the circular queue is empty. If it is not empty, judge whether its semaphore is available. If it is available, read a byte from the queue. After each byte is read, send a semaphore to the bulkoutget function (FIFO function that directly reads out) in the USB task to inform the bulkoutget function queue that data can be written to the out circular queue at this time, and then judge again, Read data from the out loop queue byte by byte until the required data size is read. When the cyclic queue is empty, first send a semaphore to inform bulkoutget function that it should write data to the queue, then reset its own semaphore, and then call the function waiting for the semaphore until the semaphore arrives. If timeout occurs, a timeout notification is sent to the embedded operating system, and a timeout signal is sent to the upper computer (PC) by writing the timeout status (0x01) to the control package

write function: void writeusb (unsigned char*write buffer, DWORD size) function:

function: embedded system application sends data to upper computer (PC) through USB interface

Parameter Description: unsigned char * writebuffer pointer to store data, DWORD size is the size of the data to be written (unit: b)

implementation process: first, judge whether the cyclic queue is full. If it is not full, judge whether its semaphore is available. If it is available, write a byte to the queue. After each byte is written, send a semaphore to the bulkindataset (FIFO function that writes in directly) function in the USB task to inform this function that data can be read from the in cyclic queue at this time; Then re judge to write data to the in circular queue byte by byte until the data with the required data size is written. When the cyclic queue is full, first send a semaphore to notify the bulkindataset function to take the data from the queue, then reset its own semaphore, then call the function waiting for the semaphore, and then write it until the semaphore arrives. If it times out, send a timeout notification to the embedded operating system, and Send a timeout signal to the upper computer (PC) by writing the timeout status (0x02) to the control package

(4) USB block transfer function

usb block transfer function is a function that directly deals with USB hardware. They directly read the FIFO of in and out transmission channels. Voi D bulkindataset (void): its function is the in transmission process, that is, reading data from the in circular queue and writing data to the in FIFO, and then processing the semaphore of the embedded operating system accordingly

void bulkoutdataget (void) its function is the out transmission process, that is, read data from the out FIFO and write data to the out circular queue, and then process the semaphore of the embedded operating system accordingly

(5) embedded operating system USB task call function

void systemin under the squeeze of high debt and low profit, it (void): MCU initialization (microprocessor control registers and state initialization process)

void usbinit (low VO recycling rate ID): USB initialization (including allocating memory to circular queues, etc.)

void usbthread (void): USB running body (USB interrupt processing in USB working process mainly includes USB block transfer function, USB interrupt status analysis and processing, etc.)

void freeusb (void): close USB and release the memory occupied by the circular queue allocated by malloc function

3.2 the upper computer (PC) part

usb function layer (usbd and HCD) is provided by Windows98, which is responsible for managing the communication between USB device driver and USB controller, loading and unloading USB driver, etc. The specific method is to generate the USB driver template of the upper computer (PC) through DriverWorks software [3], process the corresponding read-write part according to the situation of the lower computer, and finally realize the isolation between user mode applications and PC USB drivers by encapsulating the basic API functions readfile and WriteFile, so that the use of USB in the application layer of PC is as convenient as the use of serial port, Three interface functions are provided for user mode applications:

unsigned char read (void *pradbuffer, DWORD size): read data from the lower computer

Parameter Description: void *pbuffer: store the buffer of read data, DWORD size: size of data to be read (number of bytes)

return value:

0x10: driver error (refers to windows USB driver error)

0x20: insufficient memory space

0x30: the requested data size is 0 b

0x02: lower computer sending soft timeout

0x08: reading success

unsigned char write (void *pwritebuffer, DWORD size): send data to lower computer

Parameter Description: void *pbuffer; Buffer for storing written data, DWORD size; The size (in bytes) of data to be written

return value:

0x10: USB driver error (Windows USB driver error)

0x20: insufficient memory space

0x30: the requested data size is 0 b

0x01: the lower computer reads the data soft timeout

0x04: send successfully

void requestusb (void * prequestbuffer, DWORD size=5): read the operation status returned by the lower computer

Parameter Description: void *prequestbuffer:5 B control packet buffer

in which each call of read or write function is divided into several read/write sends. The specific processing is: set the number of data bytes to be read and written as X B. when x=5b, it is divided into x1=4 B and x2=1 B and sent twice (because the user-defined package is 5b, in order to distinguish it from the user-defined control package); When 5 B16 KB, the data in 16kb is divided and sent, and the part less than 16 KB is sent again. Each read/write transmission is divided into three stages: sending control packets, reading/writing data, and reading control packet status

4 conclusion

USB bidirectional communication based on s1c33l11 chip and embedded operating system

Copyright © 2011 JIN SHI