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

📄 i2cdummy.c.inc

📁 用于DRX3973或DRX39系列的芯片的控制
💻 INC
字号:
/******************************************************************************
 * FILENAME: $Id: i2cdummy.c.inc,v 1.2 2005/05/04 14:25:11 carlo Exp $
 *
 * DESCRIPTION:
 * Part of bsp_i2c module implementation.
 * Low level driver functions.
 *
 * This file contains a dummy I2C driver (stub),
 * which logs all I2C accesses made.
 *
 * USAGE:
 * Include the file in i2c.c
 * #define BSP_I2C_USE_DUMMY 1    in bsp_i2c_config.h
 *
 * NOTES:
 * $(c) 2004-2005 Micronas GmbH. All rights reserved.
 *
 * This software and related documentation (the 'Software') are intellectual
 * property owned by Micronas and are copyright of Micronas, unless specifically
 * noted otherwise.
 *
 * Any use of the Software is permitted only pursuant to the terms of the
 * license agreement, if any, which accompanies, is included with or applicable
 * to the Software ('License Agreement') or upon express written consent of
 * Micronas. Any copying, reproduction or redistribution of the Software in
 * whole or in part by any means not in accordance with the License Agreement
 * or as agreed in writing by Micronas is expressly prohibited.
 *
 * THE SOFTWARE IS WARRANTED, IF AT ALL, ONLY ACCORDING TO THE TERMS OF THE
 * LICENSE AGREEMENT. EXCEPT AS WARRANTED IN THE LICENSE AGREEMENT THE SOFTWARE
 * IS DELIVERED 'AS IS' AND MICRONAS HEREBY DISCLAIMS ALL WARRANTIES AND
 * CONDITIONS WITH REGARD TO THE SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
 * AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIT
 * ENJOYMENT, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL
 * PROPERTY OR OTHER RIGHTS WHICH MAY RESULT FROM THE USE OR THE INABILITY
 * TO USE THE SOFTWARE.
 *
 * IN NO EVENT SHALL MICRONAS BE LIABLE FOR INDIRECT, INCIDENTAL, CONSEQUENTIAL,
 * PUNITIVE, SPECIAL OR OTHER DAMAGES WHATSOEVER INCLUDING WITHOUT LIMITATION,
 * DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS
 * INFORMATION, AND THE LIKE, ARISING OUT OF OR RELATING TO THE USE OF OR THE
 * INABILITY TO USE THE SOFTWARE, EVEN IF MICRONAS HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES, EXCEPT PERSONAL INJURY OR DEATH RESULTING FROM
 * MICRONAS' NEGLIGENCE.                                                        $
 *
 * AUTHOR:
 * Martin Sinot
 *
 *****************************************************************************/

#include <stdio.h>
#include <stdarg.h>
#include "bsp_i2c.h"


/*
 * BSP log function. It opens file bsp.txt (creating if
 * necessary), appends text to the end and closes it again.
 * Syntax: see printf
 */
static int BSPLOG (char *fmt, ...)
{
    int rc = -1;
    FILE *f = fopen ("bsp.txt", "a");
    if (f)
    {
        va_list arg;

        va_start (arg, fmt);
        rc = vfprintf (f, fmt, arg);
        va_end (arg);
        fclose (f);
    }
    return rc;
}


int DRX_I2C_Error_g;

/******************************
 *
 * int SP7_I2C_Init (void)
 *
 * Prepares the platform's I2C subsystem for usage. Must be called before any
 * other I2C function is called.
 *
 * Output:
 * - TRUE       system is now ready for use
 * - FALSE      something wrong
 *
 ******************************/

DRXStatus_t DRXBSP_I2C_Init (void)
{
    BSPLOG ("<I2C Init>\n");
    return DRX_STS_OK;
}


/******************************
 *
 * Status_t SP7_I2C_Term (void)
 *
 * Shuts down the I2C subsystem of the target platform. After this, I2C
 * functions must no longer be called, until the system is initialised
 * again.
 *
 * Output:
 * - STS_OK     I2C subsystem shut down
 * - STS_ERROR  something wrong
 *
 ******************************/

DRXStatus_t DRXBSP_I2C_Term (void)
{
    BSPLOG ("<I2C Exit>\n");
    return DRX_STS_OK;
}


