在Linux中断处理程序中创建一个小延迟

时间:2022-10-23 23:46:18

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.

我正在与硬件设计小组合作开发一个中断处理程序,我们正试图弄清楚bug的位置。我正在以5khz的速度读取SPI总线上的芯片。芯片加载4个字节并触发数据就绪引脚。

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.

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

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.

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

2 个解决方案

#1


1  

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.

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

#2


0  

I assume your compiler will strip out a simple loop.

我假设您的编译器将删除一个简单的循环。

You should use volatile.

你应该使用volatile。

volatile unsigned long i;
for (i=0;i< 1000000; i++) 
  continue;

I assume also that this will not remove the problem or help you.

我还假设这不会消除问题或帮助你。

I can't believe, that a SPI peripheral has such a bug.

我无法相信,SPI外设有这样的错误。

But it's possible that you read to slow the data from the SPI-Fifo.
So some of the received data will be dropped.

但是你可能会读取SPI-Fifo数据的速度。因此,一些收到的数据将被删除。

You should check the error flags of the SPI module and check the RX-empty RX-fullflags of the SPI.

您应该检查SPI模块的错误标志,并检查SPI的RX-empty RX-fullflags。

#1


1  

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.

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

#2


0  

I assume your compiler will strip out a simple loop.

我假设您的编译器将删除一个简单的循环。

You should use volatile.

你应该使用volatile。

volatile unsigned long i;
for (i=0;i< 1000000; i++) 
  continue;

I assume also that this will not remove the problem or help you.

我还假设这不会消除问题或帮助你。

I can't believe, that a SPI peripheral has such a bug.

我无法相信,SPI外设有这样的错误。

But it's possible that you read to slow the data from the SPI-Fifo.
So some of the received data will be dropped.

但是你可能会读取SPI-Fifo数据的速度。因此,一些收到的数据将被删除。

You should check the error flags of the SPI module and check the RX-empty RX-fullflags of the SPI.

您应该检查SPI模块的错误标志,并检查SPI的RX-empty RX-fullflags。