#include "24cxx.h"
#include "delay.h"
#include "sys.h"
#include "kg_info.h"
//////////////////////////////////////////////////////////////////////////////////
//本程序只供学习使用,未经作者许可,不得用于其它任何用途
//ALIENTEK战舰STM32开发板
//24CXX驱动 代码(适合24C01~24C16,24C32~256未经过测试!有待验证!)
//正点原子@ALIENTEK
//技术论坛:www.openedv.com
//修改日期:2012/9/9
//版本:V1.0
//版权所有,盗版必究。
//Copyright(C) 广州市星翼电子科技有限公司 2009-2019
//All rights reserved
//////////////////////////////////////////////////////////////////////////////////
static uint8_t D9_L_FACTORY[49] = {0x0B,0x00,0x00,0x01,0x00,0x00,0x01,0x00,
0x00,0x00,0x00,0x00,0x04,0x00,0x02,0x02,
0x02,0x02,0x02,0x02,0x03,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x04,
0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00};
static uint8_t D9_C_FACTORY[49] = {0x0B,0x00,0x00,0x04,0x00,0x00,0x01,0x00,
0x00,0x00,0x00,0x02,0x00,0x00,0x02,0x02,
0x02,0x02,0x02,0x02,0x03,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x04,
0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00};
static uint8_t D9_CG_FACTORY[49] = {0x0B,0x00,0x00,0x05,0x00,0x00,0x01,0x00,
0x00,0x00,0x00,0x02,0x00,0x00,0x02,0x02,
0x02,0x02,0x02,0x02,0x03,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x04,
0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00};
static uint8_t D9_D_FACTORY[49] = {0x0B,0x00,0x00,0x02,0x00,0x00,0x01,0x00,
0x00,0x00,0x00,0x02,0x00,0x00,0x02,0x02,
0x02,0x02,0x02,0x02,0x03,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x04,
0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00};
//初始化IIC接口
void AT24CXX_Init(void)
{
IIC_Init();
}
//在AT24CXX指定地址读出一个数据
//ReadAddr:开始读数的地址
//返回值 :读到的数据
u8 AT24CXX_ReadOneByte(u16 ReadAddr)
{
u8 temp=0;
IIC_Start();
if(EE_TYPE>AT24C16)
{
IIC_Send_Byte(0XA0); //发送写命令
IIC_Wait_Ack();
IIC_Send_Byte(ReadAddr>>8);//发送高地址
IIC_Wait_Ack();
}else IIC_Send_Byte(0XA0+((ReadAddr/256)<<1)); //发送器件地址0XA0,写数据
IIC_Wait_Ack();
IIC_Send_Byte(ReadAddr%256); //发送低地址
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte(0XA1); //进入接收模式
IIC_Wait_Ack();
temp=IIC_Read_Byte(0);
IIC_Stop();//产生一个停止条件
return temp;
}
//在AT24CXX指定地址写入一个数据
//WriteAddr :写入数据的目的地址
//DataToWrite:要写入的数据
void AT24CXX_WriteOneByte(u16 WriteAddr,u8 DataToWrite)
{
IIC_Start();
if(EE_TYPE>AT24C16)
{
IIC_Send_Byte(0XA0); //发送写命令
IIC_Wait_Ack();
IIC_Send_Byte(WriteAddr>>8);//发送高地址
}else
{
IIC_Send_Byte(0XA0+((WriteAddr/256)<<1)); //发送器件地址0XA0,写数据
}
IIC_Wait_Ack();
IIC_Send_Byte(WriteAddr%256); //发送低地址
IIC_Wait_Ack();
IIC_Send_Byte(DataToWrite); //发送字节
IIC_Wait_Ack();
IIC_Stop();//产生一个停止条件
delay_ms(10);
}
//在AT24CXX里面的指定地址开始写入长度为Len的数据
//该函数用于写入16bit或者32bit的数据.
//WriteAddr :开始写入的地址
//DataToWrite:数据数组首地址
//Len :要写入数据的长度2,4
void AT24CXX_WriteLenByte(u16 WriteAddr,u32 DataToWrite,u8 Len)
{
u8 t;
for(t=0;t<Len;t++)
{
AT24CXX_WriteOneByte(WriteAddr+t,(DataToWrite>>(8*t))&0xff);
}
}
//在AT24CXX里面的指定地址开始读出长度为Len的数据
//该函数用于读出16bit或者32bit的数据.
//ReadAddr :开始读出的地址
//返回值 :数据
//Len :要读出数据的长度2,4
u32 AT24CXX_ReadLenByte(u16 ReadAddr,u8 Len)
{
u8 t;
u32 temp=0;
for(t=0;t<Len;t++)
{
temp<<=8;
temp+=AT24CXX_ReadOneByte(ReadAddr+Len-t-1);
}
return temp;
}
void board_store_format(void) {
unsigned int i = 0;
unsigned char write[256] = {0x00};
unsigned int pos = 0;
for (i = 0; i < 8; i++) {//2k 8
AT24CXX_Write(pos, write, 256);
pos += sizeof(write);
}
}
//检查AT24CXX是否正常
//这里用了24XX的最后一个地址(255)来存储标志字.
//如果用其他24C系列,这个地址要修改
//返回1:检测失败
//返回0:检测成功
u8 AT24CXX_Check(void)
{
uint8_t read = 0x00;
read = AT24CXX_ReadOneByte(0);
LOG("read first %02x\r\n",read);
if (0x01 == read) {
return 0;
}
AT24CXX_WriteOneByte(0,0x55);
read = AT24CXX_ReadOneByte(0);
LOG("read second %02x\r\n",read);
if (0x55 == read) {
board_store_format();
AT24CXX_WriteOneByte(0, 0x01);
#if defined(SYS_KG_TYPE_D9_L)
AT24CXX_Write(STORE_SCR_NFC_SAVE_BASE,D9_L_FACTORY, 49);
#elif defined(SYS_KG_TYPE_D9_C)
AT24CXX_Write(STORE_SCR_NFC_SAVE_BASE,D9_C_FACTORY, 49);
#elif defined(SYS_KG_TYPE_D9_CG)
AT24CXX_Write(STORE_SCR_NFC_SAVE_BASE,D9_CG_FACTORY,49);
#elif defined(SYS_KG_TYPE_D9_D)
AT24CXX_Write(STORE_SCR_NFC_SAVE_BASE,D9_D_FACTORY, 49);
#else
#error "not define system switch type for flash factory"
#endif
LOG("flash first use\r\n");
return 0;
}
return 1;
}
//在AT24CXX里面的指定地址开始读出指定个数的数据
//ReadAddr :开始读出的地址 对24c02为0~255
//pBuffer :数据数组首地址
//NumToRead:要读出数据的个数
void AT24CXX_Read(u16 ReadAddr,u8 *pBuffer,u16 NumToRead)
{
while(NumToRead)
{
*pBuffer++=AT24CXX_ReadOneByte(ReadAddr++);
NumToRead--;
}
}
//在AT24CXX里面的指定地址开始写入指定个数的数据
//WriteAddr :开始写入的地址 对24c02为0~255
//pBuffer :数据数组首地址
//NumToWrite:要写入数据的个数
void AT24CXX_Write(u16 WriteAddr,u8 *pBuffer,u16 NumToWrite)
{
while(NumToWrite--)
{
AT24CXX_WriteOneByte(WriteAddr,*pBuffer);
WriteAddr++;
pBuffer++;
}
}