{"id":1953,"date":"2023-08-30T08:00:15","date_gmt":"2023-08-30T08:00:15","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1953"},"modified":"2023-08-30T08:00:21","modified_gmt":"2023-08-30T08:00:21","slug":"building-board-support-package-bsp-for-stm32f411-nucleo64-part14-1-i2c-bus-scanner","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1953","title":{"rendered":"Building Board Support Package (BSP) for STM32F411-Nucleo64 Part14.1: I2C Bus scanner"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-6-1024x683.jpeg\" alt=\"\" class=\"wp-image-1954\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-6-1024x683.jpeg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-6-300x200.jpeg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-6-768x512.jpeg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-6-1536x1024.jpeg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-6-2048x1365.jpeg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-6-1150x767.jpeg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-6-750x500.jpeg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-6-400x267.jpeg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-6-250x167.jpeg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>In this first section of the fourteenth part of board support package on STM32F411, we shall develop the initialization function of I2C and build I2C bus scanner to get the address of the connected slave devices.<\/p>\n\n\n\n<p><\/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>Developing the header file.<\/li><li>Developing the source file.<\/li><li>Main code.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. Developing the header file:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Before we start developing the header file, take a look at this guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=416\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=416\" target=\"_blank\">here<\/a>) to find more information about about I2C and how to configure it from scratch.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>We start off with creating new header file with name of i2c_bsp.h file.<\/p>\n\n\n\n<p>Within the header, include the header file guard:<\/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_BSP_H_\n#define I2C_BSP_H_\n\n\n\n#endif<\/pre><\/div>\n\n\n\n<p>Within the header guard, include the following header files:<\/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;stm32f4xx.h&quot;\n#include &quot;stdint.h&quot;\n#include &quot;bsp_debug.h&quot;\n#include &quot;bsp.h&quot;<\/pre><\/div>\n\n\n\n<p>Since STM32F411 features 3 I2Cs bus, we shall use micros to enable clock access to each peripheral:<\/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 __I2C1_CLOCK_ENABLE()\t\tRCC-&gt;APB1ENR|=RCC_APB1ENR_I2C1EN\n#define __I2C2_CLOCK_ENABLE()\t\tRCC-&gt;APB1ENR|=RCC_APB1ENR_I2C2EN\n#define __I2C3_CLOCK_ENABLE()\t\tRCC-&gt;APB1ENR|=RCC_APB1ENR_I2C3EN<\/pre><\/div>\n\n\n\n<p>Since I2C can operate on either, standard speed (100KHz) or fast mode (400KHz), we shall create enum for the following states 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;}\">typedef enum\n{\n\tstandardSpeed=0,\n\tfastMode=1\n}I2C_MasterModeTypedef;<\/pre><\/div>\n\n\n\n<p>Since there is two type of duty mode in fast mode, t low \/t high = 2 or t low \/t high = 16\/9. Hence, we shall create enum to handle both cases:<\/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;}\">typedef enum\n{\n\tDuty_2=0,\n\tDuty_16_9=1\n}I2C_DutyModeTypedef;<\/pre><\/div>\n\n\n\n<p>For status of the I2C, we shall create enum to handle three states as following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Success.<\/li><li>Failed.<\/li><li>Timeout.<\/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;}\">typedef enum\n{\n\ti2c_success=0,\n\ti2c_failed=1,\n\ti2c_timeOut=2\n}I2C_StatusTypedef;<\/pre><\/div>\n\n\n\n<p>This will become handy in the next section of the guide.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Create new data structure to configure the I2C peripheral:<\/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;}\">typedef struct\n{\n\n\tuint8_t PeripheralFrequency;\n\n\tuint8_t MasterMode;\n\n\tuint8_t DutyMode;\n\n\tuint16_t RiseTime;\n\n\tuint16_t Clock;\n\n}I2C_ConfigTypedef;<\/pre><\/div>\n\n\n\n<p>The parameters as following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Peripheral Frequency which is the peripheral bus frequency connected to (APB1 in this case).<\/li><li>Master Mode to choose either standard or fast speed mode.<\/li><li>Duty mode for type of duty.<\/li><li>Rise time for the rise time of SCL and SDA line.<\/li><li>Clock to generate the correct frequency of SCL.<\/li><\/ul>\n\n\n\n<p>Declare the following 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;}\">void BSP_I2C_Init(I2C_TypeDef *i2c, I2C_ConfigTypedef *config);<\/pre><\/div>\n\n\n\n<p>This function shall initialize the I2C peripheral and take I2C Typedef struct for which I2C and config struct to handle the configuration and returns nothing.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Also, declare the following 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;}\">void BSP_I2C_Bus_Scan(I2C_TypeDef *i2c);<\/pre><\/div>\n\n\n\n<p>This will scan the I2C bus for the connected peripheral and takes I2C_Typedef as argument and return nothing.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Hence, the entire 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_BSP_H_\n#define I2C_BSP_H_\n\n#include &quot;stm32f4xx.h&quot;\n#include &quot;stdint.h&quot;\n#include &quot;bsp_debug.h&quot;\n#include &quot;bsp.h&quot;\n\n#define __I2C1_CLOCK_ENABLE()\t\tRCC-&gt;APB1ENR|=RCC_APB1ENR_I2C1EN\n#define __I2C2_CLOCK_ENABLE()\t\tRCC-&gt;APB1ENR|=RCC_APB1ENR_I2C2EN\n#define __I2C3_CLOCK_ENABLE()\t\tRCC-&gt;APB1ENR|=RCC_APB1ENR_I2C3EN\n\ni2c_bsp.c\ntypedef enum\n{\n\tstandardSpeed=0,\n\tfastMode=1\n}I2C_MasterModeTypedef;\n\n\ntypedef enum\n{\n\tDuty_2=0,\n\tDuty_16_9=1\n}I2C_DutyModeTypedef;\n\n\ntypedef enum\n{\n\ti2c_success=0,\n\ti2c_failed=1,\n\ti2c_timeOut=2\n}I2C_StatusTypedef;\n\n\ntypedef struct\n{\n\n\tuint8_t PeripheralFrequency;\n\n\tuint8_t MasterMode;\n\n\tuint8_t DutyMode;\n\n\tuint16_t RiseTime;\n\n\tuint16_t Clock;\n\n}I2C_ConfigTypedef;\n\n\nvoid BSP_I2C_Init(I2C_TypeDef *i2c, I2C_ConfigTypedef *config);\n\nvoid BSP_I2C_Bus_Scan(I2C_TypeDef *i2c);\n\n#endif \/* I2C_BSP_H_ *\/<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Developing the source code:<\/h2>\n\n\n\n<p>Create new source code file with name of i2c_bsp.c.<\/p>\n\n\n\n<p>Within the source file, include the 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_bsp.h&quot;<\/pre><\/div>\n\n\n\n<p>For the initialization 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;}\">void BSP_I2C_Init(I2C_TypeDef *i2c, I2C_ConfigTypedef *config)<\/pre><\/div>\n\n\n\n<p>Start by reseting the peripheral and release it from reset state 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;}\">\ti2c-&gt;CR1=I2C_CR1_SWRST;\n\n\ti2c-&gt;CR1&amp;=~I2C_CR1_SWRST;<\/pre><\/div>\n\n\n\n<p>Set the peripheral frequency 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;}\">\ti2c-&gt;CR2|= config-&gt;PeripheralFrequency;<\/pre><\/div>\n\n\n\n<p>Set the rise time:<\/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;}\">\ti2c-&gt;TRISE=config-&gt;RiseTime;<\/pre><\/div>\n\n\n\n<p>Set the master 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;}\">\ti2c-&gt;CCR|=(config-&gt;MasterMode&lt;&lt;I2C_CCR_FS_Pos);<\/pre><\/div>\n\n\n\n<p>Set the duty 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;}\">\ti2c-&gt;CCR|=(config-&gt;DutyMode&lt;&lt;I2C_CCR_DUTY_Pos);<\/pre><\/div>\n\n\n\n<p>Now to set the clock to generate the required SCL frequency as following:<\/p>\n\n\n\n<p>In case the user didn&#8217;t set the clock, the BSP shall calculate it automatically 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;}\">\tif((config-&gt;Clock)==0)\n\t{\n\t\tfloat period =1.0\/(float)(config-&gt;PeripheralFrequency);\n\n\t\tif((config-&gt;MasterMode)==standardSpeed)\n\t\t{\n\n\t\t\tuint16_t tmp = 5\/period;\n\t\t\ti2c-&gt;CCR|=(tmp&lt;&lt;I2C_CCR_CCR_Pos);\n\t\t}\n\n\t\telse\n\t\t{\n\t\t\tif(config-&gt;DutyMode==Duty_2)\n\t\t\t{\n\t\t\t\tuint16_t tmp=1.25\/period;\n\t\t\t\ti2c-&gt;CCR|=(tmp&lt;&lt;I2C_CCR_CCR_Pos);\n\t\t\t}\n\n\t\t\telse\n\t\t\t{\n\t\t\t\tuint16_t tmp=(1.25*9)\/period;\n\t\t\t\ti2c-&gt;CCR|=(tmp&lt;&lt;I2C_CCR_CCR_Pos);\n\t\t\t}\n\t\t}\n\n\n\t}<\/pre><\/div>\n\n\n\n<p>We start by calculating the period by dividing 1 over the peripheral frequency.<\/p>\n\n\n\n<p>if the mode is standard:<\/p>\n\n\n\n<p>Since the frequency is 100KHz, and the period is 5us, we can get the CCR to be 5 over the period.<\/p>\n\n\n\n<p>If the mode is fast mode,<\/p>\n\n\n\n<p>You have two modes, low \/t high = 2 or t low \/t high = 16\/9.<\/p>\n\n\n\n<p>When duty mode is  low \/t high = 2:<\/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;}\">uint16_t tmp=1.25\/period;\ni2c-&gt;CCR|=(tmp&lt;&lt;I2C_CCR_CCR_Pos);<\/pre><\/div>\n\n\n\n<p>When low \/t high = 16\/9:<\/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;}\">uint16_t tmp=(1.25*9)\/period;\ni2c-&gt;CCR|=(tmp&lt;&lt;I2C_CCR_CCR_Pos);<\/pre><\/div>\n\n\n\n<p>If the user wants custom CCR value:<\/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;}\">\telse\n\t{\n\t\ti2c-&gt;CCR|=(config-&gt;Clock&lt;&lt;I2C_CCR_CCR_Pos);\n\t}<\/pre><\/div>\n\n\n\n<p>Finally, enable the peripheral:<\/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;}\">i2c-&gt;CR1|=I2C_CR1_PE;<\/pre><\/div>\n\n\n\n<p>Hence, the initializing function 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;}\">void BSP_I2C_Init(I2C_TypeDef *i2c, I2C_ConfigTypedef *config)\n{\n\n\ti2c-&gt;CR1=I2C_CR1_SWRST;\n\n\ti2c-&gt;CR1&amp;=~I2C_CR1_SWRST;\n\n\ti2c-&gt;CR2|= config-&gt;PeripheralFrequency;\n\n\ti2c-&gt;TRISE=config-&gt;RiseTime;\n\n\ti2c-&gt;CCR|=(config-&gt;MasterMode&lt;&lt;I2C_CCR_FS_Pos);\n\n\ti2c-&gt;CCR|=(config-&gt;DutyMode&lt;&lt;I2C_CCR_DUTY_Pos);\n\n\tif((config-&gt;Clock)==0)\n\t{\n\t\tfloat period =1.0\/(float)(config-&gt;PeripheralFrequency);\n\n\t\tif((config-&gt;MasterMode)==standardSpeed)\n\t\t{\n\n\t\t\tuint16_t tmp = 5\/period;\n\t\t\ti2c-&gt;CCR|=(tmp&lt;&lt;I2C_CCR_CCR_Pos);\n\t\t}\n\n\t\telse\n\t\t{\n\t\t\tif(config-&gt;DutyMode==Duty_2)\n\t\t\t{\n\t\t\t\tuint16_t tmp=5\/period;\n\t\t\t\ti2c-&gt;CCR|=(tmp&lt;&lt;I2C_CCR_CCR_Pos);\n\t\t\t}\n\n\t\t\telse\n\t\t\t{\n\t\t\t\tuint16_t tmp=13\/period;\n\t\t\t\ti2c-&gt;CCR|=(tmp&lt;&lt;I2C_CCR_CCR_Pos);\n\t\t\t}\n\t\t}\n\n\n\t}\n\telse\n\t{\n\t\ti2c-&gt;CCR|=(config-&gt;Clock&lt;&lt;I2C_CCR_CCR_Pos);\n\t}\n\n\ti2c-&gt;CR1|=I2C_CR1_PE;\n\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>For the i2c scan bus 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;}\">void BSP_I2C_Bus_Scan(I2C_TypeDef *i2c)\n{\n\tchar data[100];\n\tuint8_t a=0;\n\tfor (uint8_t i=0;i&lt;128;i++)\n\t{\n\t\ti2c-&gt;CR1 |= I2C_CR1_START;\n\t\twhile(!(i2c-&gt;SR1 &amp; I2C_SR1_SB));\n\t\ti2c-&gt;DR=(i&lt;&lt;1|0);\n\t\twhile(!(i2c-&gt;SR1)|!(I2C1-&gt;SR2)){};\n\t\ti2c-&gt;CR1 |= I2C_CR1_STOP;\n\t\tBSP_Delay(1);\n\t\ta=(i2c-&gt;SR1&amp;I2C_SR1_ADDR);\n\t\tif (a==2)\n\t    {\n\t\t\tsprintf(data,&quot;Found I2C device at address 0x%X (hexadecimal), or %d (decimal)\\n\\r&quot;,i,i);\n\t\t\tlog_info(data);\n\t    }\n\t }\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Hence, the entire source 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;}\">#include &quot;i2c_bsp.h&quot;\n\n\nvoid BSP_I2C_Init(I2C_TypeDef *i2c, I2C_ConfigTypedef *config)\n{\n\n\ti2c-&gt;CR1=I2C_CR1_SWRST;\n\n\ti2c-&gt;CR1&amp;=~I2C_CR1_SWRST;\n\n\ti2c-&gt;CR2|= config-&gt;PeripheralFrequency;\n\n\ti2c-&gt;TRISE=config-&gt;RiseTime;\n\n\ti2c-&gt;CCR|=(config-&gt;MasterMode&lt;&lt;I2C_CCR_FS_Pos);\n\n\ti2c-&gt;CCR|=(config-&gt;DutyMode&lt;&lt;I2C_CCR_DUTY_Pos);\n\n\tif((config-&gt;Clock)==0)\n\t{\n\t\tfloat period =1.0\/(float)(config-&gt;PeripheralFrequency);\n\n\t\tif((config-&gt;MasterMode)==standardSpeed)\n\t\t{\n\n\t\t\tuint16_t tmp = 5\/period;\n\t\t\ti2c-&gt;CCR|=(tmp&lt;&lt;I2C_CCR_CCR_Pos);\n\t\t}\n\n\t\telse\n\t\t{\n\t\t\tif(config-&gt;DutyMode==Duty_2)\n\t\t\t{\n\t\t\t\tuint16_t tmp=5\/period;\n\t\t\t\ti2c-&gt;CCR|=(tmp&lt;&lt;I2C_CCR_CCR_Pos);\n\t\t\t}\n\n\t\t\telse\n\t\t\t{\n\t\t\t\tuint16_t tmp=13\/period;\n\t\t\t\ti2c-&gt;CCR|=(tmp&lt;&lt;I2C_CCR_CCR_Pos);\n\t\t\t}\n\t\t}\n\n\n\t}\n\telse\n\t{\n\t\ti2c-&gt;CCR|=(config-&gt;Clock&lt;&lt;I2C_CCR_CCR_Pos);\n\t}\n\n\ti2c-&gt;CR1|=I2C_CR1_PE;\n\n}\n\n\nvoid BSP_I2C_Bus_Scan(I2C_TypeDef *i2c)\n{\n\tchar data[100];\n\tuint8_t a=0;\n\tfor (uint8_t i=0;i&lt;128;i++)\n\t{\n\t\ti2c-&gt;CR1 |= I2C_CR1_START;\n\t\twhile(!(i2c-&gt;SR1 &amp; I2C_SR1_SB));\n\t\ti2c-&gt;DR=(i&lt;&lt;1|0);\n\t\twhile(!(i2c-&gt;SR1)|!(I2C1-&gt;SR2)){};\n\t\ti2c-&gt;CR1 |= I2C_CR1_STOP;\n\t\tBSP_Delay(1);\n\t\ta=(i2c-&gt;SR1&amp;I2C_SR1_ADDR);\n\t\tif (a==2)\n\t    {\n\t\t\tsprintf(data,&quot;Found I2C device at address 0x%X (hexadecimal), or %d (decimal)\\n\\r&quot;,i,i);\n\t\t\tlog_info(data);\n\t    }\n\t }\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Main code:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Within main.c:<\/p>\n\n\n\n<p>Include the i2c_bsp 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;}\">#include &quot;i2c_bsp.h&quot;<\/pre><\/div>\n\n\n\n<p>Declare the configuration structure 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;}\">I2C_ConfigTypedef i2c1Config;<\/pre><\/div>\n\n\n\n<p>In main function:<\/p>\n\n\n\n<p>Configure PB8 and PB9 as alternate function with open drain and pullup resistors with AF4:<\/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;}\">\tGPIO_Configure_Typedef I2C_PB8, I2C_PB9;\n\n\tGPIOB_CLOCK_ENABLE();\n\n\tI2C_PB8.PinNumber=8;\n\tI2C_PB8.OutputType=Open_Drain;\n\tI2C_PB8.Mode=Alternate_function;\n\tI2C_PB8.PullUp_PullDown=PullUp;\n\tI2C_PB8.AlternateType=AF4;\n\n\tI2C_PB9.PinNumber=9;\n\tI2C_PB9.OutputType=Open_Drain;\n\tI2C_PB9.Mode=Alternate_function;\n\tI2C_PB8.PullUp_PullDown=PullUp;\n\tI2C_PB9.AlternateType=AF4;\n\n\tGPIO_Initialization(GPIOB,&amp;I2C_PB8);\n\tGPIO_Initialization(GPIOB,&amp;I2C_PB9);<\/pre><\/div>\n\n\n\n<p>Enable clock access to I2C1:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t__I2C1_CLOCK_ENABLE();<\/pre><\/div>\n\n\n\n<p>Configure the 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;}\">\ti2c1Config.MasterMode=standardSpeed;\n\n\ti2c1Config.PeripheralFrequency=50;\n\n\ti2c1Config.RiseTime=9;\n\n\tBSP_I2C_Init(I2C1, &amp;i2c1Config);<\/pre><\/div>\n\n\n\n<p>In while one loop:<\/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(1)\n\t{\n\t\tBSP_I2C_Bus_Scan(I2C1);\n\t\tBSP_Delay(200);\n\t}<\/pre><\/div>\n\n\n\n<p>Hence, the entire 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;bsp.h&quot;\n#include &quot;uart_bsp.h&quot;\n#include &quot;exti_bsp.h&quot;\n#include &quot;bsp_debug.h&quot;\n#include &quot;spi_bsp.h&quot;\n#include &quot;dma_bsp.h&quot;\n#include &quot;i2c_bsp.h&quot;\n#include &quot;stdlib.h&quot;\n\nvoid clock_config(void);\n\nI2C_ConfigTypedef i2c1Config;\n\n\nint main()\n{\n\n\n\t#if FPU_EN\n\t\tSCB-&gt;CPACR |= ((3UL &lt;&lt; 10*2)|(3UL &lt;&lt; 11*2));\n\t#endif\n\n\tclock_config();\n\tBSP_Ticks_Init(100000000);\n\n\tGPIO_Configure_Typedef I2C_PB8, I2C_PB9;\n\n\tGPIOB_CLOCK_ENABLE();\n\n\tI2C_PB8.PinNumber=8;\n\tI2C_PB8.OutputType=Open_Drain;\n\tI2C_PB8.Mode=Alternate_function;\n\tI2C_PB8.PullUp_PullDown=PullUp;\n\tI2C_PB8.AlternateType=AF4;\n\n\tI2C_PB9.PinNumber=9;\n\tI2C_PB9.OutputType=Open_Drain;\n\tI2C_PB9.Mode=Alternate_function;\n\tI2C_PB8.PullUp_PullDown=PullUp;\n\tI2C_PB9.AlternateType=AF4;\n\n\tGPIO_Initialization(GPIOB,&amp;I2C_PB8);\n\tGPIO_Initialization(GPIOB,&amp;I2C_PB9);\n\n\t__I2C1_CLOCK_ENABLE();\n\n\ti2c1Config.MasterMode=standardSpeed;\n\n\ti2c1Config.PeripheralFrequency=50;\n\n\ti2c1Config.RiseTime=9;\n\n\tBSP_I2C_Init(I2C1, &amp;i2c1Config);\n\n\n\n\n\twhile(1)\n\t{\n\t\tBSP_I2C_Bus_Scan(I2C1);\n\t\tBSP_Delay(200);\n\t}\n}\n\nvoid clock_config(void)\n{\n\tClock_Config_Typedef clockConfig;\n\n\tclockConfig.PLL_M= 4;\n\tclockConfig.PLL_N= 200;\n\tclockConfig.PLL_P= 4;\n\n\tclockConfig.AHB1Prescaler=AHB1_Prescaler1;\n\tclockConfig.APB1Prescaler=APB1_Prescaler2;\n\tclockConfig.APB2Prescaler=APB2_Prescaler1;\n\n\tclockConfig.clockSourc=External_Oscillator;\n\tclockConfig.flash_latency= Three_wait_state;\n\n\tClock_Configuration(&amp;clockConfig);\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>In order to see the results, you need to configure SWO as shown in this guide <a href=\"https:\/\/blog.embeddedexpert.io\/?p=1908\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=1908\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>You should get the addresses of attached slave devices (1 in this case):<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"822\" height=\"726\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/Screenshot-2023-08-30-at-10.03.33-AM.png\" alt=\"\" class=\"wp-image-1955\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/Screenshot-2023-08-30-at-10.03.33-AM.png 822w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/Screenshot-2023-08-30-at-10.03.33-AM-300x265.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/Screenshot-2023-08-30-at-10.03.33-AM-768x678.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/Screenshot-2023-08-30-at-10.03.33-AM-750x662.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/Screenshot-2023-08-30-at-10.03.33-AM-400x353.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/Screenshot-2023-08-30-at-10.03.33-AM-250x221.png 250w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this first section of the fourteenth part of board support package on STM32F411, we shall develop the initialization function of I2C and build I2C bus scanner to get the address of the connected slave devices. In this guide, we shall cover the following: Developing the header file. Developing the source file. Main code. Results. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,2,11,9,12],"tags":[],"class_list":["post-1953","post","type-post","status-publish","format-standard","hentry","category-data-structures","category-embedded-systems","category-peripheral-drivers","category-state-machines","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1953"}],"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=1953"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1953\/revisions"}],"predecessor-version":[{"id":1956,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1953\/revisions\/1956"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}