在Linux中断处理程序中创建一个小的延迟 [英] create a small delay in a Linux interrupt handler

查看:286
本文介绍了在Linux中断处理程序中创建一个小的延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与一个硬件设计小组一起处理一个中断处理程序,我们正在努力弄清楚错误在哪里。我正在以5khz的SPI总线读取芯片。芯片加载4个字节并触发数据就绪引脚。



我的中断处理程序从SPI总线唤醒并读取4个字节,并将数据存储在缓冲区中。奇怪的是,每17个读取给出4个字节的全0,这是不对的。我们正在探索的一个选择是芯片在发送数据就绪信号时并不总是准备好。



所以,我知道我不能睡觉在中断处理程序中,但我想尝试引入10或20微秒的延迟。现在我有一个计数到100,000的for循环,然后处理中断。我没有看到任何变化,所以我想我可能会看到有人有更好的技术忙于等待。或者至少是一个更好的方式来弄清楚我应该经历多少循环迭代,因为我不知道这需要多长时间,或者编译器只是简单地优化整个事情。

解决方案

我不知道您是否可以访问嵌入式设备上的任何伪随机数生成库,但是进行大量乘法,后面跟着mod将确定需要一些周期。而不是简单地添加1(这在硬件级别非常快,编译器可以优化它,因为你进行静态次数)使用随机数种子(系统是否可以访问时钟? )如果可用,做大数乘法,模数或乘积运算,负数除法也是永远的。记住,在硬件层面上,划分需要最长的时间。有利于您。


I'm working on an interrupt handler with a hardware design group and we're trying to figure out where a bug is. I'm reading a chip over the SPI bus at 5khz. The chip loads 4 bytes and triggers a data ready pin.

My interrupt handler wakes up and read 4 bytes off the SPI bus and stores the data in a buffer. Strangely enough though, every 17th read gives 4 bytes of all 0's, which is not right. One of the options we're exploring is that the chip isn't always actually ready when it sends the data ready signal.

So, I know I can't sleep in an interrupt handler, but I'd like to try and introduce a delay of 10 or 20 microseconds. Right now I have a for loop which counts to 100,000 then processes the interrupt. I haven't seen any changes, so I thought I might see if someone has a better technique for busy waiting. Or at least a better way of figuring out how many loop iterations I should go through, as I'm not sure how long this takes, or if the compiler is simply optimizing out the whole thing.

解决方案

I dont know if you have access to any pseudorandom number generation libraries on your embedded device, but doing large number multiplication followed by mod will definately take some cycles. Instead of simply adding 1 (which is very fast at the hardware level and the compiler can optimize it to shifting since you're doing it a static number of times) use a random number seed (does the system have access to a time clock?) if available and do large number multiplication, modulus or factorial operations, negative number division also takes forever. Remember, division takes the longest at the hardware level. Use that to your advantage.

这篇关于在Linux中断处理程序中创建一个小的延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