{"id":1071,"date":"2022-07-06T06:50:06","date_gmt":"2022-07-06T06:50:06","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1071"},"modified":"2026-04-09T06:01:30","modified_gmt":"2026-04-09T06:01:30","slug":"getting-started-with-stm32l053-i2c-read-mode","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1071","title":{"rendered":"Getting started with STM32L053: 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<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<h2 class=\"wp-block-heading\">2. Circuit Diagram:<\/h2>\n\n\n\n<p>Since here we are using STM32L053-Nucleo64, 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<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"885\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.31.50-AM-1024x885.png\" alt=\"\" class=\"wp-image-1072\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.31.50-AM-1024x885.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.31.50-AM-300x259.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.31.50-AM-768x664.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.31.50-AM-1536x1328.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.31.50-AM-1150x994.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.31.50-AM-750x649.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.31.50-AM-400x346.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.31.50-AM-250x216.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.31.50-AM.png 1700w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"715\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-19-at-8.03.15-AM-1024x715.png\" alt=\"\" class=\"wp-image-510\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-19-at-8.03.15-AM-1024x715.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-19-at-8.03.15-AM-300x209.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-19-at-8.03.15-AM-768x536.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-19-at-8.03.15-AM-1536x1072.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-19-at-8.03.15-AM-2048x1430.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-19-at-8.03.15-AM-1150x803.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-19-at-8.03.15-AM-750x524.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-19-at-8.03.15-AM-400x279.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-19-at-8.03.15-AM-250x175.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><td>STM32L053-Nucleo64<\/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><\/figure>\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<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;IOPENR|=RCC_IOPENR_GPIOBEN;<\/pre><\/div>\n\n\n\n<p>Then we need to get the AF number for PB8 and PB9 for I2C1. From the datasheet, we can find that the AF4 is for I2C1:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"663\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.33.14-AM-1024x663.png\" alt=\"\" class=\"wp-image-1073\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.33.14-AM-1024x663.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.33.14-AM-300x194.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.33.14-AM-768x497.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.33.14-AM-1536x995.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.33.14-AM-2048x1326.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.33.14-AM-1150x745.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.33.14-AM-750x486.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.33.14-AM-400x259.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.33.14-AM-250x162.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Hence, we can create a symbolic name:<\/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;}\">const uint8_t AF04 =0x04;<\/pre><\/div>\n\n\n\n<p>Then set PB8 and PB9 to alternate function, set the output type to open drain, the speed to maximum and alternate function type.<\/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;}\">\tGPIOB-&gt;MODER&amp;=~(GPIO_MODER_MODE8|GPIO_MODER_MODE9);\n\tGPIOB-&gt;MODER|=GPIO_MODER_MODE8_1|GPIO_MODER_MODE9_1;\n\tGPIOB-&gt;OTYPER|=GPIO_OTYPER_OT_8|GPIO_OTYPER_OT_9;\n\tGPIOB-&gt;OSPEEDR|=GPIO_OSPEEDER_OSPEED8_0|GPIO_OSPEEDER_OSPEED8_1|GPIO_OSPEEDER_OSPEED9_0|GPIO_OSPEEDER_OSPEED9_1;\n\tGPIOB-&gt;AFR[1]|=(AF04&lt;&lt;0)|(AF04&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>Then enabled clock access to I2C1.<\/p>\n\n\n\n<p>From the block diagram in the datasheet of STM32L053, we can see that I2C is connected to APB1 bus:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"912\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.38.43-AM-912x1024.png\" alt=\"\" class=\"wp-image-1074\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.38.43-AM-912x1024.png 912w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.38.43-AM-267x300.png 267w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.38.43-AM-768x862.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.38.43-AM-1150x1291.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.38.43-AM-750x842.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.38.43-AM-400x449.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.38.43-AM-250x281.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/Screen-Shot-2022-07-06-at-9.38.43-AM.png 1160w\" sizes=\"(max-width: 912px) 100vw, 912px\" \/><\/figure>\n\n\n\n<p>Hence, we can enable clock access to I2C1 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;}\">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\"><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>Set the timing register (taken from STM32CubeMX):<\/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 0x00708\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<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\"><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<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 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}<\/pre><\/div>\n\n\n\n<p>Hence, the source code 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;}\">\n#include &quot;i2c.h&quot;\n\nconst uint8_t AF04 =0x04;\n\nvoid i2c_init(uint32_t timing)\n{\n\tRCC-&gt;IOPENR|=RCC_IOPENR_GPIOBEN;\n\tGPIOB-&gt;MODER&amp;=~(GPIO_MODER_MODE8|GPIO_MODER_MODE9);\n\tGPIOB-&gt;MODER|=GPIO_MODER_MODE8_1|GPIO_MODER_MODE9_1;\n\tGPIOB-&gt;OTYPER|=GPIO_OTYPER_OT_8|GPIO_OTYPER_OT_9;\n\tGPIOB-&gt;OSPEEDR|=GPIO_OSPEEDER_OSPEED8_0|GPIO_OSPEEDER_OSPEED8_1|GPIO_OSPEEDER_OSPEED9_0|GPIO_OSPEEDER_OSPEED9_1;\n\tGPIOB-&gt;AFR[1]|=(AF04&lt;&lt;0)|(AF04&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;\n\n\n\n}\n\nvoid i2_read(uint8_t slav_add, uint8_t memadd, uint8_t *data, uint8_t length )\n\t{\n\t(slav_add=slav_add&lt;&lt;1);\n\t\/*Enable I2C*\/\n\tI2C1-&gt;CR1 |=I2C_CR1_PE;\n\t\/*Set slave address*\/\n\tI2C1-&gt;CR2=(slav_add&lt;&lt;I2C_CR2_SADD_Pos);\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;I2C_CR2_SADD_Pos);\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>The header file 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;}\">#ifndef I2C_H_\n#define I2C_H_\n\n#include &quot;stdint.h&quot;\n#include &quot;stm32l0xx.h&quot;\n\nvoid i2c_init(uint32_t timing);\nvoid i2_read(uint8_t slav_add, uint8_t memadd, uint8_t *data, uint8_t length );\nvoid i2c_write(uint8_t slav_add, uint8_t *data, uint8_t length);\nvoid i2c_write_memory(uint8_t slav_add, uint8_t memadd, uint8_t *data, uint8_t length);\n#endif \/* I2C_H_ *\/\n<\/pre><\/div>\n\n\n\n<p>In the main function, we can call the initializing function and read function:<\/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.h&quot;\n#include &quot;uart.h&quot;\n#include &quot;stdio.h&quot;\n#include &quot;stdlib.h&quot;\n#define slave_add (0x68)\n\n\nuint8_t data[3],data_send[3];\n\nint bcd_to_decimal(unsigned char x) {\n    return x - 6 * (x &gt;&gt; 4);\n}\n\nint main()\n\t{\n\tuart_init();\n\ti2c_init(0x00708);\n\twhile(1)\n\t\t{\n\t\ti2_read(slave_add,0x00,data,3);\n\t\tfor (volatile int i=0;i&lt;3;i++)\n\t\t\t{\n\t\t\tdata[i]=bcd_to_decimal(data[i]);\n\n\t\t\t}\n\t\tprintf(&quot;rtc data %d %d %d\\r\\n&quot;,data[0],data[1],data[2]);\n\t\tfor (volatile int i=0;i&lt;100000;i++);\n\t\t}\n\n\t}\n<\/pre><\/div>\n\n\n\n<p>For the serial function from <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=894\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=894\" target=\"_blank\">here<\/a> and for retargeting printf from <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=800\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=800\" target=\"_blank\">here<\/a> (replace ITM_SendChar with uart_write).<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. Results:<\/h2>\n\n\n\n<p>If you run the code and open terminal application and set baudrate to 9600, 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 \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-1071","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\/1071"}],"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=1071"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1071\/revisions"}],"predecessor-version":[{"id":1075,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1071\/revisions\/1075"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}