Hardware implementation of IIC protocol to read EEPROM

I also took TMD. Anyway, my board has been unsuccessful for a long time. I don't know why. Wildfire STM32-MINI has been stuck on EV5. No matter what, the code is stained first.

1

2Project directory (board is wildfire STM32 MINI)

Serial port related code:

bsp_usart.h

#ifndef __USART_H
#define	__USART_H


#include "stm32f10x.h"
#include <stdio.h>

/**
  * Serial port macro definition, the bus and IO mounted on different serial ports are different, these macros need to be modified during transplantation
	* 1-Modify the macro of the bus clock, uart1 is mounted to the apb2 bus, and other uarts are mounted to the apb1 bus
	* 2- Macro to modify GPIO
  */

// Serial port 1-USART1
#define  DEBUG_USARTx                   USART1
#define  DEBUG_USART_CLK                RCC_APB2Periph_USART1
#define  DEBUG_USART_APBxClkCmd         RCC_APB2PeriphClockCmd
#define  DEBUG_USART_BAUDRATE           115200

// USART GPIO pin macro definition
#define  DEBUG_USART_GPIO_CLK           (RCC_APB2Periph_GPIOA)
#define  DEBUG_USART_GPIO_APBxClkCmd    RCC_APB2PeriphClockCmd

#define  DEBUG_USART_TX_GPIO_PORT         GPIOA
#define  DEBUG_USART_TX_GPIO_PIN          GPIO_Pin_9
#define  DEBUG_USART_RX_GPIO_PORT       GPIOA
#define  DEBUG_USART_RX_GPIO_PIN        GPIO_Pin_10

#define  DEBUG_USART_IRQ                USART1_IRQn
#define  DEBUG_USART_IRQHandler         USART1_IRQHandler

void USART_Config(void);

#endif /* __USART_H */

bsp_usart.c

 

#include "./usart/bsp_usart.h"


 /**
  * @brief USART GPIO configuration, working parameter configuration
  * @param none
  * @retval None
  */
void USART_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;

	// Turn on the clock of the serial port GPIO
	DEBUG_USART_GPIO_APBxClkCmd(DEBUG_USART_GPIO_CLK, ENABLE);

	// Turn on the clock of the serial peripheral
	DEBUG_USART_APBxClkCmd(DEBUG_USART_CLK, ENABLE);

	// Configure the GPIO of USART Tx to push-pull multiplexing mode
	GPIO_InitStructure.GPIO_Pin = DEBUG_USART_TX_GPIO_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &GPIO_InitStructure);

  // Configure the GPIO of USART Rx as floating input mode
	GPIO_InitStructure.GPIO_Pin = DEBUG_USART_RX_GPIO_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(DEBUG_USART_RX_GPIO_PORT, &GPIO_InitStructure);

	// Configure the working parameters of the serial port 
	// Configure the baud rate
	USART_InitStructure.USART_BaudRate = DEBUG_USART_BAUDRATE;
	// Configure pin data word length
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	// configure stop bits
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	// configure check digit
	USART_InitStructure.USART_Parity = USART_Parity_No ;
	// configure hardware flow control
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	// Configure the working mode, send and receive together
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	// Complete the initial configuration of the serial port
	USART_Init(DEBUG_USARTx, &USART_InitStructure);

	// enable serial port
	USART_Cmd(DEBUG_USARTx, ENABLE);
}


///Redirect the c library function printf to the serial port. After redirection, you can use the printf function 
int fputc( int ch, FILE *f)
{
		/* Send a byte of data to the serial port */
		USART_SendData(DEBUG_USARTx, (uint8_t) ch);

		/* Wait for the sending to finish*/ 
		while (USART_GetFlagStatus(DEBUG_USARTx, USART_FLAG_TXE) == RESET);

		return (ch);
}

///Redirect the c library function scanf to the serial port, rewrite the backward can use scanf, getchar and other functions 
int fgetc(FILE *f)
{
		/* Waiting for serial input data*/ 
		while (USART_GetFlagStatus(DEBUG_USARTx, USART_FLAG_RXNE) == RESET);

		return (int)USART_ReceiveData(DEBUG_USARTx);
}

bsp_i2c_ee.h

#ifndef __I2C_EE_H
#define	__I2C_EE_H


#include "stm32f10x.h"
#include "bsp_usart.h"

#define  EEPROM_ADDR  0xA0


void I2C_EE_Config(void);
void EEPROM_Byte_Write(uint8_t addr,uint8_t data);
void EEPROM_Page_Write(uint8_t addr,uint8_t *data,uint8_t numByteToWrite);
void EEPROM_Read(uint8_t addr,uint8_t *data,uint8_t numByteToRead);
void EEPROM_WaitForWriteEnd(void);


#endif / * __I2C_EE_H * /

bsp_i2c_ee.c

#include "bsp_i2c_ee.h"



void I2C_EE_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	I2C_InitTypeDef  I2C_InitStructure;

	// Turn on the clock of the IIC GPIO
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

	// Turn on the clock of the IIC peripheral
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

	// Configure the GPIO of IIC SCL SDA to push-pull multiplexing mode
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);



	// Configure the working parameters of the IIC 
	I2C_InitStructure.I2C_Ack = I2C_Ack_Enable ; //Enable the response 
	I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit ; //Use the 7-bit address mode 
	I2C_InitStructure.I2C_ClockSpeed ​​= 400000; //Configure the SCL clock frequency
	I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2 ;
	I2C_InitStructure.I2C_Mode = I2C_Mode_I2C ;
	I2C_InitStructure.I2C_OwnAddress1 = 0x5f; //This is the STM32 IIC's own device address, as long as it is unique on the bus

	I2C_Init(I2C1,&I2C_InitStructure);

	// enable serial port
	I2C_Cmd (I2C1, ENABLE);
}


