⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eepromp3.c

📁 TDK 6521 SOC 芯片 DEMO程序
💻 C
字号:
/***************************************************************************
 * This code and information is provided "as is" without warranty of any   *
 * kind, either expressed or implied, including but not limited to the     *
 * implied warranties of merchantability and/or fitness for a particular   *
 * purpose.                                                                *
 *                                                                         *
 * Copyright (C) 2005 Teridian Semiconductor Corp. All Rights Reserved.    *
 ***************************************************************************/
//**************************************************************************
//  DESCRIPTION: 71M651x POWER METER - Serial EEPROM Routines. 
// 
//  AUTHOR:  RGV
//
//  HISTORY: See end of file
//**************************************************************************
// File: eepromp3.c
//**************************************************************************
// High speed polling eeprom API for a Microchip 93C76C
// This can use either:
// uwreep.c, a fast, less flexible interface that uses the
// chip's on-board uwire hardware.
// uwrdio.c a bit-banging interface with more clock polarities, and
// provisions to drive SPI devices.
// Both have the same interface, uwr.h
//
#include "options.h"
#if EEPROM && (UWR_FAST || UWR_SW)
#include "stm.h"
#include "eeprom.h"
#include "uwr.h" // polling microwire interface.

static uint16x_t page_size, time_to_write;
enum EEPROM_RC data eeprom_state;

// Initialize the EEPROM interface for polling access
#pragma save
#pragma NOAREGS
void EEProm_Config (uint8_t access, uint16_t spg, uint8_t tWr) small reentrant
{
    eeprom_state = _OK;
    if (access)
    {
       page_size = spg;
       time_to_write = tWr;
   
       uwr_init();  // initialize the microwire bus
    }
    else
    {
       EX_EEPROM  = FALSE;           // Disable EEPROM non-busy interrupt.
       #if TRACE10 || M6520
       DIO &= ~DIO_EEX;              // Disconnect from external EEPROM.
       #else
       #error unhandled device type
       #endif
    }    
}
#pragma restore

#if EXTRAS
// Copy four (4) bytes to eeprom; 
enum EEPROM_RC data *memset_pr  (uint32_t dst, int32_t src)
{
    int32_t xdata xsrc = src;

    return (memcpy_prx (dst, (uint8x_t *) &xsrc, 4));
}
#endif

// manages the write enable/disable 
#pragma save
#pragma NOAREGS
static uint8_t lock(uint8_t locked) small reentrant
{
    uint8_t ok;
    static uint8_t xdata cmd[2];
    // form a 93C76C write disable or enable command:
    // Where a is the address, d is the data
    if (locked)
        cmd[0] = 0x20;  // 0010000x.xxxxxxxx == write disable
    else
        cmd[0] = 0x26;  // 0010011x.xxxxxxxx == write enable
    uwr_select (UWR_93C76C_0);
    ok = uwr_write(&cmd[0], 2);
    uwr_select (UWR_NO_DEVICE);
    return ok;
}
#pragma restore

// copy a number of bytes from xdata to eeprom; 
#pragma save
#pragma NOAREGS
enum EEPROM_RC data *memcpy_prx(uint32_t dst, 
    uint8x_t *src, int16_t len) small reentrant
{
    static uint8_t xdata cmd[3];

    if (eeprom_state != _OK)
        return &eeprom_state;

    eeprom_state = _PENDING; 

    if ( 0 == lock (0) ) // write-enable the chip
    {
        lock (1);
        eeprom_state = _ERR_NACK; 
        return &eeprom_state;
    }
    
    while (len > 0 && eeprom_state == _PENDING)
    {
//        int c;
//        for (c = 1000; c > 0; --c)
//            ;
        // form a 93C76C write command:
        // 00101xaa aaaaaaaa dddddddd
        // Where a is the address, d is the data
        cmd[0] = 0x28;
        cmd[0] |= ((dst >> 8) & 0x3);
        cmd[1] = dst & 0xff;
        cmd[2] = *src++;
        uwr_busy_wait ();
        uwr_select (UWR_93C76C_0);
        if (0 == uwr_write(&cmd[0], 3))
            eeprom_state = _ERR_PENDING;
        uwr_select (UWR_NO_DEVICE);
        --len;
        ++dst;
    }
    lock (1); // relock the chip
    if (eeprom_state == _PENDING)
        eeprom_state = _OK; 

    return &eeprom_state;
}
#pragma restore

#if CLI
// copy a number of bytes from xdata to eeprom; 
enum EEPROM_RC data *memclr_pr(void)
{
    uint8_t xdata cmd[2];

    if (eeprom_state != _OK)
        return &eeprom_state;

    eeprom_state = _PENDING; 

    if ( 0 == lock (0) ) // write-enable the chip
    {
        lock (1);
        eeprom_state = _ERR_NACK; 
        return &eeprom_state;
    }
    
    // form a 93C76C erase-all command:
    // 10010xxx
    cmd[0] = 0x24;
    cmd[1] = 0x00;
    uwr_busy_wait ();
    uwr_select (UWR_93C76C_0);
    if (0 == uwr_write(&cmd[0], 2))
        eeprom_state = _ERR_PENDING;
    uwr_select (UWR_NO_DEVICE);

