This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nRF 52 TWI in LSM9DS1

Hello,

I want to test the IKS01A1 (STM) board with the STEVAL-MKI159v1 board by placing the IKS board in the Arduino connectors on the nRF52 (PCA10056) DK board.

I want to use this driver:

https://github.com/STMicroelectronics/STMems_Standard_C_drivers/tree/master/lsm9ds1_STdC

By writing the platform_write and platform_read functions with the nRF SDK, the way the HAL_I2C_Mem_Read function is written. platform_write and platform_read is In this source: 

https://github.com/STMicroelectronics/STMems_Standard_C_drivers/blob/master/lsm9ds1_STdC/example/read_data_simple.c

From the nRF SDK I only know the writing and reading functions on the TWI from the bus, but there is no function that would write/read in the register of  TWI slave.

I wonder if there is anything like that? And what is the procedure? For HAL_I2C_Mem_Read / Write functions, this is written:

Write an amount of data in blocking mode to a specific memory address using HAL_I2C_Mem_Write ()

(+) Read a number of data in a blocking mode from a specific memory address using HAL_I2C_Mem_Read ()

the functions are, for example, here: 

https://github.com/PaxInstruments/STM32CubeF4/blob/master/Drivers/BSP/STM32469I-Discovery/stm32469i_discovery.c

Im asking for help.

I use: Segger Studio, nRF52 DK

  • I modified the functions platform_read and platform_write from read_data_simple.c (from github) like this:

    static int32_t platform_write(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len){
    
        uint8_t *i2c_address = handle;
    
        ret_code_t err_code = nrf_drv_twi_tx(&m_twi, *i2c_address, &reg, len, false);
        APP_ERROR_CHECK(err_code);
        while (m_xfer_done == false);
    
        m_xfer_done = false;
        /* Read 1 byte from the specified address - skip 3 bits dedicated for fractional part of temperature. */
        err_code = nrf_drv_twi_rx(&m_twi, *i2c_address, bufp, len);
        APP_ERROR_CHECK(err_code);
    
      return 0;
    }

    static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len){
      uint8_t *i2c_address = handle;
    
        m_xfer_done = false;
    
        /* Read 1 byte from the specified address - skip 3 bits dedicated for fractional part of temperature. */
        ret_code_t err_code = nrf_drv_twi_rx(&m_twi, *i2c_address, &reg, len);
        APP_ERROR_CHECK(err_code);
    
        m_xfer_done = false;
    
        /* Read 1 byte from the specified address - skip 3 bits dedicated for fractional part of temperature. */
        err_code = nrf_drv_twi_rx(&m_twi, *i2c_address, bufp, len);
        APP_ERROR_CHECK(err_code);
    
      return 0;
    }

    my TWI init is: 

    void twi_init (void)
    {
        ret_code_t err_code;
    
        const nrf_drv_twi_config_t twi_lm75b_config = {
           .scl                = ARDUINO_SCL_PIN,
           .sda                = ARDUINO_SDA_PIN,
           .frequency          = NRF_DRV_TWI_FREQ_100K,
           .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
           .clear_bus_init     = false
        };
    
        err_code = nrf_drv_twi_init(&m_twi, &twi_lm75b_config, NULL, NULL);
        APP_ERROR_CHECK(err_code);
    
        nrf_drv_twi_enable(&m_twi);
    }

    in my main is the first lines is: 

        APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
        NRF_LOG_DEFAULT_BACKENDS_INIT();
    
        NRF_LOG_INFO("\r\nTWI mems example started.");
        NRF_LOG_FLUSH();
        twi_init();
    
    /* Initialize inertial sensors (IMU) driver interface */
      uint8_t i2c_add_mag = LSM9DS1_MAG_I2C_ADD_L;
      lsm9ds1_ctx_t dev_ctx_mag;
      dev_ctx_mag.write_reg = platform_write;
      dev_ctx_mag.read_reg = platform_read;
      dev_ctx_mag.handle = (void*)&i2c_add_mag;
    
      /* Initialize magnetic sensors driver interface */
      uint8_t i2c_add_imu = LSM9DS1_IMU_I2C_ADD_H;
      lsm9ds1_ctx_t dev_ctx_imu;
      dev_ctx_imu.write_reg = platform_write;
      dev_ctx_imu.read_reg = platform_read;
      dev_ctx_imu.handle = (void*)&i2c_add_imu;

    other main function is equal like read_data_simple.c

    my problem: is that I get NRF_BREAKPOINT_COND on 100th line from app_error_weak.c with NRF_LOG this message: <error> app: Fatal error

  • Hi,

    Have you looked at our SDK example: nRF5_SDK_15.3.0_59ac345\examples\peripheral\twi_master_with_twis_slave?

  • I solved the problems, as you can see in in the attached code.

    github.com/.../nRF52-DK-and-LSM9DS1

    For further work we need to include the header file from:

    github.com/.../driver

    the attached example comed from this source:

    https://github.com/STMicroelectronics/STMems_Standard_C_drivers/tree/master/lsm9ds1_STdC/example

    Note for Segger studio: 

    printf should be enabled in: right mouse click on project > Options > choose Common > in search box type "float" > change Printf Floating Point Supported from No to float.
Related