GD32F470使用IIC读写EEPROM

iic.h

#ifndef __IIC_H
#define __IIC_H

#include "head.h"

#define I2C1_SPEED              400000
#define I2C1_SLAVE_ADDRESS7     0xA0
#define I2C_PAGE_SIZE           8

void i2c1_config(void);

#endif

iic.c

#include "iic.h"

void i2c1_config(void)
{
    rcu_periph_clock_enable(RCU_GPIOF);/* enable GPIOF clock */
    rcu_periph_clock_enable(RCU_I2C1);/* enable I2C1 clock */

    gpio_af_set(GPIOF, GPIO_AF_4, GPIO_PIN_1);/* connect PF1 to I2C1_SCL */
    gpio_af_set(GPIOF, GPIO_AF_4, GPIO_PIN_0);/* connect PF0 to I2C1_SDA */
    gpio_mode_set(GPIOF, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_0);
    gpio_output_options_set(GPIOF, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_0);
    gpio_mode_set(GPIOF, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_1);
    gpio_output_options_set(GPIOF, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_1);
	
    gpio_mode_set(GPIOF, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE,GPIO_PIN_2);
    gpio_output_options_set(GPIOF, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_2);
	gpio_bit_write(GPIOF,GPIO_PIN_2,RESET);//写使能
	
    rcu_periph_clock_enable(RCU_I2C1);/* enable I2C clock */
    i2c_clock_config(I2C1,I2C1_SPEED,I2C_DTCY_2);/* configure I2C clock */
    i2c_mode_addr_config(I2C1,I2C_I2CMODE_ENABLE,I2C_ADDFORMAT_7BITS,I2C1_SLAVE_ADDRESS7);/* configure I2C address */
    
    i2c_enable(I2C1);/* enable I2C1 */
    i2c_ack_config(I2C1,I2C_ACK_ENABLE);/* enable acknowledge */
}

eeprom.h

#ifndef __EEPROM_H
#define __EEPROM_H

#include "head.h"

#define EEP_FIRST_PAGE 0x00
#define I2C_OK         0
#define I2C_FAIL       1

/* I2C read and write functions */
uint8_t i2c_24c02_test(void);
/* initialize peripherals used by the I2C EEPROM driver */
void i2c_eeprom_init(void);
/* write buffer of data to the I2C EEPROM */
void eeprom_buffer_write(uint8_t *p_buffer, uint8_t write_address, uint16_t number_of_byte);
/* write one byte to the I2C EEPROM */
void eeprom_byte_write(uint8_t *p_buffer, uint8_t write_address);
/* write more than one byte to the EEPROM with a single write cycle */
void eeprom_page_write(uint8_t *p_buffer, uint8_t write_address, uint8_t number_of_byte);
/*  read data from the EEPROM */
void eeprom_buffer_read(uint8_t *p_buffer, uint8_t read_address, uint16_t number_of_byte);
/* wait for EEPROM standby state */
void eeprom_wait_standby_state(void);

#endif

eeprom.c

#include "eeprom.h"
#define EEPROM_BLOCK0_ADDRESS    0xA0
#define BUFFER_SIZE              256
uint16_t eeprom_address;

/*!
    \brief      I2C read and write functions
    \param[in]  none
    \param[out] none
    \retval     I2C_OK or I2C_FAIL 
*/
uint8_t i2c_24c02_test(void)
{
    uint16_t i;
    uint8_t i2c_buffer_write[BUFFER_SIZE];
    uint8_t i2c_buffer_read[BUFFER_SIZE];
    
    for(i = 0;i < BUFFER_SIZE;i++){ i2c_buffer_write[i]=i;}
    eeprom_buffer_write(i2c_buffer_write,EEP_FIRST_PAGE, BUFFER_SIZE); 

    eeprom_buffer_read(i2c_buffer_read,EEP_FIRST_PAGE, BUFFER_SIZE); 
    for(i = 0;i < BUFFER_SIZE;i++){printf("0x%02X ", i2c_buffer_read[i]);}
	
    return I2C_OK;
}
void i2c_eeprom_init(void)
{
    eeprom_address = EEPROM_BLOCK0_ADDRESS;
}

void eeprom_buffer_write(uint8_t* p_buffer, uint8_t write_address, uint16_t number_of_byte)
{
    uint8_t number_of_page = 0, number_of_single = 0, address = 0, count = 0;
    
    address = write_address % I2C_PAGE_SIZE;
    count = I2C_PAGE_SIZE - address;
    number_of_page =  number_of_byte / I2C_PAGE_SIZE;
    number_of_single = number_of_byte % I2C_PAGE_SIZE;
    
    /* if write_address is I2C_PAGE_SIZE aligned  */
    if(0 == address){
        while(number_of_page--){
            eeprom_page_write(p_buffer, write_address, I2C_PAGE_SIZE); 
            eeprom_wait_standby_state();
            write_address +=  I2C_PAGE_SIZE;
            p_buffer += I2C_PAGE_SIZE;
        }
        if(0 != number_of_single){
            eeprom_page_write(p_buffer, write_address, number_of_single);
            eeprom_wait_standby_state();
        }      
    }else{
       /* if write_address is not I2C_PAGE_SIZE aligned */
        if(number_of_byte < count){ 
            eeprom_page_write(p_buffer, write_address, number_of_byte);
            eeprom_wait_standby_state();
        }else{
            number_of_byte -= count;
            number_of_page =  number_of_byte / I2C_PAGE_SIZE;
            number_of_single = number_of_byte % I2C_PAGE_SIZE;
            
            if(0 != count){
                eeprom_page_write(p_buffer, write_address, count);
                eeprom_wait_standby_state();
                write_address += count;
                p_buffer += count;
            } 
            /* write page */
            while(number_of_page--){
                eeprom_page_write(p_buffer, write_address, I2C_PAGE_SIZE);
                eeprom_wait_standby_state();
                write_address +=  I2C_PAGE_SIZE;
                p_buffer += I2C_PAGE_SIZE;
            }
            /* write single */
            if(0 != number_of_single){
                eeprom_page_write(p_buffer, write_address, number_of_single); 
                eeprom_wait_standby_state();
            }
        }
    }  
}

void eeprom_byte_write(uint8_t* p_buffer, uint8_t write_address)
{
    /* wait until I2C bus is idle */
    while(i2c_flag_get(I2C1, I2C_FLAG_I2CBSY));

    /* send a start condition to I2C bus */
    i2c_start_on_bus(I2C1);
    
    /* wait until SBSEND bit is set */
    while(!i2c_flag_get(I2C1, I2C_FLAG_SBSEND));
    
	/* send slave address to I2C bus */
    i2c_master_addressing(I2C1, eeprom_address, I2C_TRANSMITTER);
    while(!i2c_flag_get(I2C1, I2C_FLAG_ADDSEND));
    
    /* clear the ADDSEND bit */
    i2c_flag_clear(I2C1,I2C_FLAG_ADDSEND);
    
    /* wait until the transmit data buffer is empty */
    while(SET != i2c_flag_get(I2C1, I2C_FLAG_TBE));
    
    /* send the EEPROM's internal address to write to : only one byte address */
	i2c_data_transmit(I2C1, 0x00);
    while(!i2c_flag_get(I2C1, I2C_FLAG_BTC));
    i2c_data_transmit(I2C1, write_address);
    while(!i2c_flag_get(I2C1, I2C_FLAG_BTC));
   
    /* send the byte to be written */
    i2c_data_transmit(I2C1, *p_buffer); 
    
    /* wait until BTC bit is set */
    while(!i2c_flag_get(I2C1, I2C_FLAG_BTC));

    /* send a stop condition to I2C bus */
    i2c_stop_on_bus(I2C1);
    
    /* wait until the stop condition is finished */
    while(I2C_CTL0(I2C1)&I2C_CTL0_STOP);
}

void eeprom_page_write(uint8_t* p_buffer, uint8_t write_address, uint8_t number_of_byte)
{
    /* wait until I2C bus is idle */
    while(i2c_flag_get(I2C1, I2C_FLAG_I2CBSY));
    
    /* send a start condition to I2C bus */
    i2c_start_on_bus(I2C1);
    
    /* wait until SBSEND bit is set */
    while(!i2c_flag_get(I2C1, I2C_FLAG_SBSEND));
    
	/* send slave address to I2C bus */
    i2c_master_addressing(I2C1, eeprom_address, I2C_TRANSMITTER);
    while(!i2c_flag_get(I2C1, I2C_FLAG_ADDSEND));
    
    /* clear the ADDSEND bit */
    i2c_flag_clear(I2C1,I2C_FLAG_ADDSEND);
    
    /* wait until the transmit data buffer is empty */
    while( SET != i2c_flag_get(I2C1, I2C_FLAG_TBE));
    /* send the EEPROM's internal address to write to : only one byte address */
	i2c_data_transmit(I2C1, 0x00);
    while(!i2c_flag_get(I2C1, I2C_FLAG_BTC));
    i2c_data_transmit(I2C1, write_address);
    while(!i2c_flag_get(I2C1, I2C_FLAG_BTC));
    
    /* while there is data to be written */
    while(number_of_byte--){  
        i2c_data_transmit(I2C1, *p_buffer);
        
        /* point to the next byte to be written */
        p_buffer++; 
        
        /* wait until BTC bit is set */
        while(!i2c_flag_get(I2C1, I2C_FLAG_BTC));
    }
    /* send a stop condition to I2C bus */
    i2c_stop_on_bus(I2C1);
    
    /* wait until the stop condition is finished */
    while(I2C_CTL0(I2C1)&I2C_CTL0_STOP);
}

void eeprom_buffer_read(uint8_t* p_buffer, uint8_t read_address, uint16_t number_of_byte)
{  
    /* wait until I2C bus is idle */
    while(i2c_flag_get(I2C1, I2C_FLAG_I2CBSY));

    if(2 == number_of_byte){
        i2c_ackpos_config(I2C1,I2C_ACKPOS_NEXT);
    }
    
    /* send a start condition to I2C bus */
    i2c_start_on_bus(I2C1);
    
    /* wait until SBSEND bit is set */
    while(!i2c_flag_get(I2C1, I2C_FLAG_SBSEND));
    
	/* send slave address to I2C bus */
    i2c_master_addressing(I2C1, eeprom_address, I2C_TRANSMITTER);
    while(!i2c_flag_get(I2C1, I2C_FLAG_ADDSEND));
    
    /* clear the ADDSEND bit */
    i2c_flag_clear(I2C1,I2C_FLAG_ADDSEND);
    
    /* wait until the transmit data buffer is empty */
    while(SET != i2c_flag_get( I2C1 , I2C_FLAG_TBE));

    /* enable I2C1*/
    i2c_enable(I2C1);
    
    /* send the EEPROM's internal address to write to */
	i2c_data_transmit(I2C1, 0x00);  
    while(!i2c_flag_get(I2C1, I2C_FLAG_BTC));
    i2c_data_transmit(I2C1, read_address);  
    while(!i2c_flag_get(I2C1, I2C_FLAG_BTC));
    
    /* send a start condition to I2C bus */
    i2c_start_on_bus(I2C1);
    
    /* wait until SBSEND bit is set */
    while(!i2c_flag_get(I2C1, I2C_FLAG_SBSEND));
    
    /* send slave address to I2C bus */
    i2c_master_addressing(I2C1, eeprom_address, I2C_RECEIVER);

    if(number_of_byte < 3){
        /* disable acknowledge */
        i2c_ack_config(I2C1,I2C_ACK_DISABLE);
    }
    /* wait until ADDSEND bit is set */
    while(!i2c_flag_get(I2C1, I2C_FLAG_ADDSEND));
    
    /* clear the ADDSEND bit */
    i2c_flag_clear(I2C1,I2C_FLAG_ADDSEND);
    
    if(1 == number_of_byte){
        /* send a stop condition to I2C bus */
        i2c_stop_on_bus(I2C1);
    }
    
    /* while there is data to be read */
    while(number_of_byte){
        if(3 == number_of_byte){
            /* wait until BTC bit is set */
            while(!i2c_flag_get(I2C1, I2C_FLAG_BTC));

            /* disable acknowledge */
            i2c_ack_config(I2C1,I2C_ACK_DISABLE);
        }
        if(2 == number_of_byte){
            /* wait until BTC bit is set */
            while(!i2c_flag_get(I2C1, I2C_FLAG_BTC));
            
            /* send a stop condition to I2C bus */
            i2c_stop_on_bus(I2C1);
        }
       /* wait until the RBNE bit is set and clear it */
        if(i2c_flag_get(I2C1, I2C_FLAG_RBNE)){
            /* read a byte from the EEPROM */
            *p_buffer = i2c_data_receive(I2C1);
            
            /* point to the next location where the byte read will be saved */
            p_buffer++; 
            
            /* decrement the read bytes counter */
            number_of_byte--;
        } 
    }
    
    /* wait until the stop condition is finished */
    while(I2C_CTL0(I2C1)&I2C_CTL0_STOP);
    /* enable acknowledge */
    i2c_ack_config(I2C1,I2C_ACK_ENABLE);

    i2c_ackpos_config(I2C1,I2C_ACKPOS_CURRENT);
}

void eeprom_wait_standby_state(void)
{
    __IO uint32_t val = 0;
    
    while(1){
        /* wait until I2C bus is idle */
        while(i2c_flag_get(I2C1, I2C_FLAG_I2CBSY));
        
        /* send a start condition to I2C bus */
        i2c_start_on_bus(I2C1);
        
        /* wait until SBSEND bit is set */
        while(!i2c_flag_get(I2C1, I2C_FLAG_SBSEND));
        
        /* send slave address to I2C bus */
        i2c_master_addressing(I2C1, eeprom_address, I2C_TRANSMITTER);
        
        /* keep looping till the Address is acknowledged or the AE flag is set (address not acknowledged at time) */
        do{
            /* get the current value of the I2C_STAT0 register */
            val = I2C_STAT0(I2C1);
            
        }while(0 == (val & (I2C_STAT0_ADDSEND | I2C_STAT0_AERR)));
        /* check if the ADDSEND flag has been set */
        if(val & I2C_STAT0_ADDSEND){
            
            /* clear ADDSEND flag */
            i2c_flag_clear(I2C1,I2C_FLAG_ADDSEND);
            
            /* send a stop condition to I2C bus */
            i2c_stop_on_bus(I2C1);
            
            /* exit the function */
            return ;
        } else {
            /* clear the bit of AERR */
            i2c_flag_clear(I2C1, I2C_FLAG_AERR);
        }
        
        /* send a stop condition to I2C bus */
        i2c_stop_on_bus(I2C1);
        /* wait until the stop condition is finished */
        while(I2C_CTL0(I2C1)&I2C_CTL0_STOP);
    }
}

  • 8
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值