📄 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -