{"id":902,"date":"2022-04-26T07:35:31","date_gmt":"2022-04-26T07:35:31","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=902"},"modified":"2022-04-26T07:35:34","modified_gmt":"2022-04-26T07:35:34","slug":"working-with-stm32f7-and-i2c-read-mode","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=902","title":{"rendered":"Working with STM32F7 and  I2C: Read Mode"},"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 this guide, we shall see how to configure I2C peripheral in STM32F7 to read certain amount of data from memory location. In this guide, we shall use DS3231 RTC to read seconds, minutes and hours.<\/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>What is I2C.<\/li><li>Circuit Diagram.<\/li><li>Configure I2C pins.<\/li><li>Configure I2C peripheral.<\/li><li>I2C Read function.<\/li><li>Code.<\/li><li>Result.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. What is I2C:<\/h2>\n\n\n\n<p>With I2C, data is transferred in&nbsp;<em>messages.&nbsp;<\/em>Messages are broken up into&nbsp;<em>frames<\/em>&nbsp;of data. Each&nbsp;message has an address frame that contains the binary address of the slave, and one or more data frames that contain the data being transmitted. The message also includes&nbsp;start and stop conditions, read\/write bits, and ACK\/NACK bits between each data frame:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.circuitbasics.com\/wp-content\/uploads\/2016\/01\/Introduction-to-I2C-Message-Frame-and-Bit-2.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" src=\"https:\/\/www.circuitbasics.com\/wp-content\/uploads\/2016\/01\/Introduction-to-I2C-Message-Frame-and-Bit-2-1024x258.png\" alt=\"Introduction to I2C - Message, Frame, and Bit\" class=\"wp-image-420891\" \/><\/a><\/figure>\n\n\n\n<p><strong>Start Condition:<\/strong>&nbsp;The SDA line switches from a high voltage&nbsp;level to a low voltage level&nbsp;<em>before<\/em>&nbsp;the SCL line switches from high to low.<\/p>\n\n\n\n<p><strong>Stop Condition:<\/strong>&nbsp;The SDA line switches from a low voltage&nbsp;level to a high voltage level&nbsp;<em>after<\/em>&nbsp;the SCL line switches from low to high.<\/p>\n\n\n\n<p><strong>Address Frame:<\/strong>&nbsp;A 7 or 10 bit sequence unique to each slave that identifies the slave when the master wants to talk to it.<\/p>\n\n\n\n<p><strong>Read\/Write Bit:&nbsp;<\/strong>A single bit specifying whether the master is sending data to the slave (low voltage level) or requesting data from it (high voltage level).<\/p>\n\n\n\n<p><strong>ACK\/NACK Bit:<\/strong>&nbsp;Each frame in a&nbsp;message is followed by an acknowledge\/no-acknowledge bit. If an&nbsp;address frame or data frame was successfully received, an ACK bit is returned to the sender from the receiving device.<\/p>\n\n\n\n<p>For more information and detailed explanations refer to this NXP documentation (<a rel=\"noreferrer noopener\" href=\"https:\/\/www.nxp.com\/docs\/en\/user-guide\/UM10204.pdf\" target=\"_blank\">here<\/a>).<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Circuit Diagram:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Since here we are using STM32F767ZI-Nucleo144, there is SDA and SCL pins in arduino connectors that allow us to connect I2C devices such as OLED display MPU9250 etc.<\/p>\n\n\n\n<p>These pins are PB8 and PB9<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1014\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-9.48.42-AM-1014x1024.png\" alt=\"\" class=\"wp-image-903\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-9.48.42-AM-1014x1024.png 1014w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-9.48.42-AM-297x300.png 297w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-9.48.42-AM-150x150.png 150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-9.48.42-AM-768x776.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-9.48.42-AM-1521x1536.png 1521w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-9.48.42-AM-1150x1162.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-9.48.42-AM-750x758.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-9.48.42-AM-400x404.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-9.48.42-AM-250x253.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-9.48.42-AM.png 1598w\" sizes=\"(max-width: 1014px) 100vw, 1014px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><td>STM32F7-Nucleo144<\/td><td>DS3231 module<\/td><\/tr><tr><td>5V<\/td><td>Vcc<\/td><\/tr><tr><td>GND<\/td><td>GND<\/td><\/tr><tr><td>D15 (PB8)<\/td><td>SCL<\/td><\/tr><tr><td>D14 (PB9)<\/td><td>SDA<\/td><\/tr><\/tbody><\/table><figcaption>D<\/figcaption><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Configure I2C pins:<\/h2>\n\n\n\n<p>From the circuit diagram, the required pins are PB8 and PB9.<\/p>\n\n\n\n<p>Now we need to configure the pins.<\/p>\n\n\n\n<p>We need first to enable clock access to GPIOB:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"573\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-9.56.50-AM-1024x573.png\" alt=\"\" class=\"wp-image-904\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-9.56.50-AM-1024x573.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-9.56.50-AM-300x168.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-9.56.50-AM-768x430.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-9.56.50-AM-1536x859.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-9.56.50-AM-1150x643.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-9.56.50-AM-750x420.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-9.56.50-AM-400x225.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-9.56.50-AM-250x141.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-9.56.50-AM.png 1598w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>From block diagram, we can see that GPIOB is connected to AHB1, hence we can enable clock access to GPIOB 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;}\">\/*Enable clock access*\/\nRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOBEN;<\/pre><\/div>\n\n\n\n<p>Then we need to configure the pins as alternate function:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"620\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.02.24-AM-1024x620.png\" alt=\"\" class=\"wp-image-905\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.02.24-AM-1024x620.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.02.24-AM-300x182.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.02.24-AM-768x465.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.02.24-AM-1536x931.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.02.24-AM-2048x1241.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.02.24-AM-1150x697.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.02.24-AM-750x454.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.02.24-AM-400x242.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.02.24-AM-250x151.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\/*Set PB8 and PB9 to alternate function*\/\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);<\/pre><\/div>\n\n\n\n<p>Then set the pin as open drain mode:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"620\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.04.03-AM-1024x620.png\" alt=\"\" class=\"wp-image-906\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.04.03-AM-1024x620.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.04.03-AM-300x182.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.04.03-AM-768x465.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.04.03-AM-1536x931.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.04.03-AM-2048x1241.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.04.03-AM-1150x697.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.04.03-AM-750x454.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.04.03-AM-400x242.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.04.03-AM-250x151.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\/*Set output to open drain*\/\n\tGPIOB-&gt;OTYPER|=GPIO_OTYPER_OT8|GPIO_OTYPER_OT9;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Then we need configure which alternate function to be used:<\/p>\n\n\n\n<p>From datasheet of stm32f767, table13, the alternate function map, we need AF4:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"188\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.05.24-AM-1024x188.png\" alt=\"\" class=\"wp-image-907\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.05.24-AM-1024x188.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.05.24-AM-300x55.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.05.24-AM-768x141.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.05.24-AM-1536x282.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.05.24-AM-2048x377.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.05.24-AM-1150x211.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.05.24-AM-750x138.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.05.24-AM-400x74.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.05.24-AM-250x46.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Hence, we create macro for that:<\/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<\/pre><\/div>\n\n\n\n<p>Now, we can configure which alternate function:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"614\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.08.34-AM-1024x614.png\" alt=\"\" class=\"wp-image-908\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.08.34-AM-1024x614.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.08.34-AM-300x180.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.08.34-AM-768x460.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.08.34-AM-1536x921.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.08.34-AM-2048x1228.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.08.34-AM-1150x689.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.08.34-AM-750x450.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.08.34-AM-400x240.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.08.34-AM-250x150.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;}\">\tGPIOB-&gt;AFR[1]|=(AF4&lt;&lt;0)|(AF4&lt;&lt;4);<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Configure I2C:<\/h2>\n\n\n\n<p>In order to configure I2C, we need to enable clock access to I2C1 which is the i2c to be used:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"371\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.12.01-AM-1024x371.png\" alt=\"\" class=\"wp-image-909\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.12.01-AM-1024x371.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.12.01-AM-300x109.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.12.01-AM-768x278.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.12.01-AM-1536x556.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.12.01-AM-2048x742.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.12.01-AM-1150x417.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.12.01-AM-750x272.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.12.01-AM-400x145.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.12.01-AM-250x91.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>From the block diagram, we can see I2C1 is connected to APB1:<\/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;}\">RCC-&gt;APB1ENR|=RCC_APB1ENR_I2C1EN;<\/pre><\/div>\n\n\n\n<p>Then we software reset the I2C:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"480\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.14.23-AM-1024x480.png\" alt=\"\" class=\"wp-image-910\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.14.23-AM-1024x480.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.14.23-AM-300x141.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.14.23-AM-768x360.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.14.23-AM-1536x721.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.14.23-AM-2048x961.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.14.23-AM-1150x540.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.14.23-AM-750x352.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.14.23-AM-400x188.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.14.23-AM-250x117.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;}\">I2C1-&gt;CR1 &amp;=~I2C_CR1_PE;<\/pre><\/div>\n\n\n\n<p>Then configure timing register (from CubeMX):<\/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 timing 0x00303D5B\nI2C1-&gt;TIMINGR=timing;<\/pre><\/div>\n\n\n\n<p>Then enable 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;}\">I2C1-&gt;CR1 |=I2C_CR1_PE;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. I2C read function:<\/h2>\n\n\n\n<p>For reading, we create function that takes 4 arguments:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Slave address.<\/li><li>Memory address.<\/li><li>Pointer to hold the data to be read.<\/li><li>Number of bytes to be read.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"449\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.18.38-AM-1024x449.png\" alt=\"\" class=\"wp-image-911\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.18.38-AM-1024x449.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.18.38-AM-300x131.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.18.38-AM-768x337.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.18.38-AM-1536x673.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.18.38-AM-1150x504.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.18.38-AM-750x329.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.18.38-AM-400x175.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.18.38-AM-250x110.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-26-at-10.18.38-AM.png 1880w\" 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;}\">\nvoid i2_read(uint8_t slav_add, uint8_t memadd, uint8_t *data, uint8_t length )\n\t{\n\t\/*Enable I2C*\/\n\tI2C1-&gt;CR1 |=I2C_CR1_PE;\n\t\/*Set slave address*\/\n\tI2C1-&gt;CR2=(slav_add&lt;&lt;1);\n\t\/*7-bit addressing*\/\n\tI2C1-&gt;CR2&amp;=~I2C_CR2_ADD10;\n\t\/*Set number to transfer to 1 for write operation*\/\n\tI2C1-&gt;CR2|=(1&lt;&lt;I2C_CR2_NBYTES_Pos);\n\t\/*Set the mode to write mode*\/\n\tI2C1-&gt;CR2&amp;=~I2C_CR2_RD_WRN;\n\t\/*Software end*\/\n\tI2C1-&gt;CR2&amp;=~I2C_CR2_AUTOEND;\n\t\/*Generate start*\/\n\tI2C1-&gt;CR2|=I2C_CR2_START;\n\t\/*Wait until transfer is completed*\/\n\twhile(!(I2C1-&gt;ISR &amp; I2C_ISR_TC)){\n\t\t\/*Check if TX buffer is empty*\/\n\t\tif(I2C1-&gt;ISR &amp; I2C_ISR_TXE)\n\t\t{\n\t\t\t\/*send memory address*\/\n\t\t\tI2C1-&gt;TXDR = (memadd);\n\t\t}\n\t}\n\t\/*Reset I2C*\/\n\tI2C1-&gt;CR1 &amp;=~I2C_CR1_PE;\n\tI2C1-&gt;CR1 |=I2C_CR1_PE;\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\/*aut generate stop after transfer completed*\/\n\tI2C1-&gt;CR2|=I2C_CR2_AUTOEND;\n\t\/*Generate start*\/\n\tI2C1-&gt;CR2|=I2C_CR2_START;\n\t\/*wait until stop is generated*\/\n\twhile(!(I2C1-&gt;ISR &amp; I2C_ISR_STOPF))\n\t\t\t{\n\t\t\t\t\/*If RX buffer is empty*\/\n\t\t\t  if(I2C1-&gt;ISR &amp;(I2C_ISR_RXNE))\n\t\t\t  \t  {\n\t\t\t\t  \/*read the data and increment the pointer*\/\n\t\t\t\t  *data++=I2C1-&gt;RXDR;\n\t\t\t\t }\n\t\t\t}\n\t\/*disable the peripheral*\/\n\tI2C1-&gt;CR1 &amp;=~I2C_CR1_PE;\n\t}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. You may download the code from here:<\/h2>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/I2C.zip\">I2C<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/I2C.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">7. 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 size-large\"><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><\/p>\n\n\n\n<p>Happy coding \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall see how to configure I2C peripheral in STM32F7 to read certain amount of data from memory location. In this guide, we shall use DS3231 RTC to read seconds, minutes and hours. In this guide, we shall cover the following: What is I2C. Circuit Diagram. Configure I2C pins. Configure I2C peripheral. [&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-902","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\/902"}],"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=902"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/902\/revisions"}],"predecessor-version":[{"id":914,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/902\/revisions\/914"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}