LINUX 产生PPM 驱动例子

APP:

 

[cpp]  view plain  copy
 
  1. //author:DriverMonkey  
  2. //phone:13410905075  
  3. //mail:bookworepeng@Hotmail.com  
  4. //qq:196568501  
  5.   
  6.   
  7. #include<stdio.h>  
  8. #include<string.h>  
  9. #include<sys/types.h>  
  10. #include<sys/stat.h>  
  11. #include<fcntl.h>  
  12. #include<unistd.h>  
  13.   
  14. #define US (1000)  
  15.   
  16. #define PPM_CHANEL 8  
  17. #define FIX_LOW_TIME 100*US  
  18. #define FIX_SYNC_TIME 5000  
  19.   
  20. static long long ppm_values[(PPM_CHANEL + 1)*2] =   
  21.        {FIX_LOW_TIME,1000*US,   // 1  
  22.        FIX_LOW_TIME,1000*US,    // 2  
  23.        FIX_LOW_TIME,1000*US,    // 3  
  24.        FIX_LOW_TIME,1000*US,    // 4  
  25.        FIX_LOW_TIME,1000*US,    // 5  
  26.        FIX_LOW_TIME,1000*US,    // 6  
  27.        FIX_LOW_TIME,1000*US,    // 7  
  28.        FIX_LOW_TIME,1000*US,    // 8  
  29.        FIX_LOW_TIME,FIX_SYNC_TIME*US, };    // 9  
  30.   
  31.   
  32. int main(int argc,char *args[])  
  33. {  
  34.     int fd;  
  35.     int channel = 0;  
  36.     long long value = 0;  
  37.       
  38.     fd=open("/dev/ppm",O_WRONLY|O_CREAT,0640);  
  39.     if(fd < 0)  
  40.         return 0;  
  41.   
  42.     if(argc > 3)   
  43.         return;  
  44.     channel = atol(args[1]);  
  45.     printf("input channle is: %d\n", channel);  
  46.       
  47.     value  = atol(args[2]);  
  48.     printf("input value is: %d\n", (int)value );  
  49.   
  50.     printf("old value is:%d\n",(int)ppm_values[channel*2 + 1]);  
  51.     ppm_values[channel*2 + 1] = value*US;  
  52.     printf("new value is:%d\n",(int)ppm_values[channel*2 + 1]);  
  53.       
  54.     write(fd,ppm_values,sizeof(ppm_values));  
  55.   
  56.     sleep(20);  
  57.     close(fd);  
  58. }  

Driver:

 

 

