#include "bl24c512.h" #include "include.h" static u32 last_write_time; bool I2C_eeprom_write_byte( u16 addr, u8 data); bool I2C_eeprom_Read_byte(u16 addr, u8 *data); void eeprom_da_mode(bool in_mode) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = SDA_PIN; GPIO_InitStructure.GPIO_Mode = (in_mode == TRUE) ? GPIO_Mode_IN_FLOATING : GPIO_Mode_Out_OD; GPIO_Init(SDA_PIN_PORT, &GPIO_InitStructure ); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = SCL_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(SCL_PIN_PORT , &GPIO_InitStructure ); } /************************************************************* 函数功能:延时连个指令周期 函数名称:delayedus 函数返回:无返回值。 函数参数:延时时间 **************************************************************/ void delayed_ms(u8 n) { u32 dy; u8 i; for(i=0;i xTaskGetTickCount() ); while (1) { if (poll_busy()) { break; } } } /************************************************************* 函数功能:EEPROM任意地址写一字节。 函数名称:I2C_eeprom_write_byte。 函数返回:无返回值。 函数参数:EEPROM的地址addr ,对应写入数据data。 **************************************************************/ bool I2C_eeprom_write_byte( u16 addr, u8 data) { unsigned char addrlow; unsigned char addrhigh; addrlow=(unsigned char)addr; addrhigh=(unsigned char)(addr>>8); wait_eeprom_free(); BL24C512I2C_GenerateSTART(); I2c_send_byte(BL24C512ADDR); if(I2C_check_ack()==0) { return FALSE; } I2c_send_byte(addrhigh); if(I2C_check_ack()==0) { return FALSE; } I2c_send_byte(addrlow); if(I2C_check_ack()==0) { return FALSE; } I2c_send_byte(data); if(I2C_check_ack()==0) { return FALSE; } BL24C512_I2C_GenerateSTOP(); last_write_time = xTaskGetTickCount(); return TRUE; } /************************************************************* 函数功能:EEPROM任意地址读一字节。 函数名称:I2C_eeprom_Read_byte。 函数返回:无返回值。 函数参数:EEPROM的地址addr。 **************************************************************/ bool I2C_eeprom_Read_byte(u16 addr, u8 *data) { unsigned char devaddr=0; unsigned char addrlow=0; unsigned char addrhigh=0; addrlow=(unsigned char)addr; addrhigh=(unsigned char)(addr>>8); wait_eeprom_free(); BL24C512I2C_GenerateSTART(); I2c_send_byte(BL24C512ADDR); if(I2C_check_ack()==0) { return FALSE; } I2c_send_byte(addrhigh); if(I2C_check_ack()==0) { return FALSE; } I2c_send_byte(addrlow); if(I2C_check_ack()==0) { return FALSE; } devaddr=(BL24C512ADDR| 0X01); BL24C512I2C_GenerateSTART(); I2c_send_byte(devaddr); if(I2C_check_ack()==0) { return FALSE; } *data=I2c_receive_byte(); I2c_send_no_ack(); delayedus(1); BL24C512_I2C_GenerateSTOP(); return TRUE; } /************************************************************* 函数功能:EEPROM写入数组,只能写入小于128byte数据,并且不能超过页范围 函数名称:I2C_eeprom_write_buf 函数返回:无返回值。 函数参数:EEPROM的地址addr。 **************************************************************/ bool I2C_eeprom_write_page(u16 addr, u8 * buf, u8 length) { unsigned char addrlow; unsigned char addrhigh; addrlow=(unsigned char)addr; addrhigh=(unsigned char)(addr>>8); wait_eeprom_free(); BL24C512I2C_GenerateSTART(); I2c_send_byte(BL24C512ADDR); if(I2C_check_ack()==0) { return FALSE; } I2c_send_byte(addrhigh); if(I2C_check_ack()==0) { return FALSE; } I2c_send_byte(addrlow); if(I2C_check_ack()==0) { return FALSE; } for (u8 i = 0; i < length; i++) { I2c_send_byte(buf[i]); if(I2C_check_ack()==0) { return FALSE; } } BL24C512_I2C_GenerateSTOP(); last_write_time = xTaskGetTickCount(); return TRUE; } /************************************************************* 函数功能:EEPROM写入数组,只能写入小于128byte数据,并且不能超过页范围 函数名称:I2C_eeprom_write_buf 函数返回:无返回值。 函数参数:EEPROM的地址addr。 **************************************************************/ bool I2C_eeprom_write_buf(u16 startAddr, u8 * buf, u16 length) { bl24c512_init(); u16 current_startAddr = startAddr; u8 first_page_byte = EEPROM_PAGE_SIZE - (startAddr % EEPROM_PAGE_SIZE); u8 * current_data_ptr = buf; u16 left_length = length; u16 integrated_Pages; u16 end_page_byte; if (length > first_page_byte) { if (!I2C_eeprom_write_page(current_startAddr, current_data_ptr, first_page_byte)) { return FALSE; } current_startAddr = current_startAddr + first_page_byte; current_data_ptr += first_page_byte; left_length = length - first_page_byte; integrated_Pages = left_length / EEPROM_PAGE_SIZE; end_page_byte = left_length % EEPROM_PAGE_SIZE; for (u16 i = 0; i < integrated_Pages; i++) { if (!I2C_eeprom_write_page(current_startAddr, current_data_ptr, EEPROM_PAGE_SIZE)) { return FALSE; } current_startAddr += EEPROM_PAGE_SIZE; current_data_ptr += EEPROM_PAGE_SIZE; } if (end_page_byte > 0) { if (!I2C_eeprom_write_page(current_startAddr, current_data_ptr, end_page_byte)) { return FALSE; } } } else { if(!I2C_eeprom_write_page(startAddr, buf, length)) { return FALSE; } } return TRUE; } /************************************************************* 函数功能:EEPROM读出数组 函数名称:I2C_eeprom_write_buf 函数返回:无返回值。 函数参数:EEPROM的地址addr。 **************************************************************/ bool I2C_eeprom_read_buf(u16 addr, u8 * buf, u16 length) { unsigned char devaddr; if (length == 0) { return FALSE; } wait_eeprom_free(); bl24c512_init(); if (I2C_eeprom_Read_byte(addr, buf) == FALSE) { return FALSE; } length--; if (length > 0) { devaddr=(BL24C512ADDR| 0X01); BL24C512I2C_GenerateSTART(); I2c_send_byte(devaddr); if(I2C_check_ack()==0) { return FALSE; } for (u16 i = 0; i < length; i++) { buf[i+ 1] = I2c_receive_byte(); if (i < length - 1) { I2c_send_ack(); } } I2c_send_no_ack(); BL24C512_I2C_GenerateSTOP(); } return TRUE; }