//write a byte to EEPROM

void EEPROM_Byte_Write(uint8_t addr,uint8_t data)
{
	//generate start signal
	I2C_GenerateSTART(I2C1,ENABLE);

	while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT) == ERROR);

	//EV5 event is detected, send device address
	I2C_Send7bitAddress(I2C1,EEPROM_ADDR,I2C_Direction_Transmitter);

  while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) == ERROR);

	//EV6 event is detected, send the address of the storage unit to be operated
	I2C_SendData (I2C1,addr);

	while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTING ) == ERROR);

  //EV8 event is detected, send data to be stored
	I2C_SendData (I2C1,data);

	while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED ) == ERROR);

	// data transfer completed
	I2C_GenerateSTOP(I2C1,ENABLE);

}



//Write multiple bytes to EEPROM (page write), each write cannot exceed 8 bytes

void EEPROM_Page_Write(uint8_t addr,uint8_t *data,uint8_t numByteToWrite)
{
	//generate start signal
	I2C_GenerateSTART(I2C1,ENABLE);

	while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT) == ERROR);

	//EV5 event is detected, send device address
	I2C_Send7bitAddress(I2C1,EEPROM_ADDR,I2C_Direction_Transmitter);

  while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) == ERROR);

	//EV6 event is detected, send the address of the storage unit to be operated
	I2C_SendData (I2C1,addr);

	while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTING ) == ERROR);


	while(numByteToWrite)
	{
		//EV8 event is detected, send data to be stored
		I2C_SendData (I2C1,*data);

		while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED ) == ERROR);

		data++;
		numByteToWrite--;

	}
	// data transfer completed
	I2C_GenerateSTOP(I2C1,ENABLE);

}



// read data from EEPROM

void EEPROM_Read(uint8_t addr,uint8_t *data,uint8_t numByteToRead)
{
	//generate start signal
	I2C_GenerateSTART(I2C1,ENABLE);

	while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT) == ERROR);

	//EV5 event is detected, send device address
	I2C_Send7bitAddress(I2C1,EEPROM_ADDR,I2C_Direction_Transmitter);

  while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) == ERROR);

	//EV6 event is detected, send the address of the storage unit to be operated
	I2C_SendData (I2C1,addr);

	while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTING ) == ERROR);


	//Second start signal 
	//Generate start signal
	I2C_GenerateSTART(I2C1,ENABLE);

	while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT) == ERROR);

	//EV5 event is detected, send device address
	I2C_Send7bitAddress(I2C1,EEPROM_ADDR,I2C_Direction_Receiver);

  while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED ) == ERROR);


	while(numByteToRead)
	{
		if(numByteToRead == 1)
		{
			//if the last byte
			I2C_AcknowledgeConfig (I2C1,DISABLE);
		}

		//EV7 event is detected	 
		while (I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED ) == ERROR);

		//EV7 event is detected, that is, the data register has new valid data	
		*data = I2C_ReceiveData(I2C1);

		data++;

		numByteToRead--;

	}


	// data transfer completed
	I2C_GenerateSTOP(I2C1,ENABLE);

	//Reconfigure ACK enable for next communication
	I2C_AcknowledgeConfig (I2C1,ENABLE);

}


//Wait for the EEPROM internal timing to complete 
void EEPROM_WaitForWriteEnd( void )
{

	do
	{
		//generate start signal
		I2C_GenerateSTART(I2C1,ENABLE);

		while(I2C_GetFlagStatus (I2C1,I2C_FLAG_SB) == RESET);

		//EV5 event is detected, send device address
		I2C_Send7bitAddress(I2C1,EEPROM_ADDR,I2C_Direction_Transmitter);
	}
	while(I2C_GetFlagStatus (I2C1,I2C_FLAG_ADDR) == RESET );

	//The internal timing of the EEPROM is completed and the transfer is completed
	I2C_GenerateSTOP(I2C1,ENABLE);
}

main.c

#include "stm32f10x.h"
#include "./usart/bsp_usart.h"
#include "./i2c/bsp_i2c_ee.h"


uint8_t readData[10]={0};
uint8_t writeData[8]={4,5,6,7,8,9,10,11};
 /**
  * @brief main function
  * @param none
  * @retval None
  */
int main(void)
{
	 uint8_t i=0;
   USART_Config();
	 printf (" This is an IIC communication experiment\n ");

	 I2C_EE_Config();
   //Write 0x55 to EEPROM 11 address,
   EEPROM_Byte_Write(11,0x55);
	 EEPROM_WaitForWriteEnd();
	 //Read the contents of the EEPROM 11 address and save it to readData (the first address of the array)
	 EEPROM_Read(11,readData,1);
	 printf("%x",readData[0]);

		//addr%8 == 0 , which is address alignment
	EEPROM_Page_Write(16,writeData,8);

	//Wait for the write operation to complete
	EEPROM_WaitForWriteEnd();

	//read data
	EEPROM_Read(16,readData,8);
	for(i=0;i<8;i++)
	{
		printf("%d ",readData[i]);
	}
    while(1);
}
/*********************************************END OF FILE**********************/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325380728&siteId=291194637