[cpp]  view plain  copy
 
  1. //author:DriverMonkey  
  2. //phone:13410905075  
  3. //mail:bookworepeng@Hotmail.com  
  4. //qq:196568501  
  5.   
  6.   
  7. #include <linux/kernel.h>    
  8. #include <linux/module.h>    
  9. #include <linux/cdev.h>    
  10. #include <linux/fs.h>    
  11. #include <linux/device.h>    
  12. #include <linux/syscalls.h>  
  13. #include <linux/interrupt.h>   
  14. #include <linux/gpio.h>  
  15. #include <linux/of_gpio.h>  
  16. #include <linux/of_platform.h>  
  17. #include <linux/uaccess.h>    
  18. #include <linux/string.h>   
  19.   
  20. #include <mach/gpio.h>  
  21. #include <mach/irqs.h>  
  22.   
  23. #define GPIO_TO_PIN(bank, gpio) (32 * (bank) + (gpio))  
  24.   
  25. #define US (1000)  
  26.   
  27. #define PPM_CHANEL 8  
  28. #define FIX_LOW_TIME 100*US  
  29.   
  30.   
  31. struct ppm_dev    
  32. {    
  33.     struct cdev cdev;    
  34.     dev_t devno;    
  35.     struct class *ppm_class;   
  36.     int message_cdev_open;    
  37. };   
  38.   
  39. struct ppm_dev ppm_dev;    
  40.   
  41.   
  42. static long long ppm_values[(PPM_CHANEL + 1)*2] =   
  43.        {FIX_LOW_TIME,1000*US,   // 1  
  44.        FIX_LOW_TIME,1000*US,    // 2  
  45.        FIX_LOW_TIME,1000*US,    // 3  
  46.        FIX_LOW_TIME,1000*US,    // 4  
  47.        FIX_LOW_TIME,1000*US,    // 5  
  48.        FIX_LOW_TIME,1000*US,    // 6  
  49.        FIX_LOW_TIME,1000*US,    // 7  
  50.        FIX_LOW_TIME,1000*US,    // 8  
  51.        FIX_LOW_TIME,5000*US, }; // 9  
  52.   
  53. ktime_t ktime;  
  54. static struct hrtimer hr_timer;  
  55.   
  56. static enum hrtimer_restart hrtimer_callback(struct hrtimer *timer)  
  57. {  
  58.     static int index = 0;  
  59.     static ktime_t ktime;  
  60.     if(index == ((PPM_CHANEL + 1)*2))  
  61.         index = 0;  
  62.     ktime.tv64 = ppm_values[index];  
  63.     hrtimer_forward(timer, timer->base->get_time(), ktime);  
  64.     index++;  
  65.     if(ktime.tv64 == FIX_LOW_TIME)  
  66.         gpio_direction_output(GPIO_TO_PIN(0,27), 0);  
  67.     else  
  68.         gpio_direction_output(GPIO_TO_PIN(0,27), 1);  
  69.   
  70.     //printk("%d\n",(int)ktime.tv64);  
  71.       
  72.     return HRTIMER_RESTART;  
  73. }  
  74.   
  75.   
  76. static int ppm_open(struct inode *node, struct file *fd)    
  77. {    
  78.     int ret = 0;  
  79.       
  80.     printk("ppm_open()++\n");   
  81.       
  82.     ktime = ktime_set( 0, 200*1000);                   // 200us  
  83.     hrtimer_init( &hr_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL );  
  84.     hr_timer.function = &hrtimer_callback;  
  85.     hrtimer_start( &hr_timer, ktime, HRTIMER_MODE_REL );  
  86.   
  87.   
  88.     printk("ppm_open()--\n");    
  89.     
  90.     return ret;    
  91. }     
  92.   
  93.  ssize_t ppm_write(struct file *pfile,   
  94.                     const char __user *buffer,   
  95.                     size_t size,   
  96.                     loff_t *pnull)  
  97.    
  98. {     
  99.   
  100.     printk("ppm_write()++\n");  
  101.       
  102.     if(size != sizeof(ppm_values))  
  103.         return 0;  
  104.   
  105.     copy_from_user(ppm_values, buffer, size);  
  106.   
  107.     printk("ppm_write()--\n");  
  108.       
  109.     return size;  
  110. }  
  111. static int ppm_fasync(int fd, struct file *filp, int mode)  
  112. {  
  113.   
  114.     printk("ppm_fasync()++\n");  
  115.       
  116.   
  117.     printk("ppm_fasync()--\n");  
  118.       
  119.     return 0;  
  120. }  
  121.   
  122. static int ppm_release(struct inode *node, struct file *fd)    
  123. {    
  124.     printk("ppm_release()++\n");   
  125.   
  126.     hrtimer_cancel(&hr_timer);    
  127.     printk("ppm_release()--\n");    
  128.     return 0;    
  129. }   
  130.   
  131.   
  132. struct file_operations meassage_operatons =    
  133. {    
  134.     .owner = THIS_MODULE,    
  135.     .open = ppm_open,    
  136.       
  137.     .write = ppm_write,  
  138.     .fasync = ppm_fasync,  
  139.     .release = ppm_release,    
  140. };    
  141.     
  142. static int __init ppm_init(void)    
  143. {    
  144.     struct ppm_dev * dev;    
  145.     int ret = 0;  
  146.     
  147.   
  148.     dev = &ppm_dev;    
  149.   
  150.     alloc_chrdev_region(&dev->devno, 0, 1, "out_ppm");    
  151.   
  152.     dev->ppm_class = class_create(THIS_MODULE, "ppm_class");    
  153.     if(IS_ERR(dev->ppm_class)) {    
  154.          printk(KERN_ERR"Err: failed in creating class./n");    
  155.          goto fail1;     
  156.      }    
  157.     device_create(dev->ppm_class, NULL, dev->devno, NULL, "ppm");    
  158.   
  159.       
  160.     //init irq  
  161.     ret = gpio_request(GPIO_TO_PIN(0,27), "ppm_inter");  
  162.     if(ret){  
  163.         printk(KERN_ERR"gpio_request() failed !\n");  
  164.         goto fail1;  
  165.     }  
  166.     ret = gpio_direction_output(GPIO_TO_PIN(0,27), 1);  
  167.     if(ret){  
  168.         printk(KERN_ERR"gpio_direction_input() failed !\n");  
  169.         goto fail2;   
  170.     }  
  171.       
  172.     cdev_init(&dev->cdev, &meassage_operatons);    
  173.     cdev_add(&dev->cdev, dev->devno, 1);    
  174.   
  175.       
  176.     if(ret){  
  177.         printk(KERN_ERR"request_irq() failed ! %d\n", ret);  
  178.         goto fail2;  
  179.     }  
  180.       
  181.    printk("ppm_to_app_init(void)--\n");        
  182.     return 0;    
  183.   
  184. fail2:    
  185.     gpio_free(GPIO_TO_PIN(0,27));     
  186. fail1:      
  187.     device_destroy(dev->ppm_class, dev->devno);    
  188.     class_destroy(dev->ppm_class);     
  189.     cdev_del(&dev->cdev);    
  190.     unregister_chrdev_region(dev->devno, 1);    
  191.   
  192.     return ret;  
  193. }    
  194. static void __exit ppm_exit(void)    
  195. {    
  196.     struct ppm_dev *dev = &ppm_dev;    
  197.     
  198.    // printk("ppm_to_app_exit(void)++\n");   
  199.     gpio_free(GPIO_TO_PIN(0,27));  
  200.   
  201.     device_destroy(dev->ppm_class, dev->devno);    
  202.     class_destroy(dev->ppm_class);     
  203.     cdev_del(&dev->cdev);    
  204.     unregister_chrdev_region(dev->devno, 1);    
  205.          
  206.    // printk("ppm_to_app_exit(void)--\n");    
  207. }    
  208. module_init(ppm_init);    
  209. module_exit(ppm_exit);    
  210.     
  211. MODULE_LICENSE("GPL");    
  212. MODULE_AUTHOR("Driver Monkey");    
  213. MODULE_DESCRIPTION("Test ppm");  

转载于:https://www.cnblogs.com/Ph-one/p/6015688.html

该数据集通过合成方式模拟了多种发动机在运行过程中的传感器监测数据,旨在构建一个用于机械系统故障检测的基准资源,特别适用于汽车领域的诊断分析。数据按固定时间间隔采集,涵盖了发动机性能指标、异常状态以及工作模式等多维度信息。 时间戳:数据类型为日期时间,记录了每个数据点的采集时刻。序列起始于2024年12月24日10:00,并以5分钟为间隔持续生成,体现了对发动机运行状态的连续监测。 温度(摄氏度):以浮点数形式记录发动机的温度读数。其数值范围通常处于60至120摄氏度之间,反映了发动机在常规工况下的典型温度区间。 转速(转/分钟):以浮点数表示发动机曲轴的旋转速度。该参数在1000至4000转/分钟的范围内随机生成,符合多数发动机在正常运转时的转速特征。 燃油效率(公里/升):浮点型变量,用于衡量发动机的燃料利用效能,即每升燃料所能支持的行驶里程。其取值范围设定在15至30公里/升之间。 振动_X、振动_Y、振动_Z:这三个浮点数列分别记录了发动机在三维空间坐标系中各轴向的振动强度。测量值标准化至0到1的标度,较高的数值通常暗示存在异常振动,可能与潜在的机械故障相关。 扭矩(牛·米):以浮点数表征发动机输出的旋转力矩,数值区间为50至200牛·米,体现了发动机的负载能力。 功率输出(千瓦):浮点型变量,描述发动机单位时间内做功的速率,取值范围为20至100千瓦。 故障状态:整型分类变量,用于标识发动机的异常程度,共分为四个等级:0代表正常状态,1表示轻微故障,2对应中等故障,3指示严重故障。该列作为分类任务的目标变量,支持基于传感器数据预测故障等级。 运行模式:字符串类型变量,描述发动机当前的工作状态,主要包括:怠速(发动机运转但无负载)、巡航(发动机在常规负载下平稳运行)、重载(发动机承受高负荷或高压工况)。 数据集整体包含1000条记录,每条记录对应特定时刻的发动机性能快照。其中故障状态涵盖从正常到严重故障的四级分类,有助于训练模型实现故障预测与诊断。所有数据均为合成生成,旨在模拟真实的发动机性能变化与典型故障场景,所包含的温度、转速、燃油效率、振动、扭矩及功率输出等关键传感指标,均为影响发动机故障判定的重要因素。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值