I2C Driver Design Based on WinCE

Source: Internet
Author: User
It is important to connect the operating system with the corresponding hardware devices and write drivers that connect the hardware and software. This article mainly discusses how to design and implement the driver with I2C interface on the S3C2410 chip under the wince operating system. It introduces in detail the compiling method of the stream driver program under wince, at the same time, the driver is compiled into the operating system through platform builder, and finally provided to users through dynamic link library.

Keywords:Wince stream-driven I2C bus driver

Introduction

With the rapid development of information technology with computer technology, communication technology and software technology as the core, embedded systems have been widely used in various industries and have greatly promoted the penetration application of the industry. An embedded system is a "Application-centric, computer-based, software and hardware cropping, adapted to the functional, reliability, cost, volume, and power consumption requirements of the application system ", it consists of embedded hardware and embedded software. Embedded software includes embedded operating systems and embedded application software. Microsoft's desktop operating system is already familiar with and used by people. The Embedded Operating System Windows ce.net is also becoming increasingly popular. Windows ce.net is a compact, efficient, and scalable 32-bit embedded operating system launched by Microsoft. It mainly targets a variety of embedded systems and products. The multi-thread, multi-task, and fully preemptible features of the system are designed for various hardware systems with strict resource restrictions. In order to connect the operating system and hardware devices, it is very important to connect the drivers of hardware and software.

The following is an analysis of Samsung's arm9-kernel chip S3C2410. It introduces how to develop the underlying device driver in the Windows ce.net system and provides I2C communication instances.

1 I2C communication protocol and S3C2410 chip Introduction

I2C (Inter Integrated Circuit) bus was launched by Philips in 1980. The I2C bus transmits information between the bus and the device using two wires (SDA and SCL), serial communication between the microcontroller and external devices, or bidirectional data transmission between the master device and the slave device. The two passthrough wires are pulled up to + 5 V. Each Integrated Circuit in the control system can read each line through a CMOS Buffer, or the level of each line can be pulled down through a gate open-circuit FET tube. Therefore, for each chip, each line is both an input line and an output line.

The I2C bus follows the synchronous serial transmission protocol, that is, the serial transmission (one-bit, one-bit), and the clock (clock) line indicates the time of the data line. Each packet has an address before it to indicate which device receives the data.

Based on ARM920T, S3C2410 is a 16/32-bit Proteus microprocessor. It is mainly used for handheld devices and features high cost performance and low power consumption, it is also one of the most embedded processors on the market. The chip has a 16 KB instruction and data cache, and a storage management unit (MMU) LCD controller, three serial ports, four DMA channels, four clock timers, eight 10-bit A/D conversion; supports I2C, I2S, SPI, Master/Slave USB, and SD/mmccard interfaces.

The I2C bus of the S3C2410 microprocessor can be in the following four modes: Master receiving mode, Master sending mode, slave receiving mode, and slave sending mode. The operation of the processor on I2C is mainly to read/write the following registers:

◇ IIC control register, iiccon (physical address 0x54000000, virtual address after memory ing );
◇ IIC control/Status Register, iicstat (physical address 0x54000004 );
◇ IIC data register, iicds (physical address 0x54000008 );
◇ IIC address register, iicadd (physical address 0x5400000c ).

In this design, the CPU works in the master mode to communicate with the following I2C interface device.

2 Windows CE driver features

The Windows ce.net driver has two models: local device drivers and stream interface drivers. The local device driver is suitable for devices integrated into the windows ce.net platform. These device drivers are required by some hardware and are created by the manufacturer of the original device to drive devices, such as keyboards, touch screens, and audio devices. These drivers will not be replaced once sold, for example, general-purpose LED Drivers, power drivers, keyboard drivers, and display drivers are all driven by local devices. For the driver of the local device, platform Builder provides some driver samples to facilitate developers to quickly develop their own drivers. When the Win CE system is started, the driver of the local device will be loaded into the system memory. The development of local drivers can be divided into hierarchical drivers and single-chip drivers. The layered driver uses the upper layer of the program that Microsoft provides to communicate with the application. It is called the module driver layer MDD (Model Device Driver ). The MDD layer communicates with the application through the device driver interface DDI (Device Driver Interface). The development driver usually does not modify the MDD layer and focuses on the underlying layers related to specific hardware, depending on the platform's device driver PDD (platform dependent driver), the PDD layer directly manages hardware through the device driver service provider interface. A Streaming Interface Device Driver (an installable Startup Program) can be provided by a third-party manufacturer to support devices added to the system. The device drivers in Windows CE work at the same protection level as the applications. When the system starts, most drivers are loaded by the device management process (device. EXE). All these drivers share the same process address space.

3 I2C bus driver design

The I2C bus driver is a real driver that is located at the OEM adaptation layer (oal) layer on the underlying Windows CE operating system kernel.

3.1 initialize I2C interrupt and write ISR routine

I2C communication is performed by operating I2C registers. In I2C communication, the four registers described above are read and written. The command status characters in these registers can be read and written to detect and control the behavior of the I2C bus. In Windows ce.net, first add the I2C interrupt number macro definition in the file oalintr. h:

# Definesysintr_i2c (sysintr_firmware + 19)

Then, add the I2C interrupt initialization, disable, and reset to the file CFW. C. The Code is as follows:

In the oeminterruptenable function, add
Case sysintr_iic:
S2410int-> rsrcpnd = bit_iic;
If (s2410int-> rintpnd & bit_iic) s2410int-> rintpnd = bit_iic;
S2410int-> rintmsk & = ~ Bit_iic;
Break;
In the oeminterruptdisable function, add
Case sysintr_iic:
S2410int-> rintmsk = bit_iic;
Break;
Add the ISR program to the armint. c file to return the defined interrupt number after the interrupt occurs. The Code is as follows:
In the oeminterrupthandler function, add
Else if (intpendval = intsrc_iic ){
S2410int-> rsrcpnd = bit_iic;/* clear interrupt */
If (s2410int-> rintpnd & bit_iic) s2410int-> rintpnd = bit_iic;
S2410int-> rintmsk = bit_iic;/* I2C interrupt prohibited */
Return (sysintr_rtc_alarm );
}

3.2 write a stream driver

The I2C bus driver adopts the standard form of Win CE stream driver. In the iic_init function, the virtualalloc () and virtualcopy () functions are used to associate the physical address of I2C in the chip with the virtual storage space of the operating system, the operation on the virtual address space is equivalent to the operation on the physical address of the chip. The address ing code is as follows:

Reg = (pvoid) virtualalloc (0, SZ, mem_reserve, page_noaccess );
If (REG ){
If (! Virtualcopy (Reg, ADDR, SZ, page_readwrite page_nocache )){
Retailmsg (debugmode, (text ("Initializing interrupt // N // R ")));
Virtualfree (Reg, SZ, mem_release );
Reg = NULL;
}
}

Here, SZ is the length of the application, and ADDR is the ing address of the actual physical address applied for the virtual address space in Win CE.
Then, perform operations on the Applied virtual address, install the streaming Driver Model in windows, and write the following functions.

Iic_init ()
In the function, it mainly initializes I2C. The main statement is as follows:
V_piicregs = (volatile iicreg *) iic_regalloc (pvoid) iic_base, sizeof (iicreg ));
V_piopregs = (volatile iopreg *) iop_regalloc (pvoid) iop_base, sizeof (iopreg ));
V_piopregs-> rgpeup = 0xc000;
V_piopregs-> rgpecon = 0xa00000;
V_piicregs-> riiccon = (1 <7) (0 <6) (1 <5) (0xf );
V_piicregs-> riicadd = 0x10;
V_piicregs-> riicstat = 0x10;
Virtualfree (pvoid) v_piopregs, sizeof (iopreg), mem_release );
V_piopregs = NULL;
If (! Startdispatchthread (piichead ))
{Iic_deinit (piichead); Return (null);} in the startdispatchthread () function, the thread is created, associated events, and interruptions. The main statement is as follows:
Interruptinitialize (36, piichead-> hiicevent, null, 0); // correlation time and interruption
Createthread (null, 0, iicdispatchthread, piichead, 0, null); // creation thread wait time
In the iicdispatchthread () function, wait for the interruption to be generated and run: waitreturn = waitforsingleobject (piichead-> hiicevent, infinite );
Iiceventhandler (piichead); // event processing function
Interruptdone (36 );
Finally, in the iic_open, iic_read, and iic_write functions, perform operations on each register and assign values to the data to obtain the data read and sent by I2C.

4 I2C driver encapsulation and addition to Windows CE

Through the above work, You can compile a DLL function, but this cannot be called a stream interface driver. Because its interface functions have not been exported, you also need to tell the linked program what function to output. to create your own def file, you can use NotePad to create one named mydrive. Def:

Library mydriver
Exports
Iic_close
Iic_deinit
Iic_init
Iic_iocontrol
Iic_open
Iic_powerdown
Iic_powerup
Iic_read
Iic_seek
Iic_write

Then, use NotePad to write a registry file named mydrive. Reg:

[HKEY_LOCAL_MACHINE/Drivers/builtin/strings]
"Index" = DWORD: 1
"Prefix" = "IIC"
"DLL" = "mydriver. dll"
"Order" = DWORD: 0

Finally, write your own CEC file. The main task is to add a build method. The task is to copy the registry to the Windows CE system directory. Add a bib file. The main function is to add the compiled mydrive. dll file to the system kernel. Save the prepared CEC file. Open Platform builder, open the "file" menu, and add the prepared CEC features to the system options. When the system is generated, you can add your own CEC feature to include the compiled I2C drive.
The above introduces the driver structure of Win CE, and provides some source code of Win CE-based I2C driver. Experiments show that the design is feasible.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.