I2C Hall Effect Sensor Demo
BGX I2C API
Starting with firmware version
1.2.2045.0, BGX is capable of I2C
communication. This vastly broadens the number and type of devices the BGX can
interact with. I2C can be used to communicate to EEPROMs, ADCs, DACs, sensors,
and much more. Types of sensors include temperature, proximity, hall effect,
etc. Common applications for I2C can range from changing the sound volume in
smart speakers to reading sensors. It is a simple protocol to use and the BGX
makes it even easier with its I2C API. The BGX I2C API consists of only one
variable and two commands. This demo will make use of the BGX I2C API and
display the ease of communicating with an I2C device.
This demo includes a BGX board, a hall effect sensor, and a magnet. The BGX is wired up to the hall effect sensor to enable I2C communication. Bringing the magnet near the sensor will send a notification to the user on BGX Commander.
The demo requires a Wireless Xpress BGX13P Starter Kit, a Si7210 Magnetic Hall Effect Sensor, and a magnet.
Wireless Xpress BGX13P Starter Kit
This Wireless Xpress Starter Kit includes the Bluetooth Xpress BGX13P module and serves as a starting point to build applications for BGX. The BGX demo makes use of the starter kit's expansion header allowing for communication between the two starter kits. More guidance on this starter kit can be found in the User Guide, the BGX13P Data Sheet, and the BGX Development App Note.
Si7210 Magnetic Hall Effect Sensor & Magnet
This demo uses the Si7210-B00V-EB I2C Postage Stamp Board and a magnet. This board and magnet is part of the Si72xx Magnetic Sensor Development Kit. The postage stamp board contains a magnetic hall effect sensor that can send magnetic field data to the BGX via I2C protocol. The BGX can control and configure the sensor to specialize it for its own needs. This makes the sensor an ideal replacement for both switch/latch and linear output magnetic sensors. More information of the Si7210 sensor can be found in the data sheet.
To set up the demo, follow these 6 steps:
- Wired Connections
- User Functions
- Button Debouncing
- Power Management
- Save Configuration
Connect the following BGX board pins to the Si7210 pins. BGX allows for any GPIO to be assigned to any signal. However, SDA, SCL, and output are on specific BGX GPIO pins due to the BGX EXP board's design. The board's hardware doesn't allow, for example, SCL to be configured to GPIO7 because GPIO7 is tied to the board's CP2102N DTR pin. DTR is an output so it will drive that line high and the I2C will not function. If SCL is put on GPIO4 and SDA on GPIO5 and you have an EFM8 or EFM32 board, then you can connect them via expansion header and run the I2C slave example from Simplicity Studio. Note that BGX firmware does not restrict these pin assignments - it depends on the hardware.
|Signal||BGX Pin||Si7210 Pin|
For a pin to be a trigger for a user function, it must be set to a ufu_level with the gfu command. Here is how to set GPIO6 to ufu_level. (Uses gfu)
> gfu 6 ufu_level Success
Now it is time to make a list of all the functions that will be called. (Uses ufu)
Set "send Magnet\r\n" to user function 0.
> ufu 0 send "Magnet!\r\n" Success
Set "send No magnet\r\n" to user function 1.
> ufu 1 send "No magnet!\r\n" Success
Finally, it is time to map user functions and GPIOs together. This is also where the event trigger is configured. (Uses uevt)
Assign GPIO6 to trigger user function 0 when the magnet is nearby. The I2C step will configure the sensor to drive the output low when the sensor detects a magnet. Therefore, the event triggers when the input to GPIO6 goes low.
> uevt 0 lo 6 Success
Assign GPIO6 to trigger user function 1 when the magnet is far. The I2C step will configure the sensor to drive the output high when the sensor does not detect any magnet. Therefore, the event triggers when the input to GPIO6 goes high.
> uevt 1 hi 6 Success
It's important to debounce the buttons so that bounces don't occur while moving the magnet. The pin reading the hall effect sensor's output needs to be debounced for ~20 ms. (Uses gdi)
> gdi 6 inw db1 Success
The BGX has a default baud rate of 115200. In Active Mode, the UART cannot be disabled and is always on. This isn't a power consumption problem because BGX has a low power mode that is activated when the baud rate is set to 9600 or lower. Active mode current consumption is greatly reduced by reducing the baud rate to 9600. (Uses ua b and uartu)
> set ua b 9600 Success > uartu Success
The goal of this section will be to change the I2C sensor output to return low when the magnet is nearby, instead of high. This can be done by using two new GPIO functions configurable via the gfu command: i2cm_sda and i2cm_scl. Due to the board design, I2C SCL will be assigned to GPIO4 and SDA to GPIO5. The i2crd and i2cwr commands will fail if either i2cm_scl or i2cm_sda is not assigned to a pin. (Uses gfu)
> gfu 4 i2cm_scl Success > gfu 5 i2cm_sda Success
Before performing I2C read and writes, the I2C Master bus speed can be increased. The speed of the I2C bus can be configured by setting the variable i2 b s. First, get the current I2C bus speed. Then change the speed to the max value allowed (1000 kHz). (Uses i2 b s)
> get i2 b s 92 > set i2 b s 1000 Success
The BGX can communicate with any I2C device using the i2crd and i2cwr commands. The address read or written is determined by the I2C device. In particular, its register definitions. The Si7210 sensor provides this information in its data sheet. The relevant register definitions are shown below.
Si7210 Register Definitions
Change the sensor output so that it will return low when the magnet is nearby, instead of high. This should be done with a read-modify-write to prevent changing the sw_op bits. First read the address containing the sw_low4field bit. sw_low4field selects the logic sense for the magnetic field. (Uses i2crd)
> i2crd 60 c6 1 30
The address 0xC6 contains the data 0x30 (0b00110000) which means the sw_low4field bit is not set. Output is low when the field is strong when the bit is set. Therefore, set the sw_low4field bit. This can be done by writing 0xB0 (0b10110000) (Uses i2cwr)
> i2cwr 60 c6 B0 Success
The last 2 steps to configure the sensor include saving the state of the sw_low4field bit and starting the magnetic measurement loop back up. Both steps can be done by modifying address 0xC4. Read the register containing the Usestore and stop bit. (Uses i2crd)
> i2crd 60 c4 1 02
0x02 means the stop bit is set and Userstore is not set. Setting the Usestore bit causes the current state of the sw_low4field bit to be saved and restored during the next sleep and wakeup sequence. This is essentially a save of the bit until the part is hard reset. Clearing the stop bit will restart the magnetic measurement loop. (Uses i2cwr)
> i2cwr 60 c4 08 Success
Finally, the entire configuration above should be saved to prevent needing to set up all the variables again. Otherwise, a reset would bring the BGX back to a default state. All the commands have been provided below to allow an easy copy and paste to a serial terminal. (Uses save)
> save Success