{"id":2666,"date":"2024-07-12T14:41:40","date_gmt":"2024-07-12T14:41:40","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=2666"},"modified":"2024-07-12T14:41:42","modified_gmt":"2024-07-12T14:41:42","slug":"getting-started-with-stm32g0-and-stm32cubeide-i2s-in-polling-mode","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=2666","title":{"rendered":"Getting Started with STM32G0 and STM32CubeIDE : I2S in Polling 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.webp\" alt=\"\" class=\"wp-image-2667\" 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.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-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-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-250x250.webp 250w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/figure><\/div>\n\n\n\n<p>In this guide on STM32G070, we shall cover I2S peripheral and take a look what is I2S and I2S feature in STM32G070.<\/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<p><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>What is I2S.<\/li><li>Feature of I2S of STM32G070.<\/li><li>Driver Development.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. What is I2S:<\/h2>\n\n\n\n<p><strong>I\u00b2S<\/strong>&nbsp;(Inter-IC Sound), pronounced \u201ceye-squared-ess,\u201d is an electrical serial bus interface standard used for connecting digital audio devices together. It facilitates the communication of&nbsp;<strong>PCM (pulse-code modulated)<\/strong>&nbsp;audio data between integrated circuits within an electronic device. Here are some key details about I\u00b2S:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Bus Configuration<\/strong>:<ul><li>The I\u00b2S bus consists of at least three lines:<ul><li><strong>Bit Clock (BCLK)<\/strong>: Also known as the \u201ccontinuous serial clock,\u201d this line synchronizes the data transmission. It pulses once for each discrete bit of data on the data lines.<\/li><li><strong>Word Clock (WS)<\/strong>: Indicates whether channel 1 (WS = 0) or channel 2 (WS = 1) is being transmitted. I\u00b2S allows two channels to be sent on the same data line.<\/li><li><strong>Multiplexed Data Line (SD)<\/strong>: Carries the actual audio data. It can be called by various names such as SDATA, SDIN, SDOUT, DACDAT, or ADCDAT.<\/li><\/ul><\/li><li>Optionally, there may be a&nbsp;<strong>Master Clock<\/strong>(typically 256 times the LRCLK) for synchronizing analog\/digital converters.<\/li><\/ul><\/li><li><strong>Timing and Synchronization<\/strong>:<ul><li>The word select clock (WS) has a 50% duty cycle and the same frequency as the sample rate. For stereo material, left audio is transmitted during the low cycle of WS, and the right channel during the high cycle.<\/li><li>Data is latched on the rising edge of the serial clock (SCK).<\/li><\/ul><\/li><li><strong>History<\/strong>:<ul><li>Introduced by Philips Semiconductor (now NXP Semiconductors) in 1986.<\/li><li>Last revised in February 2022, updating terms from \u201cmaster\u201d and \u201cslave\u201d to \u201ccontroller\u201d and \u201ctarget\u201d.<\/li><\/ul><\/li><\/ol>\n\n\n\n<p>I\u00b2S is commonly used in audio codecs, microcontrollers, digital signal processors, and other sound-processing devices.\u00a0<a href=\"https:\/\/piembsystech.com\/i2s-protocol-framing-working-applications\/\" data-type=\"URL\" data-id=\"https:\/\/piembsystech.com\/i2s-protocol-framing-working-applications\/\">It provides high-quality audio transmission, precise synchronization, and efficient data transfer, making it a preferred choice for digital audio communication<\/a>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Feature Of I2S in STM32G070:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Half-duplex communication (only transmitter or receiver)<\/p>\n\n\n\n<p>\u2022 Master or slave operations<\/p>\n\n\n\n<p>\u2022 8-bit programmable linear prescaler to reach accurate audio sample frequencies (from 8 kHz to 192 kHz)<\/p>\n\n\n\n<p>\u2022 Data format may be 16-bit, 24-bit or 32-bit<\/p>\n\n\n\n<p>\u2022 Packet frame is fixed to 16-bit (16-bit data frame) or 32-bit (16-bit, 24-bit, 32-bit data frame) by audio channel<\/p>\n\n\n\n<p>\u2022 Programmable clock polarity (steady state)<\/p>\n\n\n\n<p>\u2022 Underrun flag in slave transmission mode, overrun flag in reception mode (master and slave) and Frame Error Flag in reception and transmitter mode (slave only)<\/p>\n\n\n\n<p>\u2022 16-bit register for transmission and reception with one data register for both channel sides<\/p>\n\n\n\n<p>\u2022 Supported I 2 S protocols:<\/p>\n\n\n\n<p>\u2013 I 2 S Philips standard<\/p>\n\n\n\n<p>\u2013 MSB-justified standard (left-justified)<\/p>\n\n\n\n<p>\u2013 LSB-justified standard (right-justified)<\/p>\n\n\n\n<p>\u2013 PCM standard (with short and long frame synchronization on 16-bit channel frame or 16-bit data frame extended to 32-bit channel frame)<\/p>\n\n\n\n<p>\u2022 Data direction is always MSB first<\/p>\n\n\n\n<p>\u2022 DMA capability for transmission and reception (16-bit wide)<\/p>\n\n\n\n<p>\u2022 Master clock can be output to drive an external audio component. Ratio is fixed at 256 \u00d7 F S (where F S is the audio sampling frequency)<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Block diagram of I2S in STM32G070:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"949\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/Screenshot-2024-07-12-at-5.29.52\u202fPM-949x1024.png\" alt=\"\" class=\"wp-image-2668\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/Screenshot-2024-07-12-at-5.29.52\u202fPM-949x1024.png 949w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/Screenshot-2024-07-12-at-5.29.52\u202fPM-278x300.png 278w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/Screenshot-2024-07-12-at-5.29.52\u202fPM-768x829.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/Screenshot-2024-07-12-at-5.29.52\u202fPM-1423x1536.png 1423w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/Screenshot-2024-07-12-at-5.29.52\u202fPM-1150x1241.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/Screenshot-2024-07-12-at-5.29.52\u202fPM-750x809.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/Screenshot-2024-07-12-at-5.29.52\u202fPM-400x432.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/Screenshot-2024-07-12-at-5.29.52\u202fPM-250x270.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/Screenshot-2024-07-12-at-5.29.52\u202fPM.png 1564w\" sizes=\"(max-width: 949px) 100vw, 949px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Driver Development:<\/h2>\n\n\n\n<p>We start of by creating new project with name of I2S. For how to create new project, please refer to\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=2299\" target=\"_blank\">this guide<\/a>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>From multimedia, select I2S, the enable half duplex master 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=\"554\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_17-12-05-1024x554.jpg\" alt=\"\" class=\"wp-image-2669\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_17-12-05-1024x554.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_17-12-05-300x162.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_17-12-05-768x416.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_17-12-05-1536x831.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_17-12-05-2048x1108.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_17-12-05-1150x622.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_17-12-05-750x406.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_17-12-05-400x216.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_17-12-05-250x135.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Since there is no I2S enabled device connected, no need to enable Master Clock Output.<\/p>\n\n\n\n<p>This will automatically enable the following pins:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>PA1 as I2S Clock.<\/li><li>PA2 as I2S Data.<\/li><li>PA4 as Word Select (left right channel).<\/li><\/ul>\n\n\n\n<p>Leave the settings as default.<\/p>\n\n\n\n<p>Then from Clock Configuration, Set \/P to 4 so it will provide 32MHz. This is necessary for I2S.<\/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-12_17-12-56-1024x570.jpg\" alt=\"\" class=\"wp-image-2670\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_17-12-56-1024x570.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_17-12-56-300x167.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_17-12-56-768x427.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_17-12-56-1536x855.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_17-12-56-2048x1140.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_17-12-56-1150x640.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_17-12-56-750x417.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_17-12-56-400x223.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_17-12-56-250x139.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Thats all for the configuration.<\/p>\n\n\n\n<p>Save the project. This shall generate the code.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In User Code Begin PV, declare the following array:<\/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;}\">uint16_t i2s_data[10]=\n{\n0,1,2,3,4,5,6,7,8,9\n};<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In User Code Begin 3 in while loop:<\/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  HAL_I2S_Transmit(&amp;hi2s1, i2s_data, 10,100);\n\t  HAL_Delay(10);<\/pre><\/div>\n\n\n\n<p>The HAL_I2S_Transmit takes the following parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>handleTypedef for i2s, which is hi2s1 in this case.<\/li><li>pointer to the data to be transmitted, i2s_data in this case.<\/li><li>Size of data, in this case 10.<\/li><li>Last one is timeout.<\/li><\/ul>\n\n\n\n<p><\/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 size-large\"><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<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. 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<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><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><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide on STM32G070, we shall cover I2S peripheral and take a look what is I2S and I2S feature in STM32G070. In this guide, we shall cover the following: What is I2S. Feature of I2S of STM32G070. Driver Development. Results. 1. What is I2S: I\u00b2S&nbsp;(Inter-IC Sound), pronounced \u201ceye-squared-ess,\u201d is an electrical serial bus interface [&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-2666","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\/2666"}],"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=2666"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2666\/revisions"}],"predecessor-version":[{"id":2673,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2666\/revisions\/2673"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}