'---------------------------------------------------------------------------------------- ' Name: Eeprom_M24256-B.tig ' Type: TIGER-BASIC(tm) Source Code (example) ' Purpose: Basic subroutines for eeprom implementation ' ' (C) - Copyright Wilke Technology, P.O.Box 1727, D-52018 Aachen, Germany '---------------------------------------------------------------------------------------- ' ' Thank you for using BASIC Tigers in your products. If you have questions, ideas ' or special needs, please contact your next distributor or the Tiger support team ' and visit our web site: ' ' Wilke Technology GmbH ' The Tiger Support Team ' P.O.Box 1727, D-52018 Aachen, Germany ' Krefelder Str. 147, D-52070 Aachen, Germany ' ' email: support@wilke-technology.com (english) ' email: support@wilke.de (german) ' Phone: +WIDTH_BUT (241) 918 900 Mo to Fr, 7:00 to 16:00 (GMT) ' Fax: +WIDTH_BUT (241) 918 9068 ' ' New information, new drivers and free downloads see: ' ' www.wilke-technology.com (english) ' www.wilke.de (german) ' ' Sincerely, ' ' Your Tiger Support Team ' ' '-------------------------------------------------------------------------------------- ' These subroutines are for reading and writing an EEPROM type M24256-B and M24128-B ' which are used on the TP1000. ' ' ' Writing to EEPROM: ' ------------------ ' ' call bWriteEeprom( WriteData$, Address, Length ) ' ^ ^ ^ ' | | °----- Number of bytes to be written ' | | (WORD) ' | | ' | °------------- Starting address for write ' | in EEPROM (LONG) ' | ' °------------------------ String with data to be written ' ' Reading from EEPROM: ' -------------------- ' ' call sReadEeprom( ReadData$, Address, Length ) ' ^ ^ ^ ' | | °----- Number of bytes to be read (WORD) ' | | ' | °-------------- Starting address for read in ' | EEPROM (LONG) ' | ' °------------------------ String for read data ' '-------------------------------------------------------------------------------------- #define EEPROM_PORT 7 #define EEPROM_CLK 0 #define EEPROM_DATA 1 #define EEPROM_SPEED_REDUCTION 20 ' 0=no speed reduction, 1..255=speed reduction #define EEPROM_WRITE_OK 82h #define EEPROM_READ_OK 83h #define EEPROM_SETUP_OK 84h #define EEPROM_CMD_WRITE "A0"% ' DevSel for write operations #define EEPROM_CMD_READ "A1"% ' DevSel for read operations #define EEPROM_LEN_I2CL_STR 32 #define EEPROM_MAX_WRITING_BYTES 29 ' = 32 - 2addr - 1cmd '4 #define EEPROM_MAX_READING_BYTES 32 #define EEPROM_SIZE 64k #define EEPROM_PAGE_SIZE 128 #define WRITE_EEPROM_RETRYS 10 '-----------------------------------------------' ' stub '-----------------------------------------------' task main I2CL_SETUP ( EEPROM_PORT, EEPROM_CLK, EEPROM_DATA, EEPROM_SPEED_REDUCTION) ' Setup the Ports used to comunicate end '-----------------------------------------------' ' write on eeprom '-----------------------------------------------' sub bWriteEeprom( string spWriteData$; long lpAddr; word wpLen ) string slI2C$(EEPROM_LEN_I2CL_STR) word wlPos, wlLenWrite, wlLenRest, wlNAK byte blRetrys, blButton wlLenRest = wpLen wlPos = 0 blRetrys = 0 while wlPos < wpLen set_bit lgWatchdogList, sysvarn( ACT_TASK, 0 ) '' Build string for writing to bus wlLenWrite = limit( wlLenRest, 0, EEPROM_MAX_WRITING_BYTES ) '' shorten write length, if it reaches over page limit wlLenWrite = limit( wlLenWrite, 0, EEPROM_PAGE_SIZE - mod( lpAddr+wlPos, EEPROM_PAGE_SIZE ) ) slI2C$ = EEPROM_CMD_WRITE + "00 00"% + mid$( spWriteData$, wlPos, wlLenWrite ) slI2C$ = ntos$( slI2C$, 1, -2, lpAddr+wlPos ) I2CL_START(EEPROM_SPEED_REDUCTION) '' Set START condition on bus wlNAK = I2CL_WRITE(slI2C$) '' Write DevSel, memory addr. & data to bus I2CL_STOP(EEPROM_SPEED_REDUCTION) '' Set STOP condition on bus: now written bytes will be saved in eeprom if wlNAK = 0 then ' all bytes have been acknowleged wlPos = wlPos + wlLenWrite wlLenRest = wlLenRest - wlLenWrite blRetrys = 0 else ' not acknowledged bytes existing blRetrys = blRetrys + 1 if blRetrys > WRITE_EEPROM_RETRYS then '+++++++++++++++++++++++++++++++++++++++++++++++++ ' place your error handling here '+++++++++++++++++++++++++++++++++++++++++++++++++ wlPos = wpLen ' exit loop endif endif endwhile end '-----------------------------------------------' ' Read from eeprom '-----------------------------------------------' sub sReadEeprom( var string svpReadData$; long lpAddr; word wpLen ) string slI2C$(EEPROM_LEN_I2CL_STR) long llAddr word wlLenRead, wlNak1, wlNak2 long llLenRest string slResult$(EEPROM_MAX_READING_BYTES) byte blRetrys, blButton slResult$ = "" llAddr = lpAddr llLenRest = wpLen blRetrys = 0 while 0 < llLenRest set_bit lgWatchdogList, sysvarn( ACT_TASK, 0 ) wlLenRead = limit( llLenRest, 0, EEPROM_MAX_READING_BYTES ) '' Build string for writing BYTE addr. to bus (HI + LO byte) slI2C$ = EEPROM_CMD_WRITE slI2C$ = ntos$( slI2C$, 1, -2, llAddr) I2CL_START(EEPROM_SPEED_REDUCTION) '' Set START condition on bus wlNak1 = I2CL_WRITE(slI2C$) '' Write DevSel + memory addr. to bus for addr. selection I2CL_START(EEPROM_SPEED_REDUCTION) '' Set START condition on bus wlNak2 = I2CL_WRITE(EEPROM_CMD_READ) '' Write DevSel to bus for read operation slResult$ = I2CL_READ$(wlLenRead) '' READ wlLenRead bytes of addr. content from I2C-Bus I2CL_STOP(EEPROM_SPEED_REDUCTION) '' Set STOP condition on bus: now written bytes will be saved in eeprom if wlNak1 + wlNak2 = 0 and len(slResult$) = wlLenRead then ' all bytes have been acknowleged svpReadData$ = svpReadData$ + slResult$ llAddr = llAddr + wlLenRead llLenRest = llLenRest - wlLenRead blRetrys = 0 else ' not acknowledged bytes existing blRetrys = blRetrys + 1 if blRetrys > WRITE_EEPROM_RETRYS then '+++++++++++++++++++++++++++++++++++++++++++++++++ ' place your error handling here '+++++++++++++++++++++++++++++++++++++++++++++++++ llLenRest = 0 ' exit loop endif endif endwhile end