lupyuen.org/articles/i2c.html

1018 lines
71 KiB
HTML
Raw Normal View History

2021-01-25 12:35:16 +08:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="rustdoc">
<title>PineCone BL602 talks to I2C Sensors</title>
<!-- Begin scripts/articles/*-header.html: Article Header for Custom Markdown files processed by rustdoc, like chip8.md -->
<meta property="og:title"
content="PineCone BL602 talks to I2C Sensors"
data-rh="true">
<meta property="og:description"
content="How we call the BL602 RISC-V Hardware Abstraction Layer to access the BME280 I2C Sensor"
data-rh="true">
<meta property="og:image"
content="https://lupyuen.github.io/images/i2c-title.jpg">
<meta property="og:type"
content="article" data-rh="true">
2024-12-30 10:59:40 +08:00
<link rel="canonical" href="https://lupyuen.org/articles/i2c.html" />
2021-01-25 12:35:16 +08:00
<!-- End scripts/articles/*-header.html -->
<!-- Begin scripts/rustdoc-header.html: Header for Custom Markdown files processed by rustdoc, like chip8.md -->
<link rel="alternate" type="application/rss+xml" title="RSS Feed for lupyuen" href="/rss.xml" />
<link rel="stylesheet" type="text/css" href="../normalize.css">
<link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle">
<link rel="stylesheet" type="text/css" href="../dark.css">
<link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle">
<link rel="stylesheet" type="text/css" href="../prism.css">
<script src="../storage.js"></script><noscript>
<link rel="stylesheet" href="../noscript.css"></noscript>
<link rel="shortcut icon" href="../favicon.ico">
<style type="text/css">
#crate-search {
background-image: url("../down-arrow.svg");
}
</style>
<!-- End scripts/rustdoc-header.html -->
</head>
<body class="rustdoc">
<!--[if lte IE 8]>
<div class="warning">
This old browser is unsupported and will most likely display funky
things.
</div>
<![endif]-->
2024-02-17 20:10:07 +08:00
<!-- Begin scripts/rustdoc-before.html: Pre-HTML for Custom Markdown files processed by rustdoc, like chip8.md -->
2021-01-25 12:35:16 +08:00
<!-- Begin Theme Picker -->
<div class="theme-picker" style="left: 0"><button id="theme-picker" aria-label="Pick another theme!"><img src="../brush.svg"
width="18" alt="Pick another theme!"></button>
<div id="theme-choices"></div>
</div>
<!-- Theme Picker -->
<!-- End scripts/rustdoc-before.html -->
<h1 class="title">PineCone BL602 talks to I2C Sensors</h1>
2024-12-04 20:02:50 +08:00
<nav id="rustdoc"><ul>
<li><a href="#bl602-i2c-hardware-abstraction-layer-high-level-vs-low-level" title="BL602 I2C Hardware Abstraction Layer: High Level vs Low Level">1 BL602 I2C Hardware Abstraction Layer: High Level vs Low Level</a><ul></ul></li>
<li><a href="#connect-bl602-to-bme280-i2c-sensor" title="Connect BL602 to BME280 I2C Sensor">2 Connect BL602 to BME280 I2C Sensor</a><ul>
<li><a href="#i2c-protocol-for-bme280" title="I2C Protocol for BME280">2.1 I2C Protocol for BME280</a><ul></ul></li></ul></li>
<li><a href="#initialise-i2c-port" title="Initialise I2C Port">3 Initialise I2C Port</a><ul>
<li><a href="#select-i2c-port" title="Select I2C Port">3.1 Select I2C Port</a><ul></ul></li>
<li><a href="#assign-i2c-pins-and-set-i2c-frequency" title="Assign I2C Pins and set I2C Frequency">3.2 Assign I2C Pins and set I2C Frequency</a><ul></ul></li>
<li><a href="#enable-i2c-interrupts" title="Enable I2C Interrupts">3.3 Enable I2C Interrupts</a><ul></ul></li>
<li><a href="#register-i2c-interrupt-handler" title="Register I2C Interrupt Handler">3.4 Register I2C Interrupt Handler</a><ul></ul></li>
<li><a href="#hal-functions" title="HAL Functions">3.5 HAL Functions</a><ul></ul></li></ul></li>
<li><a href="#i2c-message" title="I2C Message">4 I2C Message</a><ul>
<li><a href="#define-i2c-message-and-buffer" title="Define I2C Message and Buffer">4.1 Define I2C Message and Buffer</a><ul></ul></li>
<li><a href="#set-i2c-operation-and-buffer" title="Set I2C Operation and Buffer">4.2 Set I2C Operation and Buffer</a><ul></ul></li>
<li><a href="#set-i2c-device-address-and-register-address" title="Set I2C Device Address and Register Address">4.3 Set I2C Device Address and Register Address</a><ul></ul></li>
<li><a href="#i2c-terms" title="I2C Terms">4.4 I2C Terms</a><ul></ul></li></ul></li>
<li><a href="#start-i2c-read" title="Start I2C Read">5 Start I2C Read</a><ul>
<li><a href="#create-i2c-message" title="Create I2C Message">5.1 Create I2C Message</a><ul></ul></li>
<li><a href="#start-i2c-transfer" title="Start I2C Transfer">5.2 Start I2C Transfer</a><ul></ul></li></ul></li>
<li><a href="#handle-i2c-interrupts" title="Handle I2C Interrupts">6 Handle I2C Interrupts</a><ul>
<li><a href="#get-i2c-message-and-interrupt-reason" title="Get I2C Message and Interrupt Reason">6.1 Get I2C Message and Interrupt Reason</a><ul></ul></li>
<li><a href="#i2c-data-received" title="I2C Data Received">6.2 I2C Data Received</a><ul></ul></li>
<li><a href="#i2c-transfer-end" title="I2C Transfer End">6.3 I2C Transfer End</a><ul></ul></li>
<li><a href="#i2c-no-acknowledge" title="I2C No Acknowledge">6.4 I2C No Acknowledge</a><ul></ul></li>
<li><a href="#i2c-data-transmitted" title="I2C Data Transmitted">6.5 I2C Data Transmitted</a><ul></ul></li>
<li><a href="#i2c-errors" title="I2C Errors">6.6 I2C Errors</a><ul></ul></li>
<li><a href="#transfer-data" title="Transfer Data">6.7 Transfer Data</a><ul></ul></li></ul></li>
<li><a href="#transmit-and-receive-i2c-data" title="Transmit and Receive I2C Data">7 Transmit and Receive I2C Data</a><ul>
<li><a href="#i2c-write-operation" title="I2C Write Operation">7.1 I2C Write Operation</a><ul></ul></li>
<li><a href="#i2c-read-operation" title="I2C Read Operation">7.2 I2C Read Operation</a><ul></ul></li></ul></li>
<li><a href="#stop-i2c-read" title="Stop I2C Read">8 Stop I2C Read</a><ul></ul></li>
<li><a href="#build-and-run-the-firmware" title="Build and Run the Firmware">9 Build and Run the Firmware</a><ul>
<li><a href="#build-the-firmware" title="Build the firmware">9.1 Build the firmware</a><ul></ul></li>
<li><a href="#flash-the-firmware" title="Flash the firmware">9.2 Flash the firmware</a><ul></ul></li>
<li><a href="#run-the-firmware" title="Run the firmware">9.3 Run the firmware</a><ul></ul></li>
<li><a href="#enter-i2c-commands" title="Enter I2C commands">9.4 Enter I2C commands</a><ul></ul></li></ul></li>
<li><a href="#why-we-need-an-embedded-os-for-i2c" title="Why we need an Embedded OS for I2C">10 Why we need an Embedded OS for I2C</a><ul>
<li><a href="#high-level-hal-unmasked" title="High Level HAL unmasked">10.1 High Level HAL unmasked</a><ul></ul></li>
<li><a href="#high-level-hal-without-freertos" title="High Level HAL without FreeRTOS">10.2 High Level HAL without FreeRTOS</a><ul></ul></li></ul></li>
<li><a href="#whats-next" title="Whats Next">11 Whats Next</a><ul></ul></li>
<li><a href="#notes" title="Notes">12 Notes</a><ul></ul></li>
<li><a href="#appendix-how-to-troubleshoot-risc-v-exceptions" title="Appendix: How to Troubleshoot RISC-V Exceptions">13 Appendix: How to Troubleshoot RISC-V Exceptions</a><ul></ul></li>
<li><a href="#appendix-test-bme280-with-bus-pirate" title="Appendix: Test BME280 with Bus Pirate">14 Appendix: Test BME280 with Bus Pirate</a><ul></ul></li></ul></nav><p>📝 <em>29 Jan 2021</em></p>
2021-01-26 10:18:42 +08:00
<p><strong><a href="https://lupyuen.github.io/articles/pinecone">PineCone BL602</a> (<a href="https://wiki.pine64.org/wiki/Nutcracker#Pinenut-01S_Module_information_and_schematics">and Pinenut</a>)</strong> is an awesome RISC-V Microcontroller Board with WiFi and Bluetooth LE Networking.</p>
2024-09-11 15:14:35 +08:00
<p>But to turn PineCone BL602 into an <strong>IoT Gadget</strong> we need one more thing…</p>
2021-01-26 10:18:42 +08:00
<p><strong>An I2C Sensor!</strong></p>
<p>Today we shall connect PineCone / Pinenut / Any BL602 Board to an I2C Sensor and read some data.</p>
2021-05-12 06:47:20 +08:00
<p>We shall also discover a feature thats unique to BL602: <strong>I2C Register Addresses</strong></p>
<p>Remember to check out the <strong>Appendix</strong> for Special Topics…</p>
2021-02-03 07:55:22 +08:00
<ol>
<li>
<p>How to troubleshoot <strong>RISC-V Exceptions</strong></p>
</li>
<li>
<p>How to test <strong>I2C Sensors</strong> with Bus Pirate</p>
</li>
</ol>
2021-01-25 12:35:16 +08:00
<p><img src="https://lupyuen.github.io/images/i2c-title.jpg" alt="PineCone BL602 RISC-V Evaluation Board connected to BME280 I2C Sensor" /></p>
<p><em>PineCone BL602 RISC-V Evaluation Board connected to BME280 I2C Sensor</em></p>
2024-03-29 16:55:46 +08:00
<h1 id="bl602-i2c-hardware-abstraction-layer-high-level-vs-low-level"><a class="doc-anchor" href="#bl602-i2c-hardware-abstraction-layer-high-level-vs-low-level">§</a>1 BL602 I2C Hardware Abstraction Layer: High Level vs Low Level</h1>
2021-05-12 06:47:20 +08:00
<p>BL602s IoT SDK contains an <strong>I2C Hardware Abstraction Layer (HAL)</strong> that we may call in our C programs to access I2C Sensors.</p>
<p>BL602s I2C HAL is packaged as two levels…</p>
2021-01-25 16:19:42 +08:00
<ol>
<li>
2024-09-11 15:14:35 +08:00
<p><strong>Low Level HAL <a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/components/hal_drv/bl602_hal/bl_i2c.c"><code>bl_i2c.c</code></a></strong>: This runs on BL602 Bare Metal.</p>
2021-01-25 16:19:42 +08:00
<p>The Low Level HAL manipulates the BL602 I2C Registers directly to perform I2C functions.</p>
</li>
<li>
2021-11-04 22:35:44 +08:00
<p><strong>High Level HAL <a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/components/hal_drv/bl602_hal/hal_i2c.c"><code>hal_i2c.c</code></a></strong>: This calls the Low Level HAL, and uses the Device Tree and FreeRTOS.</p>
2021-01-30 00:50:48 +08:00
<p>The High Level HAL is called by the <a href="https://github.com/alibaba/AliOS-Things">AliOS Firmware</a> created by the BL602 IoT SDK.</p>
2021-05-12 06:47:20 +08:00
<p>(AliOS functions are easy to identify… Their function names begin with “<code>aos_</code>”)</p>
<p>(Why does the High Level HAL use FreeRTOS? Well learn in a while)</p>
2021-01-25 16:19:42 +08:00
</li>
</ol>
2021-11-04 22:35:44 +08:00
<p>Today we shall use the <strong>Low Level I2C HAL <a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/components/hal_drv/bl602_hal/bl_i2c.c"><code>bl_i2c.c</code></a></strong> because…</p>
2021-01-25 16:19:42 +08:00
<ul>
<li>
2024-09-11 15:14:35 +08:00
<p>The Low Level I2C HAL is <strong>simpler to understand</strong>.</p>
2021-05-12 06:47:20 +08:00
<p>Well learn all about the BL602 I2C Hardware by calling the Low Level HAL Functions.</p>
2021-01-25 16:19:42 +08:00
</li>
<li>
<p>The Low Level I2C HAL <strong>works on all Embedded Operating Systems</strong>. (Not just FreeRTOS)</p>
2021-05-12 06:47:20 +08:00
<p>In the next article well port the Low Level I2C HAL to Mynewt. And hopefully the PineCone BL602 Community will port it to Arduino, RIOT, Zephyr, …</p>
2021-08-08 21:59:41 +08:00
<p><a href="https://github.com/nandojve/zephyr/blob/bouffalo/boards/riscv/dt_bl10_devkit/doc/index.rst"><strong>UPDATE: Check out Zephyr for BL602</strong></a></p>
2021-11-12 19:41:19 +08:00
<p><a href="https://github.com/bouffalolab/bl_mcu_sdk/pull/18"><strong>UPDATE: Zephyr is being ported to BL602 MCU SDK</strong></a></p>
2021-01-25 16:19:42 +08:00
</li>
<li>
<p>But the Low Level I2C HAL is <strong>not functionally complete</strong>.</p>
2021-05-12 06:47:20 +08:00
<p>(Yes we said that BL602 will <em>talk to I2C Sensors today</em>… Though we wont be able to <em>use the sensor data meaningfully yet</em>)</p>
<p>Well see in a while that the Low Level HAL requires an Embedded Operating System to function properly. (Which is beyond the scope of this article)</p>
2021-01-25 16:19:42 +08:00
</li>
</ul>
2021-11-04 22:35:44 +08:00
<p>We shall test BL602 I2C with this BL602 Command-Line Firmware (modded from BL602 IoT SDK): <a href="https://github.com/lupyuen/bl_iot_sdk/tree/master/customer_app/sdk_app_i2c"><code>sdk_app_i2c</code></a></p>
2021-01-25 16:19:42 +08:00
<p><img src="https://lupyuen.github.io/images/i2c-fail.jpg" alt="BL602 Command-Line Firmware sdk_app_i2c" /></p>
2021-05-12 06:47:20 +08:00
<p>(Dont worry, well make it hunky dory by the end of the article!)</p>
2021-01-25 16:19:42 +08:00
<p>The firmware will work on all BL602 boards, including PineCone and Pinenut.</p>
2021-01-25 17:52:26 +08:00
<p><img src="https://lupyuen.github.io/images/i2c-bme280.jpg" alt="PineCone BL602 connected to SparkFun BME280 I2C Sensor" /></p>
2021-01-28 19:29:11 +08:00
<p><em>PineCone BL602 connected to <a href="https://www.sparkfun.com/products/13676">SparkFun BME280 I2C Sensor</a></em></p>
2024-03-29 16:55:46 +08:00
<h1 id="connect-bl602-to-bme280-i2c-sensor"><a class="doc-anchor" href="#connect-bl602-to-bme280-i2c-sensor">§</a>2 Connect BL602 to BME280 I2C Sensor</h1>
2021-05-12 06:47:20 +08:00
<p>Lets connect BL602 to the <a href="https://learn.sparkfun.com/tutorials/sparkfun-bme280-breakout-hookup-guide"><strong>Bosch BME280 I2C Sensor for Temperature, Humidity and Air Pressure</strong></a></p>
<p>(Air Pressure is very useful for sensing which level of a building were on!)</p>
<p>Connect BL602 to BME280 according to the pic above…</p>
2022-03-01 17:38:23 +08:00
<div><table><thead><tr><th style="text-align: center">BL602 Pin</th><th style="text-align: center">BME280 Pin</th><th style="text-align: left">Wire Colour</th></tr></thead><tbody>
<tr><td style="text-align: center"><strong><code>GPIO 3</code></strong></td><td style="text-align: center"><code>SDA</code></td><td style="text-align: left">Green</td></tr>
<tr><td style="text-align: center"><strong><code>GPIO 4</code></strong></td><td style="text-align: center"><code>SCL</code></td><td style="text-align: left">Blue</td></tr>
<tr><td style="text-align: center"><strong><code>3V3</code></strong></td><td style="text-align: center"><code>3.3V</code></td><td style="text-align: left">Red</td></tr>
<tr><td style="text-align: center"><strong><code>GND</code></strong></td><td style="text-align: center"><code>GND</code></td><td style="text-align: left">Black</td></tr>
2021-01-25 17:44:17 +08:00
</tbody></table>
2021-12-09 17:13:53 +08:00
</div>
2021-01-28 18:02:23 +08:00
<p>(The steps in this article will work for BMP280 too)</p>
2021-05-12 06:47:20 +08:00
<p>The Low Level I2C HAL assigns GPIO 3 and 4 to the I2C Port on BL602. (See <strong>“Section 3.2.8: GPIO Function”</strong> in the <a href="https://github.com/bouffalolab/bl_docs/tree/main/BL602_RM/en"><strong>BL602 Reference Manual</strong></a>)</p>
<p>(If were using the High Level I2C HAL, the I2C Pins are defined in the Device Tree)</p>
2021-01-25 17:44:17 +08:00
<p><em>What shall we accomplish with BL602 and BME280?</em></p>
<ol>
<li>
2021-05-12 06:47:20 +08:00
<p>Well access BME280 at <strong>I2C Device ID <code>0x77</code></strong></p>
2021-01-28 19:29:11 +08:00
<p>(BME280 may be configured as Device ID <code>0x76</code> or <code>0x77</code>. <a href="https://www.sparkfun.com/products/13676">SparkFun BME280</a> in the pic above uses <code>0x77</code>)</p>
2021-01-25 17:44:17 +08:00
</li>
<li>
<p>BME280 has an I2C Register, <strong>Chip ID, at Register <code>0xD0</code></strong></p>
</li>
<li>
2024-09-11 15:14:35 +08:00
<p>Reading the Chip ID Register will give us the <strong>Chip ID value <code>0x60</code></strong></p>
2021-01-28 18:02:23 +08:00
<p>(<code>0x60</code> identifies the chip as BME280. For BMP280 the Chip ID is <code>0x58</code>)</p>
2021-01-25 17:44:17 +08:00
</li>
</ol>
2024-03-29 16:55:46 +08:00
<h2 id="i2c-protocol-for-bme280"><a class="doc-anchor" href="#i2c-protocol-for-bme280">§</a>2.1 I2C Protocol for BME280</h2>
2021-01-26 07:50:41 +08:00
<p><em>What are the data bytes that will be sent by BL602?</em></p>
2021-05-12 06:47:20 +08:00
<p>Heres the I2C Data that will be sent by BL602 to BME280…</p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-text"><code> [Start] 0xEE 0xD0 [Stop]
2021-01-25 17:44:17 +08:00
2024-12-04 20:02:50 +08:00
[Start] 0xEF [Read] [Stop]</code></pre></div>
2021-01-25 17:44:17 +08:00
<p>BL602 will initiate two I2C Transactions, indicated by <code>[Start] ... [Stop]</code></p>
<ol>
<li>
<p>In the First I2C Transaction, BL602 specifies the I2C Register to be read: <code>0xD0</code> (Chip ID)</p>
</li>
<li>
<p>In the Second I2C Transaction, BME280 returns the value of the Chip ID Register, indicated by <code>[Read]</code></p>
</li>
</ol>
<p><em>What are 0xEE and 0xEF?</em></p>
2021-05-12 06:47:20 +08:00
<p>They are the Read / Write aliases of the I2C Device ID <code>0x77</code></p>
2021-01-25 17:44:17 +08:00
<ul>
<li>
<p><code>0xEE</code> = (<code>0x77</code> * 2) + 0, for Writing Data</p>
</li>
<li>
<p><code>0xEF</code> = (<code>0x77</code> * 2) + 1, for Reading Data</p>
</li>
</ul>
2021-05-12 06:47:20 +08:00
<p>I2C uses this even / odd convention to indicate whether were writing or reading data.</p>
2021-01-25 17:44:17 +08:00
<p>To sum up: We need to reproduce on BL602 the two <code>[Start] ... [Stop]</code> transactions. Which includes sending 3 bytes (<code>0xEE</code>, <code>0xD0</code>, <code>0xEF</code>) and receiving 1 byte (<code>0x60</code>).</p>
2021-02-22 07:07:22 +08:00
<p><a href="https://lupyuen.github.io/articles/building-a-rust-driver-for-pinetimes-touch-controller">More about I2C</a></p>
2021-01-27 12:49:25 +08:00
<p><img src="https://lupyuen.github.io/images/i2c-cartoon4.png" alt="Initialise I2C Port" /></p>
2024-03-29 16:55:46 +08:00
<h1 id="initialise-i2c-port"><a class="doc-anchor" href="#initialise-i2c-port">§</a>3 Initialise I2C Port</h1>
2021-11-04 22:35:44 +08:00
<p>Remember our Command-Line Firmware <a href="https://github.com/lupyuen/bl_iot_sdk/tree/master/customer_app/sdk_app_i2c"><code>sdk_app_i2c</code></a> for testing I2C on BL602?</p>
2021-05-12 06:47:20 +08:00
<p>Heres the command for initialising the I2C Port…</p>
2024-12-04 20:02:50 +08:00
<div class="example-wrap"><pre class="language-text"><code> i2c_init</code></pre></div>
2021-11-04 22:35:44 +08:00
<p>Lets discover how this command calls the Low Level I2C HAL to initialise the I2C Port: <a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/customer_app/sdk_app_i2c/sdk_app_i2c/demo.c#L343-L369"><code>sdk_app_i2c/demo.c</code></a></p>
2024-03-29 16:55:46 +08:00
<h2 id="select-i2c-port"><a class="doc-anchor" href="#select-i2c-port">§</a>3.1 Select I2C Port</h2><div class="example-wrap"><pre class="language-c"><code>/// Init I2C Port. Based on hal_i2c_init in hal_i2c.c
2021-01-25 20:16:53 +08:00
static void test_i2c_init(char *buf, int len, int argc, char **argv) {
// Use I2C Port 0
2024-12-04 20:02:50 +08:00
const int i2cx = 0;</code></pre></div>
2021-05-12 06:47:20 +08:00
<p>Well use <strong>I2C Port 0</strong>, the one and only I2C Port on BL602.</p>
2024-03-29 16:55:46 +08:00
<h2 id="assign-i2c-pins-and-set-i2c-frequency"><a class="doc-anchor" href="#assign-i2c-pins-and-set-i2c-frequency">§</a>3.2 Assign I2C Pins and set I2C Frequency</h2><div class="example-wrap"><pre class="language-c"><code> // Init I2C Port 0 to GPIO 3 and 4
2021-01-25 20:16:53 +08:00
i2c_gpio_init(i2cx);
// Set I2C Port 0 to 500 kbps
2024-12-04 20:02:50 +08:00
i2c_set_freq(500, i2cx);</code></pre></div>
2021-01-25 22:38:01 +08:00
<p>We call <code>i2c_gpio_init</code> to assign <strong>GPIO 3 and 4 as the SDA and SCL pins</strong> for I2C Port 0.</p>
<p>Then we call <code>i2c_set_freq</code> to set the <strong>I2C Frequency</strong> to 500 kbps.</p>
2024-03-29 16:55:46 +08:00
<h2 id="enable-i2c-interrupts"><a class="doc-anchor" href="#enable-i2c-interrupts">§</a>3.3 Enable I2C Interrupts</h2>
2021-05-12 06:47:20 +08:00
<p>The I2C Port triggers <strong>I2C Interrupts</strong> after sending and receiving queued data, also when an error occurs. So we need to enable I2C Interrupts…</p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-c"><code> // Disable I2C Port 0
2021-01-25 20:16:53 +08:00
I2C_Disable(i2cx);
// Enable I2C interrupts
bl_irq_enable(I2C_IRQn);
2024-12-04 20:02:50 +08:00
I2C_IntMask(i2cx, I2C_INT_ALL, MASK);</code></pre></div>
2021-01-25 22:38:01 +08:00
<p>We disable the I2C Port, then enable I2C Interrupts on the I2C Port.</p>
2024-03-29 16:55:46 +08:00
<h2 id="register-i2c-interrupt-handler"><a class="doc-anchor" href="#register-i2c-interrupt-handler">§</a>3.4 Register I2C Interrupt Handler</h2>
2021-05-12 06:47:20 +08:00
<p>To handle I2C Interrupts we <strong>register an Interrupt Handler Function</strong></p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-c"><code> // Register the I2C Interrupt Handler
2021-01-25 20:16:53 +08:00
bl_irq_register_with_ctx(
2021-01-25 22:38:01 +08:00
I2C_IRQn, // For I2C Interrupt:
test_i2c_interrupt_entry, // Interrupt Handler
&amp;gpstmsg // Pointer to current I2C Message
2021-01-25 20:16:53 +08:00
);
2024-12-04 20:02:50 +08:00
}</code></pre></div>
2021-01-25 22:38:01 +08:00
<p>Here we register the function <code>test_i2c_interrupt_entry</code> as the Interrupt Handler Function for I2C Interrupts. (More about this function in a while)</p>
2021-05-12 06:47:20 +08:00
<p><code>gpstmsg</code> is the <strong>Interrupt Context</strong> that will be passed to the Interrupt Handler Function…</p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-c"><code>/// Global pointer to current I2C Message
2024-12-04 20:02:50 +08:00
static i2c_msg_t *gpstmsg;</code></pre></div>
2021-01-25 22:38:01 +08:00
<p><code>gpstmsg</code> points to the <strong>current I2C Message</strong> being sent or received, so that the Interrupt Handler knows which Message Buffer to use for sending and receiving data.</p>
2024-03-29 16:55:46 +08:00
<h2 id="hal-functions"><a class="doc-anchor" href="#hal-functions">§</a>3.5 HAL Functions</h2>
2021-05-12 06:47:20 +08:00
<p>Lets list down the HAL Functions called above and where they are defined…</p>
2021-11-04 22:35:44 +08:00
<p>The following functions are defined in the <strong>Low Level I2C HAL</strong>: <a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/components/hal_drv/bl602_hal/bl_i2c.c"><code>bl_i2c.c</code></a></p>
2024-12-04 20:02:50 +08:00
<div class="example-wrap"><pre class="language-text"><code>i2c_gpio_init, i2c_set_freq</code></pre></div>
2021-11-04 22:35:44 +08:00
<p>These functions are defined in the <strong>BL602 Interrupt HAL</strong>: <a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/components/hal_drv/bl602_hal/bl_irq.c"><code>bl_irq.c</code></a></p>
2024-12-04 20:02:50 +08:00
<div class="example-wrap"><pre class="language-text"><code>bl_irq_enable, bl_irq_register_with_ctx</code></pre></div>
2021-11-04 22:35:44 +08:00
<p>And these functions are defined in the <strong>BL602 Standard Driver</strong>: <a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/components/bl602/bl602_std/bl602_std/StdDriver/Src/bl602_i2c.c"><code>bl602_i2c.c</code></a></p>
2024-12-04 20:02:50 +08:00
<div class="example-wrap"><pre class="language-text"><code>I2C_Disable, I2C_IntMask</code></pre></div>
2021-01-25 22:38:01 +08:00
<p>(The BL602 Standard Driver contains low-level functions to manipulate the BL602 Hardware Registers)</p>
2021-01-27 18:15:04 +08:00
<p><img src="https://lupyuen.github.io/images/i2c-cartoon6.png" alt="I2C Message" /></p>
2024-03-29 16:55:46 +08:00
<h1 id="i2c-message"><a class="doc-anchor" href="#i2c-message">§</a>4 I2C Message</h1>
2021-01-26 00:03:22 +08:00
<p>Our objective is to <strong>read Register <code>0xD0</code></strong> from our BME280 Sensor with <strong>Device ID <code>0x77</code></strong></p>
2021-05-12 06:47:20 +08:00
<p>We specify these details in an <strong>I2C Message Struct <code>i2c_msg_t</code></strong> thats defined in the Low Level I2C HAL.</p>
2021-11-04 22:35:44 +08:00
<p>Heres how we create an I2C Message: <a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/customer_app/sdk_app_i2c/sdk_app_i2c/demo.c#L424-L442"><code>sdk_app_i2c/demo.c</code></a></p>
2024-03-29 16:55:46 +08:00
<h2 id="define-i2c-message-and-buffer"><a class="doc-anchor" href="#define-i2c-message-and-buffer">§</a>4.1 Define I2C Message and Buffer</h2><div class="example-wrap"><pre class="language-c"><code>// Define I2C message and buffer
2021-01-26 00:12:06 +08:00
static i2c_msg_t read_msg; // Message for reading I2C Data
static uint8_t read_buf[32]; // Buffer for reading I2C Data
2024-12-04 20:02:50 +08:00
int data_len = 1; // Bytes to be read</code></pre></div>
2024-09-11 15:14:35 +08:00
<p>First we define <code>read_msg</code> as a static I2C Message.</p>
2021-01-26 00:03:22 +08:00
<p>The data returned by our BME280 Sensor shall be stored in the static buffer <code>read_buf</code>.</p>
2024-03-29 16:55:46 +08:00
<h2 id="set-i2c-operation-and-buffer"><a class="doc-anchor" href="#set-i2c-operation-and-buffer">§</a>4.2 Set I2C Operation and Buffer</h2><div class="example-wrap"><pre class="language-c"><code>// Set the I2C operation
2021-01-26 00:12:06 +08:00
read_msg.i2cx = 0; // I2C Port
read_msg.direct = I2C_M_READ; // Read I2C data
2024-12-04 20:02:50 +08:00
read_msg.block = I2C_M_BLOCK; // Wait until data has been read</code></pre></div>
2021-01-26 00:03:22 +08:00
<p>Next we set the <strong>I2C Port</strong> (0) and the <strong>I2C Operation</strong> (Blocking Read).</p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-c"><code>// Set the I2C buffer
2021-01-26 00:12:06 +08:00
read_msg.buf = read_buf; // Read buffer
read_msg.len = data_len; // Number of bytes to be read
2024-12-04 20:02:50 +08:00
read_msg.idex = 0; // Index of next byte to be read into buf</code></pre></div>
2021-01-26 00:03:22 +08:00
<p>Then we assign the data buffer <code>read_buf</code> to <code>read_msg</code> and set the number of bytes to be read (1).</p>
<p><code>idex</code> is the index into the buffer <code>read_buf</code>. Our I2C Interrupt Handler will increment this index as it populates the buffer upon receiving data.</p>
2024-03-29 16:55:46 +08:00
<h2 id="set-i2c-device-address-and-register-address"><a class="doc-anchor" href="#set-i2c-device-address-and-register-address">§</a>4.3 Set I2C Device Address and Register Address</h2>
2021-05-12 06:47:20 +08:00
<p>Well be reading data from BME280, which has Device ID <code>0x77</code>.</p>
<p>We specify the <strong>Device Address</strong> like so…</p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-c"><code>// Set device address
2024-12-04 20:02:50 +08:00
read_msg.addr = 0x77; // BME280 I2C Secondary Address (Primary Address is 0x76)</code></pre></div>
2021-05-12 06:47:20 +08:00
<p><em>Now heres the really really interesting thing about BL602…</em></p>
2021-01-26 07:50:41 +08:00
<p>Remember that we will be reading Register <code>0xD0</code> (Chip ID) on BME280?</p>
2021-05-12 06:47:20 +08:00
<p>We specify the <strong>Register Address</strong> in this incredibly easy peasy way…</p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-c"><code>// Set register address
2021-01-26 00:12:06 +08:00
read_msg.subflag = 1; // Enable Register Address
read_msg.subaddr = 0xd0; // Register Address (BME280 Chip ID)
2024-12-04 20:02:50 +08:00
read_msg.sublen = 1; // Length of Register Address (bytes)</code></pre></div>
2021-01-26 07:50:41 +08:00
<p><strong>This I2C Register Address feature is unique to BL602!</strong></p>
2021-05-12 06:47:20 +08:00
<p>The I2C Register Address feature is <strong>not available</strong> on STM32 Blue Pill, Nordic nRF52, GigaDevice GD32 VF103 (RISC-V), ESP32, … Not even on Raspberry Pi Pico!</p>
<p>(Though it seems to be supported on <a href="https://mcuxpresso.nxp.com/api_doc/dev/116/group__i2c.html">NXP Microcontrollers</a> as “I2C Subaddress”)</p>
2021-01-26 07:50:41 +08:00
<p>Thus <strong>BL602 I2C works a little differently</strong> from other microcontrollers.</p>
<p>This may complicate the support for I2C in Embedded Operating Systems like Mynewt, RIOT and Zephyr. (More about this in a while)</p>
2024-03-29 16:55:46 +08:00
<h2 id="i2c-terms"><a class="doc-anchor" href="#i2c-terms">§</a>4.4 I2C Terms</h2>
2021-01-26 11:23:45 +08:00
<p>The I2C Documentation in the <a href="https://github.com/bouffalolab/bl_docs/tree/main/BL602_RM/en">BL602 Reference Manual</a> appears somewhat confusing because of the I2C Register Address feature. <a href="https://lupyuen.github.io/images/i2c-confuse.png">See this</a></p>
2021-05-12 06:47:20 +08:00
<p>In this article we shall standardise on these I2C Terms…</p>
2021-01-26 07:50:41 +08:00
<ol>
<li>
2021-05-12 06:47:20 +08:00
<p>We say <strong>“Device Address”</strong></p>
<p>(Instead of “Slave Address”, “Slave Device”)</p>
2021-01-26 07:50:41 +08:00
</li>
<li>
2021-05-12 06:47:20 +08:00
<p>We say <strong>“Register Address”</strong></p>
<p>(Instead of “Subaddress”, “Slave Device Address”, “Slave Device Register Address”)</p>
2021-01-26 07:50:41 +08:00
</li>
</ol>
2021-01-27 11:58:06 +08:00
<p><img src="https://lupyuen.github.io/images/i2c-cartoon2.png" alt="Start I2C Read" /></p>
2024-03-29 16:55:46 +08:00
<h1 id="start-i2c-read"><a class="doc-anchor" href="#start-i2c-read">§</a>5 Start I2C Read</h1>
2021-05-12 06:47:20 +08:00
<p>Now that we have created our I2C Message, lets watch it in action!</p>
<p>To begin reading data from our BME280 Sensor, we enter this command…</p>
2024-12-04 20:02:50 +08:00
<div class="example-wrap"><pre class="language-text"><code> i2c_start_read</code></pre></div>
2021-11-04 22:35:44 +08:00
<p>Lets find out what happens inside that command: <a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/customer_app/sdk_app_i2c/sdk_app_i2c/demo.c#L420-L448"><code>sdk_app_i2c/demo.c</code></a></p>
2024-03-29 16:55:46 +08:00
<h2 id="create-i2c-message"><a class="doc-anchor" href="#create-i2c-message">§</a>5.1 Create I2C Message</h2>
2021-05-12 06:47:20 +08:00
<p>We start by creating the I2C Message. We have seen this code earlier for creating the message…</p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-c"><code>// Define I2C message and buffer
2021-01-26 10:18:42 +08:00
static i2c_msg_t read_msg; // Message for reading I2C Data
static uint8_t read_buf[32]; // Buffer for reading I2C Data
static void test_i2c_start_read(char *buf, int len, int argc, char **argv) {
2021-01-26 00:03:22 +08:00
// Start reading data from I2C device
// Expect result 0x60 for BME280, 0x58 for BMP280
int data_len = 1; // Bytes to be read
memset(read_buf, 0, sizeof(read_buf));
// Set the I2C operation
read_msg.i2cx = 0; // I2C Port
read_msg.direct = I2C_M_READ; // Read I2C data
read_msg.block = I2C_M_BLOCK; // Wait until data has been read
// Set the I2C buffer
read_msg.buf = read_buf; // Read buffer
read_msg.len = data_len; // Number of bytes to be read
read_msg.idex = 0; // Index of next byte to be read into buf
// Set device address and register address
read_msg.addr = 0x77; // BME280 I2C Secondary Address (Primary Address is 0x76)
read_msg.subflag = 1; // Enable Register Address
read_msg.subaddr = 0xd0; // Register Address (BME280 Chip ID)
2024-12-04 20:02:50 +08:00
read_msg.sublen = 1; // Length of Register Address (bytes)</code></pre></div>
2021-01-28 19:50:55 +08:00
<p>(For I2C Write Operation <code>I2C_M_WRITE</code>: The Message buffer field <code>buf</code> should point to a byte array that contains the I2C Data that will be written to the I2C Register)</p>
2024-03-29 16:55:46 +08:00
<h2 id="start-i2c-transfer"><a class="doc-anchor" href="#start-i2c-transfer">§</a>5.2 Start I2C Transfer</h2>
2021-05-12 06:47:20 +08:00
<p>Now we start the I2C data transfer…</p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-c"><code> // Start the I2C transfer and enable I2C interrupts
2021-01-26 00:03:22 +08:00
gpstmsg = &amp;read_msg;
i2c_transfer_start(&amp;read_msg);
2021-01-26 10:18:42 +08:00
// do_read_data will be called to read data
// in the I2C Interrupt Handler (test_i2c_transferbytes)
2024-12-04 20:02:50 +08:00
}</code></pre></div>
2021-01-26 10:18:42 +08:00
<p>We point <code>gpstmsg</code> to our I2C Message. (Will be used for saving data into our buffer)</p>
<p>Then we call <code>i2c_transfer_start</code> to start the I2C data transfer and enable the I2C Interrupts.</p>
2021-11-04 22:35:44 +08:00
<p><code>i2c_transfer_start</code> is defined in the <strong>Low Level I2C HAL</strong>: <a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/components/hal_drv/bl602_hal/bl_i2c.c"><code>bl_i2c.c</code></a></p>
2021-01-26 10:18:42 +08:00
<p><em>How does BL602 receive the I2C data from our BME280 Sensor?</em></p>
<p>The I2C data transfer happens in the background, thanks to our <strong>I2C Interrupt Handler</strong>.</p>
<p>Our I2C Interrupt Handler receives the I2C data from the BME280 Sensor and populates our read buffer <code>read_buf</code></p>
2021-05-12 06:47:20 +08:00
<p>Lets go deep into our I2C Interrupt Handler…</p>
2021-01-27 11:58:06 +08:00
<p><img src="https://lupyuen.github.io/images/i2c-cartoon3.png" alt="Handle I2C Interrupts" /></p>
2024-03-29 16:55:46 +08:00
<h1 id="handle-i2c-interrupts"><a class="doc-anchor" href="#handle-i2c-interrupts">§</a>6 Handle I2C Interrupts</h1>
2021-05-12 06:47:20 +08:00
<p>Earlier we registered <code>test_i2c_interrupt_entry</code> as our Interrupt Handler for I2C Interrupts…</p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-c"><code>// Register the I2C Interrupt Handler
2021-01-26 11:02:35 +08:00
bl_irq_register_with_ctx(
I2C_IRQn, // For I2C Interrupt:
test_i2c_interrupt_entry, // Interrupt Handler
&amp;gpstmsg // Pointer to current I2C Message
2024-12-04 20:02:50 +08:00
);</code></pre></div>
2021-01-26 11:23:45 +08:00
<p>And the current I2C Message <code>gpstmsg</code> will be passed as our Interrupt Context.</p>
2021-11-04 22:35:44 +08:00
<p>Lets find out how our Interrupt Handler handles I2C Interrupts: <a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/customer_app/sdk_app_i2c/sdk_app_i2c/demo.c#L273-L328"><code>sdk_app_i2c/demo.c</code></a></p>
2024-03-29 16:55:46 +08:00
<h2 id="get-i2c-message-and-interrupt-reason"><a class="doc-anchor" href="#get-i2c-message-and-interrupt-reason">§</a>6.1 Get I2C Message and Interrupt Reason</h2>
2021-05-12 06:47:20 +08:00
<p>When an I2C Interrupt is triggered, we fetch the Interrupt Reason and the I2C Message (from the Interrupt Context)…</p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-c"><code>/// I2C Interrupt Handler. Based on i2c_interrupt_entry in hal_i2c.c
2021-01-26 11:02:35 +08:00
static void test_i2c_interrupt_entry(void *ctx) {
// Fetch the current I2C Message from the Interrupt Context
2021-01-26 11:51:55 +08:00
i2c_msg_t *msg = *((i2c_msg_t **)ctx);
2021-01-26 11:02:35 +08:00
// Get the reason for the interrupt
uint32_t reason = BL_RD_REG(I2C_BASE, I2C_INT_STS);
// Handle each reason and increment the Interrupt Counters
2024-12-04 20:02:50 +08:00
count_int++; // Overall interrupts</code></pre></div>
2021-05-12 06:47:20 +08:00
<p>According to the <a href="https://github.com/bouffalolab/bl_docs/tree/main/BL602_RM/en"><strong>BL602 Reference Manual</strong></a> there are 6 kinds of I2C Interrupts…</p>
2021-01-26 11:23:45 +08:00
<p><img src="https://lupyuen.github.io/images/i2c-interrupt.png" alt="BL602 I2C Interrupts" /></p>
2021-05-12 06:47:20 +08:00
<p>Some good… Some not so good. Lets handle each type of interrupt…</p>
2024-03-29 16:55:46 +08:00
<h2 id="i2c-data-received"><a class="doc-anchor" href="#i2c-data-received">§</a>6.2 I2C Data Received</h2>
2021-01-26 12:44:06 +08:00
<p>(For I2C Read Operation)</p>
2021-05-12 06:47:20 +08:00
<p>When we receive data from our I2C Sensor… Its good news!</p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-c"><code> if (BL_IS_REG_BIT_SET(reason, I2C_RXF_INT)) {
2021-01-26 11:02:35 +08:00
// Receive FIFO Ready
count_rfx++;
2021-01-26 11:51:55 +08:00
msg-&gt;event = EV_I2C_RXF_INT;
2024-12-04 20:02:50 +08:00
// Should not return</code></pre></div>
2021-01-26 12:44:06 +08:00
<p>This condition flows through to the end of our Interrupt Handler, and calls <code>test_i2c_transferbytes</code> to copy the received data into our Message Buffer and receive more data.</p>
2024-03-29 16:55:46 +08:00
<h2 id="i2c-transfer-end"><a class="doc-anchor" href="#i2c-transfer-end">§</a>6.3 I2C Transfer End</h2>
2021-01-26 12:44:06 +08:00
<p>If the I2C data transfer is ending, we call <code>test_i2c_stop</code> to disable the I2C Port.</p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-c"><code> } else if (BL_IS_REG_BIT_SET(reason, I2C_END_INT)) {
2021-01-26 11:02:35 +08:00
// Transfer End
count_end++;
2021-01-26 11:51:55 +08:00
msg-&gt;event = EV_I2C_END_INT;
test_i2c_stop(msg);
2024-12-04 20:02:50 +08:00
return; // Stop now</code></pre></div>
2021-01-26 12:44:06 +08:00
<p>This condition quits our Interrupt Handler right away.</p>
2024-03-29 16:55:46 +08:00
<h2 id="i2c-no-acknowledge"><a class="doc-anchor" href="#i2c-no-acknowledge">§</a>6.4 I2C No Acknowledge</h2>
2021-05-12 06:47:20 +08:00
<p>This is bad… We encounter I2C No Acknowledge usually when the I2C Device Address is misconfigured (say <code>0x76</code> instead of <code>0x77</code>).</p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-c"><code> } else if (BL_IS_REG_BIT_SET(reason, I2C_NAK_INT)) {
2021-01-26 11:02:35 +08:00
// No Acknowledge
count_nak++;
2021-01-26 11:51:55 +08:00
msg-&gt;event = EV_I2C_NAK_INT;
test_i2c_stop(msg);
2024-12-04 20:02:50 +08:00
return; // Stop now</code></pre></div>
2021-01-26 12:44:06 +08:00
<p>We disable the I2C Port and quit the Interrupt Handler right away.</p>
2024-03-29 16:55:46 +08:00
<h2 id="i2c-data-transmitted"><a class="doc-anchor" href="#i2c-data-transmitted">§</a>6.5 I2C Data Transmitted</h2>
2021-01-26 12:44:06 +08:00
<p>(For I2C Write Operation)</p>
2021-05-12 06:47:20 +08:00
<p>This is good, it means that the queued data has been transmitted…</p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-c"><code> } else if (BL_IS_REG_BIT_SET(reason, I2C_TXF_INT)) {
2021-01-26 11:02:35 +08:00
// Transmit FIFO Ready
count_txf++;
2021-01-26 11:51:55 +08:00
msg-&gt;event = EV_I2C_TXF_INT;
2024-12-04 20:02:50 +08:00
// Should not return</code></pre></div>
2021-01-26 12:44:06 +08:00
<p>This condition flows through to the end of our Interrupt Handler, and calls <code>test_i2c_transferbytes</code> to transmit the next 4 bytes of data from our Message Buffer.</p>
2024-03-29 16:55:46 +08:00
<h2 id="i2c-errors"><a class="doc-anchor" href="#i2c-errors">§</a>6.6 I2C Errors</h2>
2021-05-12 06:47:20 +08:00
<p>Lastly we handle the remaining errors: <strong>Arbitration Lost, FIFO Error, Unknown Error</strong></p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-c"><code> } else if (BL_IS_REG_BIT_SET(reason, I2C_ARB_INT)) {
2021-01-26 11:02:35 +08:00
// Arbitration Lost
count_arb++;
2021-01-26 11:51:55 +08:00
msg-&gt;event = EV_I2C_ARB_INT;
test_i2c_stop(msg);
2021-01-26 11:02:35 +08:00
return; // Stop now
} else if (BL_IS_REG_BIT_SET(reason,I2C_FER_INT)) {
// FIFO Error
count_fer++;
2021-01-26 11:51:55 +08:00
msg-&gt;event = EV_I2C_FER_INT;
test_i2c_stop(msg);
2021-01-26 11:02:35 +08:00
return; // Stop now
} else {
// Unknown Error
count_unk++;
2021-01-26 11:51:55 +08:00
msg-&gt;event = EV_I2C_UNKNOW_INT;
test_i2c_stop(msg);
2021-01-26 11:02:35 +08:00
// Should not return
2024-12-04 20:02:50 +08:00
}</code></pre></div>
2021-01-26 12:44:06 +08:00
<p>We disable the I2C Port and quit the Interrupt Handler right away. (Except for Unknown Error)</p>
2024-03-29 16:55:46 +08:00
<h2 id="transfer-data"><a class="doc-anchor" href="#transfer-data">§</a>6.7 Transfer Data</h2>
2021-05-12 06:47:20 +08:00
<p>For I2C Data Received and I2C Data Transmitted, our Interrupt Handler flows through to this code…</p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-c"><code> // For Receive FIFO Ready and Transmit FIFO Ready, transfer 4 bytes of data
2021-01-26 11:51:55 +08:00
test_i2c_transferbytes(msg);
2024-12-04 20:02:50 +08:00
}</code></pre></div>
2021-05-12 06:47:20 +08:00
<p><code>test_i2c_transferbytes</code> does the following…</p>
2021-01-26 12:44:06 +08:00
<ul>
<li>
2021-01-26 13:29:33 +08:00
<p><strong>For I2C Read Operation:</strong> Copy the received data into our Message Buffer (4 bytes at a time) and receive more data.</p>
2021-01-26 12:44:06 +08:00
</li>
<li>
<p><strong>For I2C Write Operation:</strong> Transmit the next 4 bytes of data from our Message Buffer.</p>
</li>
</ul>
2021-05-12 06:47:20 +08:00
<p>More about this in the next section…</p>
2021-01-27 11:07:26 +08:00
<p><img src="https://lupyuen.github.io/images/i2c-cartoon1.png" alt="Transmit and Receive I2C Data" /></p>
2024-03-29 16:55:46 +08:00
<h1 id="transmit-and-receive-i2c-data"><a class="doc-anchor" href="#transmit-and-receive-i2c-data">§</a>7 Transmit and Receive I2C Data</h1>
2021-01-26 13:29:33 +08:00
<p>BL602 I2C has a <strong>FIFO Queue (First In First Out) of 4 bytes</strong> for transmitting and receiving I2C data.</p>
2021-01-26 14:18:14 +08:00
<p>Our I2C Interrupt Handler calls <code>test_i2c_transferbytes</code> to transmit and receive data in 4-byte chunks.</p>
2021-11-04 22:35:44 +08:00
<p>Heres how it works for I2C Write and I2C Read Operations: <a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/customer_app/sdk_app_i2c/sdk_app_i2c/demo.c#L249-L271"><code>sdk_app_i2c/demo.c</code></a></p>
2024-03-29 16:55:46 +08:00
<h2 id="i2c-write-operation"><a class="doc-anchor" href="#i2c-write-operation">§</a>7.1 I2C Write Operation</h2>
2021-05-12 06:47:20 +08:00
<p>In an I2C Write Operation, we handle the I2C Data Transmitted Interrupt by <strong>transmitting the next 4 bytes from the Message Buffer</strong></p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-c"><code>/// For Rx FIFO Ready and Tx FIFO Ready, transfer 4 bytes of data.
2021-01-26 14:18:14 +08:00
/// Called by I2C Interrupt Handler. Based on i2c_transferbytes in hal_i2c.c
2021-01-26 13:29:33 +08:00
static void test_i2c_transferbytes(i2c_msg_t *msg) {
// For I2C Write Operation and I2C Data Transmitted Interrupt...
if (msg-&gt;direct == I2C_M_WRITE &amp;&amp; msg-&gt;event == EV_I2C_TXF_INT) {
if (msg-&gt;idex &lt; msg-&gt;len) {
// If there is buffer data to be transmitted, transmit 4 bytes from buffer
do_write_data(msg);
} else if (msg-&gt;idex == msg-&gt;len) {
2021-01-26 14:18:14 +08:00
// Otherwise suppress the Data Transmitted Interrupts
2021-01-26 13:29:33 +08:00
I2C_IntMask(msg-&gt;i2cx, I2C_TX_FIFO_READY_INT, MASK);
2024-12-04 20:02:50 +08:00
} </code></pre></div>
2021-01-26 14:18:14 +08:00
<p>If there is no more data to be transmitted, we suppress the I2C Data Transmitted Interrupts.</p>
2021-11-04 22:35:44 +08:00
<p><code>do_write_data</code> is defined in the <strong>Low Level I2C HAL</strong>: <a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/components/hal_drv/bl602_hal/bl_i2c.c"><code>bl_i2c.c</code></a></p>
2024-03-29 16:55:46 +08:00
<h2 id="i2c-read-operation"><a class="doc-anchor" href="#i2c-read-operation">§</a>7.2 I2C Read Operation</h2>
2021-05-12 06:47:20 +08:00
<p>In an I2C Read Operation, we handle the I2C Data Received Interrupt by <strong>copying the received bytes into the Message Buffer, 4 bytes at a time</strong></p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-c"><code> // For I2C Read Operation and I2C Data Received Interrupt...
2021-01-26 13:29:33 +08:00
} else if (msg-&gt;direct == I2C_M_READ &amp;&amp; msg-&gt;event == EV_I2C_RXF_INT) {
if (msg-&gt;idex &lt; msg-&gt;len) {
// If there is data to be received, copy 4 bytes into buffer
2021-01-26 14:18:14 +08:00
do_read_data(msg);
2021-01-26 13:29:33 +08:00
} else {
2021-01-26 14:18:14 +08:00
// Otherwise suppress the Data Received Interrupts
2021-01-26 13:29:33 +08:00
I2C_IntMask(msg-&gt;i2cx, I2C_RX_FIFO_READY_INT, MASK);
}
}
2024-12-04 20:02:50 +08:00
}</code></pre></div>
2021-01-26 14:18:14 +08:00
<p>If there is no more data to be received, we suppress the I2C Data Received Interrupts.</p>
2021-11-04 22:35:44 +08:00
<p><code>do_read_data</code> is defined in the <strong>Low Level I2C HAL</strong>: <a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/components/hal_drv/bl602_hal/bl_i2c.c"><code>bl_i2c.c</code></a></p>
<p>(FYI: <code>test_i2c_transferbytes</code> is the fixed version of <code>i2c_transferbytes</code> from the High Level I2C HAL <a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/components/hal_drv/bl602_hal/hal_i2c.c"><code>hal_i2c.c</code></a>. <a href="https://lupyuen.github.io/images/i2c-transferbytes.png">Heres the fix</a>)</p>
2021-01-27 12:49:25 +08:00
<p><img src="https://lupyuen.github.io/images/i2c-cartoon5.png" alt="Stop I2C Read" /></p>
2024-03-29 16:55:46 +08:00
<h1 id="stop-i2c-read"><a class="doc-anchor" href="#stop-i2c-read">§</a>8 Stop I2C Read</h1>
2021-05-12 06:47:20 +08:00
<p>Heres the final command that well enter into the BL602 Firmware… It terminates the I2C transfer.</p>
2024-12-04 20:02:50 +08:00
<div class="example-wrap"><pre class="language-text"><code> i2c_stop_read</code></pre></div>
2021-11-04 22:35:44 +08:00
<p>This command calls <code>test_i2c_stop</code> to close the I2C Port: <a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/customer_app/sdk_app_i2c/sdk_app_i2c/demo.c#L450-L460"><code>sdk_app_i2c/demo.c</code></a></p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-c"><code>/// Stop reading data from I2C device
2021-01-26 15:06:27 +08:00
static void test_i2c_stop_read(char *buf, int len, int argc, char **argv) {
// Stop the I2C transfer on I2C Port 0
test_i2c_stop(&amp;read_msg);
// Dump the data received
for (int i = 0; i &lt; read_msg.len; i++) {
printf(&quot;%02x\n&quot;, read_buf[i]);
}
2024-12-04 20:02:50 +08:00
}</code></pre></div>
2021-01-26 15:33:02 +08:00
<p>The command also dumps the data received in the I2C Message Buffer.</p>
2021-11-04 22:35:44 +08:00
<p><code>test_i2c_stop</code> closes the I2C Port like so: <a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/customer_app/sdk_app_i2c/sdk_app_i2c/demo.c#L236-L247"><code>sdk_app_i2c/demo.c</code></a></p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-c"><code>/// Stop the I2C Transfer. Called by I2C Interrupt Handler.
2021-01-26 15:06:27 +08:00
/// Based on i2c_callback in hal_i2c.c
static void test_i2c_stop(i2c_msg_t *msg) {
// Disable I2C Port
I2C_Disable(msg-&gt;i2cx);
// Suppress all I2C Interrupts
I2C_IntMask(msg-&gt;i2cx, I2C_INT_ALL, MASK);
// Clear any error status
i2c_clear_status(msg-&gt;i2cx);
2024-12-04 20:02:50 +08:00
}</code></pre></div>
2021-11-04 22:35:44 +08:00
<p><code>i2c_clear_status</code> is defined in the <strong>Low Level I2C HAL</strong>: <a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/components/hal_drv/bl602_hal/bl_i2c.c"><code>bl_i2c.c</code></a></p>
2021-01-28 15:05:13 +08:00
<p><img src="https://lupyuen.github.io/images/i2c-success.png" alt="Reading BME280 with sdk_app_i2c firmware" /></p>
<p><em>Reading BME280 with sdk_app_i2c firmware</em></p>
2024-03-29 16:55:46 +08:00
<h1 id="build-and-run-the-firmware"><a class="doc-anchor" href="#build-and-run-the-firmware">§</a>9 Build and Run the Firmware</h1>
2021-11-04 22:35:44 +08:00
<p>Weve read the I2C code… Lets download, flash and run the modded <a href="https://github.com/lupyuen/bl_iot_sdk/tree/master/customer_app/sdk_app_i2c"><code>sdk_app_i2c</code></a> firmware!</p>
2024-03-29 16:55:46 +08:00
<h2 id="build-the-firmware"><a class="doc-anchor" href="#build-the-firmware">§</a>9.1 Build the firmware</h2>
2021-05-12 06:47:20 +08:00
<p>Download the Firmware Binary File <strong><code>sdk_app_i2c.bin</code></strong> from…</p>
2021-01-28 15:51:42 +08:00
<ul>
<li><a href="https://github.com/lupyuen/bl_iot_sdk/releases/tag/v2.0.0"><strong>Binary Release of <code>sdk_app_i2c</code></strong></a></li>
</ul>
2021-11-04 22:35:44 +08:00
<p>Alternatively, we may build the Firmware Binary File <code>sdk_app_i2c.bin</code> from the <a href="https://github.com/lupyuen/bl_iot_sdk/tree/master/customer_app/sdk_app_i2c">source code</a></p>
2023-01-13 07:19:30 +08:00
<div class="example-wrap"><pre class="language-bash"><code>## Download the master branch of lupyuen&#39;s bl_iot_sdk
2021-11-04 22:35:44 +08:00
git clone --recursive --branch master https://github.com/lupyuen/bl_iot_sdk
2021-01-26 15:19:37 +08:00
cd bl_iot_sdk/customer_app/sdk_app_i2c
2023-01-13 07:19:30 +08:00
## TODO: Change this to the full path of bl_iot_sdk
2021-01-26 15:19:37 +08:00
export BL60X_SDK_PATH=$HOME/bl_iot_sdk
export CONFIG_CHIP_NAME=BL602
make
2023-01-13 07:19:30 +08:00
## For WSL: Copy the firmware to /mnt/c/blflash, which refers to c:\blflash in Windows
2022-02-17 12:47:36 +08:00
mkdir /mnt/c/blflash
2024-12-04 20:02:50 +08:00
cp build_out/sdk_app_i2c.bin /mnt/c/blflash</code></pre></div>
2021-01-28 15:51:42 +08:00
<p><a href="https://lupyuen.github.io/articles/pinecone#building-firmware">More details on building bl_iot_sdk</a></p>
2024-03-29 16:55:46 +08:00
<h2 id="flash-the-firmware"><a class="doc-anchor" href="#flash-the-firmware">§</a>9.2 Flash the firmware</h2>
2021-05-12 06:47:20 +08:00
<p>Follow these steps to install <code>blflash</code></p>
2021-01-28 15:51:42 +08:00
<ol>
<li>
2021-05-12 06:47:20 +08:00
<p><a href="https://lupyuen.github.io/articles/flash#install-rustup"><strong>“Install rustup”</strong></a></p>
2021-01-28 15:51:42 +08:00
</li>
<li>
2021-05-12 06:47:20 +08:00
<p><a href="https://lupyuen.github.io/articles/flash#download-and-build-blflash"><strong>“Download and build blflash”</strong></a></p>
2021-01-28 15:51:42 +08:00
</li>
</ol>
<p>We assume that our Firmware Binary File <code>sdk_app_i2c.bin</code> has been copied to the <code>blflash</code> folder.</p>
2021-01-28 16:12:28 +08:00
<p>Set BL602 to <strong>Flashing Mode</strong> and restart the board.</p>
2021-02-21 08:30:35 +08:00
<p><strong>For PineCone:</strong></p>
<ol>
<li>
<p>Set the <strong>PineCone Jumper (IO 8)</strong> to the <strong><code>H</code> Position</strong> <a href="https://lupyuen.github.io/images/pinecone-jumperh.jpg">(Like this)</a></p>
</li>
<li>
<p>Press the Reset Button</p>
</li>
</ol>
<p><strong>For BL10:</strong></p>
<ol>
<li>
<p>Connect BL10 to the USB port</p>
</li>
<li>
<p>Press and hold the <strong>D8 Button (GPIO 8)</strong></p>
</li>
<li>
<p>Press and release the <strong>EN Button (Reset)</strong></p>
</li>
<li>
<p>Release the D8 Button</p>
</li>
</ol>
2022-09-30 12:21:43 +08:00
<p><strong>For <a href="https://docs.ai-thinker.com/en/wb2">Ai-Thinker Ai-WB2</a>, Pinenut and MagicHome BL602:</strong></p>
2021-02-21 08:30:35 +08:00
<ol>
<li>
2021-02-21 08:38:09 +08:00
<p>Disconnect the board from the USB Port</p>
2021-02-21 08:30:35 +08:00
</li>
<li>
<p>Connect <strong>GPIO 8</strong> to <strong>3.3V</strong></p>
</li>
<li>
2021-02-21 08:38:09 +08:00
<p>Reconnect the board to the USB port</p>
2021-02-21 08:30:35 +08:00
</li>
</ol>
2021-05-12 06:47:20 +08:00
<p>Enter these commands to flash <code>sdk_app_i2c.bin</code> to BL602 over UART…</p>
2023-01-13 07:19:30 +08:00
<div class="example-wrap"><pre class="language-bash"><code>## For Linux:
2022-02-17 12:47:36 +08:00
blflash flash build_out/sdk_app_i2c.bin \
2021-01-26 15:19:37 +08:00
--port /dev/ttyUSB0
2023-01-13 07:19:30 +08:00
## For macOS:
2022-02-17 12:47:36 +08:00
blflash flash build_out/sdk_app_i2c.bin \
2021-01-26 15:19:37 +08:00
--port /dev/tty.usbserial-1420 \
--initial-baud-rate 230400 \
--baud-rate 230400
2021-02-19 10:39:19 +08:00
2023-01-13 07:19:30 +08:00
## For Windows: Change COM5 to the BL602 Serial Port
2024-12-04 20:02:50 +08:00
blflash flash c:\blflash\sdk_app_i2c.bin --port COM5</code></pre></div>
2021-11-21 15:46:06 +08:00
<p>(For WSL: Do this under plain old Windows CMD, not WSL, because blflash needs to access the COM port)</p>
2021-01-28 15:51:42 +08:00
<p><a href="https://lupyuen.github.io/articles/flash#flash-the-firmware">More details on flashing firmware</a></p>
2024-03-29 16:55:46 +08:00
<h2 id="run-the-firmware"><a class="doc-anchor" href="#run-the-firmware">§</a>9.3 Run the firmware</h2>
2021-05-12 06:47:20 +08:00
<p>Set BL602 to <strong>Normal Mode</strong> (Non-Flashing) and restart the board…</p>
2021-02-21 08:30:35 +08:00
<p><strong>For PineCone:</strong></p>
<ol>
<li>
<p>Set the <strong>PineCone Jumper (IO 8)</strong> to the <strong><code>L</code> Position</strong> <a href="https://lupyuen.github.io/images/pinecone-jumperl.jpg">(Like this)</a></p>
</li>
<li>
<p>Press the Reset Button</p>
</li>
</ol>
<p><strong>For BL10:</strong></p>
<ol>
<li>Press and release the <strong>EN Button (Reset)</strong></li>
</ol>
2022-09-30 12:21:43 +08:00
<p><strong>For <a href="https://docs.ai-thinker.com/en/wb2">Ai-Thinker Ai-WB2</a>, Pinenut and MagicHome BL602:</strong></p>
2021-02-21 08:30:35 +08:00
<ol>
<li>
2021-02-21 08:38:09 +08:00
<p>Disconnect the board from the USB Port</p>
2021-02-21 08:30:35 +08:00
</li>
<li>
<p>Connect <strong>GPIO 8</strong> to <strong>GND</strong></p>
</li>
<li>
2021-02-21 08:38:09 +08:00
<p>Reconnect the board to the USB port</p>
2021-02-21 08:30:35 +08:00
</li>
</ol>
2021-05-12 06:47:20 +08:00
<p>After restarting, connect to BL602s UART Port at 2 Mbps like so…</p>
2021-02-08 15:37:18 +08:00
<p><strong>For Linux:</strong></p>
2024-12-04 20:02:50 +08:00
<div class="example-wrap"><pre class="language-bash"><code>screen /dev/ttyUSB0 2000000</code></pre></div>
2021-02-08 15:37:18 +08:00
<p><strong>For macOS:</strong> Use CoolTerm (<a href="https://lupyuen.github.io/articles/flash#watch-the-firmware-run">See this</a>)</p>
<p><strong>For Windows:</strong> Use <code>putty</code> (<a href="https://lupyuen.github.io/articles/flash#watch-the-firmware-run">See this</a>)</p>
2021-05-08 15:09:32 +08:00
<p><strong>Alternatively:</strong> Use the Web Serial Terminal (<a href="https://lupyuen.github.io/articles/flash#watch-the-firmware-run">See this</a>)</p>
2021-01-28 15:51:42 +08:00
<p><a href="https://lupyuen.github.io/articles/flash#watch-the-firmware-run">More details on connecting to BL602</a></p>
2024-03-29 16:55:46 +08:00
<h2 id="enter-i2c-commands"><a class="doc-anchor" href="#enter-i2c-commands">§</a>9.4 Enter I2C commands</h2>
2021-05-12 06:47:20 +08:00
<p>Lets enter some I2C commands to read our BME280 Sensor!</p>
2021-01-28 18:02:23 +08:00
<ol>
<li>
2021-01-28 16:09:20 +08:00
<p>Press Enter to reveal the command prompt.</p>
2021-01-28 18:02:23 +08:00
</li>
<li>
2021-05-12 06:47:20 +08:00
<p>Enter <code>help</code> to see the available commands…</p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-text"><code>help
2021-09-14 20:25:10 +08:00
====User Commands====
2021-01-26 13:35:54 +08:00
i2c_status : I2C status
i2c_init : Init I2C port
i2c_start_read : Start reading I2C data
2024-12-04 20:02:50 +08:00
i2c_stop_read : Stop reading I2C data</code></pre></div></li>
2021-01-28 18:02:23 +08:00
<li>
2024-09-11 15:14:35 +08:00
<p>First we <strong>initialise our I2C Port</strong>.</p>
2021-05-12 06:47:20 +08:00
<p>Enter this command…</p>
2024-12-04 20:02:50 +08:00
<div class="example-wrap"><pre class="language-text"><code>i2c_init</code></pre></div>
2021-05-12 06:47:20 +08:00
<p>(Earlier weve seen the code for this command)</p>
2021-01-28 18:02:23 +08:00
</li>
<li>
2021-05-12 06:47:20 +08:00
<p>Before doing any I2C business, lets <strong>dump the Interrupt Counters</strong> to see which I2C Interrupts get triggered…</p>
2024-12-04 20:02:50 +08:00
<div class="example-wrap"><pre class="language-text"><code>i2c_status</code></pre></div>
2021-05-12 06:47:20 +08:00
<p>We should see…</p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-text"><code>Interrupts: 0 NACK: 0
2021-01-28 18:02:23 +08:00
Trans End: 0 Arb Lost: 0
Tx Ready: 0 FIFO Error: 0
2024-12-04 20:02:50 +08:00
Rx Ready: 0 Unknown: 0</code></pre></div>
2021-01-28 18:02:23 +08:00
<p>Which means that no I2C Interrupts have been triggered yet.</p>
</li>
<li>
2021-05-12 06:47:20 +08:00
<p>Now we <strong>start the I2C Read Operation</strong></p>
2024-12-04 20:02:50 +08:00
<div class="example-wrap"><pre class="language-text"><code>i2c_start_read</code></pre></div>
2021-05-12 06:47:20 +08:00
<p>(Weve seen the code for this command as well)</p>
2021-01-28 18:02:23 +08:00
</li>
<li>
2021-05-12 06:47:20 +08:00
<p>Again we dump the Interrupt Counters…</p>
2024-12-04 20:02:50 +08:00
<div class="example-wrap"><pre class="language-text"><code>i2c_status</code></pre></div>
2021-05-12 06:47:20 +08:00
<p>Aha Something Different! We have encountered <strong>one interrupt for Data Received</strong> (Rx Ready), because BME280 has returned some I2C data to BL602…</p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-text"><code>Interrupts: 2 NACK: 0
2021-01-28 18:02:23 +08:00
Trans End: 1 Arb Lost: 0
Tx Ready: 0 FIFO Error: 0
2024-12-04 20:02:50 +08:00
Rx Ready: 1 Unknown: 0 </code></pre></div>
2021-01-28 18:02:23 +08:00
<p>After receiving the data (one byte) from BME280 (and saving it), our Interrupt Handler terminates the I2C connection.</p>
2021-05-12 06:47:20 +08:00
<p>Hence we see <strong>one interrupt for Transaction End</strong>. Were done!</p>
2021-01-28 18:02:23 +08:00
</li>
<li>
2021-05-12 06:47:20 +08:00
<p>To <strong>check the data received</strong>, enter this command…</p>
2024-12-04 20:02:50 +08:00
<div class="example-wrap"><pre class="language-text"><code>i2c_stop_read</code></pre></div>
2021-05-12 06:47:20 +08:00
<p>Remember that were reading the Chip ID from BME280. We should see this Chip ID…</p>
2024-12-04 20:02:50 +08:00
<div class="example-wrap"><pre class="language-text"><code>60</code></pre></div>
2021-01-28 18:02:23 +08:00
<p>(For BMP280 the Chip ID is <code>0x58</code>)</p>
</li>
</ol>
<p>Congratulations! We have successfully read the BME280 Sensor from BL602 over I2C!</p>
2024-03-29 16:55:46 +08:00
<h1 id="why-we-need-an-embedded-os-for-i2c"><a class="doc-anchor" href="#why-we-need-an-embedded-os-for-i2c">§</a>10 Why we need an Embedded OS for I2C</h1>
2021-05-12 06:47:20 +08:00
<p>We have 2 problems when calling the Low Level I2C HAL…</p>
2021-01-26 17:43:02 +08:00
<ol>
<li>
2021-05-12 06:47:20 +08:00
<p>Our program <strong>doesnt wait for I2C Read/Write Operations to complete.</strong></p>
<p>If we enter the command <code>i2c_stop_read</code> really quickly, it might <strong>terminate the I2C Read Operation before its done!</strong></p>
2021-01-26 21:38:51 +08:00
<p>(Assuming we can type at superhuman speed)</p>
2021-05-12 06:47:20 +08:00
<p>The I2C data transfer happens in the background, executed by the Interrupt Handler. The Foreground Task isnt notified when the data transfer is complete.</p>
2021-01-26 21:38:51 +08:00
<p><strong>Solution:</strong> Our Interrupt Handler should use a <strong>Semaphore or a Message Queue</strong> to notify the Foreground Task when the data transfer is done.</p>
2021-01-26 17:43:02 +08:00
</li>
<li>
2021-01-26 18:07:10 +08:00
<p>Our program uses <strong>shared variables for I2C Read/Write Operations.</strong></p>
<p>Remember these?</p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-c"><code>static i2c_msg_t *gpstmsg; // Global pointer to current I2C Message
2021-01-26 18:07:10 +08:00
static i2c_msg_t read_msg; // Message for reading I2C Data
2024-12-04 20:02:50 +08:00
static uint8_t read_buf[32]; // Buffer for reading I2C Data</code></pre></div>
2021-01-26 18:07:10 +08:00
<p>These global variables will get really confused when we talk to multiple I2C Sensors.</p>
<p>In fact, the entire <strong>I2C Port is a shared resource</strong>! It needs to be protected from overlapping I2C Operations.</p>
2021-01-27 08:26:35 +08:00
<p><strong>Solution:</strong> Our program should use a <strong>Semaphore or a Mutex Lock</strong> to prevent concurrent updates to the shared variables.</p>
2021-01-26 21:38:51 +08:00
<p>We could use a <strong>Message Queue to enqueue I2C Requests</strong> and execute the I2C Requests one at a time.</p>
2021-01-26 17:43:02 +08:00
</li>
</ol>
2024-03-29 16:55:46 +08:00
<h2 id="high-level-hal-unmasked"><a class="doc-anchor" href="#high-level-hal-unmasked">§</a>10.1 High Level HAL unmasked</h2>
2021-01-28 20:00:38 +08:00
<p><em>What happens when we implement the two Solutions in FreeRTOS?</em></p>
2021-11-04 22:35:44 +08:00
<p>When we implement these two Solutions in FreeRTOS… Well get the <strong>High Level I2C HAL!</strong> (See <a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/components/hal_drv/bl602_hal/hal_i2c.c"><code>hal_i2c.c</code></a>)</p>
2021-01-29 07:47:02 +08:00
<p>Hence the High Level I2C HAL (which calls FreeRTOS) is <strong>fully functional today</strong> for processing I2C Sensor Data.</p>
2021-05-12 06:47:20 +08:00
<p><em>But the High Level I2C HAL lacks documentation… How do we use it?</em></p>
2021-01-30 08:49:46 +08:00
<p>The code explained in this article looks highly similar to the High Level I2C HAL.</p>
2021-05-12 06:47:20 +08:00
<p>Heres the list of functions weve seen in this article, and their equivalent functions in the High Level I2C HAL…</p>
2022-03-01 17:38:23 +08:00
<div><table><thead><tr><th style="text-align: left">Function In <br> This Article</th><th style="text-align: left">Function In <br> High Level HAL</th></tr></thead><tbody>
<tr><td style="text-align: left"><a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/customer_app/sdk_app_i2c/sdk_app_i2c/demo.c#L343-L369"><code>test_i2c</code><br><code>_init</code></a></td><td style="text-align: left"><a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/components/hal_drv/bl602_hal/hal_i2c.c#L272-L298"><code>hal_i2c</code><br><code>_init</code></a></td></tr>
<tr><td style="text-align: left"><a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/customer_app/sdk_app_i2c/sdk_app_i2c/demo.c#L420-L448"><code>test_i2c</code><br><code>_start_read</code></a></td><td style="text-align: left"><a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/components/hal_drv/bl602_hal/hal_i2c.c#L300-L324"><code>hal_i2c</code><br><code>_read_block</code></a></td></tr>
<tr><td style="text-align: left"><a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/customer_app/sdk_app_i2c/sdk_app_i2c/demo.c#L273-L328"><code>test_i2c</code><br><code>_interrupt</code><br><code>_entry</code></a></td><td style="text-align: left"><a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/components/hal_drv/bl602_hal/hal_i2c.c#L97-L133"><code>i2c</code><br><code>_interrupt</code><br><code>_entry</code></a></td></tr>
<tr><td style="text-align: left"><a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/customer_app/sdk_app_i2c/sdk_app_i2c/demo.c#L249-L271"><code>test_i2c</code><br><code>_transferbytes</code></a></td><td style="text-align: left"><a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/components/hal_drv/bl602_hal/hal_i2c.c#L74-L95"><code>i2c</code><br><code>_transferbytes</code></a></td></tr>
<tr><td style="text-align: left"><a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/customer_app/sdk_app_i2c/sdk_app_i2c/demo.c#L236-L247"><code>test_i2c</code><br><code>_stop</code></a></td><td style="text-align: left"><a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/components/hal_drv/bl602_hal/hal_i2c.c#L52-L72"><code>i2c</code><br><code>_callback</code></a></td></tr>
2021-01-30 08:49:46 +08:00
</tbody></table>
2021-12-09 17:13:53 +08:00
</div>
2021-01-30 11:24:15 +08:00
<ul>
<li>
<p><a href="https://help.aliyun.com/document_detail/161064.html?spm=a2c4g.11186623.6.577.492c4564jOCOjU">See the Chinese docs for High Level I2C HAL</a></p>
</li>
<li>
2021-01-28 20:00:38 +08:00
<p><a href="https://github.com/bouffalolab/bl_iot_sdk/tree/master/customer_app/sdk_app_i2c">See the original (unmodified) High Level I2C HAL Demo</a></p>
2021-01-30 11:24:15 +08:00
</li>
</ul>
2024-03-29 16:55:46 +08:00
<h2 id="high-level-hal-without-freertos"><a class="doc-anchor" href="#high-level-hal-without-freertos">§</a>10.2 High Level HAL without FreeRTOS</h2>
2021-05-12 06:47:20 +08:00
<p><em>Instead of FreeRTOS… Can we implement the two Solutions with Mynewt, RIOT or Zephyr?</em></p>
2021-01-26 21:56:26 +08:00
<p>Yes! We may implement the two Solutions with any Embedded Operating System that supports <strong>Task Synchronisation</strong> features (Semaphore, Mutex, Message Queue).</p>
<p>Thus to do meaningful work with I2C (like reading I2C Sensor Data periodically and processing the data), we need to use the <strong>Low Level I2C HAL together with an Embedded Operating System</strong>.</p>
2021-01-28 20:00:38 +08:00
<p>The High Level I2C HAL is a great reference that guides us on the proper implementation of the two Solutions on any operating system.</p>
2021-01-27 07:32:37 +08:00
<p><img src="https://lupyuen.github.io/images/i2c-hack.jpg" alt="Hacking BL602 and BME280 on a Saturday night" /></p>
<p><em>Hacking BL602 and BME280 on a Saturday Night</em></p>
2024-03-29 16:55:46 +08:00
<h1 id="whats-next"><a class="doc-anchor" href="#whats-next">§</a>11 Whats Next</h1>
2021-05-12 06:47:20 +08:00
<p>Now that we understand the inner workings of I2C on BL602…</p>
2021-01-28 19:29:11 +08:00
<ol>
<li>
2021-05-12 06:47:20 +08:00
<p>Lets <strong>port BL602 I2C to Mynewt</strong> and complete the I2C implementation…</p>
2021-01-29 11:56:42 +08:00
<p><a href="https://lupyuen.github.io/articles/gpio">(Like we did for BL602 GPIO)</a></p>
2021-01-28 19:29:11 +08:00
</li>
<li>
2021-05-12 06:47:20 +08:00
<p>Also <strong>work on BL602 SPI</strong>! Check out the article…</p>
<p><a href="https://lupyuen.github.io/articles/spi"><strong>“PineCone BL602 talks SPI too”</strong></a></p>
<p><a href="https://twitter.com/MisterTechBlog/status/1354776244018057218?s=20">(I have received ST7789 SPI displays for testing… Many thanks to my Generous Sponsor! 😀)</a></p>
2021-01-28 19:29:11 +08:00
</li>
</ol>
2021-05-12 06:47:20 +08:00
<p>Theres plenty more code in the <a href="https://github.com/bouffalolab/bl_iot_sdk"><strong>BL602 IoT SDK</strong></a> to be deciphered and documented: <strong>ADC, DAC, WiFi, Bluetooth LE,</strong></p>
<p><a href="https://wiki.pine64.org/wiki/Nutcracker"><strong>Come Join Us… Make BL602 Better!</strong></a></p>
2021-01-28 19:14:02 +08:00
<p>🙏 👍 😀</p>
2021-01-25 12:35:16 +08:00
<ul>
<li>
2024-12-28 10:07:21 +08:00
<p><a href="https://lupyuen.github.io/articles/sponsor">Sponsor me a coffee</a></p>
2021-01-25 12:35:16 +08:00
</li>
<li>
2021-01-29 08:06:10 +08:00
<p><a href="https://www.reddit.com/r/embedded_oc/comments/l7d469/pinecone_bl602_talks_to_i2c_sensors/?utm_source=share&amp;utm_medium=web2x&amp;context=3">Discuss this article on Reddit</a></p>
</li>
<li>
2021-05-12 06:47:20 +08:00
<p><a href="https://lupyuen.github.io/articles/book">Read “The RISC-V BL602 Book”</a></p>
2021-02-21 09:41:09 +08:00
</li>
<li>
2021-01-25 12:35:16 +08:00
<p><a href="https://lupyuen.github.io">Check out my articles</a></p>
</li>
<li>
<p><a href="https://lupyuen.github.io/rss.xml">RSS Feed</a></p>
</li>
</ul>
2021-05-12 06:47:20 +08:00
<p><em>Got a question, comment or suggestion? Create an Issue or submit a Pull Request here…</em></p>
2021-01-25 12:35:16 +08:00
<p><a href="https://github.com/lupyuen/lupyuen.github.io/blob/master/src/i2c.md"><code>lupyuen.github.io/src/i2c.md</code></a></p>
2024-03-29 16:55:46 +08:00
<h1 id="notes"><a class="doc-anchor" href="#notes">§</a>12 Notes</h1>
2021-01-26 16:21:07 +08:00
<ol>
<li>
<p>Check out the <strong><a href="https://github.com/pine64/ArduinoCore-bouffalo/blob/main/libraries/Wire/src/Wire.cpp">BL602 I2C HAL for Arduino</a></strong></p>
</li>
<li>
2021-05-12 06:47:20 +08:00
<p>Why is BL602s <strong>I2C Register Address</strong> feature incompatible with Mynewt (and other embedded operating systems)?</p>
2021-01-27 07:32:37 +08:00
<p>Because Mynewt exposes an I2C API that <strong>controls the I2C Stop Bit explicitly</strong>. <a href="https://mynewt.apache.org/latest/os/modules/hal/hal_i2c/hal_i2c.html#c.hal_i2c_master_write">(See this <code>last_op</code> parameter)</a></p>
<p>When porting BL602 I2C to Mynewt, we need to reconcile the two styles of I2C coding: <strong>Register Address vs Stop Bit.</strong></p>
</li>
<li>
2021-05-12 06:47:20 +08:00
<p>We talked about reading I2C Registers… What about <strong>writing to I2C Registers</strong>?</p>
2021-11-04 22:35:44 +08:00
<p>The code should be similar. The demo program contains code for writing to I2C Registers, but it hasnt been tested. And it needs cleaning up. <a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/customer_app/sdk_app_i2c/sdk_app_i2c/demo.c#L376-L418">See this</a></p>
2021-01-29 07:47:02 +08:00
</li>
<li>
2021-05-12 06:47:20 +08:00
<p>Why arent we using <strong>DMA for I2C</strong>?</p>
2021-01-29 14:35:25 +08:00
<p>DMA for I2C (and SPI) sounds overkill for an IoT Gadget. We should keep the firmware simple and easy to maintain. (Until we have more maintainers)</p>
2021-05-12 06:47:20 +08:00
<p>Well come back later to implement DMA for I2C (and SPI) if we need to do any high-speed bulk data transfer.</p>
2021-01-29 14:35:25 +08:00
</li>
<li>
2021-05-12 06:47:20 +08:00
<p><strong>BL602 SPI</strong> doesnt have a Low Level HAL… It only comes as a High Level HAL with FreeRTOS. Which will be a challenging exploration. <a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/components/hal_drv/bl602_hal/hal_spi.c">See this</a></p>
2021-01-29 07:47:02 +08:00
</li>
<li>
2021-01-29 22:38:42 +08:00
<p>This article is the expanded version of <a href="https://twitter.com/MisterTechBlog/status/1352937390776545281?s=19"><strong>this Twitter Thread</strong></a></p>
</li>
<li>
2021-01-29 11:51:01 +08:00
<p><strong>Quiz for the Reader:</strong> What could go wrong with this code?</p>
2021-01-26 16:31:42 +08:00
<p><img src="https://lupyuen.github.io/images/i2c-init.png" alt="i2c_gpio_init: What happens when i2cx is NOT I2C0" /></p>
2021-05-12 06:47:20 +08:00
<p><a href="https://twitter.com/MisterTechBlog/status/1351441955637534720?s=20"><strong>Heres The Answer</strong></a></p>
2021-11-04 22:35:44 +08:00
<p>(From Low Level I2C HAL <a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/components/hal_drv/bl602_hal/bl_i2c.c"><code>bl_i2c.c</code></a>)</p>
2021-01-26 16:31:42 +08:00
</li>
<li>
2021-01-29 11:51:01 +08:00
<p><strong>Another Quiz for the Reader:</strong> Why does this code look dubious?</p>
2021-01-26 16:31:42 +08:00
<p><img src="https://lupyuen.github.io/images/i2c-transferbytes.png" alt="i2c_transferbytes: Assignment inside Condition" /></p>
2021-05-12 06:47:20 +08:00
<p><a href="https://github.com/bouffalolab/bl_iot_sdk/issues/33"><strong>Heres The Answer</strong></a></p>
2021-11-04 22:35:44 +08:00
<p>(From High Level I2C HAL <a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/components/hal_drv/bl602_hal/hal_i2c.c"><code>hal_i2c.c</code></a>)</p>
2021-01-26 16:21:07 +08:00
</li>
</ol>
2021-01-28 18:48:21 +08:00
<p><img src="https://lupyuen.github.io/images/i2c-cartoon8.png" alt="Bug" /></p>
2024-03-29 16:55:46 +08:00
<h1 id="appendix-how-to-troubleshoot-risc-v-exceptions"><a class="doc-anchor" href="#appendix-how-to-troubleshoot-risc-v-exceptions">§</a>13 Appendix: How to Troubleshoot RISC-V Exceptions</h1>
2021-05-12 06:47:20 +08:00
<p>Heres how I tracked down my first RISC-V Exception and fixed it…</p>
2021-01-28 18:48:21 +08:00
<p><img src="https://lupyuen.github.io/images/i2c-exception.png" alt="RISC-V Exception in sdk_app_i2c" /></p>
2021-11-04 22:35:44 +08:00
<p>When our program <a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/customer_app/sdk_app_i2c/"><code>sdk_app_i2c</code></a> is sending I2C data, the program crashes with the RISC-V Exception shown above…</p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-text"><code>start_write_data
2021-09-14 20:25:10 +08:00
Exception Entry---&gt;&gt;&gt;
2021-01-28 18:48:21 +08:00
mcause 30000007, mepc 23008fe2, mtval 00000014
Exception code: 7
2024-12-04 20:02:50 +08:00
msg: Store/AMO access fault</code></pre></div>
2021-01-28 18:48:21 +08:00
<p>What does this mean?</p>
<ul>
<li>
<p><strong><code>mcause</code> (Machine Cause Register)</strong>: Tells us the reason for the exception. <a href="http://www.five-embeddev.com/riscv-isa-manual/latest/machine.html#sec:mcause">More details</a></p>
<p>The Exception Code is 7 (Store/AMO Access Fault), which means that we have accessed an invalid memory address.</p>
<p>(Probably a bad pointer)</p>
<p><a href="http://www.five-embeddev.com/riscv-isa-manual/latest/machine.html#sec:mcause">List of RISC-V Exception Codes</a></p>
</li>
<li>
<p><strong><code>mepc</code> (Machine Exception Program Counter)</strong>: The address of the code that caused the exception. <a href="http://www.five-embeddev.com/riscv-isa-manual/latest/machine.html#machine-exception-program-counter-mepc">More details</a></p>
2021-05-12 06:47:20 +08:00
<p>Well look up the code address <code>0x2300 8fe2</code> in a while.</p>
2021-01-28 18:48:21 +08:00
</li>
<li>
<p><strong><code>mtval</code> (Machine Trap Value Register)</strong>: The invalid address that was accessed. <a href="http://www.five-embeddev.com/riscv-isa-manual/latest/machine.html#machine-trap-value-register-mtval">More details</a></p>
<p>Our program attempted to access the invalid address <code>0x000 00014</code> and crashed.</p>
<p>Looks like a null pointer problem!</p>
</li>
</ul>
2021-05-12 06:47:20 +08:00
<p>Lets track down code address <code>0x2300 8fe2</code> and find out why it caused the exception…</p>
2021-01-28 18:48:21 +08:00
<p><img src="https://lupyuen.github.io/images/i2c-disassembly.png" alt="RISC-V Disassembly" /></p>
<ol>
<li>
<p>According to the RISC-V Disassembly <a href="https://github.com/lupyuen/bl_iot_sdk/releases/download/v1.0.1/sdk_app_i2c.S"><code>sdk_app_i2c.S</code></a>, the code address <code>0x2300 8fe2</code> is located in the I2C Interrupt Handler of the BL602 I2C HAL (See pic)</p>
<ul>
2021-11-04 22:35:44 +08:00
<li><a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/components/hal_drv/bl602_hal/hal_i2c.c#L97-L133"><code>i2c_interrupt_entry</code> in <code>hal_i2c.c</code></a></li>
2021-01-28 18:48:21 +08:00
</ul>
</li>
<li>
<p>Why did it crash? Because the Interrupt Context <code>ctx</code> is null!</p>
2021-05-12 06:47:20 +08:00
<p>In fact, the I2C Interrupt Handler <code>i2c_interrupt_entry</code> shouldnt have been called.</p>
2021-11-04 22:35:44 +08:00
<p>It comes from the High Level HAL <a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/components/hal_drv/bl602_hal/hal_i2c.c"><code>hal_i2c.c</code></a>, but were actually using the Low Level HAL <a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/components/hal_drv/bl602_hal/bl_i2c.c"><code>bl_i2c.c</code></a>.</p>
2021-01-28 18:48:21 +08:00
</li>
<li>
<p>Why was <code>i2c_interrupt_entry</code> set as the I2C Interrupt Handler?</p>
2021-05-12 06:47:20 +08:00
<p>Because <code>hal_i2c_init</code> was called here…</p>
2021-01-28 18:48:21 +08:00
<ul>
2021-11-04 22:35:44 +08:00
<li><a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/customer_app/sdk_app_i2c/sdk_app_i2c/main.c#L159-L199"><code>aos_loop_proc</code> in <code>main.c</code></a></li>
2021-01-28 18:48:21 +08:00
</ul>
</li>
</ol>
<p><img src="https://lupyuen.github.io/images/i2c-inithal.png" alt="I2C Init HAL" /></p>
<p>After commenting out <code>hal_i2c_init</code>, the program no longer uses <code>i2c_interrupt_entry</code> as the I2C Interrupt Handler.</p>
<p>And no more crashing!</p>
2021-05-12 06:47:20 +08:00
<p>For more RISC-V Exception troubleshooting tips, check out <strong>BL602 Stack Trace and BL602 Stack Dump</strong></p>
2021-04-04 17:12:42 +08:00
<ul>
<li>
2021-05-12 06:47:20 +08:00
<p><a href="https://lupyuen.github.io/articles/lora2#bl602-stack-trace"><strong>“BL602 Stack Trace”</strong></a></p>
2021-04-04 17:12:42 +08:00
</li>
<li>
2021-05-12 06:47:20 +08:00
<p><a href="https://lupyuen.github.io/articles/lora2#bl602-stack-dump"><strong>“BL602 Stack Dump”</strong></a></p>
2021-04-04 17:12:42 +08:00
</li>
2021-04-05 22:52:55 +08:00
<li>
2021-05-12 06:47:20 +08:00
<p><a href="https://lupyuen.github.io/articles/lora2#bl602-assertion-failures"><strong>“BL602 Assertion Failures”</strong></a></p>
2021-04-05 22:52:55 +08:00
</li>
2021-04-04 17:12:42 +08:00
</ul>
2021-01-28 18:48:21 +08:00
<p><em>How did we get the RISC-V Disassembly?</em></p>
2021-05-12 06:47:20 +08:00
<p>We generate RISC-V Disassembly <code>sdk_app_i2c.S</code> from ELF Executable <code>sdk_app_i2c.elf</code> with this command…</p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-bash"><code>riscv-none-embed-objdump \
2021-01-28 18:48:21 +08:00
-t -S --demangle --line-numbers --wide \
sdk_app_i2c.elf \
&gt;sdk_app_i2c.S \
2024-12-04 20:02:50 +08:00
2&gt;&amp;1</code></pre></div>
2021-01-29 09:29:31 +08:00
<p><em>Is it safe to comment out <code>hal_i2c_init</code>?</em></p>
2021-11-04 22:35:44 +08:00
<p>Not quite. When we comment out <code>hal_i2c_init</code>, we disable the High Level I2C HAL functions in our demo firmware <a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/customer_app/sdk_app_i2c/"><code>sdk_app_i2c</code></a></p>
2021-05-12 06:47:20 +08:00
<p>Thats the reason why we havent merged the <code>i2c</code> branch to the <code>master</code> branch…</p>
2021-01-29 09:29:31 +08:00
<ul>
<li>
2021-11-04 22:35:44 +08:00
<p><a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/customer_app/sdk_app_i2c/"><strong><code>i2c</code> Branch</strong></a> is used for testing Low Level I2C HAL</p>
2021-01-29 09:29:31 +08:00
</li>
<li>
<p><a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/customer_app/sdk_app_i2c/"><strong><code>master</code> Branch</strong></a> is used for testing High Level I2C HAL</p>
</li>
</ul>
<p>(The proper fix is to create a new command that calls <code>hal_i2c_init</code>)</p>
2021-01-30 00:58:28 +08:00
<p><em>What are the <code>aos</code> functions in the code above?</em></p>
<p>The <code>aos</code> functions are defined in <a href="https://github.com/alibaba/AliOS-Things">AliOS</a>. Remember that the High Level I2C HAL is called by AliOS Firmware.</p>
2021-01-27 18:15:04 +08:00
<p><img src="https://lupyuen.github.io/images/i2c-cartoon7.png" alt="Bus Pirate" /></p>
2024-03-29 16:55:46 +08:00
<h1 id="appendix-test-bme280-with-bus-pirate"><a class="doc-anchor" href="#appendix-test-bme280-with-bus-pirate">§</a>14 Appendix: Test BME280 with Bus Pirate</h1>
2021-01-27 22:33:29 +08:00
<p><a href="http://dangerousprototypes.com/docs/Bus_Pirate"><strong>Bus Pirate</strong></a> is a useful gadget for verifying whether our BME280 Sensor works OK. And for checking the I2C bytes that should be sent down the wire to BME280.</p>
<p><a href="http://dangerousprototypes.com/docs/I2C">(Bus Pirate also works as a simple Protocol Analyser for sniffing I2C data)</a></p>
2021-05-12 06:47:20 +08:00
<p>Heres how we test BME280 (or BMP280) with Bus Pirate…</p>
2021-01-27 22:33:29 +08:00
<p><img src="https://lupyuen.github.io/images/i2c-buspirate.jpg" alt="Bus Pirate connected to BME280" /></p>
<ol>
<li>
2021-05-12 06:47:20 +08:00
<p>Connect Bus Pirate to BME280 (or BMP280) according to the pic above…</p>
2022-03-01 17:38:23 +08:00
<div><table><thead><tr><th style="text-align: center">Bus Pirate Pin</th><th style="text-align: center">BME280 Pin</th></tr></thead><tbody>
<tr><td style="text-align: center"><strong><code>MOSI</code></strong></td><td style="text-align: center"><code>SDA</code></td></tr>
<tr><td style="text-align: center"><strong><code>CLK</code></strong></td><td style="text-align: center"><code>SCL</code></td></tr>
<tr><td style="text-align: center"><strong><code>3.3V</code></strong></td><td style="text-align: center"><code>3.3V</code></td></tr>
<tr><td style="text-align: center"><strong><code>GND</code></strong></td><td style="text-align: center"><code>GND</code></td></tr>
2021-01-27 17:36:30 +08:00
</tbody></table>
2021-12-09 17:13:53 +08:00
</div></li>
2021-01-27 17:36:30 +08:00
<li>
2021-05-12 06:47:20 +08:00
<p>Connect Bus Pirate to our computers USB port.</p>
2021-01-27 22:33:29 +08:00
<p>Open a Serial Terminal for Bus Pirate.</p>
2021-01-27 17:36:30 +08:00
</li>
<li>
2021-01-27 22:33:29 +08:00
<p>Enter <strong><code>m</code></strong> for the menu</p>
<p>Select <strong><code>I2C</code></strong></p>
<p><img src="https://lupyuen.github.io/images/i2c-buspirate2.png" alt="Bus Pirate Menu" /></p>
2021-01-27 17:36:30 +08:00
</li>
2021-01-27 22:33:29 +08:00
<li>
<p>Select <strong><code>Hardware</code></strong></p>
<p>Select <strong><code>400 kbps</code></strong></p>
<p><img src="https://lupyuen.github.io/images/i2c-buspirate3.png" alt="I2C Speed" /></p>
</li>
<li>
<p>Enter <strong><code>W</code></strong> to power up BME280</p>
<p><img src="https://lupyuen.github.io/images/i2c-buspirate4.png" alt="Power up BME280" /></p>
</li>
<li>
<p>Enter <strong><code>(1)</code></strong> to scan the I2C Bus</p>
<p><img src="https://lupyuen.github.io/images/i2c-buspirate5.png" alt="Scan I2C bus" /></p>
</li>
<li>
<p>Here we see that BME280 has been detected at I2C Address <code>0x77</code></p>
2021-05-12 06:47:20 +08:00
<p>I2C uses the even / odd address convention to indicate whether were writing or reading data. So our BME280 at address <code>0x77</code> appears as two Read / Write aliases…</p>
2021-01-27 17:36:30 +08:00
<ul>
<li>
2021-01-27 22:33:29 +08:00
<p><strong><code>0xEE</code></strong> = (<code>0x77</code> * 2) + 0, for Writing Data</p>
2021-01-27 17:36:30 +08:00
</li>
<li>
2021-01-27 22:33:29 +08:00
<p><strong><code>0xEF</code></strong> = (<code>0x77</code> * 2) + 1, for Reading Data</p>
2021-01-27 17:36:30 +08:00
</li>
</ul>
2021-01-27 22:33:29 +08:00
</li>
<li>
2021-05-12 06:47:20 +08:00
<p>To read Register <code>0xD0</code> (Chip ID) from BME280, enter this command…</p>
2024-12-04 20:02:50 +08:00
<div class="example-wrap"><pre class="language-text"><code>[0xee 0xd0] [0xef r]</code></pre></div>
2021-01-27 22:33:29 +08:00
<p>(More about this later)</p>
</li>
<li>
<p>We should see the result <code>0x60</code>, which is the Chip ID for BME280</p>
<p><img src="https://lupyuen.github.io/images/i2c-buspirate6.png" alt="Read register 0xD0" /></p>
2021-01-28 18:02:23 +08:00
<p>(For BMP280 the Chip ID is <code>0x58</code>)</p>
2021-01-27 22:33:29 +08:00
</li>
</ol>
2021-05-12 06:47:20 +08:00
<p>We tested BME280 with this Bus Pirate I2C command…</p>
2024-12-04 20:02:50 +08:00
<div class="example-wrap"><pre class="language-text"><code> [0xee 0xd0] [0xef r]</code></pre></div>
2021-01-27 22:33:29 +08:00
<p>This means that Bus Pirate will initiate two I2C Transactions, indicated by <strong><code>[ ... ]</code></strong></p>
<ol>
<li>
2024-09-11 15:14:35 +08:00
<p><strong>In the First I2C Transaction:</strong> Bus Pirate sends <strong><code>0xEE</code></strong> to indicate a Write Transaction (for address <code>0x77</code>).</p>
2021-01-27 22:33:29 +08:00
<p>Then it sends the I2C Register to be read: <strong><code>0xD0</code></strong> (Chip ID)</p>
</li>
<li>
2024-09-11 15:14:35 +08:00
<p><strong>In the Second I2C Transaction:</strong> Bus Pirate sends <strong><code>0xEF</code></strong> to indicate a Read Transaction (for address <code>0x77</code>).</p>
2021-01-27 22:33:29 +08:00
<p>BME280 returns the value of the Chip ID Register, indicated by <strong><code>r</code></strong></p>
</li>
</ol>
<p>To sum up: Bus Pirate initiates two <code>[ ... ]</code> transactions. The transactions will send 3 bytes (<code>0xEE</code>, <code>0xD0</code>, <code>0xEF</code>) and receive 1 byte (<code>0x60</code>).</p>
2021-05-12 06:47:20 +08:00
<p>This is identical to the I2C data transmitted by BL602 to BME280 that have seen earlier in the article…</p>
2021-09-26 16:32:42 +08:00
<div class="example-wrap"><pre class="language-text"><code> [Start] 0xEE 0xD0 [Stop]
2021-01-27 22:33:29 +08:00
2024-12-04 20:02:50 +08:00
[Start] 0xEF [Read] [Stop]</code></pre></div>
2021-01-27 22:33:29 +08:00
<p>For help on other Bus Pirate commands, enter <strong><code>?</code></strong></p>
<p><img src="https://lupyuen.github.io/images/i2c-buspirate1.png" alt="Bus Pirate Help" /></p>
<p><a href="http://dangerousprototypes.com/docs/I2C">Check out the I2C Guide for Bus Pirate</a></p>
2021-01-27 11:10:19 +08:00
<p><img src="https://lupyuen.github.io/images/i2c-sketch.jpg" alt="Sketching I2C cartoons" /></p>
2021-01-27 13:14:07 +08:00
<p><em>Sketching I2C cartoons. <a href="https://github.com/lupyuen/lupyuen.github.io/releases/tag/v1.0.0">Download the Photoshop images</a></em></p>
2021-01-25 12:35:16 +08:00
2024-02-17 20:10:07 +08:00
<!-- Begin scripts/rustdoc-after.html: Post-HTML for Custom Markdown files processed by rustdoc, like chip8.md -->
<!-- Begin Theme Picker and Prism Theme -->
<script src="../theme.js"></script>
<script src="../prism.js"></script>
<!-- Theme Picker and Prism Theme -->
<!-- End scripts/rustdoc-after.html -->
2021-01-25 12:35:16 +08:00
</body>
</html>