STM32F10x_Hardware I2C read and write EEPROM (standard peripheral library version)

Publisher:快乐旅途Latest update time:2020-01-09 Source: eefocusKeywords:STM32F10x Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Ⅰ. Write in front


The previous article is "STM32F10x_Simulating I2C to Read and Write EEPROM", which describes the process of using IO ports to simulate I2C bus communication and read and write EEPROM (AT24Xxx).


The main content of the previous article: I2C protocol, simulation of I2C underlying driver, EEPROM (AT24Xxx) single-byte read and write operations.


The main contents of this article: STM32 hardware I2C detailed configuration, EEPROM (AT24Xxx) multi-byte read and write operations, and problems with ST official I2C.


Example experimental results:


1. Multi-byte read and write: any address (66), write any length (129), read and print


2. Single-byte read and write: write 1 byte of data to any address (0), read and print it out

Experimental description:


1. Multi-byte reading and writing


Why does the experiment write from address 66? Why does it write 129 bytes?


Answer: Verify the accuracy of reading and writing multi-byte "non-standard address and length" of EEPROM.


I am using AT24C128 chip, the page size is 64 bytes, I start reading and writing from address 66, which is to verify the non-standard address (such as: 0, 64, 128, etc.); the write length of 129 bytes is also to verify the reading and writing of non-standard lengths (such as: 64, 128, 256, etc.).


2. Single byte read and write


I believe everyone can understand the purpose of my experiment. To verify that the byte data written and read each time are consistent.


See below for more details about this article.


Ⅱ. Example Project Download


The examples provided by the author for beginners have removed many unnecessary functions and streamlined the official code, so that beginners can understand it at a glance and provide simple and clear projects for everyone to learn.


The example projects I provided were all uploaded to the 360 ​​cloud disk after multiple tests on the board and no problems were found. You are welcome to download them for testing and reference learning.


The software project provided for download is based on Keil (MDK-ARM) V5 version and STM32F103ZE chip, but it is also applicable to other F1 models (applicable to other F1 models: follow WeChat and reply "change model").


STM32F10x_Hardware I2C read and write EEPROM (standard peripheral library version) example source code project:


http://yunpan.cn/c6b8d4mCTPpCj Access password


STM32F107VC_Hardware I2C read and write EEPROM (standard peripheral library version) example source code project:


http://yunpan.cn/c6b8HGnAGG4Mf Access password


I2C EEPROM (AT24xx) information:


https://yunpan.cn/c667rIDPgvwTf Access password 1099


STM32F1 information:


https://yunpan.cn/crBUdUGdYKam2 Access password ca90


III. Hardware I2C Configuration


The configuration of hardware I2C is actually very simple, including RCC clock, GPIO, I2C configuration, etc. The author built the project based on the F1 standard peripheral library (it is also recommended that beginners use the official standard peripheral library), mainly in the form of library (if your F1 chip is different from the provided project, you can reply to WeChat "modify the model").


1.RCC clock source

This function is located under the bsp.c file;


RCC is an area that many beginners and even those who are already working tend to overlook. Many friends feel that the peripherals they use are not normal, and a large part of this is caused by not configuring RCC.


Important Note:


A. The configuration of the peripheral RCC clock should be before the initialization of its peripherals;


B. Match the corresponding clock.


For example: RCC_APB2 peripherals should not be configured in the RCC_APB1 clock


[For example: RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); This can be compiled, but it is wrong code]


2. I2C pin configuration

This function is located under the i2c_ee.c file;


1. Use hardware I2C: GPIO_Mode_AF_OD multiplexed open-drain mode


2. Since hardware I2C is used, unlike analog I2C, IO operations are used, so the pin definition here is relatively "dead" GPIO_Pin_6 | GPIO_Pin_7.


If you are using I2C2 or pin mapping, the pins here should also change accordingly.


3. I2C Configuration

This function is located under the i2c_ee.c file;


This function is the focus of this article:


1.I2C mode: I2C_Mode = I2C_Mode_I2C;


The hardware has multiple modes:


I2C_Mode_I2C: I2C mode


I2C_Mode_SMBusDevice: SMBus device (serial machine) mode


I2C_Mode_SMBusHost: Host mode


2.I2C duty cycle: I2C_DutyCycle = I2C_DutyCycle_2;


This parameter is valid in fast I2C mode, that is, the speed is greater than 100KHz.


I2C_DutyCycle_2: 2 to 1 duty cycle


I2C_DutyCycle_16_9: 16 to 9 duty cycle


Interested friends can configure the clock higher than 100KHz (such as 400KHz), and measure the SCL pin with an oscilloscope, and you can see that the duty cycle is different.


3.I2C device address: I2C_OwnAddress1 = EEPROM_DEV_ADDR;


This parameter is the address of the first device (slave), and EEPROM_DEV_ADDR is the device address defined by our own macro.


4.I2C response: I2C_Ack = I2C_Ack_Enable;


Please understand the meaning of this parameter in conjunction with the previous article "I2C Protocol".


5. Number of address bits: I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;


This parameter is the number of bits of the device address and needs to be consistent with the following function "I2C_Send7bitAddress".


6.I2C speed: I2C_ClockSpeed ​​= I2C_SPEED;


This parameter is easy to understand. I2C_SPEED is the value "100000" defined by our macro, which means 100KHz.


IV. Hardware I2C read and write EEPROM configuration


The previous article briefly mentioned the reading and writing of single bytes of EEPROM and provided examples of multi-byte reading and writing, but did not specifically describe the specific operations of multi-byte.


The following will describe the single-byte read and write and multi-byte read and write operations in detail. Please download the "I2C EEPROM Data" and "Example Project" for reference.


Before reading and writing EEPROM (AT24Cxx), you need to understand two parameters (see the source code i2c_ee.h file):


A. "Data word" address length: that is, how many bits are there in the address where the data is stored. The specific classification (see data sheet) is as follows:


 8-bit: AT24C01, AT24C02


16-bit: AT24C04, AT24C08, AT24C16, AT24C32, AT24C64, AT24C128, AT24C256, AT24C512


B. Page length: When writing continuously, the longest page that can be written is one. After writing this page, you need to specify the address of the next page, otherwise it will be written in a loop on the previous page. The specific classification (see the data sheet) is as follows:


  8 bytes: AT24C01, AT24C02


 16 bytes: AT24C04, AT24C08, AT24C16


 32 bytes: AT24C32, AT24C64


 64 bytes: AT24C128, AT24C256


128 bytes: AT24C512


1. Single byte write


Timing diagram:

The screenshot is from the "AT24C128C Data Sheet". Single-byte writing is mainly divided into 5 steps:


1. Get Started


2. Device address/write


3. Data address


4. Write one byte of data


5. Stop


Source program:

Before operating the hardware I2C, it is necessary to detect whether the I2C is in the "busy" state. The data address is written differently depending on the length.


2. Single byte read (random)


Timing diagram:

The screenshot is from the "AT24C128C Data Sheet". Single-byte read (also random read) is mainly divided into 7 steps:


1. Get Started


2. Device address/write


3. Data address


4. Start over


5. Device address/read


6. Read one byte of data


7. Stop


Source program:

Here is a reminder that the response bits of single-byte read and multi-byte read will generate non-response due to discontinuous reading.


3. Page writing


Timing diagram:

The screenshot is from the "AT24C128C Data Sheet". The difference between page writing and single-byte writing is "continuous writing".


Note: Here, page writing means writing data in the page pointed to by the address, that is, the page where the address pointed to by the "address pointer" inside the EEPROM is located. Before each writing, we need to point the "address pointer" to an address (see the source code below). During the writing process, once the last byte is written, it will return to the first address of the page and continue writing. Therefore, after writing the page, we need to re-point the "address pointer" to the first address of the next page.


[The size of the chip page varies according to the chip, see the description at the beginning of this chapter]


Source program:

There is a reason why writing the last byte is separated out: to prevent HardFault_Handler.


4. Multi-byte write


Source program:

"Multi-byte write" is written based on "page write". From the description of page write above (writing to the last byte of the page will return to the first address of the page), we can know that multi-byte write needs to consider many situations, otherwise other data will be destroyed.


The source code above captures a simple part: the case where the address to start writing is exactly at the head address of the page. When writing data from the head address, it is necessary to determine whether the size of the data to be written is multiple pages.


[The above situation is a relatively simple one. There are other situations, which I will not describe here. I hope that you, as a beginner, can understand more. This is also a reference to the official thinking of ST, and it is beneficial to your programming thinking.]


5. Multi-byte read


Timing diagram:

The screenshot is from the "AT24C128C Data Sheet". Multi-byte reads require attention to the response.


Before the last bit of data is read in multiple bytes, an acknowledge bit must be generated, and the last bit generates a non-acknowledge bit. Please understand this in conjunction with the source code below.


Source program:

Compared with single-byte read: the previous steps 1 to 5 are the same, please focus on step 6, and pay attention to the response generated here.


Ⅴ、ST official I2C reading and writing problems


When it comes to ST's I2C problem, many people on the Internet say that there are serious I2C problems. I personally don't think there are too many problems (maybe I haven't studied it enough).

[1] [2]
Keywords:STM32F10x Reference address:STM32F10x_Hardware I2C read and write EEPROM (standard peripheral library version)

Previous article:STM32 system learning - I2C (read and write EEPROM)
Next article:stm8s input capture

Latest Microcontroller Articles
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号