    lock (1); // relock the chip
    if (eeprom_state == _PENDING)
        eeprom_state = _OK; 

    return &eeprom_state;
}
#endif

#if EXTRAS
// Read four (4) bytes; A return of nonzero is a failure.
enum EEPROM_RC data *memget_pr (int32x_t *dst, uint32_t src)
{
    return (memcpy_xpr((uint8x_t *) dst, src, 4));
}
#endif

// read bytes; a return of nonzero is a fail.
#pragma save
#pragma NOAREGS
enum EEPROM_RC data *memcpy_xpr(
    uint8x_t *dst, 
    uint32_t src, 
    int16_t len
    ) small reentrant
{
    static uint8_t xdata cmd[2];

    if (eeprom_state != _OK)
        return &eeprom_state;

    eeprom_state = _PENDING; 

    // form a 93C76C read command:
    // 00110xaa aaaaaaaa
    // Where a is the address
    cmd[0] = 0x30;
    cmd[0] |= ((src >> 8) & 0x3);
    cmd[1] = src & 0xff;
    uwr_select (UWR_93C76C_0);
    uwr_write(&cmd[0], 2);
    uwr_read(dst, len);
    uwr_select (UWR_NO_DEVICE);

    if (eeprom_state == _PENDING)
        eeprom_state = _OK; 

    return &eeprom_state;
}
#pragma restore

#if CAL_SAVE  && NV_SELECT == NV_EEPROM
// returns 1 if it worked, 0 if it timed out or failed
#pragma save
#pragma NOAREGS
bool eeprom_ok (enum EEPROM_RC data *pstatus) small reentrant
{
    RESET_WD();
    return (_OK == *pstatus);
}
#pragma restore
#endif // extras

#endif // EEPROM
/***************************************************************************
 *  $Log: eepromp3.c,v $
 *  Revision 1.21  2006/10/13 00:47:28  tvander
 *  Removed compile options for 6530, 6515;
 *  renamed 6511 and 6513 to trace11 and trace13;
 *  Binary verified unchanged from previous version.
 *
 *  Revision 1.20  2006/09/09 01:09:24  gmikef
 *  *** empty log message ***
 *
 *  Revision 1.19  2006/08/09 00:56:33  tvander
 *  *** empty log message ***
 *
 *  Revision 1.18  2006/06/15 16:36:55  tvander
 *  Fixed reentrancy on iiceep.c and eepromp.c
 *  Made parallel code changes to microwire eeprom drivers.
 *
 *  Revision 1.17  2006/06/12 21:18:06  tvander
 *  Verified compilation
 *
 *  Revision 1.16  2006/05/25 03:24:12  tvander
 *  Added timeouts to EEPROMs.  Tested all three.
 *  Newly ported calibration loader, compiles without error.
 *  RTC setting uses a software timer (less code, also frees tmr1)
 *
 *  Revision 1.15  2006/05/18 23:18:44  tvander
 *  16K and 32K
 *  First cut at new requirements.
 *  32K 6521 is grossly tested.
 *  All others have a clean compile with C51 8.02
 *
 *  Revision 1.14  2006/04/14 20:11:23  tvander
 *  Fixed, integrated with phased calibration
 *
 *  Revision 1.13  2006/04/06 21:00:04  tvander
 *  Maintained so microwire EEPROM driver still builds.
 *
 *  Revision 1.12  2006/03/08 00:00:56  tvander
 *  Revised IO so that multiplexed interrupts are centralized in io65xx.c
 *  Added default interrupts to io65xx.c
 *  Clean build.
 *  Tested CE, serial.
 *  interrupting EEPROM driver fails.
 *
 *  Revision 1.11  2006/03/06 03:31:07  Michael T. Fischer
 *  More 6530 prep.
 *
 *  Revision 1.10  2006/03/03 11:25:45  Michael T. Fischer
 *  Prep for 6530 LCD, etc.
 *
 *  Revision 1.9  2006/02/08 22:38:11  gmikef
 *  *** empty log message ***
 *
 *  Revision 1.8  2006/02/08 03:43:18  tvander
 *  Made "import" the default power measurement mode, rather than net-metering
 *
 *  Revision 1.7  2006/01/16 20:11:22  tvander
 *  Clean Keil build, all versions
 *
 *  Revision 1.6  2006/01/10 03:57:35  gmikef
 *  Added PDATA support for CE Outputs.
 *
 *  Revision 1.4  2005/11/19 00:38:37  tvander
 *  Working 3-wire EEPROM interface
 *
 *  Revision 1.3  2005/11/05 02:14:00  tvander
 *  Fixed build
 *
 *  Revision 1.2  2005/11/05 01:56:26  tvander
 *  Added EEPROM erase; Note uwreep.c is not working; don't know why.
 *
 *  Revision 1.1  2005/10/26 23:52:21  tvander
 *  Microwire EEPROM access via DIO
 *
 *
 * Copyright (C) 2005 Teridian Semiconductor Corp. All Rights Reserved.    *
 * this program is fully protected by the United States copyright          *
 * laws and is the property of Teridian Semiconductor Corporation.         *
 ***************************************************************************/

⌨️ 快捷键说明

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