[ENG] Raspberry Pi! Ep: 4 - SPI Usage - C (spidev)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi welcome to another video in this series on the raspberry pi this is the fourth episode of the series in the previous videos we learnt about the rpi boards their history how to set them up for use and learn about the usage of gpio on the rpi using the sysfs interface in this episode we talk about using the spy on raspberry pi using the c language now there are multiple ways to do so in c as well today we see how to use spy using the spyder framework in c let's go [Music] we previously saw the feature rich 40 pin connector of the raspberry pi this is the mapping of the i o pins which are brought out on the connector you have primarily three broad categories of i o power communication and sound all the pins other than power can be used as general purpose io as well you have the spi i2c and uart which are very commonly used in embedded systems you also have a couple of pwm pins which can be used to drive motors or some other actuators unfortunately as of today the raspberry pi boards don't have a hardware adc on them but the raspberry pi pico changes that before we actually see how to use this spi bus let us first learn a little bit about it it basically has two main players the spi master and the spi slave in the typical spi implementation you would see four connections the clock is generated by the spi master this is the heartbeat of the communication the second signal is the mosi or the master out slave in this is the line that carries data from the master to the slave the third connection is the miso or master in slave out this is the line that carries data from the slave to the master the fourth line is called the chip select unless and until the chip select has been asserted that is pulled low the slave should not respond to the master for example if you have an spi system that has two slaves only the one whose chip select line has been pulled low shall respond to the master one of the best features of this bus is that full duplex communication is possible using this bus that is the master and slave can exchange data at the same time another important feature is that due to the chip select mechanism multiple slaves can be connected to the same master but only one slave can and should always talk to the master typical spi masters and systems are microcontrollers microprocessors fpgas etc typical spi slaves that we see in systems are displays sensors secure elements memories and so on with this info with us let us look at how the spi bus can be used in the raspberry pi world these are some of the more popular ways of using the spi in the raspberry pi there are two ways in which you can use the spi bus using c or c plus plus or python if using c or c plus plus there are two main parts that can be chosen first path is that you can write a linux driver that will execute within the linux kernel this has several advantages like faster execution higher priority tight coupling with the kernel and so on however this is the more difficult way to use the spi bus and has a higher learning curve than the other method which is to use a framework called spy dev in the user space it has its own advantages and is pretty popular among beginners as well as intermediates and experts for certain applications if using python then using spydav is the best option to go with in fact a lot of freely available python modules use c or c plus based implementations in the background to efficiently use the spider framework as is true with almost all python implementations the learning curve for such a method is pretty low and heavy abstraction allows you to talk to your spi slave within a few lines of code in today's video we will talk about the spi usage in raspberry pi and any other linux system for that matter using the spyder framework but first let us understand what the spy dev is the spy dev is a way to use the spy or spi in the user space it uses the spy kernel driver and allows you to use the spi master in your system as a device spydev is pretty handy to get started fast and is often also used in the final implementation of a lot of systems around us it is important to know that the linux kernel supports spy in master mode only remember not everything needs a kernel driver the most important thing to know about spy dev is that it supports implementation in master mode only another thing to know is that the spy dev allows you to use the spi slave as a device this means that the spy device is available in the dev folder with the configuration parameters that can be written to or read from using simple ioctal io commands wait what is ioctal ioctyl is an abbreviation for input output control this is the system call in the linux kernel for input output operations specific to a particular device a system call is a way for any application to request some service from the linux kernel each system call or syscall has to specify at the minimum what service it expects and for what for example we just saw that the spydev is a device available for use in the user space by firing ioctal calls with spi specific parameters changes to the spy device and operations with the device can be done these spi specific parameters are also called request codes and these are unique for each device for example the request codes for spy will be different from that of i2c the request code is generally a macro defined in the appropriate header file for that device now let us look at bare minimum spy dev application say you have an application that uses spy dev in the user space and the spy dev driver running within the linux kernel what would their exchanges look like the first step is to open the spy dev device remember how we saw that the dev folder has spider entries that look like spy dev x dot y these have to be opened using file i o operations for example an open or an f open function call the next step is to configure the spy bus using ioctal calls the configuration may involve setting the clock speed configuring the spy operation mode etc the status of this operation is returned by the kernel to the application if successful the application will then send the appropriate data and receive the appropriate data the status for each of these operations is received by the application and can be used to decide the next step the request code for sbi configuration spi transmit and receive etc are all unique come let us test this flow in a quick loopback test in this loopback test we loop back our data that is we try to receive what we send out this is done by physically connecting the mosi and the miso lines of the raspberry pi io connector for instance in this case we simply connect the pins 19 and 21 of the raspberry pi io connector remember the pin 19 is mosi and the pin 21 is miso let's look at the loopback script that we have written it's a very basic spi script we have defined spi device as slash dev slash pydiv 0.0 because the spi bus is 0 and the chip select that we have used is also 0. we use a structure of the type spi ioc transfer this is available in the spy dev framework in this we tell it that we want to transfer an array of the size 32 we populate that array as the numbers 0 to 31 then we try to open the device we try to set its mode and once we are satisfied we try to set the bus speed when we are satisfied with the bus speed we try to perform the transmit operation and then print the received spi buffer let's take a look at this in action let's try it first to run the loopback without the two pins connected so now we don't have the mosi and miso connected when we run the loop back we receive nothing because it's all zeros we have not connected the lines now let's connect both the lines let's try to run the loopback again this time we have received the entire buffer that we sent this tells us that the loopback works let's go back to the video all right the loopback worked let us now do something more fun let us now try to drive an oled display using the spi interface the oled display that we will use today is named ug2832 hswe g04 it is manufactured by a company called univision technology in taiwan the link to the data sheet of this display is in the description what is more important to know is that the display controller inside this module is the ssd1306 from solomon systec the link to the datasheet of this controller is also in the description all you need to know is that the display controller is an spi slave that we will send messages to from the raspberry pi this display controller will understand the spi messages and do the needful for example it will initialize and clear the display turn it on turn it off and so on this display is 128x32 display the board that we will use also has several push buttons and leds on this to allow one to make a nice little demo this board is from microchip technology the link to the documentation of this board is also in the description and the link to purchase this one is also found in the description these are the connections that we will be doing for this example we will be connecting the spi pins that is clock miso mosi and chip select that is the ce0 on the raspberry pi board since we are using ce0 and the spi bus on the i o connector is also spi0 the device that we will work with in software is the slash dev spydev 0.0 note the display that we have chosen does not support any reads from the master so we can skip connecting the mesoline also note the gpi connections to the display that is the command and data line and the reset line which is used to enable the display the link to the code and the process to build this code is in the description do check it out come let us implement this quickly and see spi communication in action let's take a look at the software that we are going to run the first file is called the oled underscore demo.c this contains the main function and it's pretty small it prints a string that says welcome to the oled demo then it performs the configuration of the gpio and the spi for display the gpr that we are using is for the data command line and for the reset line that is connected to the display then we configure the spi bus in this case we configure the spi bus speed and the mode of operation once done we initialize the display within which we actually turn off the display we reset it we do various configurations that are needed for the display to work correctly and after that we simply start printing strings on the display all the relevant functions for configuring the gpio for configuring the spi bus and for configuring the display are present inside the file called oled underscore functions dot c all of this code is available at github in a repository please refer to the description of this video for the link to it now let us run this code all we do is run the oled underscore demo application with sudo permission so that it can access the device files as you can see the printing has now started if you look closely we are printing a string every 500 milliseconds that's it for the demo that's it that is the end of this video thank you for tuning in and do leave a comment and a like if you enjoyed this and would like more content like this see you soon with a brand new video about the world's favorite computer bye [Music]
Info
Channel: Kickstart Embedded
Views: 13,417
Rating: undefined out of 5
Keywords: embedded, raspberry pi, linux, spi, oled, peripherals, c++, programming, hardware, beginner, learn, tutorial
Id: ZFG_g7IbNtw
Channel Id: undefined
Length: 14min 29sec (869 seconds)
Published: Sat Sep 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.