GPIO callback on rising signal
leonida (5096) 6 posts |
Hi to all, Which is the max rate that the signal may have (10 us, 200us, 1ms) ? HELP ME :) |
leonida (5096) 6 posts |
Sorry I mean the MIN rate … |
Andrew Conroy (370) 740 posts |
The GPIO module will allow you set an event, triggered by a rising edge on a GPIO pin, so you don’t have to be specifically looking at that pin when it goes high, but next time you look it will tell you there was an event. You’d need to keep looping around to check if the event had occurred, and then clear it ready for the next. Unfortunately the documentation for the module is very ropey and parts of it are incorrect as it still refers to a now-superceded version and so isn’t that useful at all :( I can’t help with the max/min rate question, I’m afraid. |
leonida (5096) 6 posts |
Hi Andrew, Best Regards |
Andrew Conroy (370) 740 posts |
This is a cut-down version of the code I’m using. Going against the recent flow on here, I’m using meaningful variable names and nicely formatted code that can hopefully be understood. (Proper programmers should look away now.)
I hope this is helpful. |
leonida (5096) 6 posts |
Hi Andrew, The scenario is: the signal with a period of 160 micro sec and the RasPI that must I thought RiscOS was more reactants of Raspian (minor latency) … I saw that the code Thanks |
Andrew Conroy (370) 740 posts |
160us is a bit tight to do anything really. You can shave a few microseconds off by using the SWI number rather than the name. A quick test (with TimerMod) using GPIO_ReadEvent suggests the named SWI takes approx 25us to execute whereas the numbered SWI takes around 2us to execute. This might save you a bit of time, depends what else you need to do in terms of “some little processing”. |
Dave Higton (1515) 3526 posts |
To respond reliably every 160 microseconds is a very hard real time requirement. It’s not the sort of thing that a desktop operating system can be expected to do. It’s the province of a Real Time Operating System. What is the consequence of failing to see an edge? And how quickly must the response be completed? Is it OK to queue a number of edges and respond to all of them a bit later? If so, you might be able to add a bit of external hardware – perhaps as simple as a counter. If you must infallibly get a response every 160 us, within 160 us, then RISC OS, Raspbian and most other OS’s are not appropriate choices. Look for an RTOS, or use dedicated hardware. |
Rick Murray (539) 13841 posts |
160µs works out to be 6250Hz. The theoretical read maximum of the GPIO pins are in the 10MHz bracket, however to get any sort of reliable behaviour you’re going to need to use assembler. 1 Disclaimer: I’m famously crap at maths. Wait for Dave or Jeffrey to point out that I’m an order of magnitude wrong… 2 Converting ticks to instructions is hard with dual issue, out of order execution, some instructions taking longer than others (AND is quick, LDM less so), and stalls due to cache misses… That said, 128Kticks is a lot to work with unless you need to do heavy crunching every event. |
Rick Murray (539) 13841 posts |
On what? If my figures above are correct, then the 23us extra for the named SWI lookup implies that it’s taking a little over eighteen thousand processor ticks (on an 800MHz processor). That seems like a lot just to look up a SWI by name. |
Andrew Conroy (370) 740 posts |
I’m certainly not claiming a rigorous scientific study here. |
nemo (145) 2547 posts |
There’s a lot of SWIs and there’s no clever hashing – it’s not meant to be fast. |
leonida (5096) 6 posts |
Hi to all, It’s interesting to know that if I use a number instead of a string with a SWI call, I can I can’t loss a single rising edge: i’m implementing an FPGA’s “simple CPU” and I want use I made many tests with Raspian (without/with RealTime patch). On my experience side, without Many thanks to all |
Rick Murray (539) 13841 posts |
SWI name/number only had relevance to BASIC’s SYS command. Everything else expects a number to be embedded in the SWI (by whatever mechanism suits the language). The next questions: can a GPIO pin generate an interrupt (quite likely yes) and how is that interrupt presented to RISC OS (that’ll be the harder part)? |
leonida (5096) 6 posts |
Hi Rick, |
Martin Avison (27) 1494 posts |
I thought you said earlier that you were using the BASIC assemler? Usually, in a BASIC program which includes assembler, the BASIC assembler source is only executed twice (for the two passes normally required). This is when any SWI names are converted to a number. Once assembled, the assembled code is usually called once (but may be many times), and some of the instructions may be execucuted many thousands or milllions of times (depending how the code branches and loops). Any SWIs executed will be using the assembled ARM instructions which include the number as part of the instruction word. No further name lookups are required. Any normal BASIC using SYS calls needs any names converting to numbers by BASIC every time they are executed. |