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

📄 uwreep.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: UWREEP.C
// This implements a Microwire bus master using 
// the 6520's Microwire EEPROM access hardware.
//
// The addressing function, uwr_select(), may have to be modified for your
// project.
// 
// Many clocked serial devices do not place their serial output in a 
// high impedance state while receiving data.  The electronics driven
// by this code is not compatible with those devices.  The alternative 
// DIO-based code, uwrdio.c can be quickly modified for your need.
//
// The only clock polarity permitted by this driver's electronics makes 
// data available on a leading rising clock edge, and reads it on a 
// falling trailing clock edge.  This is not compatible with devices that 
// have other clock polarities or edges.  All four combinations of clock
// polarity and edge are available in the alternative DIO-based code, uwrdio.c
//
// At a 4.9MHz MPU clock, this implementation was measured at a 260KHz 
// clock rate, not bad, but substantially slower than the 500KHz rate of 6520
// Microwire hardware.               
//**************************************************************************
// Microwire API.
//
#include "options.h"
#if UWR_FAST
//#include "oscope.h"
#include "uwr.h"

#if !M6520 && !M6530 // the 6520 and 6530 have this interface.
#error unhandled device type
#endif

// application symbols
static uint8_t last_cs_index = 1;
static bool wait_for_ready;
#define BUSY_WAIT_MODE 0x40
#define HALF_SECOND 5000 // assumes a call of delay_clks (3), 3/32768 secs

// change this stuff for new hardware
// CS, chip select, is attached to DIO_8 on the Eval PCB; modify this 
// for your PCB
#define CS DIO_8
#define CS_POWERED DIR1 |= 0x01
#define CS_UNPOWERED DIR1 &= ~0x01
#define CS_ONE CS = 1
#define CS_ZERO CS = 0

// wait for busy to fall, timeout if it doesn't
#pragma save
#pragma NOAREGS
static uint8_t busy_timed_out (void) small reentrant
{
    uint16_t timeout;
    // wait for not-busy
    #ifdef OSCOPE_H
    timeout = 9; // try to fail close to the error; ~3ms after
    #else
    timeout = HALF_SECOND; // give lots of time before failing
    #endif

    while (0 != (EECTRL & 0x40))
    {
        RESET_WD();
        delay_clks (3);

        if (--timeout == 0)
        {
            #ifdef OSCOPE_H
            OSCOPE_ZERO;  // flag an error
            #endif
            return 1;
        }
    }

    return 0;
}
#pragma restore

// initialize the microwire system
#pragma save
#pragma NOAREGS
void uwr_init(void) small reentrant
{
    EX_EEPROM  = FALSE;        // Disable EEPROM non-busy interrupt.

    // select nothing (take no chances)
    uwr_select (UWR_NO_DEVICE);

    #if M6520
    if ((LCDX & LCD_NUM) > 16) // assure DIO_4 and DIO_5 are not LCD segments
    {
        LCDX = 16 | (LCDX & ~LCD_NUM);
    }
    #elif M6530
    LCD_MAP[ DIO4_11 ] &= ~(SEG24_DIO4 | SEG25_DIO5);
    #else
    #error unknown device
    #endif

    DIO &= ~DIO_EEX;           // select the power-up default interface
    DIO |= DIO_EEX_3W;         // three wire interface
    // perform a read to clear the busy status even if it's in wait-for-ready
    // state, and the data line has never become true
    EECTRL = 0x21;
    // wait for not-busy
    busy_timed_out(); // wait for busy

    EEDATA = 0;

    wait_for_ready = 0;

    // power up the interface's chip select
    CS_POWERED;
}
#pragma restore

// select a chip; 0 = none
// The clock polarity should be selected before the device is selected.
// Rewrite this to select the polarity and address the devices on your PCB
#pragma save
#pragma NOAREGS
void uwr_select (uint8_t cs_index) small reentrant
{
    last_cs_index = cs_index;
    CS = cs_index;
}
#pragma restore

// start of portable code

// wait for programming complete; This is probably only useful
// on microwire EEPROMs.
#pragma save
#pragma NOAREGS
void uwr_busy_wait(void) small reentrant
{
    wait_for_ready = 1;
}
#pragma restore

