Beginners Guide to SPI on the Raspberry Pi Pico (BMP280 Example)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to a basic guide on spi or serial peripheral interface in this video i'll go over some basics about how it works why would you why you would want to use it and most importantly how it works with your pico i'll be using a bmp280 temperature and pressure sensor as a sort of worked example to show you how this works but this guide will be applicable for pretty much any spi capable device that you want to use you will just have to change a few bits about what you send to the device but i will show you how to do this and how to find this data in the datasheet feel free to skip some of the basic sections using the timestamps below if you already understand the basics of spi communication as always the source code written in this video is available from my website and a written article version is also available both are linked in the video description so let's start with a little background to spi in most cases spi is a four wire communication protocol in some configurations it can be operated with just three wires but using four is the most common and so i'll cover this here these wires are mozzy master output slave input where the controller sends data to the peripheral device miso on the other hand is the master input slave output where the peripheral device sends data to the controller the sclk line is the clock signal and the cs line is the chip select line which identifies which peripheral on the spi bus will be communicated with i should clarify that the master and slave terminology is being phased out and i will use in this video controller and peripheral in most cases pull up resistors are not required with spi communication and like i squared c spi is also simply a communication interface you still need to power these peripheral devices you can chain multiple devices along the miso mozzie and clock lines like so but you will require a different chip select wire for each peripheral device the only limiting factor theoretically to the number of peripheral devices that you can connect together depends on the ability of the controller device being able to switch the voltage levels there is no hard limit on the number of devices such as with i squared c which has a finite number of addresses in most situations the most limiting factors will be the gpio pins that you actually have available to act as chip select pins to connect your spi devices to the pico you can use any of the gpio ports labeled spi in the pin out diagram highlighted in red here both the mozi miso and clock lines must be connected to the same spi controller in this case that's controller 0 or 1. the chip select pin can be attached to any gpio pin so i'm going to wire up my picot to the breadboard now i'm going to use gpio pins 16 17 18 and 19 on the pico which are the physical pins 21 22 24 and 25 respectively these pins correspond to the spi zero port for reference the miso pin is gpio pin 16 chip select pin 17 clock line pin 18 and finally mozzie pin 19. i will also power the breakout using the 3.3 volts output pin which is pin 36 and its nearest ground which is pin 33. okay so now you have an idea about how the devices are physically connected let's check out the principle of operation of the spi protocol the controller transmits a timing signal across the clock line this allows the peripheral devices to sample the transmitted signals correctly in more advanced tutorials we'll consider how to configure the clock signal phase and polarity but that is a little beyond the scope of this tutorial to initiate communication the controller brings the chip select line of the device it wants to communicate with to a low voltage then the controller sends data across the mossy line which is sampled by the peripheral device on the clock pulse in the case of a response from the peripheral the peripheral sends data along the miso line when sending anything through spi we first send what is called a control byte this contains the 7-bit register address that we want to write to or read from as well as a read or write bit in bit seven or the most significant bit a zero indicates a write request and a one indicator read request after the control byte the data is sent when communication is complete the controller brings the chip select pin back to a high voltage signaling the end of the communication okay so hopefully that gave you an idea of the actual operation of spi let's briefly cover how to actually implement this most microcontrollers have pre-built libraries or drivers such as the pico sdk for the raspberry pi pico this sdk contains functions which handle most of the low-level hardware interactions so we don't have to we will use these to set up and initialize the spi port and for the read and write operations there are three basic functions that we will use during this example for most projects these are probably the only functions you will need we initialize the spi communication with the spi init function and then we write data to the peripheral device with the spi write blocking function and obviously we're going to want to read data from the device and we do that using the spi read blocking function the blocking part simply means that the whole program waits for this communication to occur there are many other functions which are suitable for different applications and you might need them in your specific use case you can find them along with an explanation on page 167 of the pico sdk documentation for a write request to the peripheral we simply bring the chip select line low indicating the start of communication then we use the right function and then we bring the chip select line back up if we have multiple spi devices on the same bus we trigger the specific chip select line to the device that we want to control or to communicate to for a read operation we first bring the chip select line low then send the address of the specific register that we want to read and then we use the read function to receive data from that register before finally bringing the chip select line back up this might initially sound a little confusing but you'll be able to see this quite clearly once we start the examples let's get into an example and i'll tell you how these functions work we create a project as shown in the video in the cards above and in this we are going to make sure that we include the hardware spi libraries as well as the picostandard libraries in our cmake lists file in the c file we include the stdio string pico standard libraries and the hardware spi header files we are then going to define some labels for the pins that we used just to make them a little more straightforward for ourselves in the future then i'm going to define a constant value called spi port this definition takes the value spi0 which is the port that we wired the device up to in this example i'm going to use three functions two of which are to set up and configure specifically the bmp 280 chip and one is our main function this tutorial is only going to cover the main function as it demonstrates how to use the spi communication for the sake of not making this a bmp 280 tutorial i will skip through the supporting functions but i will sort of briefly explain them as i go as a reminder the source code is linked in the description if you want to take a look at them starting in our main file we use the std io init all function which will allow us to print to serial over the usb interface then we are going to initialize the spi using the spi init function whose arguments are the spi port we defined earlier and the speed of communication in hertz i'm going to use a speed of 500 kilohertz now we need to configure the gpio pins that the miso mozzie and clock line and chip select lines are connected to firstly we set mozi miso and the clock line to their spi alternate function using the gpio set function with the pin numbers that we defined earlier and the gpio func spi as arguments then we initialize the gpio for the chip select pin using the gpio init function we then set it as an output using the gpio set direction function and finally bring the voltage high using the gpio put function our gpio should now all be configured next i'm going to call one of the configuration functions i talked about earlier and copy and paste the code from the source code on my website this function reads some calibration values that are specific to each device and programmed in at the factory this helps us get suitable calibrated temperature readings now let's get into what you came here for firstly we need to configure one of the bmp 280s control registers this is a perfect example of a write operation firstly i'll create an array of two unsigned eight bit integers the first value takes the register address that we want to send data to and the second value is the data that we actually want to transmit the register address in this case is f4 but i will use the and bitwise function or bitwise operator with the hex value of 7f this operation makes sure that the most significant bit bit 7 of the address is 0 indicating a write operation the second value in the data array is what we actually want to write to register f4 which in this case is the hex value of 27. to initiate communication we bring the chip select line low with the gpio put function and we simply use the spi right blocking function to send the array to the peripheral this function takes the arguments of the spi port our data array and the size of the array in this case it's two bytes then to signal the end of the spi communication we bring the chip select pin high now we've pretty much finished configuring the bmp 280 chip obviously your case might completely differ from this and so the values you write to your device will be different but you can find all of the registers that your chip has and the configuration steps that needs in its data sheet the process of writing to those registers is exactly the same as what i've just demonstrated here next i'm going to define some variables that we're going to use in an infinite while loop this loop will constantly read the raw temperature values from three registers from the bmp 280 sensor and convert them into actual temperature values this would be a really clunky process if we had to do a read write or sorry write read sequence for each of the three temperature registers thankfully the spi registers automatically increment which means that we can change the expected received data amount to three bytes and we will actually read the three registers starting from our target register that we send this is an example of a read operation firstly the address that we want to read from is f a and we use the bitwise or function with the hex value of 80. this ensures that its most significant bit is 1 indicating a read request then we bring the chip select line low to start communication we use the spi rights blocking function with the arguments of the spi port the address of the variable which contains the target register and one byte communication size immediately after this function we call the spi read blocking function with arguments of the spi instance a zero indicating no write buffer the array which i called buffer which will store the received data and then three the last value means that we expect three bytes of data from the bmp 280. we then bring the voltage of the chip's select line back to high to signal the end of communication a little bit of logic is required to convert three 8-bit integers into one 32-bit integer this converted raw temperature value is then passed through one of the other functions that we copied from the source code this uses the calibration values that we read from the chip earlier to produce a correct temperature reading i will simply print this result over the usb serial interface using the printf function and add a one second delay now that's the coding finished we can now build this project and upload it to the pico now open your serial monitor of choice i use putty and point it to the com port which your pico will be interfacing with the baud rate will be a hundred and fifteen thousand two hundred you should now see the temperature outputs change as you heat up or cool down your chip so hopefully this video has helped you learn the basics of spi communication and how to use it on your pico if this video has helped you then please consider leaving a like and subscribing and feel free to leave any feedback or requests in the comments below
Info
Channel: Learn Embedded Systems
Views: 45,803
Rating: undefined out of 5
Keywords:
Id: s7Lud1Gqrqw
Channel Id: undefined
Length: 14min 29sec (869 seconds)
Published: Sat Feb 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.