📄 i2c.c
字号:
// ---------------------------------------------------------------------------
// DISCLAIMER:
// We (RENESAS TECHNOLOGY EUROPE) do not warrant that the Software is
// free from claims by a third party of copyright, patent, trademark,
// trade secret or any other intellectual property infringement.
//
// Under no circumstances are we liable for any of the following:
//
// 1. third-party claims against you for losses or damages;
// 2. loss of, or damage to, your records or data; or
// 3. economic consequential damages (including lost profits or
// savings) or incidental damages, even if we are informed of
// their possibility.
//
// We do not warrant uninterrupted or error free operation of the
// Software. We have no obligation to provide service, defect
// correction, or any maintenance for the Software. We have no
// obligation to supply any Software updates or enhancements to you
// even if such are or later become available.
//
// IF YOU DOWNLOAD OR USE THIS SOFTWARE YOU AGREE TO THESE TERMS.
//
// THERE ARE NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE.
// ---------------------------------------------------------------------------
// Copyright (c) 2005 RENESAS TECHNOLOGY EUROPE All rights reserved.
// ---------------------------------------------------------------------------
// S O U R C E F O R M A T
// ---------------------------------------------------------------------------
// tab width 4 (tab at 4 8 12 ...)
// ---------------------------------------------------------------------------
// Project Name: I2C
// File : i2c.c
// Description : access i2c external device
// ---------------------------------------------------------------------------
// Supported : IAR M16C C/EC++ Compiler V2.12A/W32
// Compiler
// ---------------------------------------------------------------------------
// R E V I S I O N
// ---------------------------------------------------------------------------
// Supported Compiler : IAR
// ---------------------------------------------------------------------------
// R E V I S I O N H I S T O R Y
// ---------------------------------------------------------------------------
// Date Ver Author Description
// ---------------------------------------------------------------------------
// 25/01/2005 1.0 INFORT/TMA Creation
// ---------------------------------------------------------------------------
// R E M A R K S
// Tested on device : M16C26 & M16C62P on UART2
// Used Peripherals : UARTi
// Used Interrupts : UARTiRX / UARTiTX / BUS_COLLISION_UARTi
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// Include declaration
// ---------------------------------------------------------------------------
#include "i2c.h"
#include "uart.h"
// ---------------------------------------------------------------------------
// Global variables declaration
// ---------------------------------------------------------------------------
struct stI2CAddress
{
unsigned char rw:1;
unsigned char address:7;
};
typedef union
{
unsigned char byte;
struct stI2CAddress bit;
}tI2CAddress;
static tI2CAddress slaveAddress;
static unsigned short readIndex;
static unsigned short totalReadLength;
static unsigned char *ptrReadData;
static unsigned short writeIndex;
static unsigned short writeLength;
static unsigned char *ptrWriteData;
static I2CStatus currentStatus;
// ---------------------------------------------------------------------------
// Initialise I睠 line
// Data is sent by DMA1 triggered by ACK reception
// ---------------------------------------------------------------------------
// Input Parameters : None
// ---------------------------------------------------------------------------
// Returned Parameters : None
// ---------------------------------------------------------------------------
// Modified globales: None
// ---------------------------------------------------------------------------
void I2C_init()
{
UiMR = 0x0A; // I睠 mode mode ext. clock (slave mode)
UiBRG = I2C_100_KB_SPEED; // 100KBPS (this depends on used Xin/CPU clock)
UiC0 = 0xB0; // MSB first,Tx at raising edge of CK, f1,Nch,CTS disable
UiSMR = 0x01; // I睠bus mode,Arbitration lost flag Update per bit
// Bus collision detect at raising edge of CK
UiSMR2 = 0x11; // ACK/NACK interrupt,disable SDA/SCL,UART init
UiSMR3 = 0x62; // SDA delay = 3 to 4-cycle of BRG count source
UiSMR4 = 0x30; // NACK data output(SDA=SCL="H")
UiC1 = 0x15; // Transfer/Receive enable
#ifdef DMA1_USED
DM1IC.BYTE = INT_DISABLE; // Disable DMA1 Interrupt
DM1SL.BYTE = DMA1_ACK_TRIG_SOURCE;
#endif
STA_STP_IC = STA_STP_IPL; // Start condition interrupt enable
ACK_IC = INT_DISABLE; // ACK int. disable
NACK_IC = INT_DISABLE; // NACK int. enable
currentStatus.byte = I2C_FREE_NO_ERROR;
}
// ---------------------------------------------------------------------------
// Send Data on I睠 line
// Data is sent by DMA1 triggered by ACK reception
// ---------------------------------------------------------------------------
// Input Parameters :
// 1- Slave address
// 2- Pointer to transmit buffer
// 3- Number of data to transmit
// ---------------------------------------------------------------------------
// Returned Parameters :
// TRUE or FALSE if data can be transmited
// ---------------------------------------------------------------------------
// Modified globales: None
// ---------------------------------------------------------------------------
tBoolean I2C_MasterWrite(unsigned char address, unsigned char * ptrData, unsigned short length)
{
if(currentStatus.bit.busy==TRUE)
return FALSE;
currentStatus.byte =I2C_BUSY_NO_ERROR;
#ifdef DMA1_USED
// DMA Configuration
DAR1.DWORD = (int)&UiTB; // Destination
TCR1.WORD = length-1; // Size (minus one due to DMA specification)
SAR1.DWORD = (int)ptrData; // Source
DM1CON.BYTE = 0x19; // Memory to fixed destination single transfert
DM1IC.BYTE = DMA1_IPL; // Enable DMA1 Interrupt
#else
writeLength = length; // Size (minus one due to DMA specification)
ptrWriteData = ptrData; // Source
writeIndex = 0;
#endif
slaveAddress.bit.address = address;
slaveAddress.bit.rw = 0; // Write mode
UiSMR = 0x01; // All bit clear without bit0
UiSMR4 = 0x70; // SCL output STOP enable (SCLHI=1)
UiMR = 0x00; // Disable UART function
UiMR = 0x02; // Enable IIC mode, internal clock
UiC1 = 0x10; // Transmit/Receive disable
UiSMR2 = 0x02; // UART init. disable,Clock sync. enable
UiSMR4 = 0x71; // Start condition generate (STSPSEL=0/STAREQ=1)
UiSMR4 = 0x09; // STSP output enable (STSPSEL=1/STAREQ=1)
return TRUE;
}
// ---------------------------------------------------------------------------
// Read Data on I睠 line
// Data is received by DMA1 triggered by ACK reception
// ---------------------------------------------------------------------------
// Input Parameters :
// 1- Slave address
// 2- Pointer to receive buffer
// 3- Number of data to receive
// ---------------------------------------------------------------------------
// Returned Parameters :
// TRUE or FALSE if data can be read
// ---------------------------------------------------------------------------
// Modified globales: None
// ---------------------------------------------------------------------------
tBoolean I2C_MasterRead(unsigned char address, unsigned char *ptrData, unsigned short length)
{
if(currentStatus.bit.busy==TRUE)
return FALSE;
if(length==0)
return TRUE;
currentStatus.byte =I2C_BUSY_NO_ERROR;
ptrReadData = ptrData;
readIndex = 0;
totalReadLength = length;
slaveAddress.bit.address = address;
slaveAddress.bit.rw=1;//Read mode
UiSMR = 0x01; // All bit clear without bit0
UiSMR4 = 0x70; // SCL output STOP enable (SCLHI=1)
UiMR = 0x00; // Disable UART function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -