⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 candrv.c

📁 uClinux下can接口驱动程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/*================================================================================
 Module Name:   candrv.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-2-21   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 "candrv.h"
#include "config.h"	
#include "IncludeCan.h"	
/* ==================================================================================
                                     GLOBAL VARIABLES                   
 ==================================================================================*/

unsigned int sw_irq_stat[CAN_MAX_NUM]; /* software irq handle statistic */
wait_queue_head_t can_wait_queue[CAN_MAX_NUM]; /* processes sleeping on CAN RX queue */
/**  Static Variables **/
static CAN_Dev can_devices[CAN_MAX_NUM]; /* allocated for each can controller*/

/* ==================================================================================
                                     LOCAL FUNCTIONS DECLARATIONS                   
 ==================================================================================*/
 

/* =======================================================
 Function Name:***** candrv_llseek *****
 Description:     ***** candrv_llseek entry func *****
 Created By:     ***** Chen kang *****
 Created Date:  ***** 2006-2-21 *****
 Calls:             ***** NONE *****
 Called By:       ***** NONE *****
 normal para:    *****  loff_t off; int whence; *****
 pointer para:   ***** struct file *filp; *****
 Return type:  ***** loff_t *****
 Others:         ***** NONE *****
 ==========================================================*/
static loff_t candrv_llseek(struct file *filp, loff_t off, int whence);

/* =======================================================
 Function Name:***** candrv_read *****
 Description:     ***** candrv_read entry func *****
 Created By:     ***** Chen kang *****
 Created Date:  ***** 2006-2-21 *****
 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 candrv_read(struct file *filp, char *buf, size_t count, loff_t *f_pos);


/* =======================================================
 Function Name:***** candrv_write *****
 Description:     ***** candrv_write entry func *****
 Created By:     ***** Chen kang *****
 Created Date:  ***** 2006-2-21 *****
 Calls:             ***** NONE *****
 Called By:       ***** NONE *****
 normal para:    *****  size_t count; *****
 pointer para:   ***** struct file *filp; const char *buf;loff_t *f_pos; *****
 Return type:  ***** ssize_t *****
 Others:         ***** NONE *****
 ==========================================================*/
static ssize_t candrv_write(struct file *filp, const char *buf, size_t count,loff_t *f_pos);

/* =======================================================
 Function Name:***** candrv_open *****
 Description:     ***** candrv_open entry func *****
 Created By:     ***** Chen kang *****
 Created Date:  ***** 2006-2-21 *****
 Calls:             ***** NONE *****
 Called By:       ***** NONE *****
 normal para:    ***** NONE *****
 pointer para:   ***** struct inode *inode; struct file *filp; *****
 Return type:  ***** int *****
 Others:         ***** NONE *****
 ==========================================================*/
static int candrv_open(struct inode *inode, struct file *filp);

/* =======================================================
 Function Name:***** candrv_release *****
 Description:     ***** candrv_release entry func *****
 Created By:     ***** Chen kang *****
 Created Date:  ***** 2006-2-21 *****
 Calls:             ***** NONE *****
 Called By:       ***** NONE *****
 normal para:    ***** NONE *****
 pointer para:   ***** struct inode *inode; struct file *filp; *****
 Return type:  ***** int *****
 Others:         ***** NONE *****
 ==========================================================*/
static int candrv_release(struct inode *inode, struct file *filp); 

/* =======================================================
 Function Name:***** candrv_ioctl *****
 Description:     ***** candrv_ioctl entry func *****
 Created By:     ***** Chen kang *****
 Created Date:  ***** 2006-2-21 *****
 Calls:             ***** NONE *****
 Called By:       ***** NONE *****
 normal para:    *****  unsigned int cmd;; *****
 pointer para:   ***** struct inode *inode; struct file *filp; *****
 Return type:  ***** int *****
 Others:         ***** NONE *****
 ==========================================================*/
static int candrv_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
                unsigned long param);


module_init(candrv_init);
module_exit(candrv_cleanup);

/********************************************************************************************************/


static struct file_operations candrv_fops =        /* driver info  */
{
    owner:      THIS_MODULE,
    llseek:     candrv_llseek,
    read:       candrv_read,
    write:      candrv_write,
    ioctl:      candrv_ioctl,
    open:       candrv_open,
    release:    candrv_release,
};

/* =======================================================
 Function Name:***** candrv_llseek *****
 Description:     ***** candrv_llseek entry func *****
 Created By:     ***** Chen kang *****
 Created Date:  ***** 2006-2-21 *****
 Calls:             ***** NONE *****
 Called By:       ***** NONE *****
 normal para:    *****  loff_t off; int whence; *****
 pointer para:   ***** struct file *filp; *****
 Return type:  ***** loff_t *****
 Others:         ***** NONE *****
 ==========================================================*/
static loff_t candrv_llseek(struct file *filp, loff_t off, int whence)
{
    return 0;
}

/* =======================================================
 Function Name:**** candrv_read *****
 Description:     ***** candrv_read entry func *****
 Created By:     ***** Chen kang *****
 Created Date:  ***** 2006-2-21 *****
 Calls:             ***** NONE *****
 Called By:       ***** NONE *****
 normal para:    *****  size_t count;; *****
 pointer para:   ***** struct file *filp; char *buf; *****
 Return type:  ***** ssize_t *****
 Others:         ***** NONE *****
 ==========================================================*/
static ssize_t candrv_read(struct file *filp, char *buf, size_t count,
                           loff_t *f_pos)
{
    unsigned char can_number;
    CAN_Dev * dev;
    ssize_t retval = 0;    
    if (!access_ok(VERIFY_WRITE, (void *)buf, _IOC_SIZE(cmd)))
    {
        return -EFAULT;    
    }
    dev = (CAN_Dev *)filp->private_data;
    if(dev == NULL)
    {
        return -EFAULT;
    }        
    can_number = dev->can_number;
	switch(can_number)
	{		
		case	CAN1:
		case	CAN2:
            if (1==CANRcvCyBufApp[can_number].EmptyFlag)
            {
                //printk(KERN_WARNING DEVICE_NAME ": no data is ready\n");
                //return 0; /* no data is ready*/
                wait_event_interruptible(can_wait_queue[can_number],
                (0==CANRcvCyBufApp[can_number].EmptyFlag));
                if(1==CANRcvCyBufApp[can_number].EmptyFlag)
                {
                    //printk(KERN_WARNING DEVICE_NAME ": Awoken now because of signal \n");
                    return -ERESTARTSYS;
                }
                //else
                    //printk(KERN_WARNING DEVICE_NAME ": Awoken now ,  data is ready\n");    
            }
            if(count < sizeof(stcRxBUF))
            {
                        return -EFAULT;
            }
            //printk(KERN_WARNING DEVICE_NAME ": Data is ready, CAN[%d]: ReadPoint=%d, WritePoint%d\n",
               //  can_number,CANRcvCyBufApp[can_number].ReadPoint, CANRcvCyBufApp[can_number].WritePoint);
           
            if (down_interruptible (&dev->read_sem))
            {
                return -ERESTARTSYS;
            }
            //printk(KERN_WARNING DEVICE_NAME ": irq_stat = 0x%d\n",irq_stat);
            //printk(KERN_WARNING DEVICE_NAME ": copy_to_user: buf=0x%x\n",  (unsigned int)buf); 
            sw_irq_stat[can_number]++; /* software irq hander statistic*/
            if (copy_to_user (buf, &CANRcvCyBufApp[can_number].RcvBuf[CANRcvCyBufApp[can_number].ReadPoint],
                sizeof(stcRxBUF))) 
            {
                        retval = -EFAULT;
                        goto can_read_error;
            }
            retval = sizeof(stcRxBUF);
            if (++CANRcvCyBufApp[can_number].ReadPoint >= USE_CAN_cycRCV_BUF_SIZE)
			{
				CANRcvCyBufApp[can_number].ReadPoint = 0;
			}
			CANRcvCyBufApp[can_number].FullFlag = 0;
            if(CANRcvCyBufApp[can_number].ReadPoint == CANRcvCyBufApp[can_number].WritePoint)
                CANRcvCyBufApp[can_number].EmptyFlag=1;
can_read_error:
            up (&dev->read_sem);
			break;
		default:
			return -EFAULT;
	}   
    return retval;
}

/* =======================================================
 Function Name:***** candrv_write *****
 Description:     ***** candrv_write entry func *****
 Created By:     ***** Chen kang *****
 Created Date:  ***** 2006-2-21 *****
 Calls:             ***** NONE *****
 Called By:       ***** NONE *****
 normal para:    *****  size_t count;; *****
 pointer para:   ***** struct file *filp; const char *buf; *****
 Return type:  ***** ssize_t *****
 Others:         ***** NONE *****
 ==========================================================*/
static ssize_t candrv_write(struct file *filp, const char *buf, size_t count,
                            loff_t *f_pos)
{       
    unsigned char can_number;
    CAN_Dev * dev;
    stcRxBUF wri_buff;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -