Search code examples
bluetooth-lowenergysensorsi2c

I2C communication BGM121 with onboard Si7021 on SLWSTK6101C (BGScript)


i'm working with the Silicon Laboratories BGM121 Bluetooth Low Energy Module and the SLWSTK6101C Starter Kit. On this kit is an RHT sensor (Si7021) which is connected to i2c on the board itself and through this to the module.

With the sample projects i've flashed directly without compiling via Simplicity Studio v4 it's working fine and i get appropiate values.

The Problem: I have to use the SiLabs BGScript language to programm the module. I can't do it with C cause GCC isn't supported yet and i don't have an IAR license.

I want to read out sensor data (temperature) and want to send it to an android app via BLE. The BLE part isn't the problem and i build the services/characteristics like they are described in the adopted services on bluetooth.org . I tried to use the BGScript examples from SiLabs to test if it works. But even the official SDK BGScript examples provided from the manufacturer won't work.

Here is my configuration and my code:

<!-- I2C configuration -->
<!-- Settings: SCL pin is PC11 and SDA pin is PC10 -->
<i2c scl_pin="PC11" sda_pin="PC10"/>

I2C-Communication code:

export dim i2c_result
export dim i2c_len
export dim i2c_buffer(4)
export dim temperature(5)
export dim timeout
export dim data
dim result

const sensor_slave_addr = $40

export procedure sensorRead()

    call led1_on()


    call hardware_write_i2c(0,sensor_slave_addr,1,$f3)

    i2c_result = 1
    timeout = 0
    while (i2c_result != 0) &&  (timeout < 50)
        call hardware_read_i2c(0,sensor_slave_addr,2)(i2c_result,i2c_len,i2c_buffer(0:i2c_len))
        timeout = timeout + 1
        if i2c_result = 0 then
            call led1_off()
        end if
    end while

    call hardware_stop_i2c(0)

    if(timeout < 50) then
        # Check that the I2C was read properly and the while loop didn't 
        # end because of the timeout.
        # Measurement from the I2C read is in big endian format and must be 
        # converted to little-endian by swapping bytes.
        data = i2c_buffer(0:1) << 8 | i2c_buffer(1:1)
        #call led1_off()
    else
        data = $25
    end if


    #Flags field -> 0: °C , 1: °F
    temperature(0:1)=$00
    temperature(1:4)=float(data*1757/65536-469, -1)



    call gatt_server_write_attribute_value(temperature_char, 0, 5, temperature(0:5))(result)
    call gatt_server_send_characteristic_notification($FF, temperature_char, 5, temperature(0:5))(result)

    end

The calls to the led procedures are just for testing if it starts/ends i2c communication.

I found out that i never get an success on "i2c_result". So it doesn't get the values of the sensor. But i can't see why.

Any ideas?

Thanks in advance!


Solution

  • It has been solved. SiLabs support gave me the hint that they released the new SDK v2.1 a few days ago. In this updated SDK there are the working projects.

    But you can run the old projects to with the working sensor.

    The BGM121 does not provide a fixed powersource to the sensor as other boards do. You have to set the "power-supply-pin" to logic 1 for powering the sensor. Other modules have linked the sensor staticly to logic 1.

    It's done by setting an additional GPIO in the hardware.xml file.

    <gpio port="D" pin="9" mode="pushpull" out="1" />
    

    Or setting it programmatically with a call to

    call hardware_configure_gpio(3, 9, hardware_gpio_mode_push_pull, 1)
    

    That solved everything. Now it's possible to use the sensor.