/******************************
 *
 * DRXStatus_t SP7_I2C_WriteRead  (
 *             pI2CDeviceAddr_t wDevAddr,  -- device to write to
 *             u16_t wcount,               -- nr of bytes to write
 *             u8_t* wdata,                -- the data to write
 *             pI2CDeviceAddr_t rDevAddr,  -- device to read from
 *             u16_t rcount,               -- nr of bytes to read
 *             u8_t* rdata)                -- buffer receiving the data
 *
 * Writes data to I2C device, then receives back specified number of bytes.
 * The first byte or first two bytes is the address of the I2C device to write.
 * The last byte is the device to read back the data from (normally the same
 * as the first byte, but with bit 0 set instead of cleared).
 *
 * If wDevAddr is NULL, no data is written. If rDevAddr is NULL, no data is read.
 * The difference with passing xDevAddr = NULL and xCount/xData = 0 is that
 * in the first case nothing is written/read at all, while in the second
 * case the address is written.
 *
 * Make sure that rdata is large enough to receive the requested data.
 *
 * Output:
 * - STS_OK           data successfully transferred
 *                    in that case: data received is in rdata
 * - STS_INVALID_ARG  invalid arguments passed to this function
 * - STS_ERROR        something wrong in the transfer
 *
 ******************************/

DRXStatus_t DRXBSP_I2C_WriteRead( pI2CDeviceAddr_t wDevAddr,
                                  u16_t            wCount,
                                  pu8_t            wData,
                                  pI2CDeviceAddr_t rDevAddr,
                                  u16_t            rCount,
                                  pu8_t            rData)
{
    unsigned wbytes = 0;

    if (wDevAddr)
    {
        if (!wData && wCount)
        {
            BSPLOG ("*** Null write buffer with nonzero count!\n");
            return DRX_STS_INVALID_ARG;
        }
        if (wData && !wCount)
        {
            BSPLOG ("*** Non-null write buffer with zero count!\n");
            return DRX_STS_INVALID_ARG;
        }
    }
    else if (wData || wCount)
    {
        BSPLOG ("*** Null write address with nonzero buffer or count!\n");
        return DRX_STS_INVALID_ARG;
    }
    if (rDevAddr)
    {
        if (!rData && rCount)
        {
            BSPLOG ("*** Null read buffer with nonzero count!\n");
            return DRX_STS_INVALID_ARG;
        }
        if (rData && !rCount)
        {
            BSPLOG ("*** Non-null read buffer with zero count!\n");
            return DRX_STS_INVALID_ARG;
        }
    }
    else if (rData || rCount)
    {
        BSPLOG ("*** Null read address with nonzero buffer or count!\n");
        return DRX_STS_INVALID_ARG;
    }
    if (!wDevAddr && !rDevAddr)
    {
        BSPLOG ("*** I2C Error: Nothing to do!\n");
        return DRX_STS_INVALID_ARG;
    }

    /* Start condition */
    BSPLOG ("<S>");
    if (wDevAddr)
    {
        unsigned i;
        BSPLOG (" %02X", ((int) wDevAddr->i2cAddr) & 0xFE);
        wbytes++;
        if (IS_I2C_10BIT (wDevAddr->i2cAddr))
        {
            BSPLOG (" %02X", (((int) wDevAddr->i2cAddr) >> 8) & 0xFF);
            wbytes++;
        }
        for (i = 0; i < (unsigned) wCount; i++)
        {
            BSPLOG (" %02X", ((int) wData[i]) & 0xFF);
            wbytes++;
        }
        if (rDevAddr)
        {
            BSPLOG (" <Sr>");
        }
    }
    if (rDevAddr)
    {
        BSPLOG (" %02X", (((int) rDevAddr->i2cAddr) & 0xFF) | 1);
        wbytes++;
        if (rCount)
        {
            BSPLOG (" <read %d byte%s>", (unsigned) rCount, (rCount == 1 ? "" : "s"));
        }
    }
    BSPLOG (" <P>     [%d byte%s written]\n", wbytes, (wbytes == 1 ? "" : "s"));
    return DRX_STS_OK;
}

⌨️ 快捷键说明

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