Design and Implementation of USB Bidirectional Communication in Embedded Operating System

As a serial communication bus, USB adopts the master-slave communication mode. The slave device can only passively respond to the request from the master device, and cannot initiate the request actively. With the development of embedded system technology, the requirements for interactive operation are more and more urgent, and the use of USB two-way communication can solve the above problems well. This article introduces a method based on S1C33L11 chip using the synchronization mechanism of the embedded operating system to achieve two-way communication through the operation of circular queues and custom control packets. ?

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

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

2 hardware system

2.1 Introduction to S1C33L11 and its USB Block

S1C33L11 is EPSON's 32-bit high-speed, low-power, low-voltage MCU. He is based on C33 STD 32-bit RISC CPU, with powerful functions. In addition to general peripheral devices, there are LCD controller, Camera interface, JPEG encoding, USB1.1 function controller, MAC (SPI mode) interface, SmartMedia interface, and also includes 3 oscillation circuits and 2 phase-locked loops (PLL), built-in 16kB RAM, no ROM.

S1C33L11 has built-in full-speed mode supporting USB1.1 protocol. Support 4 transmission modes of control, block, synchronization and interrupt, support 4 general channels (Epr (r = a, b, c, d)) and one control channel (endpoint0), and provide 1 for each channel (endpoint) kB FIFO.

2.2S1C33L11DMT01 Development Board Introduction

The S1C33L11DMT01 development board uses the S1C33L11F00A1 chip as the core, external 2 MB RAM, 32 MB FLASH, and STN TFT dual-screen color LCD, etc. This hardware environment is suitable for the operation of various embedded operating systems and multimedia mobile phones, PDA and other products Development. ?

3USB two-way communication design and implementation

In this paper, USB two-way communication adopts USB block transmission as the basic transmission method [1]. He consists of USB initialization, USB interrupt processing, control transmission and block transmission [2]. In the realization of two-way communication, the specific communication mechanism is: the embedded application communicates with the OUT and IN FIFO in the USB hardware module through the read and write circular queue and semaphore status, and the USB lower computer and the upper computer (PC) read and write Communication is achieved by the host computer reading and writing the control packet, and finally the purpose of USB bidirectional communication is achieved through the combination of the circular queue, the semaphore, and the control packet.

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

(1) Circular queue

Use IN to transmit a circular queue, OUT to transmit a circular queue (hereinafter referred to as queue), each queue is dynamically allocated 32 kB. The OUT queue serves as the secondary buffer during OUT transmission, that is, the FIFO data during OUT transmission must be placed in the OUT queue before it can be read and written by the embedded operating system; the IN queue serves as the secondary buffer during IN transmission, that is, IN transmission The FIFO data at the time must come from the IN queue; the embedded operating system only reads and writes the secondary buffer, and the operating system manages the queue by using a semaphore notification mechanism.

(2) Control package

To achieve two-way communication, a control packet format is specified, and the read control packet is customized outside the USB protocol.

The control packet is fixed at 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 packet is sent by the host computer, the status word specifies three types: 0x4F: the host computer requests OUT transmission, 0x49: the host computer requests IN transmission, 0x52: the host computer requests to read the status of the lower computer; when the host computer receives the control packet At the time, there are 5 types of status words: 0x00: USB idle state, 0x01: the OUT loop queue of the lower computer is full (ie OUT timeout), 0x02: IN loop queue of the lower computer is empty (ie IN timeout), 0x04: OUT transmission is successful 0x08: IN transmission succeeded.

(3) The process of reading and writing USB in the application program of embedded operating system

Reading function: void ReadUSB (unsigned char * ReadBuffer, DWORD size) function:

Function: The embedded system application program reads the data of the host computer (PC) through the USB interface.

Parameter description: unsigned char * ReadBuffer stores the data pointer, DWORD size is the size of the data to be read (unit: B).

Implementation process: first determine whether the circular queue is empty, and if it is not empty, determine whether its own semaphore is available. If it is available, read one byte from the queue. Read OUT's FIFO function) sends out a semaphore to inform the BulkOutGet function queue to write data to the OUT circular queue at this time, and then re-judgment, and read data from the OUT circular queue byte by byte until the required data is read Size. When the circular queue is empty, first send a semaphore to inform the BulkOutGet function that it should write data to this queue, then reset its own semaphore, and then call the function that waits for the semaphore, and then read it until the semaphore arrives. If it times out, it sends a timeout notification to the embedded operating system, and at the same time sends a timeout signal to the upper computer (PC) by writing the timeout status (0x01) to the control packet.

Write function: void WriteUSB (unsigned char * Write Buffer, DWORD size) function:

