{"id":1228,"date":"2022-09-28T05:25:50","date_gmt":"2022-09-28T05:25:50","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1228"},"modified":"2022-09-28T05:25:53","modified_gmt":"2022-09-28T05:25:53","slug":"getting-started-with-stm32l053-i2c-read-mode-with-dma","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1228","title":{"rendered":"Getting started with STM32L053: I2C Read Mode with DMA"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/1200px-I%C2%B2C_bus_logo.svg_-273x300.png\" alt=\"\" class=\"wp-image-417\" \/><\/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=1221\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=1221\" target=\"_blank\">here<\/a>), we saw how to configure the I2C in STM32L053 to write to slave address using DMA. Now, we shall read from a slave device using DMA.<\/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>DMA initializing.<\/li><li>I2C Read using DMA function.<\/li><li>Code.<\/li><li>Demo.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. DMA Initializing:<\/h2>\n\n\n\n<p>We shall continue from the previous topic and we start from the i2c_init function.<\/p>\n\n\n\n<p>Since the DMA1_Channel6 is used for I2C1_TX, we need to find another channel for the RX part.<\/p>\n\n\n\n<p>From the reference manual of STM32L053, we can find that DMA1_Channel7 is for I2C1_RX:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"377\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-28-at-8.14.29-AM-1024x377.png\" alt=\"\" class=\"wp-image-1229\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-28-at-8.14.29-AM-1024x377.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-28-at-8.14.29-AM-300x110.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-28-at-8.14.29-AM-768x283.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-28-at-8.14.29-AM-1536x566.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-28-at-8.14.29-AM-2048x754.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-28-at-8.14.29-AM-1150x423.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-28-at-8.14.29-AM-750x276.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-28-at-8.14.29-AM-400x147.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-28-at-8.14.29-AM-250x92.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Since the clock is already enabled, we shall configure the DMA.<\/p>\n\n\n\n<p>The DMA has the following configuration:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Memory increment mode.<\/li><li>Read from peripheral (DIR=0). <\/li><li>Transfer complete interrupt.<\/li><\/ul>\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=\"436\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-28-at-8.18.41-AM-1024x436.png\" alt=\"\" class=\"wp-image-1230\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-28-at-8.18.41-AM-1024x436.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-28-at-8.18.41-AM-300x128.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-28-at-8.18.41-AM-768x327.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-28-at-8.18.41-AM-1536x654.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-28-at-8.18.41-AM-2048x872.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-28-at-8.18.41-AM-1150x490.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-28-at-8.18.41-AM-750x319.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-28-at-8.18.41-AM-400x170.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-28-at-8.18.41-AM-250x106.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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;}\">DMA1_Channel7-&gt;CCR |=DMA_CCR_MINC|DMA_CCR_TCIE;<\/pre><\/div>\n\n\n\n<p>Set the peripheral address to be RXDR of the I2C1:<\/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;}\">DMA1_Channel7-&gt;CPAR= (uint32_t)&amp;I2C1-&gt;RXDR;<\/pre><\/div>\n\n\n\n<p>Set CxS of Channel7 to 6 which is I2C1.<\/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;}\">DMA_CSELR|=(0x06&lt;&lt;24);<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Thats all for the initializing for the DMA mode.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. I2C 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;}\">\nvoid i2_read_dma(uint8_t slav_add, uint8_t memadd, uint8_t *data, uint8_t length )\n\t{\n\t\t(slav_add=slav_add&lt;&lt;1);\n\t\t\/*Enable I2C*\/\n\t\tI2C1-&gt;CR1 |=I2C_CR1_PE;\n\t\t\/*Set slave address*\/\n\t\tI2C1-&gt;CR2=(slav_add&lt;&lt;I2C_CR2_SADD_Pos);\n\t\t\/*7-bit addressing*\/\n\t\tI2C1-&gt;CR2&amp;=~I2C_CR2_ADD10;\n\t\t\/*Set number to transfer to 1 for write operation*\/\n\t\tI2C1-&gt;CR2|=(1&lt;&lt;I2C_CR2_NBYTES_Pos);\n\t\t\/*Set the mode to write mode*\/\n\t\tI2C1-&gt;CR2&amp;=~I2C_CR2_RD_WRN;\n\t\t\/*Software end*\/\n\t\tI2C1-&gt;CR2&amp;=~I2C_CR2_AUTOEND;\n\t\t\/*Generate start*\/\n\t\tI2C1-&gt;CR2|=I2C_CR2_START;\n\t\t\/*Wait until transfer is completed*\/\n\t\twhile(!(I2C1-&gt;ISR &amp; I2C_ISR_TC)){\n\t\t\t\/*Check if TX buffer is empty*\/\n\t\t\tif(I2C1-&gt;ISR &amp; I2C_ISR_TXE)\n\t\t\t{\n\t\t\t\t\/*send memory address*\/\n\t\t\t\tI2C1-&gt;TXDR = (memadd);\n\t\t\t}\n\t\t}\n\t\t\/*Reset I2C*\/\n\t\tI2C1-&gt;CR1 &amp;=~I2C_CR1_PE;\n\t\tI2C1-&gt;CR1 |=I2C_CR1_PE;\n\t\t\/*Enable DMA RX*\/\n\t\tI2C1-&gt;CR1|=I2C_CR1_RXDMAEN;\n\t\t\/*Set slave address*\/\n\t\tI2C1-&gt;CR2=(slav_add&lt;&lt;I2C_CR2_SADD_Pos);\n\t\t\/*Set mode to read operation*\/\n\t\tI2C1-&gt;CR2|=I2C_CR2_RD_WRN;\n\t\t\/*Set length to the required length*\/\n\t\tI2C1-&gt;CR2|=((length)&lt;&lt;I2C_CR2_NBYTES_Pos);\n\t\t\/*auto generate stop after transfer completed*\/\n\t\tI2C1-&gt;CR2|=I2C_CR2_AUTOEND;\n\n\t\tDMA1_Channel7-&gt;CCR&amp;=~DMA_CCR_EN;\n\t\twhile(DMA1_Channel7-&gt;CCR &amp;DMA_CCR_EN);\n\n\t\tDMA1_Channel7-&gt;CNDTR=length;\n\t\tDMA1_Channel7-&gt;CMAR=data;\n\t\tDMA1_Channel7-&gt;CCR|=DMA_CCR_EN;\n\t\tI2C1-&gt;CR2|=I2C_CR2_START;\n\t\twhile(rx_done==0);\n\t\trx_done=0;\n\t}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">7. Code:<\/h2>\n\n\n\n<p>You may download the code from here:<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/I2C_STM32L0_DMA-1.zip\">I2C_STM32L0_DMA-1<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/I2C_STM32L0_DMA-1.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">8. Demo:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>After uploading the code to the STM32L053, open serial terminal and set the baud to 9600 and you should get something similar to this:<\/p>\n\n\n\n\n\n<p>Happy coding \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide (here), we saw how to configure the I2C in STM32L053 to write to slave address using DMA. Now, we shall read from a slave device using DMA. In this guide, we shall cover the following: DMA initializing. I2C Read using DMA function. Code. Demo. 5. DMA Initializing: We shall continue from [&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-1228","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\/1228"}],"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=1228"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1228\/revisions"}],"predecessor-version":[{"id":1232,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1228\/revisions\/1232"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}