{"id":1065,"date":"2022-07-03T05:21:49","date_gmt":"2022-07-03T05:21:49","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1065"},"modified":"2022-07-03T05:21:52","modified_gmt":"2022-07-03T05:21:52","slug":"getting-started-with-stm32l053-spi-serial-peripheral-interface","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1065","title":{"rendered":"Getting started with STM32L053: SPI (Serial Peripheral Interface)"},"content":{"rendered":"\n<p><br>In this guide, we shall discuss what is SPI and how transmit single byte over SPI.<\/p>\n\n\n\n<p>In this guide, we will cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>What is SPI.<\/li><li>SPI pin setup.<\/li><li>SPI initialize.<\/li><li>SPI transmite function.<\/li><li>Result.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. What is SPI<\/h2>\n\n\n\n<p>SPI, which stands for Serial Peripheral Interface, is a standard with a very specific hardware interface. A connection is between a master and a slave, with the master typical being a processor, and the slave being a peripheral such as a sensor, flash memory device, or a modem chip.&nbsp;&nbsp; It can also be used for processor to processor communications, but in this case, an additional handshake signal is often used. There are normally four signals between a master and a slave, the first is a clock signal, and this signal is always driven by the master, regard which device is transmitting.&nbsp; The second line is a data line for data going from the master to the slave, and this is designated as the master output, slave input line, are MOSI for short. It connects the SPI data out connection on the master and to the SPI data in connection on the slave.<\/p>\n\n\n\n<p>The next conductor is for data in the opposite direction and is labelled as the master input slave output line, are MISO for short. The last conductor is slave select with the slave chip selecting input, and is active low.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/community.nxp.com\/t5\/image\/serverpage\/image-id\/1775i2B1DEDD6CA621B46\/image-size\/large?v=v2&amp;px=999\" alt=\"pastedImage_0.png\" title=\"pastedImage_0.png\" \/><\/figure><\/div>\n\n\n\n<p>If you have more than one slave, with the first being perhaps a sensor of some kind, the slave will be dedicated to slave 1. If you add a second sensor, the top 3 interface line will be shared, but it dedicates for slave\u2019s line will be required for the second device, and the same is true of course for each of additional slave device.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/community.nxp.com\/t5\/image\/serverpage\/image-id\/1823iA304AA7FB6E2C238\/image-size\/large?v=v2&amp;px=999\" alt=\"pastedImage_1.png\" title=\"pastedImage_1.png\" \/><\/figure><\/div>\n\n\n\n<p>Most processors have a maxim of 4 slave selected lines.&nbsp; The four lines could be used effectively as a multiplexed address lines to access more than 4 slaves. You cannot have more than one master on the bus,&nbsp;&nbsp; since the interface is not support coordination between two masters as to which one is controlling the bus.<\/p>\n\n\n\n<p>Transmissions are typically sent as a sequence of bytes, but without a formal protocol, there is nothing restricting communication being&nbsp;&nbsp; byte based. Typical by frame sizes, are in 8 to 32 bits range.&nbsp; Also note the bytes and packets are not acknowledged as they are in i2c, and you could have a master synching communicating with the slave but, you don\u2019t really know of your communications are being received OK. However, some slave devices will echo bytes sent to it, which provides an acknowledgement to the master.&nbsp; it is how the data lines are synchronized with a clock signal.<\/p>\n\n\n\n<p><strong>Clock Polarity and Phasing<\/strong><\/p>\n\n\n\n<p>There are four different modes available, one mode each combination clocking in a low state and high state, with a data being read in a rising edge or falling edge of the clock signal.\u00a0 For modes 0 and 1, the clock is low in idle, which is referred to\u00a0 as clock and 0,\u00a0 For modes 2 and 3 then the clock is in high state when idle, so it has\u00a0 polarity\u00a0 , one , For modes 0 and 2, the data will be sampled by the receiving device\u00a0 on the leading edge of a clock signal.\u00a0 Relative to the idle state, which is referred to a clock phase of zero. So for mode 0, this means the rising edge of the clock and for mode 2, means the following edge of the clock, the other two modes, use a clock phase 1 which means that trailing edge of clock as a returns to an\u00a0 idle state.\u00a0 And this translates to a following edge for mode 1 and the rising edge for mode 3.\u00a0 Mode 0 is the most commonly supported setting. If multiple slaves in the same bus, you may have to reconfigure the settings for the master to which modes when you want to communicate with a different slave.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/community.nxp.com\/t5\/image\/serverpage\/image-id\/1822i008127221D9DF901\/image-size\/large?v=v2&amp;px=999\" alt=\"pastedImage_2.png\" title=\"pastedImage_2.png\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">2. SPI pin setup:<\/h2>\n\n\n\n<p>First thing first, we need to locate the pins of SPI1 in STM32L051 Nucleo-64. We can find those information in the datasheet of our microcontrol.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"608\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.04.39-AM-1024x608.png\" alt=\"\" class=\"wp-image-1066\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.04.39-AM-1024x608.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.04.39-AM-300x178.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.04.39-AM-768x456.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.04.39-AM-1536x911.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.04.39-AM-2048x1215.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.04.39-AM-1150x682.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.04.39-AM-750x445.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.04.39-AM-400x237.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.04.39-AM-250x148.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p>From the table above, we conclude that SPI has alternate function 5 and the following pins:<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><td>Pin&nbsp;<\/td><td>SPI Function<\/td><\/tr><tr><td>PA5<\/td><td>SCK (Serial Clock)<\/td><\/tr><tr><td>PA6<\/td><td>MISO (Master In Slave Out)<\/td><\/tr><tr><td>PA7<\/td><td>MOSI (Master Out Slave In)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>After than we can start by enabling clock access to GPIOA as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t\/*Enable clock access to GPIOA*\/\n\tRCC-&gt;IOPENR |= RCC_IOPENR_IOPAEN;<\/pre><\/div>\n\n\n\n<p>Also, set PA0 as general purpose output to toggle the CS line.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t\/*Set PA0 as output *\/\n\tGPIOA-&gt;MODER|=(1&lt;&lt;0);\n\tGPIOA-&gt;MODER&amp;=~(1&lt;&lt;1);<\/pre><\/div>\n\n\n\n<p>Then, set PA5,PA6 and PA7 a alternate mode:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t\/*Set PA5,PA6,PA7 mode to alternate function*\/\n\t\/*PA5*\/\n\tGPIOA-&gt;MODER &amp;=~(1U&lt;&lt;10);\n\tGPIOA-&gt;MODER |=(1U&lt;&lt;11);\n\n\t\/*PA6*\/\n\tGPIOA-&gt;MODER &amp;=~(1U&lt;&lt;12);\n\tGPIOA-&gt;MODER |=(1U&lt;&lt;13);\n\n\t\/*PA7*\/\n\tGPIOA-&gt;MODER &amp;=~(1U&lt;&lt;14);\n\tGPIOA-&gt;MODER |=(1U&lt;&lt;15);<\/pre><\/div>\n\n\n\n<p>Select which alternate mode(AF0):<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\/*Set PA5,PA6,PA7 alternate function type to SPI1 (AF0)*\/\n\n\t\/*PA5*\/\n\tGPIOA-&gt;AFR[0] &amp;= ~(1U&lt;&lt;20);\n\tGPIOA-&gt;AFR[0] &amp;= ~(1U&lt;&lt;21);\n\tGPIOA-&gt;AFR[0] &amp;= ~(1U&lt;&lt;22);\n\tGPIOA-&gt;AFR[0] &amp;= ~(1U&lt;&lt;23);\n\n\t\/*PA6*\/\n\tGPIOA-&gt;AFR[0] &amp;= ~(1U&lt;&lt;24);\n\tGPIOA-&gt;AFR[0] &amp;= ~(1U&lt;&lt;25);\n\tGPIOA-&gt;AFR[0] &amp;= ~(1U&lt;&lt;26);\n\tGPIOA-&gt;AFR[0] &amp;= ~(1U&lt;&lt;27);\n\n\t\/*PA7*\/\n\tGPIOA-&gt;AFR[0] &amp;= ~(1U&lt;&lt;28);\n\tGPIOA-&gt;AFR[0] &amp;= ~(1U&lt;&lt;29);\n\tGPIOA-&gt;AFR[0] &amp;= ~(1U&lt;&lt;30);\n\tGPIOA-&gt;AFR[0] &amp;= ~(1U&lt;&lt;31);<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">3. Initialize SPI:<\/h2>\n\n\n\n<p>Before we initialize the SPI, we need to know which bus SPI1 is connected to:<\/p>\n\n\n\n<p>From the datasheet, we can see that SPI is connected to APB2.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"908\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.09.13-AM-908x1024.png\" alt=\"\" class=\"wp-image-1067\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.09.13-AM-908x1024.png 908w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.09.13-AM-266x300.png 266w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.09.13-AM-768x866.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.09.13-AM-750x845.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.09.13-AM-400x451.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.09.13-AM-250x282.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.09.13-AM.png 912w\" sizes=\"(max-width: 908px) 100vw, 908px\" \/><\/figure>\n\n\n\n<p>Hence, we can enable clock access to SPI as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t\/*Enable clock access to SPI1 module*\/\n\tRCC-&gt;APB2ENR |= RCC_APB2ENR_SPI1EN;<\/pre><\/div>\n\n\n\n<p>Then we set the SPI<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Set SSM and SSI<br>Set SPI to master mode<\/li><li>Finally enable SPI module <\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"275\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.12.43-AM-1024x275.png\" alt=\"\" class=\"wp-image-1068\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.12.43-AM-1024x275.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.12.43-AM-300x80.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.12.43-AM-768x206.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.12.43-AM-1536x412.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.12.43-AM-2048x549.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.12.43-AM-1150x308.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.12.43-AM-750x201.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.12.43-AM-400x107.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-03-at-8.12.43-AM-250x67.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t\/*Set mode to MASTER*\/\n\tSPI1-&gt;CR1 |= (1U&lt;&lt;2);\n\n\n\t\/*Select software slave management by\n\t* setting SSM=1 and SSI=1*\/\n\tSPI1-&gt;CR1 |= (1&lt;&lt;8);\n\tSPI1-&gt;CR1 |= (1&lt;&lt;9);\n\n\t\/*Enable SPI module*\/\n\tSPI1-&gt;CR1 |= (1&lt;&lt;6);<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. SPI write function:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void spi_transmit(uint8_t *data,uint32_t size)\n{\n\tuint32_t i=0;\n\tuint8_t temp;\n\n\twhile(i&lt;size)\n\t{\n\t\t \/*Wait for the TXE bit to set in the Status Register*\/\n\t\t\/*This will indicate that the transmit buffer is empty*\/\n\t\t while (!((SPI1-&gt;SR)&amp;(1&lt;&lt;1))) {};\n\t\t   \/*Write the data to the Data Register*\/\n\t\t   SPI1-&gt;DR = data[i];\n\t\t   i++;\n\t}\n\t \/*Wait for the TXE bit to set in the Status Register*\/\n\t while (!((SPI1-&gt;SR)&amp;(1&lt;&lt;1))) {};\n\n\t \/*Wait for the BSY bit to reset in the Status Register*\/\n\t while (((SPI1-&gt;SR)&amp;(1&lt;&lt;7))) {};\n\n\t \/*Clear OVR flag*\/\n\ttemp = SPI1-&gt;DR;\n\ttemp = SPI1-&gt;SR;\n}<\/pre><\/div>\n\n\n\n<p>The function takes two arguments, first pointer to the data and second one is the amount of data to be transferred.<\/p>\n\n\n\n<p> <\/p>\n\n\n\n<p>For CS line toggle, it requires two function, cs_low and cs_high as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void cs_low()\n\t{\n\tGPIOA-&gt;BSRR=GPIO_BSRR_BR_0;\n\t}\n\nvoid cs_high()\n\t{\n\tGPIOA-&gt;BSRR=GPIO_BSRR_BS_0;\n\t}<\/pre><\/div>\n\n\n\n<p>Finally the SPI header file as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\/*\n * spi.h\n *\n *  Created on: Jul 2, 2022\n *      Author: hussamaldean\n *\/\n\n#ifndef SPI_H_\n#define SPI_H_\n\n#include &quot;stdint.h&quot;\nvoid spi_init();\nvoid spi_transmit(uint8_t *data,uint32_t size);\nvoid cs_low();\nvoid cs_high();\n#endif \/* SPI_H_ *\/\n<\/pre><\/div>\n\n\n\n<p>In the main function:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;spi.h&quot;\n#include &quot;string.h&quot;\nuint8_t data[5]={0x01,0x02,0x03,0x04,0x05};\n\nint main(void)\n{\n\n\tspi_init();\n\n\twhile(1)\n\t\t{\n\t\t\tcs_low();\n\t\t\tspi_transmit(data,5);\n\t\t\tcs_high();\n\t\t\t\/*Delay a little bit*\/\n\t\t\tfor (int i=0;i&lt;10000;i++);\n\n\t\t}\n\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Results:<\/h2>\n\n\n\n<p>When you connect logic analyzer to PA5, PA7 and PA0, you will get the following results:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"156\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-02-at-4.36.43-PM-1024x156.png\" alt=\"\" class=\"wp-image-1069\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-02-at-4.36.43-PM-1024x156.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-02-at-4.36.43-PM-300x46.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-02-at-4.36.43-PM-768x117.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-02-at-4.36.43-PM-1536x234.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-02-at-4.36.43-PM-2048x311.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-02-at-4.36.43-PM-1150x175.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-02-at-4.36.43-PM-750x114.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-02-at-4.36.43-PM-400x61.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-02-at-4.36.43-PM-250x38.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall discuss what is SPI and how transmit single byte over SPI. In this guide, we will cover the following: What is SPI. SPI pin setup. SPI initialize. SPI transmite function. Result. 1. What is SPI SPI, which stands for Serial Peripheral Interface, is a standard with a very specific hardware [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,11,12],"tags":[],"class_list":["post-1065","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1065"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1065"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1065\/revisions"}],"predecessor-version":[{"id":1070,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1065\/revisions\/1070"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}