www.pudn.com > SMSC USB2.0.zip > eeprom.c
/*============================================================================
____________________________________________________________________________
______________________________________________
SSSS M M CCCC Standard Microsystems Corporation
S MM MM SSSS C Austin Design Center
SSS M M M S C 11000 N. Mopac Expressway
S M M SSS C Stonelake Bldg. 6, Suite 500
SSSS M M S CCCC Austin, Texas 78759
SSSS ______________________________________________
____________________________________________________________________________
Copyright(C) 1999, Standard Microsystems Corporation
All Rights Reserved.
This program code listing is proprietary to SMSC and may not be copied,
distributed, or used without a license to do so. Such license may have
Limited or Restricted Rights. Please refer to the license for further
clarification.
____________________________________________________________________________
Notice: The program contained in this listing is a proprietary trade
secret of SMSC, Hauppauge, New York, and is copyrighted
under the United States Copyright Act of 1976 as an unpublished work,
pursuant to Section 104 and Section 408 of Title XVII of the United
States code. Unauthorized copying, adaption, distribution, use, or
display is prohibited by this law.
____________________________________________________________________________
Use, duplication, or disclosure by the Government is subject to
restrictions as set forth in subparagraph(c)(1)(ii) of the Rights
in Technical Data and Computer Software clause at DFARS 52.227-7013.
Contractor/Manufacturer is Standard Microsystems Corporation,
80 Arkay Drive, Hauppauge, New York, 1178-8847.
____________________________________________________________________________
____________________________________________________________________________
eeprom.h.h - Contains code to communicate with the I2EPROM.
Microchip or eq. see eeprom.h for details.
____________________________________________________________________________
comments tbd
____________________________________________________________________________
Revision History
Date Who Comment
________ ___ _____________________________________________________________
06/26/01 rcc initial version
09/25/01 tbh rearranged
11/25/01 cds made eeprom_write_disable non-static
============================================================================*/
#include "project.h"
//------------------------------------------------------------------------------
//the registers below are gpio7(used for clocking EEPROM)
//and gpio4 is used for data in of EEPROM
//gpio2 is used for chip select
//gpio3 is used for data out
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// prototypes
// function to set gpio4 "sclock" alternately high then low
static void clock_out(void) reentrant;
// function to send up start bit procedure on EEPROM
static void start_bit(void) reentrant;
// function to send up stop bit procedure on EEPROM
static void stop_bit(void) reentrant;
// transmit bit out gipo pin
static void trans_bit(uint8 bitwrite) reentrant;
// transmit byte takes a byte to transmit
static void byte_out(uint8 outbyte) reentrant;
// function to get bit in from gpio5.
static uint8 recv_bit(void) reentrant;
// delay component
static void delay(uint8 timeout)reentrant;
// delay needed after each byte write
static void eeprom_write_delay(void) reentrant;
//Always call this function before attempting to write to eeprom
// enables eeprom for writing
//------------------------------------------------------------------------------
// useful macros
#define _delay_one_micro();
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
#define _set_util_config_reg(); \
{ x_util_config = kbm_set_util_reg_as_gpios; }
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
#define _eesclock_gpio_high(); \
{ x_gpioa_out |= CLK; }
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
#define _eesclock_gpio_low(); \
{ x_gpioa_out &= ~CLK; }
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
#define _eesdata_gpio_high(); \
{ x_gpioa_out |= DI; }
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
#define _eesdata_gpio_low(); \
{ x_gpioa_out &= ~DI; }
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
#define _eeschp_sel_gpio_high(); \
{ x_gpioa_out |= CS; }
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
#define _eeschp_sel_gpio_low(); \
{ x_gpioa_out &= ~CS; }
//------------------------------------------------------------------------------
// Send two 'write' op code "two bits"
//------------------------------------------------------------------------------
#define _opcode_write(); \
{ \
_eesdata_gpio_low(); \
clock_out(); \
_eesdata_gpio_high(); \
clock_out(); \
}
//------------------------------------------------------------------------------
// Send two 'read' op code "two bits"
//------------------------------------------------------------------------------
#define _opcode_read(); \
{ \
_eesdata_gpio_high(); \
clock_out(); \
_eesdata_gpio_low(); \
clock_out(); \
}
//------------------------------------------------------------------------------
//Send two 'enable' op code "two bits"
//------------------------------------------------------------------------------
#define _opcode_ewds_ewen(); \
{ \
_eesdata_gpio_low(); \
clock_out(); \
_eesdata_gpio_low(); \
clock_out(); \
}
//------------------------------------------------------------------------------
//Start Bit routine start_bit
//------------------------------------------------------------------------------
static void start_bit(void) reentrant
{
_set_util_config_reg();
//Set direction as out
//for CLK,DI, and CS
x_gpioa_dir |=(CLK|DI|CS);
_eeschp_sel_gpio_high();
//set data in high
_eesdata_gpio_high();
//send clock
clock_out();
}
//------------------------------------------------------------------------------
//Stop bit routine
//------------------------------------------------------------------------------
static void stop_bit(void) reentrant
{
//Send clock low
_eesclock_gpio_low();
//send CS low
_eeschp_sel_gpio_low();
//Delay a bit
_nop_();
}
//------------------------------------------------------------------------------
//clock_out Function to act as clock train keep calling
//to get a train of clocks starts low leaves sclock low
//------------------------------------------------------------------------------
static void clock_out(void) reentrant
{
//make sure clock is indeed low _|-|_
_eesclock_gpio_low();
//Delay a bit
_nop_();
//start clock train clock goes high
_eesclock_gpio_high();
//Delay a bit
_nop_();
//clock goes low
_eesclock_gpio_low();
//Delay a bit
_nop_();
}
//------------------------------------------------------------------------------
//trans_bit Function to transmit a bit to the EEPROM
//accepts k_pin_high(one) or k_pin_low(zero) as an argument BOOL
//------------------------------------------------------------------------------
static void trans_bit(uint8 bitwrite) reentrant
{
//if high set data line
if(bitwrite)
{
_eesdata_gpio_high();
}
//if low set data line
else
{
_eesdata_gpio_low();
}
//toggle the clock
clock_out();
}
//------------------------------------------------------------------------------
//bit_out Function takes a byte and transmits it to the EEPROM
//------------------------------------------------------------------------------
static void byte_out(uint8 outbyte) reentrant
{
uint8 x;
//Start with MSB to transmit!!!!!!!!!!!!!!!!!!!
//Will loop 8 times(8 bits)
for(x=0; x<=7; x++)
{
if((outbyte & kbm_MSB_only_mask))
{ //its a one send a one
trans_bit(k_pin_high);
}
else
{
trans_bit(k_pin_low);
} //its a zero send one
outbyte = outbyte << 1; //Rotate byte left by 1
}
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
static uint8 recv_bit() reentrant
{
uint8 inbit = 0x00;
_set_util_config_reg(); //set up so these pins are gpios
//Set direction in
x_gpioa_dir &= ~DO; //sets direction to input
//clock goes low -- just in case it was high before
_eesclock_gpio_low();
//Delay a bit
_nop_();
//start clock train clock goes high
_eesclock_gpio_high();
_nop_();
inbit = x_gpioa_in & DO; //stick in inbit is in wrong bit position though
//MSB comes first from gpio 3
//should rotate
inbit = inbit >> k_data_rotate_right; //now its in LSB spot
//clock goes low
_eesclock_gpio_low();
//Delay a bit
_nop_();
return(inbit); //return in MSB of inbit
}
//------------------------------------------------------------------------------
//bit_out Function takes an address and receives it from the EEPROM
//Sent start, write addr, byte in returns a BYTE
//------------------------------------------------------------------------------
static uint8 byte_in() reentrant
{
uint8 temp = 0;
uint8 x;
//Start with MSB to receive Yes!!!!! rcc
//Will loop 8 times(8 bits)
for(x=0; x<=7; x++)
{
temp = temp << 1; //Rotate byte left by 1
temp = temp | recv_bit();
}
return(temp);
}
//------------------------------------------------------------------------------
//function to enable erase and write features
//on power up chip is disabled from write until is receives this function
//Must call before writes can happen!!!
//------------------------------------------------------------------------------
void eeprom_write_enable() reentrant
{
uint8 x;
//Send start bit
start_bit();
_opcode_ewds_ewen();
//Send 9 1s to enable write/erase
for(x=0; x<=8; x++)
{
_eesdata_gpio_high(); //clock in a 1
clock_out();
}
//clean up
_eesdata_gpio_low(); //put data in low
//send stop bit to CS
stop_bit();
}
//------------------------------------------------------------------------------
//function to disable erase and write features
//on power up chip is disabled from write
//------------------------------------------------------------------------------
void eeprom_write_disable() reentrant
{
int x;
//Send start bit
start_bit();
//Send two op code "two bits"
_opcode_ewds_ewen();
//Send 9 0s to disable write/erase
for(x=0; x<=8; x++)
{
_eesdata_gpio_low(); //clock in a 0
clock_out();
}
//clean up
_eesdata_gpio_low(); //put data in low
//send stop bit to CS
stop_bit();
}
//------------------------------------------------------------------------------
//function to write byte at address
//parameters are address and byte to write
//------------------------------------------------------------------------------
void eeprom_write(uint8 addr, uint8 dataout) reentrant
{
trace2(0, dev, 0, "eeprom_write(%d, %02X)", addr, dataout) ;
//Send start bit
start_bit();
//Send two write op code "two bits"
_opcode_write();
//send extra clock
// tbh lo on 9 bit address for '66 part
_eesdata_gpio_low(); \
clock_out();
//send address byte
byte_out(addr); //This can be confusing write MSB first
byte_out(dataout); //data goes out
trace2(0, eep, 0, "byte_write = %02X addr = %02X", dataout, addr);
//send stop bit to CS
stop_bit();
eeprom_write_delay();
}
//------------------------------------------------------------------------------
//function to read byte at address
//parameters are address and return byte MSB first
//------------------------------------------------------------------------------
uint8 eeprom_read(uint8 addr) reentrant
{
uint8 bytein;
trace1(0, dev, 0, "eeprom_read(%d)", addr) ;
//Send start bit
start_bit();
//Send two read op code "two bits"
_opcode_read();
//send extra clock
clock_out();
//send address byte
byte_out(addr);
//clock the byte in
bytein = byte_in();
trace2(0, eep, 0, "byte_read = %02X addr = %02X", bytein, addr);
//send stop bit to CS
stop_bit();
return(bytein);
}
//------------------------------------------------------------------------------
//Function to delay 4 msec after each byte write
//------------------------------------------------------------------------------
void eeprom_write_delay() reentrant
{
uint16 x;
for(x=0; x<120; x++)
{
delay(0xFF);
}
}
//------------------------------------------------------------------------------
//Function to delay
//------------------------------------------------------------------------------
static void delay(uint8 timeout) reentrant
{
if(timeout <= 6)
return;
// If we got here, timeout was >=7, so will be >=1 after subtracting 6.
// As a result, we do not have to worry about wrap-around on pre-decrement.
timeout -= 1;
while(--timeout)
{
_nop_();
}
}
#if 0
****************************************************************************
* delay
****************************************************************************
* Function provides a(fairly) accurate software-controlled delay @ MCUCLK=30 MHZ.
* For requested delays <= 6uS, the actual delay is 5 uS.
* For delays > 6uS, the actual delay is 0.5 uS longer than requested.
* The above times include call/return overhead.
* CHECK ABOVE CALCULATION
****************************************************************************
*
* Looking at the compiler listing, the overhead, including call/return,
* is ?? instruction cycles, which is ??? uS @ 30 MHZ.
* need to calculate time base on new 3 clocks/cycle 8051 used in 201 chip
* As a result, this code subtracts 6 uS from the requested value,
* and returns immediately if it is 6 is or less; if this happens,
* the actual delay time is 5 uS.
; FUNCTION _?delay(BEGIN)
0000 1500 E DEC ?C_IBP
0002 A800 E MOV R0,?C_IBP
0004 A607 MOV @R0,AR7
; SOURCE LINE # 284
; SOURCE LINE # 286
0006 A800 E MOV R0,?C_IBP
0008 E6 MOV A,@R0
0009 D3 SETB C
000A 9406 SUBB A,#06H
000C 400C JC ?C0033
000E ?C0032:
; SOURCE LINE # 292
000E A800 E MOV R0,?C_IBP
0010 16 DEC @R0
0011 ?C0034:
; SOURCE LINE # 293
0011 A800 E MOV R0,?C_IBP
0013 16 DEC @R0
0014 E6 MOV A,@R0
0015 6003 JZ ?C0033
; SOURCE LINE # 294
; SOURCE LINE # 295
0017 00 NOP
; SOURCE LINE # 296
0018 80F7 SJMP ?C0034
; SOURCE LINE # 297
001A ?C0033:
001A 0500 E INC ?C_IBP
001C 22 RET
; FUNCTION _?delay(END)
#endif
//---eof------------------------------------------------------------------------