Function: The embedded system application sends data to the host computer (PC) through the USB interface.

Parameter description: unsigned char * WriteBuffer Pointer for storing data, DWORD size is the size of the data to be written (unit: B).

Implementation process: First determine whether the circular queue is full. If it is not full, determine whether its own semaphore is available. If it is available, write a byte to the queue. After each byte is written, write to the BulkInDataSet in the USB task (write directly IN FIFO function) The function sends a semaphore to inform this function that it can read data from the IN circular queue at this time; then re-judgment to write data to the IN circular queue one by one in turn until the data of the required data size is written . When the circular queue is full, send a semaphore to inform the BulkInDataSet function that the data should be taken from the queue, then reset its own semaphore, then call the function that waits for the semaphore, and then write until the semaphore arrives. The embedded operating system issues a timeout notification and at the same time sends a timeout signal to the upper computer (PC) by writing a timeout status (0x02) to the control packet.

(4) USB block transfer function

The USB block transfer function is a function that directly interacts with the USB hardware, and they directly read the FIFO of the IN and OUT transfer channels. voi d BulkInDataSet (void): Its function is the IN transmission process, that is, read data from the IN circular queue and write data to the IN FIFO, and then deal with the embedded operating system semaphore accordingly.

void BulkOutDataGet (void) whose function is the OUT transmission process, that is, read data from the OUT FIFO and write data to the OUT circular queue, and then deal with the embedded operating system semaphore accordingly.

(5) USB task calling function of embedded operating system

void SystemInit (void): MCU initialization (initialization process of each control register and state of the microprocessor)

void USBInit (void): USB initialization (including the allocation of memory to the circular queue, etc.)

void USBThread (void): USB running body (USB work process to process USB interrupt mainly includes USB block transfer function, USB interrupt status analysis processing, etc.).

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

3.2 Host computer (PC) part

The USB function layer (USBD and HCD) is provided by Windows98, which is responsible for managing the communication between the USB device driver and the USB controller, loading and unloading the USB driver, etc. The specific method is to generate the host computer (PC) USB driver template [3] through DriverWorks software, process the corresponding read and write parts according to the situation of the lower computer, and finally implement the user mode application program by encapsulating the basic API functions ReadFile, WriteFile The isolation of the USB driver of the PC makes the use of USB by the PC application layer as convenient as the use of the serial port. It provides three interface functions for user-mode applications:

unsigned char Read (void * pReadBuffer, DWORD Size): read data from the lower computer

Parameter description: void * pBuffer: buffer for storing 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: The lower computer sends a soft timeout

0x08: read successfully

unsigned char Write (void * pWriteBuffer, DWORD Size): send data to the lower computer

Parameter description: void * pBuffer; buffer to store written data, DWORD Size; size of data to be written (number of bytes).

return value:

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

0x20: insufficient memory space

0x30: The requested data size is 0 B

0x01: Soft timeout for reading data from lower computer

0x04: Successfully sent

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

Each call of Read or Write function is divided into several read / write sending. The specific processing is: Set the number of data bytes to be read and written to XB. When X = 5B, divide it into X1 = 4 B and X2 = 1 B to send it twice (because the custom package is 5 B, in order to communicate with the custom control Packets are distinguished); when 5 B16 kB, the data in 16 kB 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

The USB bidirectional communication based on the embedded operating system based on the S1C33L11 chip strictly follows the USB1.1 protocol, making full use of the built-in functions of the S1C33L11 chip and the role of the embedded operating system. It has strong interaction and has nothing to do with the device in the embedded operating system. Sexual characteristics.

It's not only a LED Wall Clock, but a wall decoration for home.

As a professional and creative clock manufacturer who has great R & D alibity in new products, it's our lattest designed Wall Clock With Light for wall decor.

With both battery and electricity function, the clock hand is luminous at night, you'll easily read time at night, and enjoy this lighting wall decor at night.

For battery use, it will last about 4 months for 4pcs batteries, and remember to replace all th batteries in one time, do not mix new and old ones to keep this clock in good condition.

We'd suggest this Light Clock is great for home decoration, also for party decoration, office wall decor, and even for bar wall decor and coffee shop wall decor, enjoy a lighting wall decor with time showed.

Easy to hang on the wall, durable quality and easy match with home furniture and with different colors LED light on the clock hand for selection.

Enjoy your night with this gorgeours Light Decor while time passsing by...

Mirror Wall Clock

Mirrored Wall Clock,Mirror Clock,Mirror Clock Large,Mirror Clock With Lighted Hand

Guangzhou Huanyu Clocking Technologies Co., Ltd. , https://www.findclock.com