<spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> STM32_I2C_USE_I2C2</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> TRUE</span></span></code></pre></div><table><thead><tr><th><code>mcuconf.h</code> Setting</th><th>Description</th><th>Default</th></tr></thead><tbody><tr><td><code>STM32_I2C_BUSY_TIMEOUT</code></td><td>Time in milliseconds until the I2C command is aborted if no response is received</td><td><code>50</code></td></tr><tr><td><code>STM32_I2C_XXX_IRQ_PRIORITY</code></td><td>Interrupt priority for hardware driver XXX (THIS IS AN EXPERT SETTING)</td><td><code>10</code></td></tr><tr><td><code>STM32_I2C_USE_DMA</code></td><td>Enable/Disable the ability of the MCU to offload the data transfer to the DMA unit</td><td><code>TRUE</code></td></tr><tr><td><code>STM32_I2C_XXX_DMA_PRIORITY</code></td><td>Priority of DMA unit for hardware driver XXX (THIS IS AN EXPERT SETTING)</td><td><code>1</code></td></tr></tbody></table><p>Configuration-wise, you'll need to set up the peripheral as per your MCU's datasheet -- the defaults match the pins for a Proton-C, i.e. STM32F303.</p><table><thead><tr><th><code>config.h</code> Overrride</th><th>Description</th><th>Default</th></tr></thead><tbody><tr><td><code>I2C_DRIVER</code></td><td>I2C peripheral to use - I2C1 -><code>I2CD1</code>, I2C2 -><code>I2CD2</code> etc.</td><td><code>I2CD1</code></td></tr><tr><td><code>I2C1_SCL_PIN</code></td><td>The pin definition for SCL</td><td><code>B6</code></td></tr><tr><td><code>I2C1_SCL_PAL_MODE</code></td><td>The alternate function mode for SCL</td><td><code>4</code></td></tr><tr><td><code>I2C1_SDA_PIN</code></td><td>The pin definition for SDA</td><td><code>B7</code></td></tr><tr><td><code>I2C1_SDA_PAL_MODE</code></td><td>The alternate function mode for SDA</td><td><code>4</code></td></tr></tbody></table><p>The following configuration values depend on the specific MCU in use.</p><h3id="arm-configuration-i2cv1"tabindex="-1">I2Cv1 <aclass="header-anchor"href="#arm-configuration-i2cv1"aria-label="Permalink to "I2Cv1 {#arm-configuration-i2cv1}""></a></h3><ul><li>STM32F1xx</li><li>STM32F2xx</li><li>STM32F4xx</li><li>STM32L0xx</li><li>STM32L1xx</li></ul><p>See <ahref="https://www.playembedded.org/blog/stm32-i2c-chibios/#7_I2Cv1_configuration_structure"target="_blank"rel="noreferrer">this page</a> for the I2Cv1 configuration structure.</p><table><thead><tr><th><code>config.h</code> Override</th><th>Default</th></tr></thead><tbody><tr><td><code>I2C1_OPMODE</code></td><td><code>OPMODE_I2C</code></td></tr><tr><td><code>I2C1_CLOCK_SPEED</code></td><td><code>100000</code></td></tr><tr><td><code>I2C1_DUTY_CYCLE</code></td><td><code>STD_DUTY_CYCLE</code></td></tr></tbody></table><h3id="arm-configuration-i2cv2"tabindex="-1">I2Cv2 <aclass="header-anchor"href="#arm-configuration-i2cv2"aria-label="Permalink to "I2Cv2 {#arm-configuration-i2cv2}""></a></h3><ul><li>STM32F0xx</li><li>STM32F3xx</li><li>STM32F7xx</li><li>STM32L4xx</li></ul><p>See <ahref="https://www.playembedded.org/blog/stm32-i2c-chibios/#8_I2Cv2_I2Cv3_configuration_structure"target="_blank"rel="noreferrer">this page</a> for the I2Cv2 configuration structure.</p><table><thead><tr><th><code>config.h</code> Override</th><th>Default</th></tr></thead><tbody><tr><td><code>I2C1_TIMINGR_PRESC</code></td><td><code>0U</code></td></tr><tr><td><code>I2C1_TIMINGR_SCLDEL</code></td><td><code>7U</code></td></tr><tr><td><code>I2C1_TIMINGR_SDADEL</code></td><td><code>0U</code></td></tr><tr><td><code>I2C1_TIMINGR_SCLH</code></td><td><code>38U</code></td></tr><tr><td><code>I2C1_TIMINGR_SCLL</code></td><td><code>129U</code></td></tr></tbody></table><h2id="api"tabindex="-1">API <aclass="header-anchor"href="#api"aria-label="Permalink to "API {#api}""></a></h2><h3id="api-i2c-init"tabindex="-1"><code>void i2c_init(void)</code><aclass="header-anchor"href="#api-i2c-init"aria-label="Permalinkto"`voidi2c_init(void)`{#api-i
<spanclass="line"><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> gpio_set_pin_input</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(B6);</span><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // Try releasing special pins for a short time</span></span>
<spanclass="line"><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> wait_ms</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">10</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // Wait for the release to happen</span></span>
<spanclass="line"></span>
<spanclass="line"><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> palSetPadMode</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(GPIOB, </span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">6</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">PAL_MODE_ALTERNATE</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">4</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) </span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">|</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> PAL_STM32_OTYPE_OPENDRAIN </span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">|</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> PAL_STM32_PUPDR_PULLUP);</span><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // Set B6 to I2C function</span></span>
<spanclass="line"><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> palSetPadMode</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(GPIOB, </span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">7</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">PAL_MODE_ALTERNATE</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">4</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) </span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">|</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> PAL_STM32_OTYPE_OPENDRAIN </span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">|</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> PAL_STM32_PUPDR_PULLUP);</span><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // Set B7 to I2C function</span></span>
<spanclass="line"><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><hr><h3id="api-i2c-transmit"tabindex="-1"><code>i2c_status_t i2c_transmit(uint8_t address, uint8_t *data, uint16_t length, uint16_t timeout)</code><aclass="header-anchor"href="#api-i2c-transmit"aria-label="Permalink to "`i2c_status_t i2c_transmit(uint8_t address, uint8_t *data, uint16_t length, uint16_t timeout)` {#api-i2c-transmit}""></a></h3><p>Send multiple bytes to the selected I2C device.</p><h4id="api-i2c-transmit-arguments"tabindex="-1">Arguments <aclass="header-anchor"href="#api-i2c-transmit-arguments"aria-label="Permalink to "Arguments {#api-i2c-transmit-arguments}""></a></h4><ul><li><code>uint8_t address</code><br> The 7-bit I2C address of the device.</li><li><code>uint8_t *data</code><br> A pointer to the data to transmit.</li><li><code>uint16_t length</code><br> The number of bytes to write. Take care not to overrun the length of <code>data</code>.</li><li><code>uint16_t timeout</code><br> The time in milliseconds to wait for a response from the target device.</li></ul><h4id="api-i2c-transmit-return"tabindex="-1">Return Value <aclass="header-anchor"href="#api-i2c-transmit-return"aria-label="Permalink to "Return Value {#api-i2c-transmit-return}""></a></h4><p><code>I2C_STATUS_TIMEOUT</code> if the timeout period elapses, <code>I2C_STATUS_ERROR</code> if some other error occurs, otherwise <code>I2C_STATUS_SUCCESS</code>.</p><hr><h3id="api-i2c-receive"tabindex="-1"><code>i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout)</code><aclass="header-anchor"href="#api-i2c-receive"aria-label="Permalink to "`i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout)` {#api-i2c-receive}""></a></h3><p>Receive multiple bytes from the selected I2C device.</p><h4id="api-i2c-receive-arguments"tabindex="-1">Arguments <aclass="header-anchor"href="#api-i2c-receive-arguments"aria-label="Permalink to "Arguments {#api-i2c-receive-arguments}""></a></h4><ul><li><code>uint8_t address</code><br> The 7-bit I2C address of the device.</li><li><code>uint8_t *data</code><br> A pointer to the buffer to read into.</li><li><code>uint16_t length</code><br> The number of bytes to read. Take care not to overrun the length of <code>data</code>.</li><li><code>uint16_t timeout</code><br> The time in milliseconds to wait for a response from the target device.</li></ul><h4id="api-i2c-receive-return"tabindex="-1">Return Value <aclass="header-anchor"href="#api-i2c-receive-return"aria-label="Permalink to "Return Value {#api-i2c-receive-return}""></a></h4><p><code>I2C_STATUS_TIMEOUT</code> if the timeout period elapses, <code>I2C_STATUS_ERROR</code> if some other error occurs, otherwise <code>I2C_STATUS_SUCCESS</code>.</p><hr><h3id="api-i2c-write-register"tabindex="-1"><code>i2c_status_t i2c_write_register(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout)</code><aclass="header-anchor"href="#api-i2c-write-register"aria-label="Permalink to "`i2c_status_t i2c_write_register(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout)` {#api-i2c-write-register}""></a></h3><p>Writes to a register with an 8-bit address on the I2C device.</p><h4id="api-i2c-write-register-arguments"tabindex="-1">Arguments <aclass="header-anchor"href="#api-i2c-write-register-arguments"aria-label="Permalink to "Arguments {#api-i2c-write-register-arguments}""></a></h4><ul><li><code>uint8_t devaddr</code><br> The 7-bit I2C address of the device.</li><li><code>uint8_t regaddr</code><br> The register address to write to.</li><li><code>uint8_t *data</code><br> A pointer to the data to transmit.</li><li><code>uint16_t length</code><br> The number of bytes to write. Take care not to overrun the length of <code>data</code>.</li><li><code>uint16_t timeout</code><br> The time in milliseconds to wait for a respon