#include "gd32e50x.h"
#include "gd32e507z_eval.h"
#include "bsp.h"
int main(void)
{
//初始化时钟串口等外设
i2c0_eeprom_init();
i2c_bl24xx_page_test();
while(1)
{
i2c_bl24xx_read_test();
vTaskDelay(1000);
}
}
bsp_iic.c
void i2c0_eeprom_init(void)
{
bl24xx_address = EEPROM_BLOCK0_ADDRESS;
i2c0_gpio_config();
i2c0_config();
}
void i2c0_gpio_config(void)
{
/* enable GPIOB clock */
rcu_periph_clock_enable(RCU_GPIOB);
/* enable I2C0 clock */
rcu_periph_clock_enable(RCU_I2C0);
/* connect PB6 to I2C0_SCL */
/* connect PB7 to I2C0_SDA */
gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_6 | GPIO_PIN_7);
}
void i2c0_config(void)
{
i2c_deinit(I2C0);
/* enable I2C clock */
rcu_periph_clock_enable(RCU_I2C0);
/* configure I2C clock */
i2c_clock_config(I2C0,I2C0_SPEED,I2C_DTCY_2);
/* configure I2C address */
i2c_mode_addr_config(I2C0,I2C_I2CMODE_ENABLE,I2C_ADDFORMAT_7BITS,I2C0_SLAVE_ADDRESS7 + 10);
/* enable I2C0 */
i2c_enable(I2C0);
/* enable acknowledge */
i2c_ack_config(I2C0,I2C_ACK_ENABLE);
}
uint8_t i2c_bl24xx_page_test(void)
{
uint16_t i;
uint8_t i2c_buffer_write[BUFFER_SIZE];
for(i = 0;i < BUFFER_SIZE;i++)
{
i2c_buffer_write[i]=7;
// i2c_buffer_write[i]=i + 3;
printf("0x%02X ",i2c_buffer_write[i]);
if(15 == i%16){
printf("\r\n");
}
}
/* EEPROM data write */
bl24xx_page_write(i2c_buffer_write,EEP_FIRST_PAGE, BUFFER_SIZE);
printf("\r\nAT24C02 reading...\r\n");
for(uint16_t i = 0; i < 1000; i ++)
{
for(uint16_t m = 0; m < 100; m ++);
}
return 0;
}
void i2c_bl24xx_read_test(void)
{
uint8_t i2c_read_buffer[200];
bl24xx_byte_read(i2c_read_buffer, 0x00 * 0x10, 96);
for(int i = 0;i < 96;i++)
{
printf("0x%02X ", i2c_read_buffer[i]);
if(15 == i%16)
{
printf("\r\n");
}
if(63 == i%64)
{
printf("\r\n");
}
}
}
void bl24xx_page_write(uint8_t* p_buffer, uint16_t write_address, uint16_t number_of_byte)
{
//获取互斥信号量,IIC不能同时读写
// xSemaphoreTake(Fault_Semaphore_Frame,portMAX_DELAY);
/* wait until I2C bus is idle */
while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C0);
/* wait until SBSEND bit is set */
/* start condition finish */
while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
/* send slave address to I2C bus */
i2c_master_addressing(I2C0, bl24xx_address, I2C_TRANSMITTER);
/* wait until ADDSEND bit is set */
/* address send finish and get a ack */
while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
i2c_flag_clear(I2C0,I2C_FLAG_ADDSEND);
/* wait until the transmit data buffer is empty */
while(SET != i2c_flag_get(I2C0, I2C_FLAG_TBE));
i2c_data_transmit(I2C0, (uint8_t)(write_address >> 8));
while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
i2c_data_transmit(I2C0, (uint8_t)(write_address & 0xFF));
while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
// /* send the byte to be written */
// i2c_data_transmit(I2C0, *p_buffer);
//
// /* wait until BTC bit is set */
// while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
while(number_of_byte)
{
i2c_data_transmit(I2C0, *p_buffer);
while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
p_buffer++;
number_of_byte--;
}
/* send a stop condition to I2C bus */
i2c_stop_on_bus(I2C0);
/* wait until the stop condition is finished */
while(I2C_CTL0(I2C0)&0x0200);
vTaskDelay(5); //写完成
//写完,释放互斥信号量
// xSemaphoreGive(Fault_Semaphore_Frame);
}
void bl24xx_byte_read(uint8_t* p_buffer, uint16_t read_address, uint16_t number_of_byte)
{
//获取互斥信号量
//xSemaphoreTake(Fault_Semaphore_Frame,portMAX_DELAY);
/* wait until I2C bus is idle */
while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
if(2 == number_of_byte){
i2c_ackpos_config(I2C0,I2C_ACKPOS_NEXT);
}
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C0);
/* wait until SBSEND bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
/* send slave address to I2C bus */
i2c_master_addressing(I2C0, bl24xx_address, I2C_TRANSMITTER);
/* wait until ADDSEND bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
/* clear the ADDSEND bit */
i2c_flag_clear(I2C0,I2C_FLAG_ADDSEND);
/* wait until the transmit data buffer is empty */
while(SET != i2c_flag_get( I2C0 , I2C_FLAG_TBE));
/* enable I2C0*/
i2c_enable(I2C0);
/* send the EEPROM's internal address to write to */
i2c_data_transmit(I2C0, (uint8_t)(read_address >> 8));
while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
i2c_data_transmit(I2C0, (uint8_t)(read_address & 0xFF));
while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C0);
/* wait until SBSEND bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
/* send slave address to I2C bus */
i2c_master_addressing(I2C0, bl24xx_address, I2C_RECEIVER);
if(number_of_byte < 3){
/* disable acknowledge */
i2c_ack_config(I2C0,I2C_ACK_DISABLE);
}
/* wait until ADDSEND bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
/* clear the ADDSEND bit */
i2c_flag_clear(I2C0,I2C_FLAG_ADDSEND);
if(1 == number_of_byte){
/* send a stop condition to I2C bus */
i2c_stop_on_bus(I2C0);
}
/* 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(I2C0, I2C_FLAG_BTC));
/* disable acknowledge */
i2c_ack_config(I2C0,I2C_ACK_DISABLE);
}
if(2 == number_of_byte){
/* wait until BTC bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
/* send a stop condition to I2C bus */
i2c_stop_on_bus(I2C0);
}
/* wait until the RBNE bit is set and clear it */
if(i2c_flag_get(I2C0, I2C_FLAG_RBNE)){
/* read a byte from the EEPROM */
*p_buffer = i2c_data_receive(I2C0);
/* 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(I2C0)&0x0200);
/* enable acknowledge */
i2c_ack_config(I2C0,I2C_ACK_ENABLE);
i2c_ackpos_config(I2C0,I2C_ACKPOS_CURRENT);
//读取完,释放互斥信号量
//xSemaphoreGive(Fault_Semaphore_Frame);
}
bsp_iic.h
#ifndef __BSP_IIC_H_
#define __BSP_IIC_H_
#include "bsp.h"
#define I2C1_SPEED 400000
#define I2C1_SLAVE_ADDRESS7 0xE0
#define I2C0_SPEED 400000
#define I2C0_SLAVE_ADDRESS7 0xA0
#define I2C_PAGE_SIZE 128
#define I2C_OK 0
#define I2C_FAIL 1
#define EEPROM_BLOCK0_ADDRESS 0xA0
#define BUFFER_SIZE 64
#define EEP_FIRST_PAGE 0x00
uint16_t bl24xx_address;
uint16_t eeprom_address;
void i2c0_gpio_config(void);
void i2c0_config(void);
void i2c0_eeprom_init(void);
void bl24xx_byte_read(uint8_t* p_buffer, uint16_t read_address, uint16_t number_of_byte);
void bl24xx_page_write(uint8_t* p_buffer, uint16_t write_address, uint16_t number_of_byte);
#endif
注意:iic不能同时去读写,因为是半双工的通信,只能互锁,或者加互斥信号量,读的时候不能写,写的时候不能读取,此外,eeprom在写操作完了,不能马上读取,因为硬件在写入,所以iic不能拿到ACK响应,根据BL24C128A手册,可以查看到,不管按字节,还是按页写入,都需要3ms 的时间,因此,在执行写入操作时,应该等待3ms以上,再进行读取,否则在芯片写入期间,无法通讯。
友情提醒:重要的事说三遍,重要的事说三遍,重要的事说三遍!!!!!!!
在配置任何外设的时候,首先将外设复位,也就是反初始化,否则,当你复位的时候,你会出现各种奇葩的问题。一定要反初始化,还有,当你开启任何一个中断时,一定要先清除一下中断的标志,否则复位的时候,会莫名其妙进中断!!