/** * Copyright (C) 2022 Bosch Sensortec GmbH. All rights reserved. * * SPDX-License-Identifier: BSD-3-Clause */ #include #include #include #include "bma4_defs.h" #include "common.h" #include #include #include #include #include #include /******************************************************************************/ /*! Macro definitions */ /*! BMA4xy shuttle board ID */ #define BMA4XY_SHUTTLE_ID UINT16_C(0x141) /*! Read write length varies based on user requirement */ #define BMA4_READ_WRITE_LEN UINT8_C(46) #define I2C0_NODE DT_NODELABEL(mysensor) static const struct i2c_dt_spec dev_i2c = I2C_DT_SPEC_GET(I2C0_NODE); /******************************************************************************/ /*! Static variable definition */ /*! Variable that holds the I2C device address or SPI chip selection */ static uint8_t dev_addr; /******************************************************************************/ /*! User interface functions */ int8_t rslt; /*! * I2C read function map to nRF52832 platform */ BMA4_INTF_RET_TYPE bma4_i2c_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, void *intf_ptr) { //add i2c function for nrf52832 uint8_t buffer[] = {reg_addr, *reg_data}; rslt = i2c_write_read_dt(&dev_i2c, &buffer[0],sizeof(buffer[0]), reg_data,sizeof(reg_data)); if(rslt != 0){ printk("Failed to write/read I2C device address %x at Reg. %x \r\n", dev_i2c.addr,buffer[0]); } return rslt; } /*! * I2C write function map to nRF52832 platform */ BMA4_INTF_RET_TYPE bma4_i2c_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len, void *intf_ptr) { //add i2c function for nrf52832 uint8_t buffer[] = {reg_addr, *reg_data}; rslt= i2c_burst_write_dt(&dev_i2c, buffer[0], *reg_data, len); if(rslt != 0){ printk("Failed to write I2C device address %x at Reg. %x \r\n", dev_i2c.addr,buffer[0]); } return rslt; } // BMA4_INTF_RET_TYPE bma4_spi_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, void *intf_ptr) // { // uint8_t dev_address = *(uint8_t*)intf_ptr; // (void)intf_ptr; // return coines_read_spi(COINES_SPI_BUS_0, dev_address, reg_addr, reg_data, (uint16_t)len); // } // BMA4_INTF_RET_TYPE bma4_spi_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len, void *intf_ptr) // { // uint8_t dev_address = *(uint8_t*)intf_ptr; // (void)intf_ptr; // return coines_write_spi(COINES_SPI_BUS_0, dev_address, reg_addr, (uint8_t *)reg_data, (uint16_t)len); // } void bma4_delay_us(uint32_t period, void *intf_ptr){ k_msleep(period); return; } /*! * @brief Function to select the interface between SPI and I2C. * Also to initialize coines platform */ int8_t bma4_interface_init(struct bma4_dev *bma, uint8_t intf, enum bma4_variant variant) { int8_t rslt = BMA4_OK; if (bma != NULL) { /* Bus configuration : I2C */ if (intf == BMA4_I2C_INTF) { printf("I2C Interface \n"); //added code to initialize i2c bus on nRF52832 to BMA456 static const struct i2c_dt_spec dev_i2c = I2C_DT_SPEC_GET(I2C0_NODE); if (!device_is_ready(dev_i2c.bus)) { printk("I2C bus %s is not ready!\n\r",dev_i2c.bus->name); } /* To initialize the user I2C function */ dev_addr = BMA4_I2C_ADDR_SECONDARY; bma->intf = BMA4_I2C_INTF; bma->bus_read = bma4_i2c_read; bma->bus_write = bma4_i2c_write; } // /* Bus configuration : SPI */ // else if (intf == BMA4_SPI_INTF) // { // printf("SPI Interface \n"); // /* To initialize the user SPI function */ // dev_addr = COINES_SHUTTLE_PIN_7; // bma->intf = BMA4_SPI_INTF; // bma->bus_read = bma4_spi_read; // bma->bus_write = bma4_spi_write; // } /* Assign variant information */ bma->variant = variant; /* Assign device address to interface pointer */ bma->intf_ptr = &dev_addr; /* Configure delay in microseconds */ bma->delay_us = bma4_delay_us; /* Configure max read/write length (in bytes) ( Supported length depends on target machine) */ bma->read_write_len = BMA4_READ_WRITE_LEN; /* Set Performance mode status */ bma->perf_mode_status = BMA4_DISABLE; k_msleep(100); k_msleep(200); } else { rslt = BMA4_E_NULL_PTR; } return rslt; } /*! * @brief Prints the execution status of the APIs. */ void bma4_error_codes_print_result(const char api_name[], int8_t rslt) { if (rslt != BMA4_OK) { printf("%s\t", api_name); if (rslt == BMA4_E_NULL_PTR) { printf("Error [%d] : Null pointer\r\n", rslt); } else if (rslt == BMA4_E_COM_FAIL) { printf("Error [%d] : Communication failure\r\n", rslt); } else if (rslt == BMA4_E_CONFIG_STREAM_ERROR) { printf("Error [%d] : Invalid configuration stream\r\n", rslt); } else if (rslt == BMA4_E_SELF_TEST_FAIL) { printf("Error [%d] : Self test failed\r\n", rslt); } else if (rslt == BMA4_E_INVALID_SENSOR) { printf("Error [%d] : Device not found\r\n", rslt); } else if (rslt == BMA4_E_OUT_OF_RANGE) { printf("Error [%d] : Out of Range\r\n", rslt); } else if (rslt == BMA4_E_AVG_MODE_INVALID_CONF) { printf("Error [%d] : Invalid bandwidth and ODR combination in Accel Averaging mode\r\n", rslt); } else { /* For more error codes refer "*_defs.h" */ printf("Error [%d] : Unknown error code\r\n", rslt); } } }