{"id":1207,"date":"2022-09-18T05:37:03","date_gmt":"2022-09-18T05:37:03","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1207"},"modified":"2022-09-18T05:37:06","modified_gmt":"2022-09-18T05:37:06","slug":"working-with-stm32f7-and-i2c-write-mode-with-dma","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1207","title":{"rendered":"Working with STM32F7 and I2C: Write 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\" target=\"_blank\">here<\/a>), we took a look at I2C read mode to read set of registers from DS3231 RTC module using DMA mode. In this guide, we shall use DMA to write 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 write 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>Continuing from the previous guide, we shall configure the DMA to write to I2C slave device.<\/p>\n\n\n\n<p>First, we need to figure which DMA, Stream and channel responsible for I2C1_TX. We can find that from the reference manual.<\/p>\n\n\n\n<p>From the reference manual Table 27 which describes the DMA1 requests map, we can see that you have Stream6 and Stream7 for I2C1_TX. We shall use Stream 6 in this guide.<\/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=\"468\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.12.33-AM-1024x468.png\" alt=\"\" class=\"wp-image-1210\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.12.33-AM-1024x468.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.12.33-AM-300x137.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.12.33-AM-768x351.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.12.33-AM-1536x703.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.12.33-AM-2048x937.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.12.33-AM-1150x526.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.12.33-AM-750x343.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.12.33-AM-400x183.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.12.33-AM-250x114.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>After selecting the Stream6, we shall disable the Stream and wait until it 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_Stream6-&gt;CR &amp;=~DMA_SxCR_EN;\n\t\twhile((DMA1_Stream6-&gt;CR &amp; DMA_SxCR_EN)==1);<\/pre><\/div>\n\n\n\n<p>Then configure the DMA as following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Set channel to 3.<\/li><li>Memory increment.<\/li><li>Transfer complete interrupt.<\/li><li>Direction Memory to peripheral.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"349\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.18.33-AM-1024x349.png\" alt=\"\" class=\"wp-image-1211\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.18.33-AM-1024x349.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.18.33-AM-300x102.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.18.33-AM-768x262.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.18.33-AM-1536x524.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.18.33-AM-2048x699.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.18.33-AM-1150x392.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.18.33-AM-750x256.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.18.33-AM-400x137.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.18.33-AM-250x85.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_Stream6-&gt;CR|=(CH1&lt;&lt;25)|DMA_SxCR_MINC|DMA_SxCR_TCIE|DMA_SxCR_DIR_0;<\/pre><\/div>\n\n\n\n<p>Enable DMA1_Stream6 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_Stream6_IRQn);<\/pre><\/div>\n\n\n\n<p>Set the peripheral address to I2C1-&gt;TXDR:<\/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_Stream6-&gt;PAR=(uint32_t )&amp;I2C1-&gt;TXDR;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. I2C Write using DMA:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>First declare a function that takes three arguments:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Slave address.<\/li><li>buffer that hold the data to be written.<\/li><li>Length of the data to be written.<\/li><\/ul>\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_write_dma(uint8_t slav_add, uint8_t *data, uint8_t length)<\/pre><\/div>\n\n\n\n<p>We start with enabling the I2C and DMA for write mode:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"417\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.24.32-AM-1024x417.png\" alt=\"\" class=\"wp-image-1212\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.24.32-AM-1024x417.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.24.32-AM-300x122.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.24.32-AM-768x313.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.24.32-AM-1536x626.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.24.32-AM-2048x835.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.24.32-AM-1150x469.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.24.32-AM-750x306.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.24.32-AM-400x163.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-18-at-8.24.32-AM-250x102.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\t\t\/*Enable I2C*\/\n\t\t\tI2C1-&gt;CR1 |=I2C_CR1_PE;\n\t\t\t\/*Enable DMA TX*\/\n\t\t\tI2C1-&gt;CR1|=I2C_CR1_TXDMAEN;<\/pre><\/div>\n\n\n\n<p>Then set the slave address in CR2 register:<\/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;}\">I2C1-&gt;CR2=(slav_add&lt;&lt;1);<\/pre><\/div>\n\n\n\n<p>Select 7-bit addressing 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;}\">I2C1-&gt;CR2&amp;=~I2C_CR2_ADD10;<\/pre><\/div>\n\n\n\n<p>Set the length as the parameter set 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;}\">\/*Set number to transfer to length for write operation*\/\n\t\t\tI2C1-&gt;CR2|=(length&lt;&lt;I2C_CR2_NBYTES_Pos);<\/pre><\/div>\n\n\n\n<p>Set to write 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;}\">I2C1-&gt;CR2&amp;=~I2C_CR2_RD_WRN;<\/pre><\/div>\n\n\n\n<p>Set auto end.<\/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;}\">I2C1-&gt;CR2|=I2C_CR2_AUTOEND;<\/pre><\/div>\n\n\n\n<p>Set the memory address of DMA1_Stream6 to be the buffer:<\/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_Stream6-&gt;M0AR=(uint32_t )data;<\/pre><\/div>\n\n\n\n<p>Number of transfer to be the 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_Stream6-&gt;NDTR=length;<\/pre><\/div>\n\n\n\n<p>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_Stream6-&gt;CR |=DMA_SxCR_EN;<\/pre><\/div>\n\n\n\n<p>Launch the I2C communication:<\/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;}\">I2C1-&gt;CR2|=I2C_CR2_START;<\/pre><\/div>\n\n\n\n<p>Wait until the transfer completed (depends on your application):<\/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\twhile(tx_finished==0);\n\t\t\ttx_finished=0;<\/pre><\/div>\n\n\n\n<p>For interrupt handler of DMA1_Stream6:<\/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;}\">\n\nvoid DMA1_Stream6_IRQHandler(void)\n\t{\n\tif(DMA1-&gt;HISR &amp; DMA_HISR_TCIF6){\n\t\ttx_finished=1;\n\t\tDMA1-&gt;HIFCR=DMA_HIFCR_CTCIF6;\n\t\t}\n\n\t}\n<\/pre><\/div>\n\n\n\n<p>Hence the entire code as following:<\/p>\n\n\n\n<p>i2c_dma.c<\/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 tx_finished;\nvoid i2c_write_dma(uint8_t slav_add, uint8_t *data, uint8_t length)\n\t{\n\n\t\t\t\/*Enable I2C*\/\n\t\t\tI2C1-&gt;CR1 |=I2C_CR1_PE;\n\t\t\t\/*Enable DMA TX*\/\n\t\t\tI2C1-&gt;CR1|=I2C_CR1_TXDMAEN;\n\t\t\t\/*Set slave address*\/\n\t\t\tI2C1-&gt;CR2=(slav_add&lt;&lt;1);\n\t\t\t\/*7-bit addressing*\/\n\t\t\tI2C1-&gt;CR2&amp;=~I2C_CR2_ADD10;\n\t\t\t\/*Set number to transfer to length for write operation*\/\n\t\t\tI2C1-&gt;CR2|=(length&lt;&lt;I2C_CR2_NBYTES_Pos);\n\t\t\t\/*Set the mode to write mode*\/\n\t\t\tI2C1-&gt;CR2&amp;=~I2C_CR2_RD_WRN;\n\t\t\t\/*hardware end*\/\n\t\t\tI2C1-&gt;CR2|=I2C_CR2_AUTOEND;\n\n\t\t\tDMA1_Stream6-&gt;M0AR=(uint32_t )data;\n\t\t\tDMA1_Stream6-&gt;NDTR=length;\n\t\t\tDMA1_Stream6-&gt;CR |=DMA_SxCR_EN;\n\t\t\t\/*Generate start*\/\n\t\t\tI2C1-&gt;CR2|=I2C_CR2_START;\n\t\t\twhile(tx_finished==0);\n\t\t\ttx_finished=0;\n\t}\n<\/pre><\/div>\n\n\n\n<p>i2c_dma.h:<\/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_write_dma(uint8_t slav_add, uint8_t *data, uint8_t length);<\/pre><\/div>\n\n\n\n<p>You may download the source code from here (includes read\/write mode):<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/I2C_DMA.zip\">I2C_DMA<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/I2C_DMA.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">3. Code:<\/h2>\n\n\n\n<p>In main.c:<\/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[2],data_rec[1],data_rec[0]);\n\t\t\t\tdelay(100);\n\n\n\t\t\t}\n\n\n\t}\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">4. Results:<\/h2>\n\n\n\n<p>Open your favourite Serial terminal and set the baudrate to 115200 and you should get this:<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"I2C Demo on STM32F767-Nucleo\" width=\"1170\" height=\"658\" src=\"https:\/\/www.youtube.com\/embed\/QBCUqBx_Q0Q?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Happy coding \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 DMA mode. In this guide, we shall use DMA to write those register. In this guide, we shall cover the following: I2C configuration. I2C write 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-1207","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\/1207"}],"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=1207"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1207\/revisions"}],"predecessor-version":[{"id":1214,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1207\/revisions\/1214"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}