freescale

来自「Freescale 系列单片机常用模块与综合系统设计」· 代码 · 共 631 行 · 第 1/3 页

TXT
631
字号
/** ###################################################################
**     THIS COMPONENT MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
**     Filename  : I2C_Slave.C
**     Project   : IIC_Slave
**     Processor : MC9S08JM60CLHE
**     Component : InternalI2C
**     Version   : Component 01.267, Driver 01.24, CPU db: 3.00.046
**     Compiler  : CodeWarrior HCS08 C Compiler
**     Date/Time : 2010-1-15, 10:53
**     Abstract  :
**          This component encapsulates the internal I2C communication 
**          interface. The implementation of the interface is based 
**          on the Philips I2C-bus specification version 2.0. 
**          Interface features:
**          MASTER mode
**            - Multi master communication
**            - The combined format of communication possible 
**              (see "Automatic stop condition" property)
**            - 7-bit slave addressing (10-bit addressing can be made as well)
**            - Acknowledge polling provided
**            - No wait state initiated when a slave device holds the SCL line low
**            - Holding of the SCL line low by slave device recognized as 'not available bus'
**            - Invalid start/stop condition detection provided
**          SLAVE mode
**            - 7-bit slave addressing
**            - General call address detection provided
**     Settings  :
**         Serial channel              : IIC
**
**         Protocol
**             Mode                    : SLAVE
**             Slave address           : 2
**             Empty character         : 0
**
**         Initialization
**
**             Bean function           : Enabled
**             Events                  : Enabled
**
**         Registers
**             Input buffer            : IICD      [$005C]
**             Output buffer           : IICD      [$005C]
**             Control register        : IICC1     [$005A]
**             Status register         : IICS      [$005B]
**             Baud setting reg.       : IICF      [$0059]
**             Address register        : IICA      [$0058]
**
**         Interrupt
**             Vector name             : Viic
**             Priority                : 
**
**         Used pins                   :
**       ----------------------------------------------------------
**            Function    | On package |    Name
**       ----------------------------------------------------------
**              SDA       |     61     |  PTC1_SDA
**              SCL       |     60     |  PTC0_SCL
**       ----------------------------------------------------------
**     Contents  :
**         SendChar        - byte I2C_Slave_SendChar(byte Chr);
**         RecvChar        - byte I2C_Slave_RecvChar(byte *Chr);
**         SendBlock       - byte I2C_Slave_SendBlock(void* Ptr, word Siz, word *Snt);
**         RecvBlock       - byte I2C_Slave_RecvBlock(void* Ptr, word Siz, word *Rcv);
**         ClearTxBuf      - byte I2C_Slave_ClearTxBuf(void);
**         ClearRxBuf      - byte I2C_Slave_ClearRxBuf(void);
**         GetCharsInTxBuf - word I2C_Slave_GetCharsInTxBuf(void);
**         GetCharsInRxBuf - word I2C_Slave_GetCharsInRxBuf(void);
**
**     Copyright : 1997 - 2009 Freescale Semiconductor, Inc. All Rights Reserved.
**     
**     http      : www.freescale.com
**     mail      : support@freescale.com
** ###################################################################*/


/* MODULE I2C_Slave. */


#pragma MESSAGE DISABLE C4002          /* Disable warning C4002 "Result not used" */

#include "Events.h"
#include "I2C_Slave.h"

/*SerFlag bits*/
#define OVERRUN_ERR      0x01          /* Overrun error flag bit   */
#define WAIT_RX_CHAR     0x02          /* Wait for received char. flag bit (Master)  */
#define CHAR_IN_TX       0x04          /* Char is in TX buffer (Master)    */
#define CHAR_IN_RX       0x08          /* Char is in RX buffer     */
#define FULL_TX          0x10          /* Full transmit buffer     */
#define IN_PROGRES       0x20          /* Communication is in progress (Master) */
#define FULL_RX          0x40          /* Full receive buffer      */
#define MSxSL            0x80          /* Master x Slave flag bit  */

static byte InpLenS;                   /* Length of input buffer's content */
static byte InpPtrRS;                  /* Index for reading from input buffer */
static byte InpPtrWS;                  /* Index for writing to input buffer */
static byte InpBufferS[8];             /* Input buffer I2C commmunication */
static byte OutLenS;                   /* Length of output bufer's content */
static byte OutPtrRS;                  /* Index for reading from output buffer */
static byte OutPtrWS;                  /* Index for writing to output buffer */
static byte OutBufferS[8];             /* Output buffer for I2C commmunication */
volatile word I2C_Slave_SndRcvTemp;    /* Temporary variable for SendChar (RecvChar) when they call SendBlock (RecvBlock) */
static byte ChrTemp;                   /* Temporary variable for SendChar method */
volatile byte I2C_Slave_SerFlag;       /* Flags for serial communication */
                                       /* Bits: 0 - OverRun error */
                                       /*       1 - Wait for received char. flag bit (Master) */
                                       /*       2 - Char is in TX buffer (Master) */
                                       /*       3 - Char in RX buffer */
                                       /*       4 - Full TX buffer */
                                       /*       5 - Running int from TX */
                                       /*       6 - Full RX buffer */
                                       /*       7 - Master x Slave */
