Page 1 of 1
Frequency of interrupt handler call
Posted: Tue Aug 14, 2012 9:28 am
by hazelnusse
I'm running an STM32F107 @ 72MHz and am using a timer peripheral in input capture mode. I have 4 interrupts enabled: UE, IC1, IC2, IC3. Based on the timer settings and the input signals coming in the frequencies of these events are:
UE (overflow): 50 Hz
IC1: 10-100 Hz
IC2: 10-100 Hz
IC3: 125-2500 Hz
I'm wondering if there is a reasonably easy way to determine whether my system will be able to keep up with the frequency of the interrupts. Obviously it depends on the length of my interrupt handler, which is here
https://github.com/hazelnusse/robot.bicycle/blob/master/Interrupts.cpp. I've tried to look at the .lst file that ends up in the build directory, but I've found it to be surprisingly large (1.9 megabytes) given that I only have one function that is about 40 lines of code with no function calls, so I'm not sure how to determine which instructions corresponds to the actual code associated with my ISR. Under the worst possible scenario, my interrupt handler would be called at 2.7kHz -- which seems really fast. If I fiddle with some settings, I can get it down to around 845Hz. I have CH_FREQUENCY at 1000, so this could potentially be running the interrupt handler at higher frequencies than the OS.
Can anybody comment on how frequently they call their interrupt handler in their own applications, and how many instructions their handlers are? Do my interrupt frequencies seem feasible?
Luke
Re: Frequency of interrupt handler call
Posted: Tue Aug 14, 2012 10:30 am
by Giovanni
Hi,
Your best option is to instrument your ISRs.
In each ISR you can use a time measurement object (TM driver) in order to calculate the ISR duration, then you could use simple math to calculate the overall load.
Note that the TM object calculates a minumum/maximum time, you have to use the "minimum" because the maximum is affected by preemption of higher priority interrupts.
I also recommend to study and run the IRQ_STORM test application, it is designed to enter the IRQ saturation zone. Looking at what you described you are very far from saturation.
Anyway, if you are near a IRQ saturation then you would begin to lose interrupts, you would notice it.
Giovanni
Re: Frequency of interrupt handler call
Posted: Tue Aug 14, 2012 11:00 pm
by hazelnusse
Giovanni,
Thanks for your response, I will study the example you suggested.
Can you offer any suggestions on how to wade through the .lst file that is generated for each .c/.cpp file? It seems like ${ODFLAGS}=-x -syms is such that you don't get the source next to the assembly in the corresponding .lst file, and the .map and .dmp files don't seem to have the src/assembly side by side. Is that src/assembly listing somewhere else?
If I defer processing of the event to a thread, then the data that thread will need includes the timer status register, three of the capture compare registers, and the state of 3 pins on a GPIO port. The main purpose of using this peripheral is to keep track of the speed, so that my control loop (running at 5ms) can use the most recent speed estimate (based on number of timer ticks between input capture events, handling timer overflow carefully). Do you think doing this work in the handler is appropriate, or should I be deferring that processing to a thread? If I should defer, what is the best and safest way to get the data (SR, CCR[0], CCR[1], CCR[2], and GPIOD->IDR) to the thread? I looked at the GenericQueue data structure but it seems to be designed for byte oriented data -- is this the type of structure I would want to use to pass the data to a thread, only adjusted for my data size?
Thanks,
Luke
Re: Frequency of interrupt handler call
Posted: Wed Aug 15, 2012 6:49 am
by Giovanni
You need to modify rules.mk if you want C source mixed with assembler but be warned that it is not much readable after optimizations, if I remember well there is an option flag to add to the assembler.
About the thread, if you need to pass complex structures between an ISR and a thread probably the best mechanism is a mailbox. You can post a pointer to a structure with your data.
You may have two mailboxes, one for sending data, the other one with the unused structures returned by the thread to the ISR.
Giovanni
Re: Frequency of interrupt handler call
Posted: Wed Aug 15, 2012 9:11 am
by hazelnusse
Now that I think about it, I'm not sure my main loop thread (running at 5ms) needs all of the timer counts when the input capture events occur -- it only needs the most recent one. Could I have the interrupt handler write the data to a single static location (hence it would always be the most recent), and have the main loop thread read from that location? The handler would be the only one writing, and my main loop would be the only one reading that memory location.
Is there any risk of data corruption if the main loop was interrupted by the handler, which then updated one of the timer counts that the main loop was about to fetch? The data is essentially 3 32-bit words, so it seems like the worst that could happen would be that the main loop doesn't quite get the most recently updated value, which wouldn't be a big deal in my application. Can I be sure there won't be any data corruption in this scenario?
Luke
Re: Frequency of interrupt handler call
Posted: Wed Aug 15, 2012 9:42 am
by mabl
32 bit reads and writes are atomic. So the worst that could happen is that the 3 values are not in sync. You could also protect it by a semaphore or chSysLock().
Re: Frequency of interrupt handler call
Posted: Wed Aug 15, 2012 10:09 am
by hazelnusse
mabl,
Thanks for the reply. The rising edges of the signals that are going into the three input capture channels connected to my timer will not be synchronous, so they are inherently out of sync. But I think what you said about the reads/writes being atomic should mean that the worst that could happen would that my main loop would read a value right before it gets updated by the handler, so it would be slightly old by the time it gets used. But I don't think this will matter for my application. So I think I should be ok.
Luke