基于uclinux 的CAN總線嵌入式驅(qū)動編程
模塊首先對引用的庫函數(shù)進(jìn)行申明,并且定義:
#define IOPMOD (*(volatile unsigned *)0x3ff5000)
#define IOPDATA (*(volatile unsigned *)0x3ff5008)
#define IOPCON (*(volatile unsigned *)0x3ff5004)
#define EXTDBWTH(*(volatile unsigned *)0x3ff5
#define SYSCFG(*(volatile unsigned *)0x
主要有以下幾個(gè)模塊:
void can_init(void)
{
SYSCFG =SYSCFG 0x0fffffffd;
EXTDBWTH =EXTDBWTH 0x00ff0ff;
IOPMOD=0xf0ff;
IOPDATA=0x6000; 寄存器地址0,MOD寄存器
IOPDATA= IO_PDATA0xdfff; ALE=0 配置MOD寄存器
IOPDATA= IO_PDATA|0x3f; ; 復(fù)位模式、使能
IOPDATA=0x6006; ;寄存器地址6,總線定時(shí)器0寄存器
IOPDATA=IO_PDATA0xdfff; ALE=0配置寄存器
IOPDATA= IO_PDATA|0x3f; 跳轉(zhuǎn)寬度、波特率設(shè)置
……;配置總線定時(shí)器1、驗(yàn)收代碼寄存器等
IOPDATA=0x6000; SJA1000 寄存器地址0,MOD寄存器
IOPDATA=IO_PDATA0xdfff; ALE=0配置MOD寄存器
IOPDATA= IO_PDATA0xfe;寫復(fù)位位,進(jìn)入工作模式
result = register_chrdev(254,"can",can_fops);申請主設(shè)備號
if (result0) {
printk(KERN_WARNING "CAN:can’t getmajor ", result);
return result;
}
在該驅(qū)動程序中,定義結(jié)構(gòu)變量can_fops為應(yīng)用程序訪問內(nèi)核的接口:
static struct file_operations can_fops = {
read: can_read,
write: can_write,
open: can_open,
release: can_release,
};
static int can_release(struct inode *inode, struct file *file)
{
MOD_DEC_USE_COUNT; ;用戶減計(jì)數(shù)
Return 0;
}
static int can_open(struct inode *inode,struct file *file)
{
Scull_Dev *dev;
Int num = NUM(inode->i_rdev); 設(shè)備號
Int type = TYPE(inode->i_rdev); 設(shè)備類型
If (num>=scull_nr_devs) return -ENODEV;
dev = scull_devices[num];
flip->private_data = dev;
MOD_INC_USE_COUNT; 用戶數(shù)人工計(jì)數(shù)
IOPCON=0x16; //xIRQ0
disable_irq(INT_can);
if(request_irq(INT_can, can_rx,
SA_INTERRUPT, "can rx isr","can")) {
printk("s3c4510-can: Can't get irq %d",
INT_sja1000);
return -EAGAIN;
}
printk("can has get irq 0");
enable_irq(INT_can);
…… ; 配置SJA1000內(nèi)部中斷及屏蔽寄存器
return 0;
}
7 結(jié)束語
本文介紹了CAN總線在嵌入式系統(tǒng)中的驅(qū)動編程,對CAN總線技術(shù)在嵌入式系統(tǒng)中的應(yīng)用進(jìn)行了探索.本設(shè)計(jì)已在通信用逆變電源遠(yuǎn)程監(jiān)控系統(tǒng)中應(yīng)用.
參考文獻(xiàn):
[1].S3C4510Bdatasheethttp://www.dzsc.com/datasheet/S3C4510B_59.html.
[2].RISCdatasheethttp://www.dzsc.com/datasheet/RISC_1189725.html.
[3].SJA1000datasheethttp://www.dzsc.com/datasheet/SJA1000_609075.html.
[4].PCA82C200datasheethttp://www.dzsc.com/datasheet/PCA82C200_554.html.
[5].p12datasheethttp://www.dzsc.com/datasheet/p12_2043488.html.
[6].p13datasheethttp://www.dzsc.com/datasheet/p13_2043489.html.
[7].p15datasheethttp://www.dzsc.com/datasheet/p15_1204152.html.
評論