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

📄 iicdio.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: IICDIO.C
// This implements an I2C bus using DIO_4 and DIO_5
// At a 4.9MHz MPU clock, this implementation was measured at a 263KHz 
// clock rate, substantially faster than the 78KHz provided by the 
// hardware controller.
// However, in the 6520's brownout mode, when run by eepromp.c, it 
// forces the chip into sleep mode, and does not access the eeprom.  
// This issue is not yet isolated.  iiceep.c does not have this issue.
//               
//**************************************************************************
// IIC API.
//
#include "options.h"
#if I2C_POLLED && I2C_FAST
#include "iic.h"

// change this stuff for new hardware
// SCL is attached to DIO_4 in both M6510 and M6520
#define SCL DIO_4
#define SCL_POWERED DIR0 |= 0x10
#define SCL_UNPOWERED DIR0 &= ~0x10
#define SCL_ONE SCL = 1
#define SCL_ZERO SCL = 0
// SDA is attached to DIO_5
#define SDA DIO_5
#define SDA_POWERED DIR0 |= 0x20
#define SDA_UNPOWERED DIR0 &= ~0x20
#define SDA_ONE SDA = 1
#define SDA_ZERO SDA = 0

#pragma save
#pragma NOAREGS
void IICInit(void) small reentrant
{
    EX_EEPROM  = FALSE;           // Disable EEPROM non-busy interrupt.
    DIO &= ~DIO_EEX; // DIO_4 and DIO_5 become DIOs, not EEPROM interface
    #if TRACE10 || M6520
    if ((LCDX & LCD_NUM) > 16) // DIO_4 and DIO_5 are not LCD segments
    {
        LCDX = 16 | (LCDX & ~LCD_NUM);
    }
    #elif M6530
    #else
    #error unknown device
    #endif
    SDA_ONE;
    SCL_ONE;
    SDA_UNPOWERED; // the PCB's pull up resistors will hold them high
    SCL_UNPOWERED;
}
#pragma restore

// delay loop
// #define DELAY 10
#ifdef DELAY
static void delay(void)
{
   uint_fast8_t cnt;
   for (cnt = DELAY; cnt > 0; --cnt)
   {
   }
}
#endif

// start of portable code
// The start condition has the data change while the clock is high,
// but ends with the clock low so the clock can clock bits.
#ifdef DELAY
#define START_CONDITION \
    SDA_POWERED; \
    SCL_POWERED; \
    delay(); \
    SDA_ONE; \
    delay(); \
    SCL_ONE; \
    delay(); \
    SDA_ZERO; \
    delay(); \
    SCL_ZERO
#else
#define START_CONDITION \
    SDA_POWERED; \
    SCL_POWERED; \
    SDA_ONE; \
    SCL_ONE; \
    SDA_ZERO; \
    SCL_ZERO
#endif

// the stop condition makes the data go high when the clock is high
// It has to end with both lines high.
#ifdef DELAY
#define STOP_CONDITION \
    SDA_POWERED; \
    delay(); \
    SDA_ZERO; \
    delay(); \
    SCL_ONE; \
    SDA_ONE; \
    SDA_UNPOWERED; \
    SCL_UNPOWERED
#else
#define STOP_CONDITION \
    SDA_POWERED; \
    SDA_ZERO; \
    SCL_ONE; \
    SDA_ONE; \
    SDA_UNPOWERED; \
    SCL_UNPOWERED
#endif

#ifdef DELAY
#define PUT_BIT(_b_,_bit_) \
    if (0 == (_b_ & _bit_)) \
    { \
        SDA_ZERO; \
    } \
    else \
    { \
        SDA_ONE; \
    } \
    delay(); \
     SCL_ONE; \
    delay(); \
    SCL_ZERO
#else
#define PUT_BIT(_b_,_bit_) \
    if (0 == (_b_ & _bit_)) \
    { \
        SDA_ZERO; \
    } \
    else \
    { \
        SDA_ONE; \
    } \
     SCL_ONE; \
    SCL_ZERO
#endif

#define PUT_BYTE(_b_) \
    PUT_BIT(_b_,128); \
    PUT_BIT(_b_,64); \
    PUT_BIT(_b_,32); \
    PUT_BIT(_b_,16); \
    PUT_BIT(_b_,8); \
    PUT_BIT(_b_,4); \
    PUT_BIT(_b_,2); \
    PUT_BIT(_b_,1)

#ifdef DELAY
#define GET_BIT(_b_,_bit_) \
    delay(); \
    SCL_ONE; \
    if (SDA != 0) \
    { \
        _b_ |= _bit_; \
    } \
    delay(); \
    SCL_ZERO
#else
#define GET_BIT(_b_,_bit_) \
    SCL_ONE; \
    if (SDA != 0) \
    { \
        _b_ |= _bit_; \
    } \
    SCL_ZERO
#endif

#define GET_BYTE(_b_) \
    GET_BIT(_b_,128); \
    GET_BIT(_b_,64); \
    GET_BIT(_b_,32); \
    GET_BIT(_b_,16); \
    GET_BIT(_b_,8); \
    GET_BIT(_b_,4); \
    GET_BIT(_b_,2); \
    GET_BIT(_b_,1)

// get a bit, used to reset some parts
#pragma save
#pragma NOAREGS
uint8_t IICGetBit(void) small reentrant
{
    uint8_t b;
    b = 0;
    GET_BIT(b,1);
    return b;
}
#pragma restore

// an IIC bus's start condition
#pragma save
#pragma NOAREGS
void IICStart(void) small reentrant
{
    START_CONDITION;
    // leaves the SDA powered; the only routine to do so, but this is ok
    // because it is almost always followed by a write of a command.
}
#pragma restore

