?? externrtcdriver.c
字號:
/*================================================================================
Module Name: externrtcdriver.c
General Description:
==================================================================================
Honeywell Confidential Proprietary
ACS - Security (Asia Pacific) R&D Software Operations
(c) Copyright Honeywell 2006, All Rights Reserved
Revision History:
Modification Tracking
Author Date Ver Number Description of Changes
---------------- ------------ ---------- -------------------------
Chen Kang Date: 2006-7-13 Version: ver 1.0.0
Portability: Indicate if this module is portable to other compilers or
platforms. If not, indicate specific reasons why is it not portable.
==================================================================================
INCLUDE FILES
==================================================================================*/
#include "externrtcdriver.h"
#include "LPC2294.h"
/* ==================================================================================
GLOBAL VARIABLES
==================================================================================*/
volatile uint8 I2C_sla; // 從機地址
volatile uint8 I2C_suba; // 子地址
volatile uint8 *I2C_buf; // 數據緩沖區指針 (讀操作時會被更改)
volatile uint8 I2C_num; // 操作數據個數 (會被更改)
volatile uint8 I2C_end; // 操作結束標志,為1時表示操作結束,為0xFF時表示操作失敗 (會被設置)
volatile uint8 I2C_suba_en; // 子地址使能控制,讀操作時請設置為1,寫操作時請設置為2 (會被更改)
/* ==================================================================================
LOCAL FUNCTIONS DECLARATIONS
==================================================================================*/
/* =======================================================
Function Name:***** RTC_init() *****
Description: ***** RTC_init() entry func *****
Created By: ***** Chen Kang *****
Created Date: ***** 2006-7-13 *****
Calls: ***** NONE *****
Called By: ***** NONE *****
normal para: ***** NONE *****
pointer para: ***** NONE *****
Return type: ***** int *****
Others: ***** NONE *****
==========================================================*/
static int RTC_init();
/* =======================================================
Function Name:***** RTC_read() *****
Description: ***** RTC_read() entry func *****
Created By: ***** Chen Kang *****
Created Date: ***** 2006-7-13 *****
Calls: ***** NONE *****
Called By: ***** NONE *****
normal para: ***** size_t count; *****
pointer para: ***** struct file *filp; char *buf;loff_t *f_pos; *****
Return type: ***** ssize_t *****
Others: ***** NONE *****
==========================================================*/
static ssize_t RTC_read(struct file *filp, char *buf, size_t count,loff_t *f_pos);
/* =======================================================
Function Name:***** RTC_write() *****
Description: ***** RTC_write() entry func *****
Created By: ***** Chen Kang *****
Created Date: ***** 2006-7-13 *****
Calls: ***** NONE *****
Called By: ***** NONE *****
normal para: ***** size_t count; *****
pointer para: ***** struct file *filp; char *buf;loff_t *f_pos; *****
Return type: ***** ssize_t *****
Others: ***** NONE *****
==========================================================*/
static ssize_t RTC_write(struct file *filp, char *buf, size_t count,loff_t *f_pos);
/* =======================================================
Function Name:***** RTC_open() *****
Description: ***** RTC_open() entry func *****
Created By: ***** Chen Kang *****
Created Date: ***** 2006-7-13 *****
Calls: ***** NONE *****
Called By: ***** NONE *****
normal para: ***** NONE *****
pointer para: ***** struct inode *inode; struct file *filp; *****
Return type: ***** int *****
Others: ***** NONE *****
==========================================================*/
static int RTC_open(struct inode *inode, struct file *filp);
/* =======================================================
Function Name:***** RTC_irq_handle() *****
Description: ***** RTC_irq_handle() entry func *****
Created By: ***** Chen Kang *****
Created Date: ***** 2006-7-13 *****
Calls: ***** NONE *****
Called By: ***** NONE *****
normal para: ***** void; *****
pointer para: ***** NONE *****
Return type: ***** int *****
Others: ***** NONE *****
==========================================================*/
static int RTC_irq_handle(void);
static struct file_operations RTC_fops = /* driver info */
{
owner: THIS_MODULE,
open: RTC_open,
read: RTC_read,
write: RTC_write,
};
/* =======================================================
Function Name:***** RTC_init() *****
Description: ***** RTC_init() entry func *****
Created By: ***** Chen Kang *****
Created Date: ***** 2006-7-13 *****
Calls: ***** NONE *****
Called By: ***** NONE *****
normal para: ***** NONE *****
pointer para: ***** NONE *****
Return type: ***** int *****
Others: ***** NONE *****
==========================================================*/
static int RTC_init()
{
int ret;
I2SCLH = I2SCLL = 55; // 晶振為11.0592MHz,Fpclk = 2.7648MHz
/* 設置I2C中斷允許 */
//VICIntSelect = 0x00000000; // 設置所有通道為IRQ中斷
//VICVectCntl0 = 0x29; // I2C通道分配到IRQ slot 0,即優先級最高
//VICVectAddr0 = (int)IRQ_I2C; // 設置I2C中斷向量地址
//VICIntEnable |= 0x0200; // 使能I2C中斷
PINSEL0 &= 0xffffff0f; //set to 0000
PINSEL0 |= 0x00000050;//set to 0101
ret = register_chrdev(MAJOR_NR, DEVICE_NAME, &RTC_fops);
if (ret < 0)
{
printk(KERN_ERR DEVICE_NAME ":init failed. %d\n", MAJOR_NR);
return ret;
}
printk(KERN_WARNING DEVICE_NAME ": init OK.\n");
ret = request_irq(LPC22xx_INTERRUPT_I2C,RTC_irq_handle,SA_INTERRUPT,DEVICE_NAME,NULL);
if (ret< 0)
{
printk(KERN_ERR DEVICE_NAME ":request irq failed. \n");
return ret;
}
printk(KERN_WARNING DEVICE_NAME ": register irq OK.\n");
return 0;
}
/* =======================================================
Function Name:***** RTC_open() *****
Description: ***** RTC_open() entry func *****
Created By: ***** Chen Kang *****
Created Date: ***** 2006-7-13 *****
Calls: ***** NONE *****
Called By: ***** NONE *****
normal para: ***** NONE *****
pointer para: ***** struct inode *inode; struct file *filp; *****
Return type: ***** int *****
Others: ***** NONE *****
==========================================================*/
static int RTC_open(struct inode *inode, struct file *filp)
{
return 0;
}
/* =======================================================
Function Name:***** RTC_irq_handle() *****
Description: ***** RTC_irq_handle() entry func *****
Created By: ***** Chen Kang *****
Created Date: ***** 2006-7-13 *****
Calls: ***** NONE *****
Called By: ***** NONE *****
normal para: ***** void; *****
pointer para: ***** NONE *****
Return type: ***** int *****
Others: ***** NONE *****
==========================================================*/
static int RTC_irq_handle(void)
{
uint8 sta;
sta = I2STAT; // 讀出I2C狀態字
switch(sta)
{
case 0x08: // 己發送起始條件
if(1==I2C_suba_en)
{
I2DAT = I2C_sla&0xFE; // 指定子地址讀時,先寫入地址
}
else
{
I2DAT = I2C_sla; // 否則直接發送從機地址
}
I2CONCLR = 0x28; // SI=0
break;
case 0x10:
I2DAT = I2C_sla; // 重啟動總線后,發送從地址
I2CONCLR = 0x28; // SI=0
break;
case 0x18: // 已發送SLA+W,并已接收應答
if(0==I2C_suba_en) // 無子地址,則直接發送數據
{
if(I2C_num>0)
{
I2DAT = *I2C_buf++;
I2CONCLR = 0x28;
I2C_num--;
}
else
{
I2CONSET = 0x10; // 無數據發送,結束總線
I2CONCLR = 0x28;
I2C_end = 1; // 設置總線操作結束標志
}
break;
}
if(1==I2C_suba_en) // 發送子地址
{
I2DAT = I2C_suba;
I2CONCLR = 0x28;
}
if(2==I2C_suba_en)
{
I2DAT = I2C_suba;
I2CONCLR = 0x28;
I2C_suba_en = 0; // 子地址己處理
}
break;
case 0x28: // 已發送I2C數據,并接收到應答
if(0==I2C_suba_en) // 無子地址,則直接發送數據
{
if(I2C_num>0)
{
I2DAT = *I2C_buf++;
I2CONCLR = 0x28;
I2C_num--;
}
else
{
I2CONSET = 0x10; // 無數據發送,結束總線
I2CONCLR = 0x28;
I2C_end = 1;
}
break;
}
if(1==I2C_suba_en) // 若是指定地址讀,則重新啟動總線
{
I2CONSET = 0x20;
I2CONCLR = 0x08;
I2C_suba_en = 0; // 子地址己處理
}
break;
case 0x20:
case 0x30:
case 0x38:
I2CONCLR = 0x28; // 總線進入不可尋址從模式
I2C_end = 0xFF; // 總線出錯,設置標志
break;
case 0x40: // 己發送SLA+R,并已接收到應答
if(1==I2C_num) // 最后一字節,接收數據后發送非應答信號
{
I2CONCLR = 0x2C; // AA=0,接收到數據后產生非應答
}
else // 接收數據并發送應答信號
{
I2CONSET = 0x04; // AA=1,接收到數據后產生應答
I2CONCLR = 0x28;
}
break;
case 0x50:
*I2C_buf++ = I2DAT; // 讀取數據
I2C_num--;
if(1==I2C_num)
{
I2CONCLR = 0x2C; // AA=0,接收到數據后產生非應答
}
else
{
I2CONSET = 0x04; // AA=1,接收到數據后產生應答
I2CONCLR = 0x28;
}
break;
case 0x58:
*I2C_buf++ = I2DAT; // 讀取最后一字節數據
I2CONSET = 0x10; // 結束總線
I2CONCLR = 0x28;
I2C_end = 1;
break;
case 0x48:
I2CONCLR = 0x28; // 總線進入不可尋址從模式
I2C_end = 0xFF;
break;
default:
break;
}
VICVectAddr = 0x00; // 中斷處理結束
return(1);
}
/* =======================================================
Function Name:***** RTC_read() *****
Description: ***** RTC_read() entry func *****
Created By: ***** Chen Kang *****
Created Date: ***** 2006-7-13 *****
Calls: ***** NONE *****
Called By: ***** NONE *****
normal para: ***** size_t count; *****
pointer para: ***** struct file *filp; char *buf;loff_t *f_pos; *****
Return type: ***** ssize_t *****
Others: ***** NONE *****
==========================================================*/
static ssize_t RTC_read(struct file *filp, char *buf, size_t count,loff_t *f_pos)
{
/* 參數設置 */
I2C_sla = CSI24WC02+1;
I2C_suba = 0x02;
I2C_buf = buf;
I2C_num = count;
I2C_suba_en = 1;
I2C_end = 0;
I2CONCLR = 0x2C;
I2CONSET = 0x60; // 設置為主機,并啟動總線
while(1)
{
if(1==I2C_end)
{
return(0);
}
}
}
/* =======================================================
Function Name:***** RTC_write() *****
Description: ***** RTC_write() entry func *****
Created By: ***** Chen Kang *****
Created Date: ***** 2006-7-13 *****
Calls: ***** NONE *****
Called By: ***** NONE *****
normal para: ***** size_t count; *****
pointer para: ***** struct file *filp; char *buf;loff_t *f_pos; *****
Return type: ***** ssize_t *****
Others: ***** NONE *****
==========================================================*/
static ssize_t RTC_write(struct file *filp, char *buf, size_t count,loff_t *f_pos)
{
I2C_sla = CSI24WC02;
I2C_suba = 0x02;
I2C_buf = buf;
I2C_num = count;
I2C_suba_en = 2;
I2C_end = 0;
I2CONCLR = 0x2C;
I2CONSET = 0x60; // 設置為主機,并啟動總線
while(1)
{
if(1==I2C_end)
{
return(0);
}
}
}
module_init(RTC_init);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -