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

📄 comedi_ad.c

📁 linux驱动开发comedi的一些例子
💻 C
字号:
/* Analog to Digital: This is the COMEDI_AD.c file       *//* Creates and interface for using COMEDI Device Drivers *//* This file is from RTiC_Lab. The require include file for RTiC device drivers */#include "main.h"/* This two include files are from COMEDI */#include "comedi_module.h"#include "comedi.h"/*************************************************//*    I need to include a header file called "device_config.h" to    configure the devices *//* #include "device_config.h" *//*************************************************//* Four functions have to be implemented. They are: * int RTiC_init_AD_card(void); * void RTiC_AD1_run(void); * void RTiC_AD2_run(void); * int RTiC_stop_AD_card(void);                          *//* There are few things that have to be planned before using COMEDI and   RTiC_lab together. For COMEDI the user has to device the minor number for   the device. She does it when "comedi_config /dev/comediX" is called. *//* COMEDI uses three structures when it calls the trig[0] functions. The   prototype for this call is:   int trig[0](comedi_device *,comedi_subdevice *,comedi_trig *)   We need the following structures:*/static comedi_device    *cd;static comedi_subdevice *cs;static comedi_trig      *ct;/* We need an data value for COMEDI ...*/static sampl_t comedi_data;/* ... and an unsigned int to tell COMEDI what channel to read and how */static unsigned int comedi_channel;/*******************************************************************************//* NOTE: This three next values can be change by the user to fit his/her needs *//* The range for the analog to digital convertions     */#ifndef COMEDI_RTIC_RANGE#define COMEDI_RTIC_RANGE 9#endif/* The reference for the analog to digital convertions */#ifndef COMEDI_RTIC_AREF#define COMEDI_RTIC_AREF  AREF_GROUND#endif/* According with the X value in "comedi_config /dev/comediX" */#ifndef RTiC_COMEDI_MINOR_ADC#define RTiC_COMEDI_MINOR_ADC 0#endif/*******************************************************************************//* This function is called at initialization by RTiC_lab */int RTiC_init_AD_card(void){  int i=0;  int right_subdevice = 0;  printk("In initialization \n");  /* Get comedi_device structure according with RTiC_COMEDI_MINOR_ADC */  cd = comedi_get_device_by_minor(RTiC_COMEDI_MINOR_ADC);    /* Allocate memory for the structure needed by COMEDI */    ct = (comedi_trig*) (kmalloc(sizeof(comedi_trig), GFP_KERNEL));  if(ct == NULL)    return -ENOMEM;  memset(ct,0,sizeof(comedi_trig));  /*  int n_subdevices;      comedi_subdevice *subdevices; */    /* Look up for the subdevice "COMEDI_SUBD_AI": Analog Input      by the end of this loop, cs will point to the apropriate subdevice */  for (i=0; i < cd->n_subdevices && (right_subdevice == 0); i++){    cs = cd->subdevices+i; /* Isn't it the same subdevices[i] than subdevices+i? */    if (cs->type == COMEDI_SUBD_AI)      right_subdevice = 1;  }    if (right_subdevice != 1)    return(-1); /* The device is not there. Maybe return ENXIO 6		   No such device or address                     */  /* Check that the value of NUM_CONTROL_INPUTS do not exceed the number of      available ADC channels                                                 */  if (NUM_CONTROL_INPUTS > cs->n_chan)    return (-1);  ct->data = &comedi_data;  ct->n_chan = 1;  return 0;}/* This function is called from RTiC_Lab code every period */void RTiC_AD1_run(void){  /* COMEDI reads the values from the ADC and returns them in     a non blocking read. We don't need this function for RTiC-lab */}/* This function is called from RTiC_Lab code every period */void RTiC_AD2_run(void){  int index;  /* In this function I have to read the ADC from 0 to NUM_CONTROL_INPUTS,     the result must be assign to the RTiC_sensor array                     */   /* Read all the NUM_CONTROL_INPUTS channels */    for (index=0; index<NUM_CONTROL_INPUTS; index++)    {                 /* How should COMEDI read the channels  */            comedi_channel = CR_PACK(index,COMEDI_RTIC_RANGE,COMEDI_RTIC_AREF );      ct->chanlist = &comedi_channel;      cs->trig[0](cd, cs, ct);            *(RTiC_sensor+index) = ((float) (ct->data[0]/400));      printk("[%d]->%X ", index, ct->data[0]);    }      /* printk("in rtic_adc2 \n"); */}/* This function is called at the end from RTiC_lab */int RTiC_stop_AD_card(void){  printk("In stop card \n");  /* Free the memory for comedi_trig_struct */  kfree(ct);  /* The memory for comedi_device_struct is free by COMEDI */  return 0;}

⌨️ 快捷键说明

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