How to Enable I2C on the OKdo E1 Board Using the NXP MCUXpresso IDE

How to Enable I2C on the OKdo E1 Board Using the NXP MCUXpresso IDE

The OKdo E1 board is a small but powerful development board that can be used in various applications, including industrial IoT, building control and automation, consumer electronics, and more. When getting started with the E1 board, it’s important to know what features it possesses to get the most out of the board. 

I2C — a popular two-wire serial communication protocol that’s commonly used for exchanging information between low-speed devices and peripherals in embedded projects — can be enabled on the E1 board using the MCUXpresso IDE toolchain from NXP Semiconductors. 

In this article, learn how to enable I2C by allowing the LPC55S69 MCU on the E1 board to communicate with a Bosch BME280 environmental sensor present on the Microe Weather click module. 

I2C: A Favorable Serial Communication Protocol 

I2C is a popular two-wire serial communication protocol commonly used to exchange information between low-speed devices and peripherals in embedded projects. It allows multiple follower (formerly “slave”) devices to communicate with one or more leaders (formerly “master”) in a network over a short distance. 

Each I2C bus consists of two signals: SDA (data) and SCL (clock). The currently active leader device generates the clock. A follower device can pull the clock low to signal the leader that it should wait. Because I2C drivers are open-drain, each data line requires a pull-up resistor to restore the signal to high when no device is pulling it low.

When using I2C for communicating information, the signaling must adhere to a specific format for other devices to understand it. Messages are split into two types: an address frame and one or more data frames. With the address frame, the leader device identifies the follower with which it wants to communicate. The data frames are where data is exchanged. To initiate a transmission, the leader leaves SCL high and pulls SDA low (the start condition).

The start condition is then followed by an address frame, which must be transmitted in every new communication round. The address is followed by a read/write bit — the ninth bit is the ACK/NACK bit, indicating whether the transmission was successful. The acknowledgment is present after every frame that’s transmitted. After that, data can be placed on the SDA line by the device in control. Once all the data frames have been sent, the leader generates the stop condition.

No alt text provided for this image

On the E1 board, the I2C signals are located on the pads named PIO1_20 (SCL) and PIO1_21 (SDA):

No alt text provided for this image

Using MCUXpresso Tools to Enable I2C on the LPC55S69 MCU

Allowing the MCU to communicate with the aforementioned environmental sensor requires the creation of a new project.

In the new project, the clocks configuration tool — discussed in a previous article and video — is used to create a new functional group called ‘BOARD_BootClockPLL150M_FRO12’. The PLL is also configured to be referenced from the clock named fro_12m. The fro_12m clock source is used to enable the FLEXCOMM_4 clock to serve as the I2C clock source:

No alt text provided for this image

Once the clock configuration is complete, use the following button to switch to the Pins view within the MCUXpresso IDE:

No alt text provided for this image

This tool allows the user to select which of the pad functions is brought out to a pin. In this case, the two pads PIO1_20 and PIO1_21 are configured for their I2C functions (SCL and SDA). Utilize the search bar to show the desired GPIO pad only:

No alt text provided for this image

A checkbox is shown in the first column of the table that lists the results. Clicking the box makes it possible to select the desired function for that pin:

No alt text provided for this image

Once a selection is made in the pop-up window, the pins configuration tool routes the pad, and it shows up as a new entry in the ‘Routed Pins’ view at the bottom of the perspective.

At the end of this step, pin 4 (pad GPIO1_20) should be routed to FC4_TXD_SCL_MISO_WS and the assigned function for GPIO1_21 should be FC4_SDA:

No alt text provided for this image

With these configuration steps complete, the peripherals icon can be used to open the IDE’s peripherals perspective:

No alt text provided for this image

The tool identifies that the FlexComm4 pins are configured to be used as I2C. However, the I2C peripheral hasn’t been set up yet, so the tool reports a warning. The FC4 peripheral can be initialized by right-clicking the FlexComm4-related sign in the problems view:

No alt text provided for this image

Selecting this option opens a dialog box where the desired function for FlexComm4 (which is I2C in this scenario) can be selected:

No alt text provided for this image

Upon selecting the proposed fix, the “Flexcomm Interface I2C configuration” window pops up, allowing access to the top-level settings for the I2C module. In this example, the only option to change is the mode. Because the E1 board will be a leader device, “Transfer” is the appropriate option for this project:

No alt text provided for this image

his concludes all of the necessary configuration steps to enable I2C on the E1 board. Upon clicking the “Update Code” button at the top of the IDE, the software generates all the necessary configuration code.

For a summary of the configuration steps, check out the video MCUXpresso Peripheral Config Tutorial: I2C with OKdo E1 Board

A Look at the BME280 Sensor

Once the setup is complete, it’s time to begin reading and writing to the BME280 sensor. In total, there are 55 registers on the BME280. Bosh provides the sensor’s calibration, making it easy to convert the raw sensor data to calibrated values. Most of the device’s registers contain that calibration data. Besides that, the BME280 contains a few control registers, and, of course, registers that hold the measured values.

No alt text provided for this image

Before the sensor can be used, it must be configured by writing to the control registers. Then, a conversion can be triggered. In this project, the conversion is triggered by setting the first bit (Bit 0) in the ctrl_meas register at address 0xF4. It takes the BME280 several milliseconds to convert the analog sensor values into digital data, and the sensor sets a measuring bit during this time.

As previously mentioned, the follower address must transmit first, followed by the R/W bit. After the BME280 acknowledges the byte, the register address transmits (0xF4 in this example). After another ACK, the data to be written is sent (0x25 in this case). This causes the BME280 to switch into forced mode.

Reading the BME280 Environmental Sensor

The I2C SDK driver function I2C_MasterTransferNonBlocking handles reading the environmental sensor. It takes three parameters:

  1. The base address of the I2C peripheral (implemented in FLEXCOMM4_PERIPHERAL)
  2. The address of the I2C configuration handle (created at line 601)
  3. The address of the transfer structure (called masterTransfer)

The user code should define and populate the transfer structure, which happens in lines 395-402. Requesting data from the sensor works similarly.

The code for this project can be downloaded here. A more detailed explanation of the code is also available on the embeddedpro YouTube channel.

After reading the data and calibrating the result according to the Bosh double-precision float algorithm (see the BME280 datasheet, appendix A), the E1 board should output something similar to the text in the following screenshot:

No alt text provided for this image

Summary

The OKdo E1 board is a low-cost development board that packs many complex features into a small footprint PCB. The small form-factor and the many features make it a complex board. However, the board’s standard toolchain — the Eclipse-based MCUXpresso IDE — makes working with the E1 board and other NXP MCU-enabled development boards an easy to learn project. 

The IDE comes with all of the needed features for developing embedded applications. The required tools are integrated into a single IDE and switching between different applications is not required. With this powerful setup, the configuration of the MCU’s pins, the board’s clocks, and the peripherals and drivers can be done in no time. The quick configuration keeps embedded developers from wasting their time fighting with complicated software, focusing instead on productive work.

The NXP MCUXpresso IDE toolchain can be applied to many embedded projects. More information and examples based on MCUXpresso IDE and Software Development Kit (SDK) can be found at the MCUXpresso product pageNXP’s community site also offers an abundance of useful information, including application notes, tutorials, videos, and more.

by Mark Dunnett, on Behalf of NXP

要查看或添加评论,请登录

王思辉的更多文章

社区洞察

其他会员也浏览了