{"id":1201,"date":"2022-09-14T14:33:46","date_gmt":"2022-09-14T14:33:46","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1201"},"modified":"2022-09-18T05:09:56","modified_gmt":"2022-09-18T05:09:56","slug":"working-with-stm32f7-and-i2c-read-mode-with-dma","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1201","title":{"rendered":"Working with STM32F7 and 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>In the previous guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=902\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=902\" target=\"_blank\">here<\/a>), we took a look at I2C read mode to read set of registers from DS3231 RTC module using polling mode. In this guide, we shall use DMA to read those register.<\/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>I2C configuration.<\/li><li>I2C read using DMA.<\/li><li>Code.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. I2C configuration:<\/h2>\n\n\n\n<p>First thing firs, declare a function that takes single argument for timing :<\/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 i2c_dma_init(uint32_t timing){}<\/pre><\/div>\n\n\n\n<p>Then we shall initialize the I2C as shown in previous guide:<\/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;}\">#define AF4 0x04\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOBEN;\n\tGPIOB-&gt;MODER|=GPIO_MODER_MODER8_1|GPIO_MODER_MODER9_1;\n\tGPIOB-&gt;MODER&amp;=~(GPIO_MODER_MODER8_0|GPIO_MODER_MODER9_0);\n\tGPIOB-&gt;OTYPER|=GPIO_OTYPER_OT8|GPIO_OTYPER_OT9;\n\tGPIOB-&gt;AFR[1]|=(AF4&lt;&lt;0)|(AF4&lt;&lt;4);\n\n\tRCC-&gt;APB1ENR|=RCC_APB1ENR_I2C1EN;\n\tI2C1-&gt;CR1 &amp;=~I2C_CR1_PE;\n\tI2C1-&gt;TIMINGR=timing;\n\tI2C1-&gt;CR1 |=I2C_CR1_PE;<\/pre><\/div>\n\n\n\n<p>Now, we enable clock access to the DMA. Before that, we need to find which DMA, stream and channel for I2C1_RX. From the reference manual, DMA request table,  DMA1_Stream5 Channel1 is for I2C1_TX. Hence we shall enable clock access to DMA1:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"501\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.16.20-PM-1024x501.png\" alt=\"\" class=\"wp-image-1202\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.16.20-PM-1024x501.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.16.20-PM-300x147.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.16.20-PM-768x376.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.16.20-PM-1536x752.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.16.20-PM-2048x1002.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.16.20-PM-1150x563.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.16.20-PM-750x367.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.16.20-PM-400x196.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.16.20-PM-250x122.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;}\">RCC-&gt;AHB1ENR|=RCC_AHB1ENR_DMA1EN;<\/pre><\/div>\n\n\n\n<p>Then disable the stream and wait until the stream is disabled:<\/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\tDMA1_Stream5-&gt;CR &amp;=~DMA_SxCR_EN;\n\t\twhile((DMA1_Stream5-&gt;CR &amp; DMA_SxCR_EN)==1);<\/pre><\/div>\n\n\n\n<p>Set the following in control register (CR):<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Channel to Channel 1<\/li><li>Memory increment.<\/li><li>Transfer Complete interrupt.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"501\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.19.10-PM-1024x501.png\" alt=\"\" class=\"wp-image-1203\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.19.10-PM-1024x501.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.19.10-PM-300x147.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.19.10-PM-768x376.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.19.10-PM-1536x752.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.19.10-PM-2048x1002.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.19.10-PM-1150x563.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.19.10-PM-750x367.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.19.10-PM-400x196.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.19.10-PM-250x122.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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;}\">#define CH1\t0x01\nDMA1_Stream5-&gt;CR|=(CH1&lt;&lt;25)|DMA_SxCR_MINC|DMA_SxCR_TCIE;<\/pre><\/div>\n\n\n\n<p>Enable DMA1_Stream5 interrupt in NVIC:<\/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;}\">NVIC_EnableIRQ(DMA1_Stream5_IRQn);<\/pre><\/div>\n\n\n\n<p>Set the peripheral address to be the RXDR:<\/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_Stream5-&gt;PAR=(uint32_t )&amp;I2C1-&gt;RXDR;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. I2C Read using DMA:<\/h2>\n\n\n\n<p>We start of by declaring a function that takes four arguments:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Slave address<\/li><li>Memory address.<\/li><li>Buffer to store the read data.<\/li><li>Length of data to be read.<\/li><\/ul>\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;}\">void i2c_read_dma(uint8_t slav_add, uint8_t memadd, uint8_t *data, uint8_t length)<\/pre><\/div>\n\n\n\n<p>For the sending memory address, it is the same part from the previous guide:<\/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\t\/*Enable I2C*\/\n\t\tI2C1-&gt;CR1 |=I2C_CR1_PE;\n\t\t\/*Disable DMA for short period of time*\/\n\t\t\/*Set slave address*\/\n\t\tI2C1-&gt;CR2=(slav_add&lt;&lt;1);\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{\n\t\t\t\t\/*Check if TX buffer is empty*\/\n\t\t\t\tif(I2C1-&gt;ISR &amp; I2C_ISR_TXE)\n\t\t\t\t\t{\n\t\t\t\t\t\t\/*send memory address*\/\n\t\t\t\t\t\tI2C1-&gt;TXDR = (memadd);\n\t\t\t\t\t}\n\t\t\t}\n\t\/*Reset I2C*\/\n\tI2C1-&gt;CR1 &amp;=~I2C_CR1_PE;\n\tI2C1-&gt;CR1 |=I2C_CR1_PE;<\/pre><\/div>\n\n\n\n<p>In the CR1 register of I2C, enable RXDMA bit:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"535\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.24.32-PM-1024x535.png\" alt=\"\" class=\"wp-image-1204\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.24.32-PM-1024x535.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.24.32-PM-300x157.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.24.32-PM-768x401.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.24.32-PM-1536x803.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.24.32-PM-2048x1070.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.24.32-PM-1150x601.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.24.32-PM-750x392.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.24.32-PM-400x209.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-14-at-5.24.32-PM-250x131.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;}\">\t\/*Enable DMA TX*\/\n\tI2C1-&gt;CR1|=I2C_CR1_RXDMAEN;<\/pre><\/div>\n\n\n\n<p>Set the slave address:<\/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\/*Set slave address*\/\n\tI2C1-&gt;CR2=(slav_add&lt;&lt;1);<\/pre><\/div>\n\n\n\n<p>Set the mode to be read mode:<\/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\/*Set mode to read operation*\/\n\tI2C1-&gt;CR2|=I2C_CR2_RD_WRN;<\/pre><\/div>\n\n\n\n<p>Set the length of the data to be read:<\/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\/*Set length to the required length*\/\n\tI2C1-&gt;CR2|=((length)&lt;&lt;I2C_CR2_NBYTES_Pos);<\/pre><\/div>\n\n\n\n<p>Enable autoend:<\/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\/*auto generate stop after transfer completed*\/\n\tI2C1-&gt;CR2|=I2C_CR2_AUTOEND;<\/pre><\/div>\n\n\n\n<p>Now, for the DMA part, set the memory address to be the buffer passed by the user:<\/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_Stream5-&gt;M0AR=(uint32_t)data;<\/pre><\/div>\n\n\n\n<p>Set transfer length:<\/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_Stream5-&gt;NDTR=length;<\/pre><\/div>\n\n\n\n<p>Finally enable the stream:<\/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_Stream5-&gt;CR |=DMA_SxCR_EN;<\/pre><\/div>\n\n\n\n<p>Launch I2C:<\/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;}\">\tI2C1-&gt;CR2|=I2C_CR2_START;<\/pre><\/div>\n\n\n\n<p>Wait until the transfer is completed:<\/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_finished==0);\n\trx_finished=0;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>For interrupt handler:<\/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 DMA1_Stream5_IRQHandler(void)\n\t{\n\t\tif(DMA1-&gt;HISR &amp; DMA_HISR_TCIF5){\n\t\trx_finished=1;\n\t\tDMA1-&gt;HIFCR=DMA_HIFCR_CTCIF5;\n\t\t}\n\n\t}\n<\/pre><\/div>\n\n\n\n<p>Hence, the codes 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;}\">#include &quot;i2c_dma.h&quot;\n\n#include &quot;stm32f7xx.h&quot;\n\n\nvolatile uint8_t rx_finished;\n\nvoid i2c_dma_init(uint32_t timing)\n\t{\n\t\t#define AF4 0x04\n\t\t#define CH1\t0x01\n\t\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOBEN;\n\t\tGPIOB-&gt;MODER|=GPIO_MODER_MODER8_1|GPIO_MODER_MODER9_1;\n\t\tGPIOB-&gt;MODER&amp;=~(GPIO_MODER_MODER8_0|GPIO_MODER_MODER9_0);\n\t\tGPIOB-&gt;OTYPER|=GPIO_OTYPER_OT8|GPIO_OTYPER_OT9;\n\t\tGPIOB-&gt;AFR[1]|=(AF4&lt;&lt;0)|(AF4&lt;&lt;4);\n\n\t\tRCC-&gt;APB1ENR|=RCC_APB1ENR_I2C1EN;\n\n\t\tI2C1-&gt;CR1 &amp;=~I2C_CR1_PE;\n\t\t\/*Enable I2C*\/\n\t\tI2C1-&gt;TIMINGR=timing;\n\t\tI2C1-&gt;CR1 |=I2C_CR1_PE;\n\n\t\t\/*Configuring DMA1_Stream5*\/\n\t\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_DMA1EN;\n\t\tDMA1_Stream5-&gt;CR &amp;=~DMA_SxCR_EN;\n\t\twhile((DMA1_Stream5-&gt;CR &amp; DMA_SxCR_EN)==1);\n\t\tDMA1_Stream5-&gt;CR|=(CH1&lt;&lt;25)|DMA_SxCR_MINC|DMA_SxCR_TCIE;\n\t\tNVIC_EnableIRQ(DMA1_Stream5_IRQn);\n\t\tDMA1_Stream5-&gt;PAR=(uint32_t )&amp;I2C1-&gt;RXDR;\n\t}\n\n\nvoid i2c_read_dma(uint8_t slav_add, uint8_t memadd, uint8_t *data, uint8_t length)\n\t{\n\n\t\t\/*Enable I2C*\/\n\t\tI2C1-&gt;CR1 |=I2C_CR1_PE;\n\t\t\/*Disable DMA for short period of time*\/\n\t\t\/*Set slave address*\/\n\t\tI2C1-&gt;CR2=(slav_add&lt;&lt;1);\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{\n\t\t\t\t\/*Check if TX buffer is empty*\/\n\t\t\t\tif(I2C1-&gt;ISR &amp; I2C_ISR_TXE)\n\t\t\t\t\t{\n\t\t\t\t\t\t\/*send memory address*\/\n\t\t\t\t\t\tI2C1-&gt;TXDR = (memadd);\n\t\t\t\t\t}\n\t\t\t}\n\t\/*Reset I2C*\/\n\tI2C1-&gt;CR1 &amp;=~I2C_CR1_PE;\n\tI2C1-&gt;CR1 |=I2C_CR1_PE;\n\t\/*Enable DMA TX*\/\n\tI2C1-&gt;CR1|=I2C_CR1_RXDMAEN;\n\t\/*Set slave address*\/\n\tI2C1-&gt;CR2=(slav_add&lt;&lt;1);\n\t\/*Set mode to read operation*\/\n\tI2C1-&gt;CR2|=I2C_CR2_RD_WRN;\n\t\/*Set length to the required length*\/\n\tI2C1-&gt;CR2|=((length)&lt;&lt;I2C_CR2_NBYTES_Pos);\n\t\/*auto generate stop after transfer completed*\/\n\tI2C1-&gt;CR2|=I2C_CR2_AUTOEND;\n\tDMA1_Stream5-&gt;M0AR=(uint32_t)data;\n\tDMA1_Stream5-&gt;NDTR=length;\n\tDMA1_Stream5-&gt;CR |=DMA_SxCR_EN;\n\t\/*Generate start*\/\n\tI2C1-&gt;CR2|=I2C_CR2_START;\n\twhile(rx_finished==0);\n\trx_finished=0;\n\t}\n\n\nvoid DMA1_Stream5_IRQHandler(void)\n\t{\n\t\tif(DMA1-&gt;HISR &amp; DMA_HISR_TCIF5){\n\t\trx_finished=1;\n\t\tDMA1-&gt;HIFCR=DMA_HIFCR_CTCIF5;\n\t\t}\n\n\t}\n<\/pre><\/div>\n\n\n\n<p>For the header file:<\/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;}\">#ifndef I2C_DMA_H_\n#define I2C_DMA_H_\n\n\n#include &quot;stdint.h&quot;\n\n\n\nvoid i2c_dma_init(uint32_t timing);\nvoid i2c_read_dma(uint8_t slav_add, uint8_t memadd, uint8_t *data, uint8_t length);\n\n\n#endif \/* I2C_DMA_H_ *\/\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Code:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>In main.c file:<\/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;}\">#include &quot;uart.h&quot;\n#include &quot;i2c_dma.h&quot;\n#include &quot;stdlib.h&quot;\n#include &quot;stm32f7xx.h&quot;\n#define slave_add (0x68)\nuint8_t data_rec[3];\nuint8_t data_send[4];\n\n\n\nvoid delay(int ms)\n{\n\tint i;\n\tSysTick-&gt;LOAD=16000-1;\n\tSysTick-&gt;VAL=0;\n\tSysTick-&gt;CTRL=0x5;\n\t\tfor ( i=0;i&lt;ms;i++)\n\t\t{\n\t\t\twhile(!(SysTick-&gt;CTRL &amp;0x10000)){}\n\t\t}\n\tSysTick-&gt;CTRL=0;\n\n}\n\n\nint bcd_to_decimal(unsigned char x) {\n    return x - 6 * (x &gt;&gt; 4);\n}\n\n\nint main(void)\n\t{\n\n\t\tuart3_tx_init();\n\t\ti2c_dma_init(0x00303D5B);\n\n\t\tprintf(&quot;init\\r\\n&quot;);\n\t\twhile(1)\n\t\t\t{\n\t\t\t\ti2c_read_dma(slave_add,0x00,data_rec,3);\n\t\t\t\tfor (volatile int i=0;i&lt;3;i++)\n\t\t\t\t\t{\n\t\t\t\t\t\tdata_rec[i]=bcd_to_decimal(data_rec[i]);\n\t\t\t\t\t}\n\t\t\t\tif(data_rec[0]!=0){\n\t\t\t\tif(data_rec[0]%5==0){\n\t\t\t\t\t\tdata_send[0]=0x00;\n\t\t\t\t\t\tdata_send[1]=0x00;\n\t\t\t\t\t\tdata_send[2]=(rand() % 5);\n\t\t\t\t\t\tdata_send[3]=(rand() % 5);\n\t\t\t\t\t\ti2c_write_dma(slave_add,data_send,4);\n\t\t\t\t\t\t}}\n\t\t\t\tprintf(&quot;rtc data %d %d %d\\r\\n&quot;,data_rec[0],data_rec[1],data_rec[2]);\n\t\t\t\tdelay(100);\n\n\n\t\t\t}\n\n\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\">4. Results:<\/h2>\n\n\n\n<p>If you run the code and open terminal application and set baudrate to 115200, you should get the following:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"725\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.34.15-AM-1024x725.png\" alt=\"\" class=\"wp-image-913\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.34.15-AM-1024x725.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.34.15-AM-300x212.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.34.15-AM-768x544.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.34.15-AM-1150x814.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.34.15-AM-750x531.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.34.15-AM-400x283.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.34.15-AM-250x177.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.34.15-AM.png 1164w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>From left to right:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Seconds.<\/li><li>Minutes.<\/li><li>Hours.<\/li><\/ul>\n\n\n\n<p>Happy coding&nbsp;\ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide (here), we took a look at I2C read mode to read set of registers from DS3231 RTC module using polling mode. In this guide, we shall use DMA to read those register. In this guide, we shall cover the following: I2C configuration. I2C read using DMA. Code. Results. 1. I2C configuration: [&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-1201","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\/1201"}],"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=1201"}],"version-history":[{"count":3,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1201\/revisions"}],"predecessor-version":[{"id":1209,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1201\/revisions\/1209"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}