打印

一个基于DSP2812的DS1390串行EEPROM的驱动源代码

[复制链接]
591|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
Peonys|  楼主 | 2017-8-11 10:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
一个基于DSP2812的DS1390串行EEPROM的驱动源代码

一个基于DSP2812的DS1390串行EEPROM的驱动源代码
前段时间写的一个EEPROM的程序,已经测试通过。
常见的EEPROM总线结构共有三种:I2C,Microwire,SPI.。
DS1390就是一个很常见的串行EEPROM芯片。区别于不同的型号和厂家,引脚定义似稍有不同,但是大致类似的,需要注意的是CS不能直接拉高,哪怕系统里只有一个芯片,因为CS对时序的控制还有很多作用。其次,注意ORG将决定存贮器的组织方式,分为8X和16X两种。PE在有些厂家的芯片上没有(比如Fairchild的芯片),即为NC或NU,Microchip 的产品需要上拉到VCC。最后要注意的是CS最好加个下拉电阻。具体细节可以参照各型号的数据手册,另外也有些应用笔记可以参考,只要读懂一个系列uC的代码,操作顺序就是大同小异的,可以举一反三



以下就是源代码的部分函数:
#include <stdlib.h>
#include "DSP28_Device.h"
unsigned int uiData=0; // 16-bit Data word
/** P R O T O T Y P E S ********************************************/
void McEEP_Init(void);                         // Init. function
void McEEP_Bitout(void);                       // Bit output function
void McEEP_Bitin(void);                        // Bit input function
void McEEP_Byteout(void);                      // Byte output function
void McEEP_Bytein(void);                       // Byte input function
void McEEP_SendCommand(void);                  // Command output function
void McEEP_Poll(void);                         // Ready/Busy polling function
            
void McEEP_Ewen(void);                         // Erase/Write Enable function
void McEEP_Ewds(void);                         // Erase/Write Disable function
void McEEP_WriteByte(unsigned char);           // 8-bit Write function
void McEEP_WriteWord(unsigned int);            // 16-bit Write function
void McEEP_WriteAllWord(unsigned int);         // Write all cells with same value
unsigned char McEEP_ReadByte(void);            // 8-bit Read function
unsigned int McEEP_ReadWord(void);             // 16-bit Read function
void McEEP_EraseByte(void);        // 8-bit Erase function  
void McEEP_EraseWord(void);        // 16-bit Erase function  
void McEEP_EraseAll(void);        // Erase all cells
void Set_Delay(unsigned int);     //delay time
void Delay_10ms(void);            //delay time


/********************************************************************
* Function:         void EEP_Bitout(void)
*
* Description:      This function outputs the MSb of buffer to the
*                   serial EEPROM device.
*******************************************************************/
void McEEP_Bitout(void)
{
//     unsigned char i=0;
     if (ucBuffer & 0x80)                   // Check if next bit is a 1
     {                                    // Get MSb
         GpioDataRegs.GPFDAT.bit.GPIOF0=1; // If so, send a 1
     }
     else                                 // Otherwise
     {
         GpioDataRegs.GPFDAT.bit.GPIOF0=0; // Send a 0
     }
     Delay_10ms();                        // Avoid violating Tckh,may be more   
     GpioDataRegs.GPFDAT.bit.GPIOF2=1;    // Bring SCK high to latch data
     Delay_10ms();                        // Avoid violating Tckh,may be more
     GpioDataRegs.GPFDAT.bit.GPIOF2=0;    // Bring SCK low for next bit
     Delay_10ms();                        // Avoid violating Tckh,may be more
} //end (void)
/********************************************************************
* Function:         void EEP_Bitin(void)
*
* Description:      This function inputs a bit from the serial EEPROM
*                   device and stores it in the LSb of buffer.
*******************************************************************/
void McEEP_Bitin(void)
{
//     unsigned char i=0;
     ucBuffer &= 0xFE;                    // Assume next bit(low bit) will be 0
     GpioDataRegs.GPFDAT.bit.GPIOF2=1;    // Bring SCK to latch data
     Delay_10ms();                        // Avoid violating Tckh
     GpioDataRegs.GPFDAT.bit.GPIOF2=0;    // Bring SCK low, and read at low jump
     Delay_10ms();
     if (GpioDataRegs.GPFDAT.bit.GPIOF1==1) // Check if DI is high(1)
     {
         ucBuffer |= 0x01;                // If high, set next bit
     }                                    // else remain the LSb as 0
} //end McEEP_Bitin(void)
/********************************************************************
* Function:         void EEP_Byteout(void)
*
* Description:      This function outputs the byte specified in
*                   buffer to the serial EEPROM device.
*******************************************************************/
void McEEP_Byteout(void)
{
     unsigned char i;                     // Loop counter
     for (i = 0; i < 8; i++)              // Loop through each bit
     {
         McEEP_Bitout();                  // Output bit
         ucBuffer = ucBuffer << 1;        // Rotate left for next bit
     }
} // end McEEP_Byteout(void)

/********************************************************************
* Function:         void EEP_Bytein(void)
*
* Description:      This function inputs a byte from the serial
*                   EEPROM device and stores it in buffer.
*******************************************************************/
void McEEP_Bytein(void)
{
     unsigned char i;                     // Loop counter
     ucBuffer = 0;
     for (i = 0; i < 8; i++)              // Loop through each bit
     {
         ucBuffer = ucBuffer << 1;        // Rotate left for next bit
         McEEP_Bitin();                   // Input bit
     }
} // end McEEP_Bytein(void)

/********************************************************************
* Function:         void EEP_SendCommand(void)
*
* Description:      This function sends the Start bit and opcode
*                   specified in the MSb's of ucCommand, as well as
*                   the required number of uiAddress or dummy bits,
*                   to the serial EEPROM device.
*******************************************************************/
void McEEP_SendCommand(void)
{
     static unsigned char i;              // Loop counter
     static unsigned int cmd_addr;        // Variable for ucCommand & uiAddress
     static unsigned int temp;            // Temp. variable
     cmd_addr = uiAddress;                // Copy uiAddress to cmd_addr;
   
     // First, align uiAddress bits to be combined with ucCommand
     for (i = 0; i < (16-NUMBITS); i++)   // Skip through unused addr. bits
     {
         cmd_addr = cmd_addr << 1;        // Rotate left to skip bit
     }
     // Next, combine ucCommand into uiAddress word
     cmd_addr &= 0x1FFF;                  // Mask off upper 3 bits
     temp = ucCommand;                    // Copy ucCommand value to temp
     cmd_addr |= (temp<<8);               // Combine uiAddress & ucCommand
     // Finally, output entire ucCommand to device
     for (i = 0; i < NUMBITS; i++)        // Loop through each bit
     {
         ucBuffer = (char)(cmd_addr >> 8); // Copy uiAddress MSB to buffer
         McEEP_Bitout();                  // Output next bit
         cmd_addr = cmd_addr << 1;        // Rotate left for next bit
     }
} // end McEEP_SendCommand(void)

/********************************************************************
* Function:         void EEP_Poll(void)
*
* Description:      This function brings CS high to initiate the
*                   Ready/Busy polling feature. DO is then continuously
*                   polled to see when it goes high, thus indicating
*                   that the write cycle has completed.
*******************************************************************/
void McEEP_Poll(void)
{
     GpioDataRegs.GPFDAT.bit.GPIOF3=1;    // Set CS high
     Delay_10ms();                        // Avoid violating Tsv
     while (GpioDataRegs.GPFDAT.bit.GPIOF1==0) {}; // Wait until DI is high
     GpioDataRegs.GPFDAT.bit.GPIOF3=0; // Bring CS low
} // end McEEP_Poll(void)

/********************************************************************
* Function:         void EEP_Ewen(void)
*
* Description:      This function sends an EWEN ucCommand to the device.
*                   Once this ucCommand has been given, writing to the
*                   device array will be enabled, and will remain as
*                   such until an EWDS ucCommand is given or power is
*                   removed from the device.
*******************************************************************/
void McEEP_Ewen(void)
{
     ucCommand=EWEN_CMD;                // Load EWEN ucCommand value
     uiAddress=0;                       // Clear uiAddress word
     GpioDataRegs.GPFDAT.bit.GPIOF3=1;    // Enable Chip Select
     McEEP_SendCommand();                 // Output ucCommand to device
     GpioDataRegs.GPFDAT.bit.GPIOF3=0;    // Disable Chip Select
     Delay_10ms();
} // end Ewen(void)

/********************************************************************
* Function:         void EEP_Ewds(void)
*
* Description:      This function sends an EWDS ucCommand to the device.
*                   It is good practice to always send this ucCommand
*                   after completing a write, so as to avoid the
*                   corruption of data stored in the device.
*******************************************************************/
void McEEP_Ewds(void)
{
     ucCommand=EWDS_CMD;                // Load EWDS ucCommand value
     uiAddress=0;                       // Clear uiAddress word
     GpioDataRegs.GPFDAT.bit.GPIOF3=1;    // Enable Chip Select
     McEEP_SendCommand();                 // Output ucCommand to device
     GpioDataRegs.GPFDAT.bit.GPIOF3=0;    // Disable Chip Select
} // end Ewds(void)

/********************************************************************
* Function:         void EEP_WriteByte(unsigned char data)
*
* Description:      This function writes the 8-bit value stored in
*                   data to the serial EEPROM device, at the location
*                   specified by uiAddress.
*******************************************************************/
void McEEP_WriteByte(unsigned char data)
{
     ucCommand = WRITE_CMD;               // Load WRITE ucCommand value
     GpioDataRegs.GPFDAT.bit.GPIOF3=1;    // Enable Chip Select
     McEEP_SendCommand();                 // Output ucCommand to device
     ucBuffer = data;                     // Copy data to buffer
     McEEP_Byteout();                     // Output byte
     GpioDataRegs.GPFDAT.bit.GPIOF3=0;    // Disable Chip Select
     Delay_10ms();
     McEEP_Poll();                        // Begin ready/busy polling
} // end WriteX8(unsigned char data)
/********************************************************************
* Function:         void EEP_WriteWord(unsigned int data)
*
* Description:      This function writes the 16-bit value stored in
*                   data to the serial EEPROM device, at the location
*                   specified by uiAddress.
*******************************************************************/
void McEEP_WriteWord(unsigned int data)
{
     ucCommand = WRITE_CMD;               // Load WRITE ucCommand value
   
     GpioDataRegs.GPFDAT.bit.GPIOF3=1;    // Enable Chip Select
     McEEP_SendCommand();                 // Output ucCommand to device
     ucBuffer = (char)(data >> 8);        // Copy data MSB to buffer
     McEEP_Byteout();                     // Output byte,发高8位数据
     ucBuffer = (char)data;               // Copy data LSB to buffer
     McEEP_Byteout();                     // Output byte,发低8位数据
     GpioDataRegs.GPFDAT.bit.GPIOF3=0;    // Disable Chip Select
     Delay_10ms();      // Delay for Tcs low
     McEEP_Poll();                        // Begin ready/busy polling
} // end WriteX16(unsigned int data)

/********************************************************************
* Function:         unsigned char EEP_ReadByte(void)
*
* Description:      This function reads an 8-bit value from the
*                   serial EEPROM device, from the location
*                   specified by uiAddress, returns it.
*******************************************************************/
unsigned char McEEP_ReadByte(void)
{
     ucCommand = READ_CMD;                // Load READ ucCommand value
     GpioDataRegs.GPFDAT.bit.GPIOF3=1;    // Enable Chip Select
     McEEP_SendCommand();                 // Output ucCommand to device
     McEEP_Bytein();                      // Input byte from device
     GpioDataRegs.GPFDAT.bit.GPIOF3=0;    // Disable Chip Select
     return ucBuffer;                     // Return value
} // end ReadX8(void)

/********************************************************************
* Function:         unsigned int EEP_ReadWord(void)
*
* Description:      This function reads a 16-bit value from the
*                   serial EEPROM device, from the location
*                   specified by uiAddress, returns it.
*******************************************************************/
unsigned int McEEP_ReadWord(void)
{
     unsigned int retval;                 // Return value variable
     ucCommand = READ_CMD;                // Load READ ucCommand value
     GpioDataRegs.GPFDAT.bit.GPIOF3=1;    // Enable Chip Select
     McEEP_SendCommand();                 // Output ucCommand to device
     McEEP_Bytein();                      // Input byte from device
     retval = ucBuffer;                   // Copy byte to retval MSB
     McEEP_Bytein();                      // Input byte from device
     retval = (retval << 8) | ucBuffer;   // Copy byte to retval LSB
     GpioDataRegs.GPFDAT.bit.GPIOF3=0;    // Disable Chip Select
     Delay_10ms();                        // Avoid violating Tckh,may be more
    ucBuffer = 0;
     return retval;                       // Return value
} // end ReadX16(void)

/********************************************************************
* Function:         void McEEP_EraseByte();
*
* Description:      This function erase a 8-bit value from the
*                   serial EEPROM device, from the location
*                   specified by uiAddress.
*******************************************************************/
void McEEP_EraseByte()
{
   ucCommand = ERASE_CMD;    // Load ERASE ucCommand value
   GpioDataRegs.GPFDAT.bit.GPIOF3=1; // Enable Chip Select
   McEEP_SendCommand();    // Output ucCommand to device
   GpioDataRegs.GPFDAT.bit.GPIOF3=0; // Disable Chip Select
   Delay_10ms();      // Avoid violating timming  
   McEEP_Poll();   
}

/********************************************************************
* Function:         void McEEP_EraseWord(void)
*
* Description:      This function erase a 16-bit value from the
*                   serial EEPROM device, from the location
*                   specified by uiAddress.
*******************************************************************/
void McEEP_EraseWord(void)
{
   ucCommand = ERASE_CMD;     // Load ERASE ucCommand value
   GpioDataRegs.GPFDAT.bit.GPIOF3=1; // Enable Chip Select
   McEEP_SendCommand();     // Output ucCommand to device
   GpioDataRegs.GPFDAT.bit.GPIOF3=0; // Disable Chip Select
   Delay_10ms();       // Avoid violating timming  
   McEEP_Poll();       // Begain Ready/Busy polling
}
/********************************************************************
* Function:         void McEEP_EraseAll(void)
*
* Description:      This function erase all cells' value from the
*                   serial EEPROM device.
*******************************************************************/
void McEEP_EraseAll(void)
{
    ucCommand = ERAL_CMD;      // Load ERAL ucCommand value
    GpioDataRegs.GPFDAT.bit.GPIOF3=1; // Enable Chip Select
    McEEP_SendCommand();      // Output ucCommand to device
    GpioDataRegs.GPFDAT.bit.GPIOF3=0; // Disable Chip Selcet
    Delay_10ms();        // Avoid violating Tc  
    McEEP_Poll();        // Begain Ready/Busy polling
}

/********************************************************************
* Function:         void McEEP_WriteAllWord(unsigned int)
*
* Description:      This function write all cells with same value .
*                  
*******************************************************************/
void McEEP_WriteAllWord(unsigned int data)
{
     ucCommand = WRAL_CMD;                // Load WRITE ucCommand value
     GpioDataRegs.GPFDAT.bit.GPIOF3=1;    // Enable Chip Select
     McEEP_SendCommand();                 // Output ucCommand to device
     ucBuffer = (char)(data >> 8);        // Copy data MSB to buffer
     McEEP_Byteout();                     // Output byte
     ucBuffer = (char)data;               // Copy data LSB to buffer
     McEEP_Byteout();                     // Output byte
     GpioDataRegs.GPFDAT.bit.GPIOF3=0;    // Disable Chip Select
     Delay_10ms();      // Delay for Tcs low
     McEEP_Poll();                        // Begin ready/busy polling  
}

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

640

主题

901

帖子

5

粉丝