?? iic.c
字號:
/*
*********************************************************
* Copyright (c)
* All rights reserved.
*
* 文件名稱:iic.c
* 文件標(biāo)識:
* 摘 要:S3C2410 IIC-bus Master Tx/Rx mode Test Program
* (Interrupt / Non Interrupt (Polling))
* 當(dāng)前版本:1.0
* 作 者:劉征
* 完成日期:2005.4.3
*
* 取代版本:
* 作 者:
* 完成日期:
*********************************************************
*/
/*
*********************************************************
* 頭文件
*********************************************************
*/
#include <string.h>
#include "2410addr.h"
#include "2410lib.h"
#include "def.h"
#include "iic.h"
/*
*********************************************************
* 變量
*********************************************************
*/
static U8 _iicData[IICBUFSIZE];
static volatile int _iicDataCount;
static volatile int _iicStatus;
static volatile int _iicMode;
static int _iicPt;
//===================================================================
// S3c2410 IIC configuration
// GPE15=IICSDA, GPE14=IICSCL
// "Interrupt mode" for IIC block
//===================================================================
/*
*********************************************************
* 函數(shù)介紹:本函數(shù)是測試iic使用中斷的程序。
* 輸入?yún)?shù):無
* 輸出參數(shù):無
* 返回值 :無
*********************************************************
*/
void Test_Iic(void)
{
unsigned int i,j,save_E,save_PE;
static U8 data[256];
Uart_Printf("[ IIC Test(Interrupt) using KS24C080 ]\n");
save_E = rGPECON;
save_PE = rGPEUP;
rGPEUP |= 0xc000; //Pull-up disable
rGPECON |= 0xa00000; //GPE15:IICSDA , GPE14:IICSCL
pISR_IIC = (unsigned)IicInt;//設(shè)置中斷處理函數(shù)入口
rINTMSK &= ~(BIT_IIC);
//Enable ACK, Prescaler IICCLK=PCLK/16, Enable interrupt, Transmit clock value Tx clock=IICCLK/16
// If PCLK 50.7MHz, IICCLK = 3.17MHz, Tx Clock = 0.198MHz
rIICCON = (1<<7) | (0<<6) | (1<<5) | (0xf);
rIICADD = 0x10; //2410 slave address = [7:1]
rIICSTAT = 0x10; //IIC bus data output enable(Rx/Tx)
Uart_Printf("Write test data into KS24C080\n");
for(i=0;i<256;i++)
{
Delay(1500);//延遲的目的是為了能充分表現(xiàn)中斷的產(chǎn)生
Wr24C080(0xa0,(U8)i,i);//寫入數(shù)據(jù)
}
for(i=0;i<256;i++)
data[i] = 0;
Uart_Printf("Read test data from KS24C080\n");
for(i=0;i<256;i++)
Rd24C080(0xa0,(U8)i,&(data[i])); //讀出數(shù)據(jù)
//Line changed 0 ~ f
for(i=0;i<16;i++)
{
for(j=0;j<16;j++)
Uart_Printf("%2x ",data[i*16+j]);
Uart_Printf("\n");
}
rINTMSK |= BIT_IIC;
rGPEUP = save_PE;
rGPECON = save_E;
}
/*
*********************************************************
* 函數(shù)介紹:本函數(shù)是iic寫程序,將數(shù)據(jù)寫入KS24C080(IIC EEPROM)。
* 輸入?yún)?shù):slvAddr--基地址(0XA0)
* addr--地址偏移量
* data--寫入的數(shù)據(jù)
* 輸出參數(shù):無
* 返回值 :無
*********************************************************
*/
void Wr24C080(U32 slvAddr,U32 addr,U8 data)
{
_iicMode = WRDATA;
_iicPt = 0;
_iicData[0] = (U8)addr;
_iicData[1] = data;
_iicDataCount = 2;
rIICDS = slvAddr; //0xa0
rIICSTAT = 0xf0; //MasTx,Start
//Clearing the pending bit isn't needed because the pending bit has been cleared.
while(_iicDataCount!=-1);//_iicDataCount在中斷處理程序里被減減
_iicMode = POLLACK;//得到ACK
while(1)
{
rIICDS = slvAddr;
_iicStatus = 0x100;
rIICSTAT = 0xf0; //MasTx,Start
rIICCON = 0xaf; //Resumes IIC operation.
while(_iicStatus==0x100);
if(!(_iicStatus&0x1))
break; //When ACK is received
}
rIICSTAT = 0xd0; //Stop MasTx condition
rIICCON = 0xaf; //Resumes IIC operation.
Delay(1); //Wait until stop condtion is in effect.
//Write is completed.
}
/*
*********************************************************
* 函數(shù)介紹:本函數(shù)是iic讀程序,將數(shù)據(jù)從KS24C080(IIC EEPROM)讀出。
* 輸入?yún)?shù):slvAddr--基地址(0XA0)
* addr--地址偏移量
* *data--讀出數(shù)據(jù)放入的BUF的首地址
* 輸出參數(shù):無
* 返回值 :無
*********************************************************
*/
void Rd24C080(U32 slvAddr,U32 addr,U8 *data)
{
_iicMode = SETRDADDR;
_iicPt = 0;
_iicData[0] = (U8)addr;
_iicDataCount = 1;
rIICDS = slvAddr;
rIICSTAT = 0xf0; //MasTx,Start
//Clearing the pending bit isn't needed because the pending bit has been cleared.
while(_iicDataCount!=-1);
_iicMode = RDDATA;
_iicPt = 0;
_iicDataCount = 1;
rIICDS = slvAddr;
rIICSTAT = 0xb0; //MasRx,Start
rIICCON = 0xaf; //Resumes IIC operation.
while(_iicDataCount!=-1);
*data = _iicData[1];
}
/*
*********************************************************
* 函數(shù)介紹:本函數(shù)是iic中斷處理程序。
* 輸入?yún)?shù):無
* 輸出參數(shù):無
* 返回值 :無
*********************************************************
*/
void __irq IicInt(void)
{
U32 iicSt,i;
rSRCPND = BIT_IIC; //Clear pending bit
rINTPND = BIT_IIC;
iicSt = rIICSTAT;
if(iicSt & 0x8){} //When bus arbitration is failed.
if(iicSt & 0x4){} //When a slave address is matched with IICADD
if(iicSt & 0x2){} //When a slave address is 0000000b
if(iicSt & 0x1){} //When ACK isn't received
switch(_iicMode)
{
case POLLACK:
_iicStatus = iicSt;
break;
case RDDATA:
if((_iicDataCount--)==0)
{
_iicData[_iicPt++] = rIICDS;
rIICSTAT = 0x90; //Stop MasRx condition
rIICCON = 0xaf; //Resumes IIC operation.
Delay(1); //Wait until stop condtion is in effect.
//Too long time...
//The pending bit will not be set after issuing stop condition.
break;
}
_iicData[_iicPt++] = rIICDS; //The last data has to be read with no ack.
if((_iicDataCount)==0)
rIICCON = 0x2f; //Resumes IIC operation with NOACK.
else
rIICCON = 0xaf; //Resumes IIC operation with ACK
break;
case WRDATA:
Uart_Printf("iic __irq WRDATA happen!\n");
if((_iicDataCount--)==0)
{
rIICSTAT = 0xd0; //Stop MasTx condition
rIICCON = 0xaf; //Resumes IIC operation.
Delay(1); //Wait until stop condtion is in effect.
//The pending bit will not be set after issuing stop condition.
break;
}
rIICDS = _iicData[_iicPt++]; //_iicData[0] has dummy.
for(i=0;i<10;i++); //for setup time until rising edge of IICSCL
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -