{"id":1310,"date":"2022-11-02T04:38:09","date_gmt":"2022-11-02T04:38:09","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1310"},"modified":"2022-11-02T04:45:44","modified_gmt":"2022-11-02T04:45:44","slug":"working-with-stm32-and-acceleration-sensor-adxl345-in-spi-mode-part-1","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1310","title":{"rendered":"Working with STM32 and Acceleration Sensor: ADXL345 in SPI mode Part 1"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"333\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/41RNUutQb0L.jpg\" alt=\"\" class=\"wp-image-1299\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/41RNUutQb0L.jpg 500w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/41RNUutQb0L-300x200.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/41RNUutQb0L-400x266.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/41RNUutQb0L-250x167.jpg 250w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In the previous guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=1298\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=1298\" target=\"_blank\">here<\/a>), we saw to configure the ADXL345 to work in I2C mode and get the acceleration values.<\/p>\n\n\n\n<p>In this guide, we shall use the same sensor is SPI mode.<\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Connection.<\/li><li>Pin configuration.<\/li><li>SPI configuration.<\/li><li>SPI write function.<\/li><li>SPI read function.<\/li><li>Slave select function.<\/li><li>Slave deselect function.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Connection:<\/h2>\n\n\n\n<p>The connection between STM32F411-Nucleo and adxl345 module is as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"631\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/SPI_ADXL345_pinout-1024x631.png\" alt=\"\" class=\"wp-image-1311\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/SPI_ADXL345_pinout-1024x631.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/SPI_ADXL345_pinout-300x185.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/SPI_ADXL345_pinout-768x474.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/SPI_ADXL345_pinout-1150x709.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/SPI_ADXL345_pinout-750x462.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/SPI_ADXL345_pinout-400x247.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/SPI_ADXL345_pinout-250x154.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/SPI_ADXL345_pinout.png 1239w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><td>STM32F4<\/td><td>ADXL345<\/td><\/tr><tr><td>5V<\/td><td>Vcc<\/td><\/tr><tr><td>GND<\/td><td>GND<\/td><\/tr><tr><td>PA9<\/td><td>CS<\/td><\/tr><tr><td>PA6<\/td><td>SDO<\/td><\/tr><tr><td>PA7<\/td><td>SDA<\/td><\/tr><tr><td>PA5<\/td><td>SCL<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Pins Configuration:<\/h2>\n\n\n\n<p>Since the required pins are PA5 to PA7 and PA9. Hence, we start of by enabling clock access to GPIOA:<\/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;}\">RCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN; \/\/enable clock for GPIOA<\/pre><\/div>\n\n\n\n<p>Set PA5,PA6 and PA7 to alternate 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;}\">GPIOA-&gt;MODER|=GPIO_MODER_MODE5_1|GPIO_MODER_MODE6_1|GPIO_MODER_MODE7_1; \/\/set PA5, PA6 and PA7 to alternate function mode\nGPIOA-&gt;MODER &amp;=~(GPIO_MODER_MODE5_0|GPIO_MODER_MODE6_0|GPIO_MODER_MODE7_0);<\/pre><\/div>\n\n\n\n<p>Set PA9 to General purpose output:<\/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;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">GPIOA-&gt;MODER|=GPIO_MODER_MODE9_0;\nGPIOA-&gt;MODER&amp;=~(GPIO_MODER_MODE9_1);\n<\/pre><\/div>\n\n\n\n<p>Since SPI is AF05, we need to set PA5, PA6 and PA7 to AF5 in AFRL register:<\/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;}\">GPIOA-&gt;AFR[0]|=(0x05&lt;&lt;20)|(0x05&lt;&lt;24)|(0x05&lt;&lt;28);<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. SPI Configuration:<\/h2>\n\n\n\n<p>In order to make ADXL345 working in SPI, the SPI parameters shall be as following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>SPI clock shall not be greater than 4MHz (Clock divider of 4 in this guide (F4 default frequency is 16MHz)).<\/li><li>CPOL=1 and CPHA=1.<\/li><li>MSB first.<\/li><li>Master mode.<\/li><\/ul>\n\n\n\n<p>For more information about SPI, please check this guide <a href=\"https:\/\/blog.embeddedexpert.io\/?p=466\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=466\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"256\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-02-at-7.44.19-AM-1024x256.png\" alt=\"\" class=\"wp-image-1314\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-02-at-7.44.19-AM-1024x256.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-02-at-7.44.19-AM-300x75.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-02-at-7.44.19-AM-768x192.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-02-at-7.44.19-AM-1536x384.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-02-at-7.44.19-AM-2048x512.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-02-at-7.44.19-AM-1150x287.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-02-at-7.44.19-AM-750x187.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-02-at-7.44.19-AM-400x100.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-02-at-7.44.19-AM-250x62.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Hence, the configuration 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;}\">\/*Enable clock access to SPI1 module*\/\n\tRCC-&gt;APB2ENR |= RCC_APB2ENR_SPI1EN;\n\n\t\/*Set clock to fPCLK\/4*\/\n\tSPI1-&gt;CR1 |=(1U&lt;&lt;3);\n\tSPI1-&gt;CR1 &amp;=~(1U&lt;&lt;4);\n\tSPI1-&gt;CR1 &amp;=~(1U&lt;&lt;5);\n\n\t\/*Set CPOL to 1 and CPHA to 1*\/\n\tSPI1-&gt;CR1 |=(1U&lt;&lt;0);\n\tSPI1-&gt;CR1 |=(1U&lt;&lt;1);\n\n\t\/*Enable full duplex*\/\n\tSPI1-&gt;CR1 &amp;=~(1U&lt;&lt;10);\n\n\t\/*Set MSB first*\/\n\tSPI1-&gt;CR1 &amp;= ~(1U&lt;&lt;7);\n\n\t\/*Set mode to MASTER*\/\n\tSPI1-&gt;CR1 |= (1U&lt;&lt;2);\n\n\t\/*Set 8 bit data mode*\/\n\tSPI1-&gt;CR1 &amp;= ~(1U&lt;&lt;11);\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<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 spi1_transmit(uint8_t *data,uint32_t size)\n{\n\tuint32_t i=0;\n\n\twhile(i&lt;size)\n\t{\n\t\t\/*Wait until TXE is set*\/\n\t\twhile(!(SPI1-&gt;SR &amp; (SPI_SR_TXE))){}\n\n\t\t\/*Write the data to the data register*\/\n\t\tSPI1-&gt;DR = data[i];\n\t\ti++;\n\t}\n\t\/*Wait until TXE is set*\/\n\twhile(!(SPI1-&gt;SR &amp; (SPI_SR_TXE))){}\n\n\t\/*Wait for BUSY flag to reset*\/\n\twhile((SPI1-&gt;SR &amp; (SPI_SR_BSY))){}\n\n\t\/*Clear OVR flag*\/\n\t(void)SPI1-&gt;DR;\n\t(void)SPI1-&gt;SR;\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. SPI Read function:<\/h2>\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 spi1_receive(uint8_t *data,uint32_t size)\n{\n\twhile(size)\n\t{\n\t\t\/*Send dummy data*\/\n\t\tSPI1-&gt;DR =0;\n\n\t\t\/*Wait for RXNE flag to be set*\/\n\t\twhile(!(SPI1-&gt;SR &amp; (SPI_SR_RXNE))){}\n\n\t\t\/*Read data from data register*\/\n\t\t*data++ = (SPI1-&gt;DR);\n\t\tsize--;\n\t}\n}\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">6. Slave Select:<\/h2>\n\n\n\n<p>In order to select the slave, you need to set CS line to 0 for the specific slave.<\/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;}\">GPIOA-&gt;BSRR=GPIO_BSRR_BR9;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">7. Slave Deselect:<\/h2>\n\n\n\n<p>To deselect the salve, set the pin to high.<\/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;}\">GPIOA-&gt;BSRR=GPIO_BSRR_BS9;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In part 2, we shall see how to read the adxl345 data.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Stay tuned and happy coding \ud83d\ude42 <\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide (here), we saw to configure the ADXL345 to work in I2C mode and get the acceleration values. In this guide, we shall use the same sensor is SPI mode. In this guide, we shall cover the following: Connection. Pin configuration. SPI configuration. SPI write function. SPI read function. Slave select function. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1310","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1310"}],"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=1310"}],"version-history":[{"count":2,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1310\/revisions"}],"predecessor-version":[{"id":1315,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1310\/revisions\/1315"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}