// an IIC bus's ending condition
#pragma save
#pragma NOAREGS
void IICStop(void) small reentrant
{
    STOP_CONDITION;
}
#pragma restore

// transmit a counted string of bytes to an IIC bus
#pragma save
#pragma NOAREGS
uint8_t IICWrite(uint8_t xdata *pchOut, int16_t cnt) small reentrant
{
    uint_fast8_t b;
    uint_fast8_t ack;
    for(ack = 0; (0 == ack) && (cnt > 0); --cnt)
    {
        b = *pchOut++;
        SDA_POWERED;  // power SDA only for output
        PUT_BYTE(b);
        SDA_UNPOWERED;
        GET_BIT(ack, 1); // get the ack bit
    }
    return ack;
}
#pragma restore

#if CLI
// transmit a counted string of identical bytes to an IIC bus
#pragma save
#pragma NOAREGS
uint8_t IICPad(int16_t cnt, uint8_t bIn)small reentrant
{
    uint_fast8_t b;
    uint_fast8_t ack;
    b = bIn;
    for(ack = 0; (0 == ack) && (cnt > 0); --cnt)
    {
        SDA_POWERED;  // power SDA only for output
        PUT_BYTE(b);
        SDA_UNPOWERED;
        GET_BIT(ack, 1); // get the ack bit
    }
    return ack;
}
#pragma restore
#endif

// receive a counted string of bytes from an IIC bus
#pragma save
#pragma NOAREGS
void IICRead(uint8x_t *pchIn, int16_t cnt) small reentrant
{
    uint_fast8_t b;
 
    SDA_UNPOWERED;  // insurance: can't write and read at the same time!
    if (cnt > 0)
    {
        for(;;)
        {
            b = 0;       // get_byte() ors bits in, so start with 0
            GET_BYTE(b); // get a byte from the bus
            *pchIn++ = b; // store it
        if (--cnt <= 0) break; // leave loop after last byte
            SDA_POWERED;
            PUT_BIT(0,0); // send ack for all bytes except last byte
            SDA_UNPOWERED;
        }
    }
}
#pragma restore

#endif // EEPROM

/***************************************************************************
 * $Log: iicdio.c,v $
 * Revision 1.24  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.23  2006/09/27 00:55:33  tvander
 * Blanks for tabs
 *
 * Revision 1.22  2006/09/09 01:09:31  gmikef
 * *** empty log message ***
 *
 * Revision 1.21  2006/08/09 00:56:34  tvander
 * *** empty log message ***
 *
 * Revision 1.20  2006/08/08 00:43:39  tvander
 * Added debugged ST24512 EEPROM driver.
 *
 * Revision 1.19  2006/06/15 16:36:56  tvander
 * Fixed reentrancy on iiceep.c and eepromp.c
 * Made parallel code changes to microwire eeprom drivers.
 *
 * Revision 1.18  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.17  2006/03/08 03:06:21  gmikef
 * *** empty log message ***
 *
 * Revision 1.16  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.15  2006/03/06 03:31:24  Michael T. Fischer
 * More 6530 prep.
 *
 * Revision 1.14  2006/03/03 11:25:53  Michael T. Fischer
 * Prep for 6530 LCD, etc.
 *
 * Revision 1.13  2006/01/10 03:58:06  gmikef
 * Added PDATA support for CE Outputs.
 *
 * Revision 1.11  2005/11/05 02:14:00  tvander
 * Fixed build
 *
 * Revision 1.10  2005/11/05 01:56:26  tvander
 * Added EEPROM erase; Note uwreep.c is not working; don't know why.
 *
 * Revision 1.9  2005/10/31 17:37:59  tvander
 * Includes improved EEPROM code with uwire.
 * Clean build, all build trees (Thank-you, Mike!)
 *
 * Revision 1.8  2005/10/20 18:39:40  tvander
 * Ported 2-wire EEPROM code from 6511/6513, including interrupting version, polling version for DIO, and polling version using the 2-wire logic.
 *
 * Revision 1.7  2005/09/22 23:45:05  tvander
 * Clean build all models and unit tests, updated copyright to be fore Teridian
 *
 * Revision 1.6  2005/05/03 00:39:43  tvander
 * Incorporated event reporting in tmr0,tmr1 and unit tests.
 * Retested stm, trm0, tmr1.
 * Incorporated untested changes in io651x.h
 *
 * Revision 1.5  2005/04/21 01:59:37  gmikef
 * *** empty log message ***
 *
 * Revision 1.7  2005/04/09 02:04:25  gmikef
 * *** empty log message ***
 *
 * Revision 1.4  2005/03/23 19:19:30  tvander
 * Added untested timer functions.
 * Updated iicdio and iiceep to reflect improvements in 6510 code.
 * ser0 and ser1 updated to provide features for flag.
 *
 * Revision 1.3  2005/03/22 00:56:00  tvander
 * Rolled date
 * Changed misc.c to disable interrupts in Ce_Int_Enable().
 * Changed calls to memcpy_xp to never write outside the buffer.
 * Changed the help files to be more accurate.
 *
 * Revision 1.3  2005/03/12 00:16:59  tvander
 * Integrated memory types.
 *
 * Revision 1.2  2005/03/11 22:57:02  tvander
 * Added bool, and 8/16/32 data structures
 *
 * Revision 1.1  2005/03/11 22:19:14  tvander
 * *** empty log message ***
 *
 * Revision 1.2  2005/02/17 18:32:26  tvander
 * Added automatic check-in logging to all source code.
 *
 * 2004 DECEMBER 22;    First Version
 *
 * Revision 1.1  2005/02/01 20:04:41  tvander
 * Reorganization
 *
 * Revision 1.2  2005/01/26 19:52:36  tvander
 * Added update log
 *
 * 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 + -