/*
*********************************************************
* Copyright (c)
* All rights reserved.
*
* 文件名称:iic.c
* 文件标识:
* 摘 要:S3C2410 IIC-bus Master Tx/Rx mode Test Program
* (Interrupt / Non Interrupt (Polling))
* 当前版本: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
//===================================================================
/*
*********************************************************
* 函数介绍:本函数是测试iic使用中断的程序。
* 输入参数:无
* 输出参数:无
* 返回值 :无
*********************************************************
*/
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;//设置中断处理函数入口
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);//延迟的目的是为了能充分表现中断的产生
Wr24C080(0xa0,(U8)i,i);//写入数据
}
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])); //读出数据
//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;
}
/*
*********************************************************
* 函数介绍:本函数是iic写程序,将数据写入KS24C080(IIC EEPROM)。
* 输入参数:slvAddr--基地址(0XA0)
* addr--地址偏移量
* data--写入的数据
* 输出参数:无
* 返回值 :无
*********************************************************
*/
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.
}
/*
*********************************************************
* 函数介绍:本函数是iic读程序,将数据从KS24C080(IIC EEPROM)读出。
* 输入参数:slvAddr--基地址(0XA0)
* addr--地址偏移量
* *data--读出数据放入的BUF的首地址
* 输出参数:无
* 返回值 :无
*********************************************************
*/
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];
}
/*
*********************************************************
* 函数介绍:本函数是iic中断处理程序。
* 输入参数:无
* 输出参数:无
* 返回值 :无
*********************************************************
*/
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
rIICCON = 0xaf; //resumes IIC operation.
break;
case SETRDADDR:
if((_iicDataCount--)==0)
break; //IIC operation is stopped because of IICCON[4]
rIICDS = _iicData
iic.rar_s3c2410 iic
版权申诉
155 浏览量
2022-09-14
15:33:20
上传
评论
收藏 3KB RAR 举报
小贝德罗
- 粉丝: 70
- 资源: 1万+
最新资源
- Pytorch-pytorch深度学习教程之逻辑回归.zip
- Pytorch-pytorch深度学习教程之双向循环网络.zip
- Pytorch-pytorch深度学习教程之卷积神经网络.zip
- Pytorch-pytorch深度学习教程之前馈神经网络.zip
- Pytorch-pytorch深度学习教程之线性回归.zip
- Pytorch-pytorch深度学习教程之基本操作.zip
- 基于QT的地图可视化桌面系统后台数据库为MySQL5.7源码.zip
- 基于simulink的PLL锁相环系统仿真【包括模型,文档,参考文献,操作步骤】
- 基于EM-GMM模型的目标跟踪和异常行为检测matlab仿真【包括程序,注释,参考文献,操作步骤,说明文档】
- 2109010044_胡晨燕_选课管理数据库设计与实现.prj
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