Implementation of I2C device control method based on ARM

Publisher:psi33Latest update time:2011-07-01 Keywords:ARM Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1 Introduction

The I2C bus is a two-wire serial bus developed by Philips. It is widely used to connect microcontrollers and peripheral devices because of its simplicity, high efficiency and low interconnection cost. AT91SAM7X256 is an industrial-grade chip based on ARM7 launched by Atmel in 2005. It is valued by developers in the embedded field for its small size, low power consumption, wide connection methods, rich processing resources and flexible control. This article introduces the use of the I2C controller TWI interface (two-wired interface) of AT91SAM7X256, and takes the I2C device E2PROM and calendar clock chip as examples to realize the reading and storage of time data by AT91SAM7X256. At the same time, in order to verify whether the reading and storage of time data are correct, the data in E2PROM is read to the memory for inspection using the online emulator J-LINK of AT91SAM7X256.

2 Hardware Design

2.1 Hardware module structure

The hardware module structure of the circuit is shown in Figure 1.

The TWI interface of AT91SAM7X256 consists of a clock line TWCK and a data line TWD, and the generated signal timing complies with the I2C bus specification; PCF8563 is an industrial-grade clock chip with I2C bus interface function launched by Philips; AT24C08 is a two-wire serial port E2PROM launched by Atmel that complies with the I2C specification. The TWCK and TWD of AT91SAM7X256 are connected to the SCL and SDA of the chip PCF8563 and AT24C08 respectively, and the CPU reads and stores the time data through the TWI interface. In order to ensure that the CPU does not conflict with the PCF8563 and AT24C08, this article connects the A2 pin of AT24C08 to a high level. Since the I2C bus is at a high level when idle, in order to realize the "wired AND" function, the devices connected to the bus are all open collectors, so the bus needs to be connected to an external pull-up resistor R. The TWI of AT91SAM7X256 has two working modes: master and slave. In this article, AT91SAM7X256 is the controller and should work in master mode.

Implementation of I2C device control method based on ARM

2.2 TWI interface of AT91SAM7X256

The TWD and TWCK pins of AT91SAM7X256 are multiplexed with the I/O pins of the device. At the same time, AT91SAM7X256 adopts a power-saving solution that controls functional units individually. The power management unit PMC controls whether the clocks of each functional unit are working. Therefore, to use the TWI interface, you need to first configure TWD and TWCK to the peripheral connection and open circuit state, and then configure PMC to make the TWI clock in working state.

The TWI interface can provide a transmission rate of up to 400 kb/s. In order to adapt the data transmission rate to different applications, the signal frequency of TWCK can be adjusted by configuring the control register TWI_CWEG of the clock pulse generator.

The signal timing generated by the TWI interface complies with the I2C bus specification. When reading/writing 1 byte of data, the master device needs to provide the slave device address, internal address, read/write control, and start and stop flags. In the process of sending and receiving data, the control register TWI_CR, master mode register TWI_MMR, internal address register TWI_IADR, status register TWI_SR, transmission holding register TWI_THR and reception holding register TWI_RHR are mainly used. The slave device address is set in TWI_MMR, and the internal address of the slave device is set in TWI_IADR; whether to send the start signal and stop signal is set in TWI_CR; NAK (no response), OVER (operation error), TXRDY (send ready), RXRDY (receive ready), TX-COMP (transmission complete) and other status bits are obtained by querying WI_SR.

The process of writing data includes: when TWI_THR writes data, the CPU generates a start signal to start the transmission, the data in TWI_THR is transmitted by TWD after parallel-to-serial conversion, and when the CPU receives the response signal from the slave device, the TXRDY of TWI_SR will automatically set to "1", indicating that the data has been written to the slave device. The process of reading data includes: after the CPU sends a start signal, if the RXRDY bit of TWI_SR is "1", it means that there is data waiting to be received in TWI_RHR. When the data in TWI_RHR is read out, RXRDY is automatically set to "0". When the reading/writing of data is completed, the CPU will generate a stop signal to end the transmission, and the TXCOMP of TWI_SR will automatically set to "1".

2.3 How to use the PCF8563 calendar clock chip

