{"id":2683,"date":"2024-07-24T05:23:57","date_gmt":"2024-07-24T05:23:57","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=2683"},"modified":"2024-07-24T05:24:00","modified_gmt":"2024-07-24T05:24:00","slug":"getting-started-with-stm32g0-and-stm32cubeide-i2s-in-dma-mode","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=2683","title":{"rendered":"Getting Started with STM32G0 and STM32CubeIDE : I2S in DMA mode"},"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\/07\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-4-2-1-1.webp\" alt=\"\" class=\"wp-image-2684\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-4-2-1-1.webp 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-4-2-1-1-300x300.webp 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-4-2-1-1-150x150.webp 150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-4-2-1-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=2666\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=2666\" target=\"_blank\">here<\/a>), we took a look how to configure the I2S peripheral of STM32G070 and send the data using polling mode. In this guide, we shall use DMA to transmit the data.<\/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 and configuration.<\/li><li>Developing the driver.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Enabling the DMA:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Continue from the previous guide:<\/p>\n\n\n\n<p>From multimedia, select I2S:<\/p>\n\n\n\n<p>From the I2S select DMA settings, then click on add. Select any DMA Stream available and keep the settings as is. Feel free to select circular mode in case you want to keep streaming same data over and over.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"574\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-24_07-54-05-1024x574.jpg\" alt=\"\" class=\"wp-image-2685\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-24_07-54-05-1024x574.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-24_07-54-05-300x168.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-24_07-54-05-768x430.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-24_07-54-05-1536x861.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-24_07-54-05-2048x1148.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-24_07-54-05-1150x644.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-24_07-54-05-750x420.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-24_07-54-05-400x225.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-24_07-54-05-250x141.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Next, from NVIC settings, enable SPI global interrupt as following:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"570\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-24_07-54-53-1024x570.jpg\" alt=\"\" class=\"wp-image-2686\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-24_07-54-53-1024x570.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-24_07-54-53-300x167.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-24_07-54-53-768x428.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-24_07-54-53-1536x856.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-24_07-54-53-2048x1141.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-24_07-54-53-1150x641.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-24_07-54-53-750x418.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-24_07-54-53-400x223.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-24_07-54-53-250x139.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Save the project and this will generate the required codes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Driver Development:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>In main.c file:<\/p>\n\n\n\n<p>In user begin PV section of the main.c file, declare the following the variable as volatile since this will be modified in the interrupt handler:<\/p>\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;}\">volatile uint8_t done=0;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In user code begin 3 in while loop of main function:<\/p>\n\n\n\n<p>Transmit the data over I2S using DMA 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;}\">HAL_I2S_Transmit_DMA(&amp;hi2s1, i2s_data, 10);<\/pre><\/div>\n\n\n\n<p>The function tasks the following parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Pointer to i2s handler which is hi2s1 in this case.<\/li><li>Data buffer to be transmitted, which i2s_data in the case.<\/li><li>Size of the data.<\/li><\/ul>\n\n\n\n<p>You notice that there is no timeout since this will be handled by the DMA and no need to use timeout.<\/p>\n\n\n\n<p>Next, wait for the done to be 1 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;}\">while(done==0);<\/pre><\/div>\n\n\n\n<p>Here, we are just waiting until the data has been transmitted.<\/p>\n\n\n\n<p>Next, set done back to 0 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;}\">done=0;<\/pre><\/div>\n\n\n\n<p>Wait 10 ms before starting again:<\/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_Delay(10);<\/pre><\/div>\n\n\n\n<p>Next, in user code begin 4, this function shall be called every time when I2S finishes transferring the data using DMA.<\/p>\n\n\n\n<p>The function 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 HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s)\n{\n\tdone=1;\n}<\/pre><\/div>\n\n\n\n<p>Within the function, set done to 1.<\/p>\n\n\n\n<p>Thats all for the driver development.<\/p>\n\n\n\n<p>Save the project build the project and run it:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"28\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_15-03-3-1024x28.jpg\" alt=\"\" class=\"wp-image-2671\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_15-03-3-1024x28.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_15-03-3-300x8.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_15-03-3-768x21.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_15-03-3-1536x43.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_15-03-3-1150x32.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_15-03-3-750x21.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_15-03-3-400x11.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_15-03-3-250x7.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_15-03-3.jpg 1734w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Results:<\/h2>\n\n\n\n<p>Connect logic analyzer to the pin and set the decoding mode to i2s and you should get this:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"216\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/Screenshot-2024-07-12-at-5.21.45\u202fPM-1024x216.png\" alt=\"\" class=\"wp-image-2672\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/Screenshot-2024-07-12-at-5.21.45\u202fPM-1024x216.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/Screenshot-2024-07-12-at-5.21.45\u202fPM-300x63.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/Screenshot-2024-07-12-at-5.21.45\u202fPM-768x162.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/Screenshot-2024-07-12-at-5.21.45\u202fPM-1536x324.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/Screenshot-2024-07-12-at-5.21.45\u202fPM-2048x432.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/Screenshot-2024-07-12-at-5.21.45\u202fPM-1150x243.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/Screenshot-2024-07-12-at-5.21.45\u202fPM-750x158.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/Screenshot-2024-07-12-at-5.21.45\u202fPM-400x84.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/Screenshot-2024-07-12-at-5.21.45\u202fPM-250x53.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Happy coding \ud83d\ude09 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide (here), we took a look how to configure the I2S peripheral of STM32G070 and send the data using polling mode. In this guide, we shall use DMA to transmit the data. In this guide, we shall cover the following: Enabling the DMA and configuration. Developing the driver. Results. 1. Enabling the [&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-2683","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\/2683"}],"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=2683"}],"version-history":[{"count":6,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2683\/revisions"}],"predecessor-version":[{"id":2692,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2683\/revisions\/2692"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}