// write a counted length of bytes to the microwire interface
// The interface always enters hHiZ after the last bit of the 
// last byte.  If a busy-wait is requested, it may wait for
// the acknowledgement from the EEPROM.
#pragma save
#pragma NOAREGS
uint8_t uwr_write(uint8x_t *pchOut, uint16_t cnt) small reentrant
{
    #ifdef OSCOPE_H
    OSCOPE_INIT; OSCOPE_ONE;
    #endif

    if ( busy_timed_out () ) // wait for not-busy
        return 0;

    for(; cnt > 1; --cnt)
    {
        EEDATA = *pchOut++;  // put out a byte
        EECTRL = 0x08;       // write 8 bits

        if ( busy_timed_out () ) // wait for not-busy
            return 0;

    }
    if (cnt == 1)
    {
        EEDATA = *pchOut++; // put out the last byte

        if (!wait_for_ready) // this is not an EEPROM data write.
        {
            EECTRL = 0x28; // write the last 8 bits with HiZ

            if ( busy_timed_out () ) // wait for not-busy
                return 0;
        }
        else // wait for ready
        {
            uint8_t cs_index = last_cs_index;

            // write the last 8 bits, but leave the data bus powered
            EECTRL = 0x08;
            wait_for_ready = 0;

            // wait for not-busy to indicate that the data is sent
            if ( busy_timed_out () ) // wait for not-busy
                return 0;

            // toggle selection so the EEPROM produces an acknowledge
            uwr_select (UWR_NO_DEVICE);

            uwr_select (cs_index);

            // This has to be a read because the data line
            // will be driven by the device when selected.
            // It has to count at least 1 bit, or the BUSY
            // status bit will never go high.
            EEDATA = 0; // force bus low with a bit
            EECTRL = 0xA8; // write with a wait for ready & HIZ

            // the eeprom needs a little bit of time
            // to pull the line down.
            if ( busy_timed_out () ) // wait for not-busy
                return 0;
        }
    }
    return 1;
}
#pragma restore

// receive a counted string of bytes from a microwire bus
#pragma save
#pragma NOAREGS
void uwr_read(uint8x_t *pchIn, uint16_t cnt) small reentrant
{
    #ifdef OSCOPE_H
    OSCOPE_INIT; OSCOPE_ONE;
    #endif

    if ( busy_timed_out () ) // wait for not-busy
        return;

    for(; cnt != 0; --cnt)
    {
        EECTRL = 0x18; // read 8 bits

        if ( busy_timed_out () ) // wait for not-busy
            return;

        *pchIn++ = EEDATA; // store it
    }
}
#pragma restore

#endif // UW_FAST

/***************************************************************************
 * $Log: uwreep.c,v $
 * Revision 1.17  2006/09/09 01:10:24  gmikef
 * *** empty log message ***
 *
 * Revision 1.16  2006/09/06 02:12:02  tvander
 * Fixed too-long timeout.
 *
 * Revision 1.15  2006/08/30 21:55:14  gmikef
 * *** empty log message ***
 *
 * Revision 1.14  2006/08/30 02:09:13  gmikef
 * *** empty log message ***
 *
 * Revision 1.13  2006/08/09 00:56:38  tvander
 * *** empty log message ***
 *
 * Revision 1.12  2006/06/15 19:56:37  tvander
 * Coded fixes to enable operation from interrupts.
 *
 * Revision 1.11  2006/05/18 23:18:46  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.10  2006/03/08 00:00:58  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.9  2006/03/06 03:32:50  Michael T. Fischer
 * More 6530 prep.
 *
 * Revision 1.8  2006/03/03 11:26:40  Michael T. Fischer
 * Prep for 6530 LCD, etc.
 *
 * Revision 1.7  2006/01/16 20:11:23  tvander
 * Clean Keil build, all versions
 *
 * Revision 1.6  2006/01/10 03:59:11  gmikef
 * Added PDATA support for CE Outputs.
 *
 * Revision 1.4  2005/12/07 01:40:44  tvander
 * Factored out the time-out logic.
 * Added optionally-compiled o-scope code for debugging.
 *
 * Revision 1.3  2005/11/19 00:38:37  tvander
 * Working 3-wire EEPROM interface
 *
 * Revision 1.2  2005/11/05 01:56:27  tvander
 * Added EEPROM erase; Note uwreep.c is not working; don't know why.
 *
 * Revision 1.1  2005/10/29 02:25:34  tvander
 * Working Microwire drivers
 *
 * Revision 1.1  2005/10/26 23:52:22  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 + -