{"id":2584,"date":"2024-06-08T13:41:37","date_gmt":"2024-06-08T13:41:37","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=2584"},"modified":"2024-06-08T13:41:40","modified_gmt":"2024-06-08T13:41:40","slug":"getting-started-with-stm32g0-and-stm32cubeide-spi-rx-dma","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=2584","title":{"rendered":"Getting Started with STM32G0 and STM32CubeIDE: SPI RX DMA"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"400\" height=\"400\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/06\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-4-2-1.webp\" alt=\"\" class=\"wp-image-2585\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/06\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-4-2-1.webp 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/06\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-4-2-1-300x300.webp 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/06\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-4-2-1-150x150.webp 150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/06\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-4-2-1-250x250.webp 250w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/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=2560\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=2560\" target=\"_blank\">here<\/a>), we took a look how to transmit data over SPI bus using DMA. In this guide, we shall use the DMA to receive data over SPI from a slave device (MPU9250) in this case.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Enabling the DMA RX for SPI.<\/li><li>Modification for MPU9250 source code.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. Enabling the DMA RX for SPI:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>By continuing from <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=2560\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=2560\" target=\"_blank\">this guide<\/a>.<\/p>\n\n\n\n<p>Enable the DMA for reception of data as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"584\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/06\/2024-06-08_16-23-19-1024x584.jpg\" alt=\"\" class=\"wp-image-2586\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/06\/2024-06-08_16-23-19-1024x584.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/06\/2024-06-08_16-23-19-300x171.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/06\/2024-06-08_16-23-19-768x438.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/06\/2024-06-08_16-23-19-1536x876.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/06\/2024-06-08_16-23-19-2048x1168.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/06\/2024-06-08_16-23-19-1150x656.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/06\/2024-06-08_16-23-19-750x428.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/06\/2024-06-08_16-23-19-400x228.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/06\/2024-06-08_16-23-19-250x143.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>From communication select SPI.<\/li><li>Select DMA Settings.<\/li><li>Add SPI_RX.<\/li><li>Leave everything as default.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>Also, enable UART, for more details, please refer to&nbsp;<a href=\"https:\/\/blog.embeddedexpert.io\/?p=2316\" target=\"_blank\" rel=\"noreferrer noopener\">this guide<\/a>.<\/p>\n\n\n\n<p>Save the project and this should generate the code.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Modification of MPU9250 Source Code:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>From <a href=\"https:\/\/blog.embeddedexpert.io\/?p=2537\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=2537\" target=\"_blank\" rel=\"noreferrer noopener\">this guide<\/a>, we shall modify the source code as following:<\/p>\n\n\n\n<p>In the declared variables, add the following variable:<\/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;}\">volatile uint8_t rx_Done;<\/pre><\/div>\n\n\n\n<p>Since this variable changes in the interrupt, we need to set it as volatile to prevent compiler optimization.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>At the end of the source code, add the following 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;}\">void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)\n{\n\trx_Done=1;\n\n}<\/pre><\/div>\n\n\n\n<p>This function shall be called when the DMA received the data from the SPI bus.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In MPU9250_accelUpdate function, we shall replace this 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;}\">HAL_SPI_Receive(&amp;hspi1, accelBuf, 6, 100);<\/pre><\/div>\n\n\n\n<p>With this 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;}\">HAL_SPI_Receive_DMA(&amp;hspi1, accelBuf, 6);<\/pre><\/div>\n\n\n\n<p>This function shall received the data over SPI using DMA.<\/p>\n\n\n\n<p>Wait for the DMA to finish transfer:<\/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;}\">\twhile(rx_Done==0);\n\trx_Done=0;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In <strong>MPU9250_gyroUpdate<\/strong> function:<\/p>\n\n\n\n<p>Replace this 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;}\">HAL_SPI_Receive(&amp;hspi1, gyroBuf, 6, 100);<\/pre><\/div>\n\n\n\n<p>With this 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;}\">HAL_SPI_Receive_DMA(&amp;hspi1, gyroBuf, 6);<\/pre><\/div>\n\n\n\n<p>Also, wait for the DMA to finish transfer:<\/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 HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)\n{\n\trx_Done=1;\n\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Thats all for the modification.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Results:<\/h2>\n\n\n\n<p>Open your serial monitor application, select the COM port and set the baud to be 115200 and you should get the following:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"738\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-24-at-7.59.23\u202fAM-1024x738.png\" alt=\"\" class=\"wp-image-2543\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-24-at-7.59.23\u202fAM-1024x738.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-24-at-7.59.23\u202fAM-300x216.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-24-at-7.59.23\u202fAM-768x553.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-24-at-7.59.23\u202fAM-1150x828.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-24-at-7.59.23\u202fAM-750x540.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-24-at-7.59.23\u202fAM-400x288.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-24-at-7.59.23\u202fAM-250x180.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-24-at-7.59.23\u202fAM.png 1166w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Happy coding\u00a0\ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide (here), we took a look how to transmit data over SPI bus using DMA. In this guide, we shall use the DMA to receive data over SPI from a slave device (MPU9250) in this case. In this guide, we shall cover the following: Enabling the DMA RX for SPI. Modification for [&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-2584","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\/2584"}],"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=2584"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2584\/revisions"}],"predecessor-version":[{"id":2587,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2584\/revisions\/2587"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}