/*
** ===================================================================
**     Method      :  I2C_Slave_Interrupt (component InternalI2C)
**
**     Description :
**         The method services the interrupt of the selected peripheral(s)
**         and eventually invokes event(s) of the bean.
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
#define RXAK 0x01
#define SRW  0x04
#define IBAL 0x10
#define IAAS 0x40

#define ON_ARBIT_LOST 0x01
#define ON_FULL_RX    0x02
#define ON_RX_CHAR    0x04
#define ON_FREE_TX    0x08
#define ON_TX_CHAR    0x10
#define ON_OVERRUN    0x20
#define ON_TX_EMPTY   0x40

ISR(I2C_Slave_Interrupt)
{
  byte Flags = 0;                      /* Temporary variable for flags */
  byte Status;                         /* Safe status register */

  Status = IICS;
  IICS_IICIF = 1;                      /* Clear interrupt flag and ARBL flag if set (by means of the read modify write effect) */
  if(IICC1_MST) {                      /* Is device in master mode? */
  }
  else {
    if(Status & IAAS) {                /* Addressed as a slave? */
      if(Status & SRW) {               /* Read? */
        IICC1_TX = 1;                  /* Switch to Tx mode */
        if (OutLenS) {                 /* Is number of bytes in the transmit buffer greater then 0? */
          OutLenS--;                   /* Decrease number of chars in the transmit buffer */
          IICD = OutBufferS[OutPtrRS++]; /* Store char to transmitter register */
          if(OutPtrRS >= 8) {          /* Is the index out of the transmit buffer? */
            OutPtrRS = 0;              /* Set index on the first item into the transmit buffer */
          }
          Flags |= ON_TX_CHAR;         /* Set OnTxChar flag */
        }
        else {
          IICD = I2C_Slave_EOF;        /* Store the empty char to the transmit register */
        }
      }
      else{
        IICC1_TX = 0;                  /* Switch to Rx mode */
        (void)IICD;                    /* Dummy read character */
      }
    }
    else {
      if(Status & IBAL) {              /* Arbitration lost? */
      }
      else {
        if(IICC1_TX) {                 /* Transmit? */
          if(Status & RXAK) {          /* NACK from receiver */
            IICC1_TX = 0;              /* Switch to Rx mode */
            (void)IICD;                /* Dummy read character */
          }
          else {
            if (OutLenS) {             /* Is number of bytes in the transmit buffer greater then 0? */
              OutLenS--;               /* Decrease number of chars in the transmit buffer */
              IICD = OutBufferS[OutPtrRS++]; /* Store char to transmitter register */
              if(OutPtrRS >= 8) {      /* Is the index out of the transmit buffer? */
                OutPtrRS = 0;          /* Set index on the first item into the transmit buffer */
              }
              Flags |= ON_TX_CHAR;     /* Set OnTxChar flag */
            }
            else {
              IICD = I2C_Slave_EOF;    /* Store the empty char to the transmit register */
            }
          }
        }
        else {
          if(InpLenS < 8) {            /* Is number of bytes in the receive buffer lower than size of buffer? */
            InpLenS++;                 /* Increse number of chars in the receive buffer */
            InpBufferS[InpPtrWS++] = IICD; /* Save received char to the receive buffer */
            if(InpPtrWS >= 8) {        /* Is the index out of the receive buffer? */
              InpPtrWS = 0;            /* Set index on the first item into the receive buffer */
            }
            Flags |= ON_RX_CHAR;       /* If yes then set the OnRxChar flag */
          }
          else {
            (void)IICD;                /* Dummy read data register */
            I2C_Slave_SerFlag |= FULL_RX; /* Set flag "full RX buffer" */
          }
        }
      }
    }
    if(Flags & ON_RX_CHAR) {           /* Is OnRxChar flag set? */
      I2C_Slave_OnRxChar();            /* If yes then invoke user event */
    }
    if (Flags & ON_TX_CHAR) {          /* Is OnTxChar flag set? */
      I2C_Slave_OnTxChar();            /* If yes then invoke user event */
    }
  }

⌨️ 快捷键说明

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