Raspberry Pi Pico PIO - Ep. 4 - PIO Timing

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome to life with david i'm david and today i'm continuing my research into the programmable input output features of the rp2040 last time we successfully and quickly transferred parallel data from a raspberry pi pico to a tim-based 6502 computer using pio but parallel data is easy now i'm ready to try some more time critical applications before i do that i need to know more about the pio program flow so why don't you join me as we learn more about the timing features of the rp2040pio this is the fourth video on my series about the rp2040 programmable input output we've already used several instructions to write a program to transfer parallel data using handshaking the next step is to address time sensitive applications like serial communications or video and audio signals there are many applications that have been developed to precisely time signals pio has several instructions and options to control the time critical signals they include frequency delay no op wrap and weight let's look at an example here we have a very simple micro python program we're using the set instruction to turn on a pin and then turn it off like the side set option the set instruction can be used to not only control up to five contiguous gpio pins but it can also assign values to the five least significant bits of the x or y scratch registers or to control the data direction of the set pins in this case we have specified the set instruction to control one gpio pin we'll instantiate this program using a clock frequency of two kilohertz and map the set pin to gpio pin 16. even though i call it the clock frequency it's actually the 125 megahertz system clock frequency divided down for use inside this one pio state machine two kilohertz is the slowest stable clock speed we can use on the pico any lower than that and the clock could be any frequency each instruction inside the pio program takes one clock cycle in this case gpio pin 16 will go high for 500 microseconds and then go low for 500 microseconds gpio pin 16 is connected to the green led i've connected this pin to my oscilloscope and frequency counter so we can see what's happening to the waveform let's start the program the scope is set to 200 microseconds per division i'm measuring a frequency of one kilohertz which is the clock frequency of two kilohertz divided by the number of clock cycles in the loop part of the pio program in this case two note that the waveform has a fifty percent duty cycle increase the clock frequency by a factor of 10 to 20 kilohertz i'll increase the sweep speed to 20 microseconds per division again note that the duty cycle is 50 percent the measured frequency is 10 kilohertz again the clock frequency divided by the number of clock cycles pio can operate at a clock frequency of up to 125 megahertz but my test equipment can't come close to measuring that so let's increase the clock frequency to 1 megahertz i'll increase the scope sweep to 2 microseconds per division now i'm measuring a waveform frequency of 500 kilohertz with a duty cycle of 50 percent we can modify the duty cycle of the waveform by using no op commands no op isn't listed as one of the nine pio instructions it's called a pseudo instruction because it doesn't do anything it just takes up time and space wait that's what my wife says about me anyway let's add a no op after we turn the gpio pin high this has one clock cycle to the pio program i expect the signal frequency will drop to 333 kilohertz which is one megahertz divided by three cycles i also expect that the on time will be double the off time let's try it yep just as we expected now let's add a no op after we turn the gpio pin off this brings the total number of clock cycles to 4 and should bring the waveform duty cycle back to 50 percent as expected the waveform frequency is 250 kilohertz as i said before the no op takes up time and space but we only have room for 32 instructions what if we want a delay but we can't afford the space for one or more no-ops luckily pio has a delay option that can be added to each instruction this adds a delay of up to 31 clock cycles after the instruction executes we implement this by adding a decimal number from 0 to 31 inside the square brackets after the main instruction let's get rid of the first no op and replace it with a delay of 1 after we turn the gpio pin on the waveform should look the same i'll stop and restart the program and there's no difference in the waveform now let's change the delay to the maximum 31. this lowers the waveform frequency since there are now 34 clock cycles in the program loop in addition the duty cycle is about 95 percent since there are 32 cycles where the signal is high and only two where the signal is low now let's remove the last no op and add a delay of 31 to the offset instruction this brings the waveform frequency way down since there are now 64 clock cycles for every instruction loop however the duty cycle has returned to 50 percent [Music] a no up is good for a couple things it can carry a delay and a side set instruction let's remove the 31 cycle delay on the first set instruction and instead add a no op with a 30 cycle delay the result is the same the number of clock cycles in the loop is still 64 with 32 high and 32 low [Music] for giggles and grins let's slow the clock frequency way down to 2 kilohertz now the waveform frequency has slowed to about 31 hertz so you can see that there's a lot of flexibility in frequency selection [Music] one thing to point out is that the maximum number of delay cycles that can be added is a function of how many side sets are specified and whether size set is optional if a side set is used on every pio instruction we'll lose one bit of delay time for each side set we specify however if size set is optional and used on only a few instructions we'll lose an additional bit of delay time because the pio needs another bit to determine which instructions you size set in this demonstration we have only specified side set on one of the two instructions note that we can only add seven cycles of delay but not eight this is only three bits of delay [Music] however if we add side set to all the instructions we can add 15 cycles of delay but not 16 which is four bits of delay [Music] sometimes we need to perform some instruction at the beginning of the pio program before we get into the main loop we can accomplish this with a label and a jump instruction in this example we are sending a long on pulse followed by a long off pulse before we get into the main loop this works fine except the jump instruction takes one clock cycle the main loop now takes three clock cycles which slows the output frequency and causes an asymmetric waveform however we can eliminate that extra clock cycle by using the wrap feature of pio in this example we've replaced the label and jump with a wrap target and wrap statement unlike jump rap takes no clock cycle the program now takes only two clock cycles and the waveform is symmetric the last item on the timing list is the weight instruction which is an instruction that stalls the pio program until some condition is satisfied the condition can be the status of an indexed inpin a specific gpio pin or an interrupt flag in episode 2 of the pio chronicles i covered weight in some detail so you might want to check out that video thanks for joining me today we learned about some of the pio features used to modify program flow to achieve the very precise timing requirements for serial communications as well as video and audio signals next time i'd like to jump into the jump instruction in more detail so stay tuned if you like this video or you think someone else might please give it a thumbs up if not give it a thumbs down the more likes this video has the more youtube will recommend it to others also please leave a comment or suggestion for things to do i hope to do more of these videos so please subscribe and click on the bell for notifications of new videos let's get together next time for another day in life with david see you soon [Music] [Music] [Music] you
Info
Channel: Life with David
Views: 1,896
Rating: undefined out of 5
Keywords: Raspberry Pi Pico, PIO Timing, Delay vs sideset, PIO, RP 2040
Id: 7PDuECyrgX0
Channel Id: undefined
Length: 11min 56sec (716 seconds)
Published: Fri Jun 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.