eeprom.c

来自「Discription: This multi-master driver pr」· C语言 代码 · 共 66 行

C
66
字号
		 /*******************************************************************
 *
 *    DESCRIPTION:	eeprom.c - module for reading and writing EEPROM
 *
 *    AUTHOR:			 O. Kessel-Deynet
 *
 *      Lightmaze AG    
 *
 *******************************************************************/

/** include files **/
#include "Types/Sfr.h"
#include "i2c.h"
#include <string.h>
#include "Rtos.h"

extern byte wrtNoStopCond;

/** public functions **/

bool writeEEProm(byte adr, byte len, byte* pdata, byte devAdr)
{
	bool back = true;
	byte bcount, blen, arrayPoint;
	byte *bpdata;
	byte writeBuffer[17];
// EEPROM is flashed in pages of max. 16 bytes in one "row", i.e. with the
// same four most significant bits in the address.
	
	arrayPoint = 0;
	while(len > 0) {
		bcount = 0x10 - ((adr + arrayPoint) & 0x0f);
		blen = (bcount < len) ? bcount : len;
		bpdata = &(pdata[arrayPoint]);
		writeBuffer[0] = adr + arrayPoint;
		memcpy(&(writeBuffer[1]), bpdata, blen);
		if( masterWriteI2C( blen+1, writeBuffer, devAdr)) {
			back = false;
			break;
		}

		OS_Delay(10); // this delay is the duration of the internal write cycle of the EEPROM
		arrayPoint += blen;
		len -= blen;
	}
	return back;
}

bool readEEProm(byte adr, byte len, byte *pdata, byte devAdr)
{

// this flag determines that no stop condition should be sent after the dummy write.
// It is automatically cleared by the I2C driver after usage.
	bool back = true;
	wrtNoStopCond = 1;

	if( masterWriteI2C( 1, &adr, devAdr ))
	{
		back = false;
	} else {
		masterReadI2C(len, pdata, devAdr);
	}
	return back;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?