According to the I2C protocol, PCF8563 has a unique device address of 0A2H. This article focuses on the method of reading the hour, minute, and second data of PCF8563. The internal registers used here include the control/status register 1 (address 00H), the second register (address 02H), the minute register (address 03H), and the hour register (address 04H). Since the hour, minute, and second data are stored in the register in BCD format, the high bits of each time register are invalid. [page]

To make PCF8563 work in normal mode, it is necessary to set the control/status register 1 to 00H. At the same time, in order to store the correct time data, it is necessary to mask the invalid high bits in the read data. If you need to calibrate the time, you only need to write the hour, minute and second registers.

2.4 How to use AT24C08

AT24C08 is an E2PROM with a capacity of 8192 b (1024 B). AT24C08 is divided into 4 pages, each page has 256 bytes, so if you want to access a unit, you need 10 bits for addressing, of which the highest two bits are the page address and the lower 8 bits are the address within the page. The definition of the device address is shown in Figure 2, where P1P0 corresponds to the page address, and pin A2 can set two sets of device addresses for AT24C08. When A2 is low, the device addresses of the 4 pages are 0A0H, 0A2H, 0A4H, 0A6H respectively; when A2 is high; otherwise, they are 0A8H, 0AAH, 0ACH, 0AEH. Therefore, in order to avoid device address conflicts between AT24C08 and PCF8563, A2 needs to be connected to a high level.

Implementation of I2C device control method based on ARM

The write operation of AT24C08 supports two modes: "byte write" and "page write". In the "byte write" mode, the master device needs to provide a start signal, device address, internal address and stop signal for each byte written; the "page write" mode is to write data continuously, and the master device needs to provide a start flag, device address and internal address, and send a stop flag after all the data are written.

The read operation of AT24C08 supports three modes: "Current Address Read", "Random Read" and "Sequential Read". "Current Address Read" means reading 1 byte from the current internal address unit, so the master device only needs to provide the start signal, device address and stop signal; "Random Read" means reading 1 byte from any internal address unit, so the master device needs to provide a start signal, device address, write operation, device internal address and stop signal, set the internal address of the device, and then read the data in the "Current Address Read" mode; "Sequential Read" means reading multiple bytes continuously from the current address, so the master device needs to provide the start signal, device address, read operation, and send a stop signal after all the data are read.

分页

In order to quickly read and write data, this article uses page write to write data to AT24C08; and uses a combination of "random read" and "sequential read" to read AT24C08 data.

3 Software Design

3.1 Design of TWI initialization program

According to the functional characteristics of TWI, the initialization of TWI includes the following 4 steps:

(1) Configure the PIO controller so that the multiplexed pin drives the TWI signal;

(2) Configure PMC to put the TWI clock in working state;

(3) Configure TWI to work in master mode. In this paper, the CPU is the master device, and the calendar and storage chip are slave devices;

(4) Set the data transmission rate and configure the TWI clock waveform generator register.

3.2 Design of PCF8563 Driver

In order to control the working mode of PCF8563, it is necessary to write control words to it; in order to obtain the time information output by PCF8563, it is necessary to perform a read operation on it. The process of reading/writing data is shown in Figure 3.

This article designs and writes the following read and write functions:

Implementation of I2C device control method based on ARM

Among them, pTwi is a structure pointer, which points to a structure storing TWI registers. Each TWI register can be accessed through pTwi; address represents the device address; im_address represents the internal address of the device; data represents the variable pointer for reading and writing data.

Implementation of I2C device control method based on ARM

Therefore, if PCF8563 is operated in normal mode and the "time" data is read, the following code can be used:

Implementation of I2C device control method based on ARM

3.3 Design of AT24C08 Driver

Since AT24C08 consists of 4 pages with different device addresses and uses a continuous read and write data operation mode, the reading and writing of AT24C08 and PCF8563 have the following differences.

(1) First set the start flag of TWI_CR, then read/write the data of TWI interface through TWI_RHR and TWI_THR; before sending the last data, set the stop flag of TWI_CR.

(2) For reading and writing of multi-byte data, if all data have not been transmitted, the stop signal will not be sent. Therefore, it is necessary to determine whether to read TWI_RHR and write TWI_THR by judging TXRDY and RXRDY in the TWI_SR register, and whether the stop signal appears is used as the basis for judging whether to stop sending and receiving.

(3) Since the data volume and starting unit are random, it is possible that one page cannot be written. Therefore, it is necessary to calculate how many pages are required for a given data volume and starting unit parameters so that the device address can be changed when switching pages.

This article designs and writes the following read functions and write functions.

Implementation of I2C device control method based on ARM

Among them, nb is the number of bytes read/written, and data is the first pointer to the array variable storing the read/write data.

3.4 Software Debugging and Operation

This article uses IAR development environment and J-LINK simulator to debug and load the software online. Call the function to complete the following program design: first, read a number of data from PCF8563 and write them into AT24C08; second, read the data in AT24C08 into the array variable. Set a breakpoint after reading the AT24C08 data in the program, observe the data stored in the array, and verify the correctness of the driver.

4 Conclusion

This paper introduces the use of PCF8563 and AT24C08. By analyzing the TWI interface control method of AT91SAM7X256 based on ARM core, the driver programs of PCF8563 and AT24C08 are designed to realize the reading and storage of clock data.

The driver module designed in this paper has been successfully used in the intelligent coal mine substation experimental system to complete the recording function of historical time data. At the same time, this paper provides a reference method for ARM to control multiple I2C devices.

Keywords:ARM Reference address:Implementation of I2C device control method based on ARM

Previous article:Portable pipeline leak detector based on ARM core microprocessor
Next article:Research on Embedded Linux Application Development Based on ARM

Recommended ReadingLatest update time:2024-11-16 20:50

ARM's 7 working modes and their switching
1. The ARM system CPU has the following 7 working modes: 1. User mode (usr): normal program execution state 2. Fast interrupt mode (fiq): Process fast interrupts and support high-speed data transmission or channel processing 3. Interrupt mode (irq): handle normal interrupts 4. Management mode (svc): protection mode us
[Microcontroller]
ARM's 7 working modes and their switching
ARM-Linux S5PV210 UART driver (3) ----Serial port core layer, key structures, interface relationships
Although a specific UART device driver can be designed according to the design method of tty driver, that is, defining tty_driver and implementing the member functions of tty_operations, Linux has implemented a general tty driver layer for UART devices in the file serial_core.c, called the serial port core layer. In
[Microcontroller]
ARM-Linux S5PV210 UART driver (3) ----Serial port core layer, key structures, interface relationships
The beginning of learning ARM assembly instructions
Instruction set learning   1. ARM Instruction Set     1. Instruction format     2. Condition Code     3. ARM Memory access instructions     1) LDR/ STR - Load/Store Instructions     2) LDM/STM - Multiple register load/store instructions     3) SWP - Register and memory e
[Microcontroller]
Talk about the use of ARM NEON SIMD architecture extensions in Zynq SoC
Inside all Zynq All Programmable SoCs, you'll find a dual-core ARM Cortex-A9 MPCore processor, and both processors in the Zynq SoC feature the ARM NEON SIMD architecture extensions. So why would you want to use the ARM NEON SIMD extensions? Because you can get a significant performance boost for your software. You may
[Microcontroller]
Talk about the use of ARM NEON SIMD architecture extensions in Zynq SoC
ARM LCD and LCD controller
Since we mentioned LCD, the first thing we must understand is its types. CD (liquid crystal display) is a display that uses liquid crystal to control transmittance counting to achieve color. Compared with traditional CRT displays, it has many advantages: light and thin, low energy consumption, low radiation, etc., and
[Microcontroller]
ARM LCD and LCD controller
ARM CPU register organization
ARM CPU provides 16 general registers and one status register (CPSR) for user mode, and also provides a backup status register (SPSR) between different modes. For some conventions of software and hardware, the following three registers have different meanings, so please pay attention when using them: R13 is used as
[Microcontroller]
ARM CPU register organization
7 working modes of ARM system
1. The ARM system CPU has the following 7 working modes:   User mode (usr) Most programs run in user mode Privileged Mode   System mode (sys) runs privileged operating system tasks Abnormal Mode Interrupt mode (irq)         Fast interrupt mode (fiq) must process interrupt requests quick
[Microcontroller]
7 working modes of ARM system
Arm leads the software-defined car revolution and moves towards the future of the automotive industry
The automotive technology sector is at a critical inflection point, with the future relying on dynamic and adaptable systems that continuously enhance the driving experience through software. Today, a car has 650 million lines of code, compared to a Boeing 737, which has only 15 million lines of code. This num
[Automotive Electronics]
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号