Fastest FPS on Your Raspberry Pi SPI LCD Screen - RetroPie playable on a cheap LCD panel

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
using a smaller cheaper serial driven lcd screen with your raspberry pi can cause a lot of issues when it comes to frame rate in games but with a different driver and a few tweaks we can get to a really playable gaming system even using some of the very cheap generic lcd screens from china so let me show you how it's done [Music] hi and welcome to bytes and bits if you've been using a raspberry pi with one of these little spi based lcd screens maybe you're putting together a little retro games console then you'll find that these produce a pretty atrocious frame rate um something like 5 frames per second again because of this um serial data channel which they use in one of my previous videos videos we had a look at setting that up using the standard software and we could see that with a few tweaks and really just increasing that spi data rate we could get to a a usable frame rate i wouldn't say it was a great frame rate but it was a usable frame rate in this video we're going to take that one step further we're going to ditch the standard driver software and install a much much better better optimized as well set of driver software and that will get us heading towards this sort of idealized 60 frames per second so let's get straight in and see what we're going to do in my previous video where we used the standard drivers for these lcd screens and i'll put a link up in the top right hand corner there we worked out that to get our 50 frames per second we would need to drive our serial bus at around about 150 megabits per second and that just wasn't possible we were really sort of topping out on my particular screen at around about 24 megabits per second so that technique of simply spitting the information out on the serial channel faster obviously came to us limitations so what we need to do is have a look if there are any other ways in which we can optimize how we send data to our display to actually create our animations so the second way to get more frames per second coming out across our serial channel is to send less data per frame so in this way we reduce our data rate simply by sending less information now that doesn't sound like a very good idea but if we look at our individual frame so let's look at four frames in our aladdin game that we were using um and then examine a particular block of pixels on each of these frames if we look at each of these although our animation in this particular game is full screen scrolling this little block of pixels doesn't change at all even over four frames so when we take twin frames we'll actually find that there are quite large blocks of the actual screen that don't change and the way these lcd screens work is that once we have set a pixel's color it won't change unless we overwrite it so if we only send across the pixels which have changed on each frame all of a sudden we're no longer sending 2.5 megabits per frame but perhaps maybe a quarter of that or 10th of that or maybe even less for some games so all of a sudden if we combine this with our faster spi rate we can jump from our sort of 15 to 20 frames per second on our previous optimization up towards our 60 frames per second which is really what we need to achieve that super smooth animation so we'll obviously need some software to make all of this happen and luckily there's a very talented github user who goes by the name of juj and if you head across to his github account you'll find an fbcp ili 9341 repository so if we go to that this should possibly then is designed to create exactly what we've been looking for so this idea of both speeding up the spi bus data rate but also then doing this intelligent screen update where we just send pixels that need to be updated out to the screen rather than just blindly sending the entire pixel display every single frame now the code is slightly more complicated to set up and use but we'll go through that step by step so you can see exactly how this all fits together and how we can get it working for your particular display so if we look at the repository page and scroll down through the readme section there is an installation part that takes you through the whole process of getting this up and running now the the driver is termed an ili 9341 driver and that's what it was originally written for but it does cover a wide range of screens so um whatever small lcd or spi lcd you're using this on driver is going to be able to work for that so the process of installing it is that first of all we have to remove any of the previous driver software so if you've been following through my previous video where we installed just a normal sort of wave share stuff we do need to make sure that that's not in place anymore so i'll take you through how to remove that we then need to download the get repository from this um repo build that so there is a bit of terminal based um coding we need to do or not not so much coding but typing in some commands and we need to then select which options we need to build this software with and you can see here there's a range of things that we can do depending on which particular lcd screen we're using or which particular chips perhaps that uses and so on it does look a little bit complicated but it's not so bad once you get down to the nitty-gritty of it and as i say that's what i'm going to take you through now to put together and i'll do it through with my wave share one and then i'll swap across to a different display which is um a an ultra cheap um little lcd tft screen and to show you that we can actually get any of those up and running as well very easily on our raspberry pi even though they're just designed more for sort of the arduino market so i'm back on to my raspberry pi and i'll need to be able to access the terminal on this so um i've connected up my hdmi display and a keyboard and mouse um if you're happy using ssh then please use that but we do need to have some way of accessing and viewing the raspberry pi without the lcd screen and mine is set up where we were at the end of my previous video so we're running emulation station or or retropie and it's all set up with the wave share drivers on my lcd screen and that's all up and running and working okay so we need to then start removing those existing lcd drivers so i'm going to come out of emulation station and we will need to edit our config.txt file okay so we're going to sudo nano slash boot slash config.txt and this is the file that sort of sets up a number of the services inside our raspberry pi so if i scroll down a bit on here now if you haven't yet installed any lcd drivers then obviously some of these bits will not be in place so you won't have to remove them but there's a couple things we do need to make sure we do so down here we have the command line that turns on the hardware spi channel now the new driver has its own version of the software which is an optimized spi driver so we actually need to turn off the hardware driver through this config.txt file if we don't do that this will get interfere with the operation of the new driver and it just simply won't work so we need to turn off our spi driver buffer down here then okay so this is with the audio we can leave that but this line here which um invokes the wave share driver or if you've got some other lcd driver you'll find something similar there we need to then obviously turn that off um so all i'm doing here is i'm just commenting out these lines so they're just sitting in there if i ever need to reinstall them but we need to turn off that driver so that we can then of course use the new one now at the bottom of my code here um i have some hdmi display setup parameters now if you have not yet installed a an lcd driver you might find that you don't have this block down here it's not essential but it is good to reduce the hdmi display resolution down so that we're not having to deal with the full hd screen each time we know that our lcd screen is going to be a much lower resolution so taking our hdmi resolution down just helps the process of of translating that co that screen display into a um the right resolution for our lcd screen so if you haven't yet got anything in there um do have a look at what i've put down here again all of the code that i'm doing here um i will put that into my main website so each of these videos that i produce i do produce a page for each of them in my main bytes and bits websites i'll put links for that down in the description so you can get across there and and and not have to read this off the screen but that little block of code on the end there just to say sets up the hdmi display and we will be tweaking that a little bit later on to just gently improve the quality of our display so that takes it out of our config.txt file so if i do um control x to come out of this um and yes to save that and with the same file name i also need to edit the boot up file for my raspberry pi so i'm going to do another sudo nano so this is in the etc folder and it's the rc dot local file if we go in there um if you have again installed one of these wave share drivers you will probably see a line that looks like that whereas fbcp and then ampersand again if you haven't yet installed anything this won't be here and you won't have to worry about it but we need to remove that so that is another frame buffer um copy um piece of software which the waveshare style drivers use and of course that's going to be replaced by our own one now with this ili 9341 driver so let's uncomment that and then of course x to save that control x sorry and yes to save that and so on so that's removed the software from the boot up configuration on our raspberry pi so obviously we'll now need to reboot so that that's all gone so my raspberry pi has booted back up we're going through retropie i've quit out of emulation station so i'm now at the terminal prompt so to begin with then we're going to have to download this repository and we actually have to compile and build the code ourselves with the various options that we need so first of all we need to make sure that we have the ability to create make files for the compilation so we need to install a little utility called cmake which is probably already on your raspberry pi but just to make sure that it's there we just do the pseudo upgrade install cmake and that makes sure that that little application is in place we're then going to have to download the repository so we're going to do that into our home directory so if you're not already in your home directory do your cd tilde and that will take you to your home directory we're going to use get to clone the repository down and the repository we want to clone is in github dot com slash and it's j m u j slash fbcp and then ili 9341.git so that command there uses get and that will copy that repository from the website down to our home directory so we now need to if i do an ls to see what's on the file now you can see that i now have that fbcp ili 9341 sitting in a folder so i want to cd into that so fb c p so let me just change into that directory and we now want to make our own version of the software with our particular settings for our lcd screen so we're going to build that inside our little folder so we need to make a folder for this i'm going to call my folder build because that's just these way of doing it we're then going to change inside that folder sorry so we're now sitting inside that folder and now we want to create what's known as a make file so so make file is a set of instructions for the com the c compiler which will put together the software and compile it for us with certain sets of options so we need to work out what those options are so if we come back over to the github repository for this project you'll see that we're basically running through this set of instructions that are detailed out here so we're currently about to use the cmake app to create the make file for our our particular setup so we need to have a look at these options down here and this is where it does get as a slightly confusing as to which ones you need to use so you can see that we have here uh various sets of sort of pre-made um lcd screens and it's a matter of working out which one you've got and i find the easiest way to do this is to have a look at the um resolutions of your screen sizes of screen and see if you can find one that sort of matches in best with yours um so we've been using the wave share drivers and i've found that for my particular lcd screen this wave share driver here which does say for the wave share 35b and i know if you looked at my previous video we were using it as a 35a but this one quotes the correct resolution and i know mine is compatible with wave share so for me this one actually does work for my display and this is where sometimes it's going to be a little bit of hit and miss um i will take you through this process of making the um executable file for our particular lcd screen but sometimes we will do that and it won't work and what we'll need to do then is come back in here delete what we had done before recompile it with the next set of options and then try that out so that's where this is a bit more complicated so we will have to build the software test it out if it doesn't work rebuild the software with different options until we get a working set of software so i'm going to go through one which will i know it's going to work on my screen just so we can see what happens when you get it right and then i'll show you how to rebuild that software as well so i'm going to use this option here which is the wave share 35b i'm going to turn that on and that will set up all the correct pins and the correct way in which the software is going to talk to my lcd screen we then need to come down and say we'll go through some of these options a bit later on but we need to come down to setting the speed so the first option then as i say sets up the connections and the way in which it's driven and then we're going to come down here and then work out how fast we want to drive our spi channel so we can see here we have this clock divisor number which must be an even number now what that does is the main chip inside are our raspberry pi runs at around about well the clock inside that is running at around about 400 megahertz so we need to then divide that down to get our spi clock frequency so that's all that's going to happen here so whatever we set this divisor number to be we will divide 400 megahertz by that and that will give us our spi clock frequency so again we need to make sure that that is or our screen is capable of coping with that so i'm going to start off with a a higher divider value just to make sure that we when we do try this screen out if we have got our our screen specification wrong then i will note that that's what's wrong and not that we are driving the screen at too high a frequency so i'm going to start off with a clock divisor off as they suggest here 30 which will take it down to about sort of um 12 or 13 hertz megahertz which i know that my screen is capable of coping with so then if i don't get it to work i know that i've just got other settings wrong once i get a working screen i can obviously then start playing around with this clock divisor to try and get myself to as fast a clock rate as my screen will cope with so let's go on recompile or create our makefile and compile and see if we can get something working so i've already typed out my cmake line um i do struggle typing and talking at the same time so i'm using the cemic command i have then set the option for that wave share 35b with the ili 9486 driver chip i've then set my dspi bus clock divisor to 30 and then after i've put a two dots which means that it will create this cmake file in the folder above the one we're currently in so i'm currently in the build folder so it will actually make this um make file in the folder above that so that's what the two dots are for now that is actually important to put that in there so let's run that command to get that make file created okay so let's create those build files and now we just need to make that so we do make and minus j oh let's come out of my caps lock and that should now have given us so it said it's created build target um fbcp ili 9341 so we should be able to just run that file so sudo and fbcp hyphen ili9 and that should hopefully run our lcd screen and then we have our lcd screen up and running so you can see that it is now mimicking my hdmi source there are there is a little bit of data at the top of the screen which is a little bit of statistics which will show us how well the screen is running and we we can leave that on for now it's on by default when you compile and that just lets you see um how well the screen's running i'm going to leave that on for now so as we go through we can see that if we get better frame rates um just how how better they are so and then eventually we will get to the final set of parameters which will build our um executable file from and at that point we can actually turn off the statistics and that then won't get in the way of our final game display but we now have a a running piece of code and that at the moment we have had to run that by hand by typing in that very last command so we need to of course make sure that this runs when we first boot up our display so that we can actually get it running inside our retropie so ctrl c gets me back to my command prompt and then i need to reboot my raspberry pi and there we have our retropie running on our lcd screen so in the previous videos i had been using a super nintendo game aladdin to test out the performance of this so let's go into that game and see how that fares on this new setup so the game has a sort of start screen where it goes through a couple of the levels and we use that then to compare because we have exactly the same sequence of animations so as we look at the display we can see that we're getting a reasonable frame rate but now again the display seems to turn very blurry so if i pause the animation at this point we can see that up in the top left corner we have a frames per second indicator so at the moment we're heading 20 frames per second but what happens with this driver is that it does all of its optimizations but if that's not enough to hit the target frame rate it actually has a further optimization where it actually interlaces the screen and what that means is that instead of sending every single row of pixels out to the screen it actually only sends every other row of pixels for one frame and then in the next frame it will send the second half of that image and what that means really is that we end up because the display um leaves behind the older pixels when we send the first half of the image on say the odd numbered pixel rows we get one frame displayed then the second time when we put in the even numbered rows we're actually displaying a second frame so we actually end up with half of one frame and half of the next frame on screen and that adds to this sort of motion blur effect so the number you see there um the 20 is the frames per second the i and the p then is interlaced and progressive sometimes you'll have a mixture of two numbers where it's doing some interlaced frames some progressive frames and then the number in red after that that's actually the number of frames that have been dropped in other words just frames it just did not have enough time to display at all so really we're going to have to increase our optimization and increase our spi speed to see if we can then get it working just a bit better and this is what i mean about having to recontinually tweak this driver setup so we're going to have to go back in rebuild it with some new settings and see if we can get it to work that bit better so i've quit out of the game and i'm now going to quit out of emulation station and that takes me back then to my raspberry pi um console so we're going to have to recompile a new version of the software so at the moment i'm in my home directory i've list that you can see that i have that driver folder there so i'm going to cd into that so cd i'm fb and i'm in there and now we have that listed out um it's a bit more than i thought there um but we know that we have a folder called build which is where we actually built up that um version of the software so i'm going to completely delete that directory so i'm going to do rm minus r and then my build directory and we get rid of that i'm now going to redo the commands that we did before so i'm going to make a directory called build so i'm going to remake that directory i'm going to cd into it and then i'm going to re make my make files so if i just use my up arrow i can go past the last commands i used and we should find eventually the one we made they make so which is that one there so again we're targeting our wave share because we know that wave share driver actually does work it's just this clock device that we have to play about with and this really is where we now have to start tweaking this and this is really going to be trial and error on your particular lcd screen to find out which values let you run it as fast as possible while still keeping it working so we've seen that 30 is a bit too slow and we end up having to do too much the interlacing display so let's take it down to 10 and see what happens there so again we're going to make that and then we have to do our make minus j to actually compile the files so that creates a file with the same name as before so if we reset our raspberry pi now that should then boot up but now using this new version so let's go through and reboot them so i've booted back up again and as you can probably see the although my hdmi display is fine so i know that the raspberry pi is up and running and that retropie is working um as you can see on the lcd screen it's just we've lost the display now so obviously setting that divisor to 10 is setting the spi bandwidth just a bit too high for the screen to work so it really is a matter now of just repeating this process until we find that nice sweet spot for it running fast enough to give us um a good frame rate while still giving us a nice clear display so after some playing around somebody going through that process of rebuilding it with different settings i finally got to a divisor of 18 that is working for me so i remember you must always use an even number i found that 16 um let me see maybe half of the screen at a time so it was no good at all and 18 has given me um this display here which is is pretty stable so far um at that speed so let's have a look and see what it's like with the actual game itself so if we come in here and again start up that aladdin game so we're really looking here then at that frame rate and the sort of skip frames and so on so if you look at that we can see that we're actually getting a reasonable frame rate um and the interlacing is still happening now and again but not as much and definitely the skip frames is a lot less in the actual game play then um for me that looks pretty good and we're getting a nice frame rate so we're sort of up in the 30s and 40s and so on consistently um the motion blur has pretty much gone it's still there sometimes but not nowhere near as much as it was um so for me i would rate this as now a a very playable system um so um that seems fine and if we compare it to the very original so that's the default settings unoptimized using the normal sort of wave share driver we can see that the it really is a very very marked difference okay it's not absolutely perfect yes we still have a little bit of that interlacing going on but i think we now have a very playable system so there's a couple of extra things we need to do to finish this off and the first is to remove those statistics on the screen so i'm back into my console and i'm going to change directory into my fbcp again i'm going to remove that build directory i'm then going to make it again and change into it i'm going to find my last cmake command okay so we did that and i'm now going to add a new parameter in here so it's minus d statistics so d start so d stat is sticks there we go just make sure it's all spelled correctly and that needs to be set equal to zero the default setting for that is one which displays that sort of basic um statistics so i'm gonna run that reset the raspberry pi and um see what happens and there we have a nice clean display now ready for us to start gaming so there's one very final optimization which you can do it's not entirely necessary but it does make a very slight difference and that is to change the hdmi resolution so that it matches our lcd screen so at the moment um or this fbcp driver is is taking in effect pictures of the hdmi display and then scaling that to fit on our lcd so especially some sort of smaller text and so on can sometimes get a little bit um corrupted doing that so we can just simply come in here so we need to change our config.txt file so we need to do sudo nano and that's in our boot directory and it's config.txt this is one of the files we edited right at the very very start of of this um video and down the bottom we have our hdmi parameters and we were setting up a custom little resolution there so that so hdmi group 2 mode 7 lets us set up a specific resolution on our screen so what we can do here is we can change that so my particular screen is 480 pixels by 320 and we want to run that at 60 frames per second so if i save that the problem with doing this though is you must do this once you have everything else correct for your lcd screen and you're happy that your lcd screen is actually working correctly because once we do this um some of these lcd screens them if we set our hdmi to match that resolution we will actually lose our hdmi signal so you'll have if you want to come back and edit this or make any changes you'll then have to go through your lcd screen or else um use ssh to remotely log into the raspberry pi so as i say this is something you don't have to do it is probably worth doing um but say only do it once you have or you are sure that everything is working on your lcd screen so i'm going to run this and we'll see if it makes a much of a difference so let's uh come out of this and of course then we'll have to reboot the raspberry pi so that's it running with the matching hdmi resolution and my hdmi monitor is still running actually but there we have it on the lcd screen and to be honest i think yes the the especially the text down the very bottom there the very bottom menu those thin lines um they do seem much clearer with this um if we actually go into then a game so let's come back here and let's try that latin game again see how that works you can see again the the text um ratio there has changed and if we come into the actual display not sure if it has made a difference there to the actual game display um but definitely that is doing as best as it can so i'm sure that we will get certain improvements in certain places but i say um it's because it's such an easy did at the end i think it's probably worth doing um what's the say it's not made any change then to the quality of the graphics and the frame rates and so on uh but anyway that's something that you can do if you want to say do make sure you do that after you've got the lcd screen working so using this juj driver then you should be able to get a pretty good gaming display on any of these relatively cheap spi based lcd screens on your raspberry pi now even the one i'm using here which is specifically designed for the raspberry pi and plugs into the gpio port those are around about what i was paying about 25 pounds for that um the sort of hdmi based ones tend to come in at around about the 40. but there are a set of little lcd displays you can pick up for around about 10 pounds which tend to be based around um another little sort of ili 9341 um display driver a lot of those are aimed at the arduino market and they don't come with a raspberry pi connector but of course they are quite cheap this driver that we've just been using can of course be used to drive those um pretty much as well as we've been driving this um wave share style one and an actual fact that the wave show what i'm doing is is actually running quite slowly so i'm having a time to cover that in this video i think we've already hit about sort of 40 minutes almost um but the actual techniques are the same but i'll actually go through the connecting that up and then setting up some of the parameters some of the um extra parameters when we um compile our driver software we need to put a number of extra parameters in there which allow us to specify which pins we're using to drive one of these some generic displays but i'll put that into a separate video but i'll say if you have got one of the raspberry pi based ones then obviously this driver will give you much much better performance than the standard drivers that come with them so um there's been quite a lot of code and uh things to remember and type in out of this video so please do make sure you visit my main website um where each of these videos that i produce i put in a page which will help you with all of the code and things you need to remember for that particular um project so if you look in the description down below you'll you'll find a link to that webpage and you'll get all of the codes that you need to type in for these parameters and so on and links to off to all of the repositories and and everything you need so if you have enjoyed this video and you want to get more um videos like this do please click the subscribe button and click on that notification bell to make sure that you get a message every time i put up some new content and as i say if you want to have a look at those really cheap um lcd screens for around about a 10 pounds for a nice 3.2 inch display then i will cover that in one of the next videos i'm using this driver but showing you which of the parameters you need to set up for that so hope you're having fun with all this um have fun with your lcd screens and playing your retro games have a go at building a little handheld or something and i will see you in one of the next videos bye for now don't forget to visit the course pages for this project there you'll be able to download the code for this lesson and get lots of extra hints and tips you'll also get access to all my other programming electronics and gaming projects all the links are in the description below for more games programming electronics projects and retro gaming please make sure you like this video subscribe to my youtube channel and visit my website you
Info
Channel: Bytes N Bits
Views: 679
Rating: undefined out of 5
Keywords: raspberry pi, retro gaming, lcd
Id: w1PQXpp6jC8
Channel Id: undefined
Length: 37min 55sec (2275 seconds)
Published: Tue Apr 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.