freescale
来自「Freescale 系列单片机常用模块与综合系统设计」· 代码 · 共 579 行 · 第 1/2 页
TXT
579 行
/** ###################################################################
** THIS COMPONENT MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
** Filename : I2C_Master.C
** Project : IIC_Master
** 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, 9:52
** 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 : MASTER
** Auto stop condition : yes
** SCL frequency : 375 kHz
**
** Initialization
**
** Target slave address : 8
** 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_Master_SendChar(byte Chr);
** RecvChar - byte I2C_Master_RecvChar(byte *Chr);
** SendBlock - byte I2C_Master_SendBlock(void* Ptr, word Siz, word *Snt);
** RecvBlock - byte I2C_Master_RecvBlock(void* Ptr, word Siz, word *Rcv);
** GetCharsInTxBuf - word I2C_Master_GetCharsInTxBuf(void);
** GetCharsInRxBuf - word I2C_Master_GetCharsInRxBuf(void);
** SelectSlave - byte I2C_Master_SelectSlave(byte Slv);
**
** Copyright : 1997 - 2009 Freescale Semiconductor, Inc. All Rights Reserved.
**
** http : www.freescale.com
** mail : support@freescale.com
** ###################################################################*/
/* MODULE I2C_Master. */
#pragma MESSAGE DISABLE C4002 /* Disable warning C4002 "Result not used" */
#include "Events.h"
#include "I2C_Master.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 I2C_Master_SlaveAddr; /* Variable for Slave address */
static word InpLenM; /* Length of input bufer's content */
static byte *InpPtrM; /* Pointer to input buffer for Master mode */
static word OutLenM; /* Length of output bufer's content */
static byte * OutPtrM; /* Pointer to output buffer for Master mode */
volatile word I2C_Master_SndRcvTemp; /* Temporary variable for SendChar (RecvChar) when they call SendBlock (RecvBlock) */
static byte ChrTemp; /* Temporary variable for SendChar method */
volatile byte I2C_Master_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_Master_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_Master_Interrupt)
{
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? */
if(IICC1_TX) { /* Is device in Tx mode? */
if(Status & RXAK) { /* NACK received? */
IICC1_MST = 0; /* Switch device to slave mode (stop signal sent) */
IICC1_TX = 0; /* Switch to Rx mode */
OutLenM = 0; /* No character for sending */
InpLenM = 0; /* No character for reception */
I2C_Master_SerFlag &= ~(CHAR_IN_TX|WAIT_RX_CHAR|IN_PROGRES); /* No character for sending or reception*/
}
else {
if(OutLenM) { /* Is any char. for transmitting? */
OutLenM--; /* Decrease number of chars for the transmit */
IICD = *(OutPtrM)++; /* Send character */
}
else {
if(InpLenM) { /* Is any char. for reception? */
if(InpLenM == 1) { /* If only one char to receive */
IICC1_TXAK = 1; /* then transmit ACK disable */
}
else {
IICC1_TXAK = 0; /* else transmit ACK enable */
}
IICC1_TX = 0; /* Switch to Rx mode */
(void)IICD; /* Dummy read character */
}
else {
I2C_Master_SerFlag &= ~IN_PROGRES; /* Clear flag "busy" */
IICC1_MST = 0; /* Switch device to slave mode (stop signal sent) */
IICC1_TX = 0; /* Switch to Rx mode */
I2C_Master_OnTransmitData(); /* Invoke OnTransmitData event */
}
}
}
}
else {
InpLenM--; /* Decrease number of chars for the receive */
if(InpLenM) { /* Is any char. for reception? */
if(InpLenM == 1) {
IICC1_TXAK = 1; /* Transmit ACK disable */
}
}
else {
IICC1_MST = 0; /* If no, switch device to slave mode (stop signal sent) */
IICC1_TXAK = 0; /* Transmit ACK enable */
}
*(InpPtrM)++ = IICD; /* Receive character */
if(!InpLenM) { /* Is any char. for reception? */
I2C_Master_OnReceiveData(); /* Invoke OnReceiveData event */
}
}
}
else {
if(Status & IBAL) { /* Arbitration lost? */
OutLenM = 0; /* No character for sending */
InpLenM = 0; /* No character for reception */
I2C_Master_SerFlag &= ~(CHAR_IN_TX|WAIT_RX_CHAR|IN_PROGRES); /* No character for sending or reception*/
IICC1_TX = 0; /* Switch to Rx mode */
}
}
}
/*
** ===================================================================
** Method : I2C_Master_SendChar (component InternalI2C)
**
** Description :
** When working as a MASTER, this method writes one (7-bit
** addressing) or two (10-bit addressing) slave address
** bytes inclusive of R/W bit = 0 to the I2C bus and then
** writes one character (byte) to the bus. The slave address
** must be specified before, by the "SelectSlave" or
** "SelectSlave10" method or in the bean initialization
** section, "Target slave address init" property. If
** interrupt service is enabled and the method returns
** ERR_OK, it doesn't mean that transmission was successful.
** The state of transmission is obtainable from
** (OnTransmitData, OnError or OnArbitLost) events.
** When working as a SLAVE, this method writes a character
** to the internal output slave buffer and, after the master
** starts the communication, to the I2C bus. If no character
** is ready for a transmission (internal output slave buffer
** is empty), the Empty character will be sent (see "Empty
** character" property).
** Parameters :
** NAME - DESCRIPTION
** Chr - Character to send.
** Returns :
** --- - Error code, possible codes:
** ERR_OK - OK
** ERR_SPEED - This device does not work in
** the active speed mode
** ERR_DISABLED - Device is disabled
** ERR_BUSY - The slave device is busy, it
** does not respond by an acknowledge (only
** in master mode and when interrupt
** service is disabled)
** ERR_BUSOFF - Clock timeout elapsed or
** device cannot transmit data
** ERR_TXFULL - Transmitter is full (slave
** mode only)
** ERR_ARBITR - Arbitration lost (only when
** interrupt service is disabled and in
** master mode)
** ===================================================================
*/
byte I2C_Master_SendChar(byte Chr)
{
if((IICS_BUSY)||(InpLenM)||(I2C_Master_SerFlag&(CHAR_IN_TX|WAIT_RX_CHAR|IN_PROGRES))) { /* Is the bus busy */
return ERR_BUSOFF; /* If yes then error */
}
ChrTemp = Chr; /* Save character */
return (I2C_Master_SendBlock(&ChrTemp, (word)1, (word*)&I2C_Master_SndRcvTemp)); /* Send character and return */
}
/*
** ===================================================================
** Method : I2C_Master_RecvChar (component InternalI2C)
**
** Description :
** When working as a MASTER, this method writes one (7-bit
** addressing) or two (10-bit addressing) slave address
** bytes inclusive of R/W bit = 1 to the I2C bus, then reads
** one character (byte) from the bus and then sends the stop
** condition. The slave address must be specified before, by
** the "SelectSlave" or "SelectSlave10" method or in bean
** initialization section, property "Target slave address
** init". If interrupt service is enabled and the method
** returns ERR_OK, it doesn't mean that transmission was
** finished successfully. The state of transmission must be
** tested by means of events (OnReceiveData, OnError or
** OnArbitLost). In case of successful transmission,
** received data is ready after OnReceiveData event is
** called.
** When working as a SLAVE, this method reads a character
** from the input slave buffer.
** Parameters :
** NAME - DESCRIPTION
** * Chr - Received character.
** Returns :
** --- - Error code, possible codes:
** ERR_OK - OK
** ERR_SPEED - This device does not work in
** the active speed mode
** ERR_DISABLED - Device is disabled
** ERR_BUSY - The slave device is busy, it
** does not respond by the acknowledge
** (only in master mode and when interrupt
** service is disabled)
** ERR_BUSOFF - Clock timeout elapsed or
** device cannot receive data
** ERR_RXEMPTY - No data in receiver (slave
** mode only)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?