#include #include #include #include #include #include /******************************************************************************/ /*! User interface functions */ #define LOG 1 #ifdef LOG void print_i2c_data(const unsigned char *data, size_t len) { size_t i = 0; for (i = 0; i < len; i++) { if (i % 16 == 0) { printf("\n"); } printf("0x%02x ", data[i]); } printf("\n"); } #endif /*! * I2C read function map to COINES platform */ BMI2_INTF_RETURN_TYPE bmi2_i2c_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, void *intf_ptr) { #ifdef LOG printf("reading: reg_addr=0x%02x, len=%d\n",reg_addr,len); #endif BMI2_INTF_RETURN_TYPE ret = i2c_read(intf_ptr, reg_addr, reg_data, (size_t)len) != len; #ifdef LOG printf("data="); print_i2c_data(reg_data,len); #endif return ret; } /*! * I2C write function map to COINES platform */ BMI2_INTF_RETURN_TYPE bmi2_i2c_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len, void *intf_ptr) { #ifdef LOG printf("writing: reg_addr=0x%02x, len=%d\n",reg_addr,len); #endif BMI2_INTF_RETURN_TYPE ret = i2c_write(intf_ptr, reg_addr, reg_data, (size_t)len) != len; #ifdef LOG printf("data="); print_i2c_data(reg_data,len); #endif return ret; } /*! * Delay function map to posix api */ void bmi2_delay_us(uint32_t period, void *intf_ptr) { usleep(period); } /*! * @brief Function to initialize i2c interface */ int8_t bmi2_interface_init(struct bmi2_dev *bmi) { int8_t rslt = BMI2_OK; if (bmi != NULL) { int bus; /* Open i2c bus /dev/i2c-0 */ if ((bus = i2c_open("/dev/i2c-0")) == -1) { printf("could not open /dev/i2c-0"); exit(1); } static struct i2c_device device; memset(&device, 0, sizeof(device)); device.bus = bus; /* Bus 0 */ device.addr = 0x68; /* Slave address is 0x68, 7-bit */ device.iaddr_bytes = 1; /* Device internal address is 1 byte */ device.page_bytes = 2048; /* Device are capable of 2048 bytes per page */ /* To initialize the user I2C function */ bmi->intf = BMI2_I2C_INTF; bmi->read = bmi2_i2c_read; bmi->write = bmi2_i2c_write; /* Assign I2CDevice to interface pointer */ bmi->intf_ptr = &device; /* Configure delay in microseconds */ bmi->delay_us = bmi2_delay_us; /* Configure max read/write length (in bytes) ( Supported length depends on target machine) */ bmi->read_write_len = 8192; /* Assign to NULL to load the default config file. */ bmi->config_file_ptr = NULL; } else { rslt = BMI2_E_NULL_PTR; } return rslt; } /*! * @brief Prints the execution status of the APIs. */ void bmi2_error_codes_print_result(int8_t rslt) { switch (rslt) { case BMI2_OK: /* Do nothing */ break; case BMI2_W_FIFO_EMPTY: printf("Warning [%d] : FIFO empty\r\n", rslt); break; case BMI2_W_PARTIAL_READ: printf("Warning [%d] : FIFO partial read\r\n", rslt); break; case BMI2_E_NULL_PTR: printf( "Error [%d] : Null pointer error. It occurs when the user tries to assign value (not address) to a pointer," " which has been initialized to NULL.\r\n", rslt); break; case BMI2_E_COM_FAIL: printf( "Error [%d] : Communication failure error. It occurs due to read/write operation failure and also due " "to power failure during communication\r\n", rslt); break; case BMI2_E_DEV_NOT_FOUND: printf("Error [%d] : Device not found error. It occurs when the device chip id is incorrectly read\r\n", rslt); break; case BMI2_E_INVALID_SENSOR: printf( "Error [%d] : Invalid sensor error. It occurs when there is a mismatch in the requested feature with the " "available one\r\n", rslt); break; case BMI2_E_SELF_TEST_FAIL: printf( "Error [%d] : Self-test failed error. It occurs when the validation of accel self-test data is " "not satisfied\r\n", rslt); break; case BMI2_E_INVALID_INT_PIN: printf( "Error [%d] : Invalid interrupt pin error. It occurs when the user tries to configure interrupt pins " "apart from INT1 and INT2\r\n", rslt); break; case BMI2_E_OUT_OF_RANGE: printf( "Error [%d] : Out of range error. It occurs when the data exceeds from filtered or unfiltered data from " "fifo and also when the range exceeds the maximum range for accel and gyro while performing FOC\r\n", rslt); break; case BMI2_E_ACC_INVALID_CFG: printf( "Error [%d] : Invalid Accel configuration error. It occurs when there is an error in accel configuration" " register which could be one among range, BW or filter performance in reg address 0x40\r\n", rslt); break; case BMI2_E_GYRO_INVALID_CFG: printf( "Error [%d] : Invalid Gyro configuration error. It occurs when there is a error in gyro configuration" "register which could be one among range, BW or filter performance in reg address 0x42\r\n", rslt); break; case BMI2_E_ACC_GYR_INVALID_CFG: printf( "Error [%d] : Invalid Accel-Gyro configuration error. It occurs when there is a error in accel and gyro" " configuration registers which could be one among range, BW or filter performance in reg address 0x40 " "and 0x42\r\n", rslt); break; case BMI2_E_CONFIG_LOAD: printf( "Error [%d] : Configuration load error. It occurs when failure observed while loading the configuration " "into the sensor\r\n", rslt); break; case BMI2_E_INVALID_PAGE: printf( "Error [%d] : Invalid page error. It occurs due to failure in writing the correct feature configuration " "from selected page\r\n", rslt); break; case BMI2_E_SET_APS_FAIL: printf( "Error [%d] : APS failure error. It occurs due to failure in write of advance power mode configuration " "register\r\n", rslt); break; case BMI2_E_AUX_INVALID_CFG: printf( "Error [%d] : Invalid AUX configuration error. It occurs when the auxiliary interface settings are not " "enabled properly\r\n", rslt); break; case BMI2_E_AUX_BUSY: printf( "Error [%d] : AUX busy error. It occurs when the auxiliary interface buses are engaged while configuring" " the AUX\r\n", rslt); break; case BMI2_E_REMAP_ERROR: printf( "Error [%d] : Remap error. It occurs due to failure in assigning the remap axes data for all the axes " "after change in axis position\r\n", rslt); break; case BMI2_E_GYR_USER_GAIN_UPD_FAIL: printf( "Error [%d] : Gyro user gain update fail error. It occurs when the reading of user gain update status " "fails\r\n", rslt); break; case BMI2_E_SELF_TEST_NOT_DONE: printf( "Error [%d] : Self-test not done error. It occurs when the self-test process is ongoing or not " "completed\r\n", rslt); break; case BMI2_E_INVALID_INPUT: printf("Error [%d] : Invalid input error. It occurs when the sensor input validity fails\r\n", rslt); break; case BMI2_E_INVALID_STATUS: printf("Error [%d] : Invalid status error. It occurs when the feature/sensor validity fails\r\n", rslt); break; case BMI2_E_CRT_ERROR: printf("Error [%d] : CRT error. It occurs when the CRT test has failed\r\n", rslt); break; case BMI2_E_ST_ALREADY_RUNNING: printf( "Error [%d] : Self-test already running error. It occurs when the self-test is already running and " "another has been initiated\r\n", rslt); break; case BMI2_E_CRT_READY_FOR_DL_FAIL_ABORT: printf( "Error [%d] : CRT ready for download fail abort error. It occurs when download in CRT fails due to wrong " "address location\r\n", rslt); break; case BMI2_E_DL_ERROR: printf( "Error [%d] : Download error. It occurs when write length exceeds that of the maximum burst length\r\n", rslt); break; case BMI2_E_PRECON_ERROR: printf( "Error [%d] : Pre-conditional error. It occurs when precondition to start the feature was not " "completed\r\n", rslt); break; case BMI2_E_ABORT_ERROR: printf("Error [%d] : Abort error. It occurs when the device was shaken during CRT test\r\n", rslt); break; case BMI2_E_WRITE_CYCLE_ONGOING: printf( "Error [%d] : Write cycle ongoing error. It occurs when the write cycle is already running and another " "has been initiated\r\n", rslt); break; case BMI2_E_ST_NOT_RUNING: printf( "Error [%d] : Self-test is not running error. It occurs when self-test running is disabled while it's " "running\r\n", rslt); break; case BMI2_E_DATA_RDY_INT_FAILED: printf( "Error [%d] : Data ready interrupt error. It occurs when the sample count exceeds the FOC sample limit " "and data ready status is not updated\r\n", rslt); break; case BMI2_E_INVALID_FOC_POSITION: printf( "Error [%d] : Invalid FOC position error. It occurs when average FOC data is obtained for the wrong" " axes\r\n", rslt); break; default: printf("Error [%d] : Unknown error code\r\n", rslt); break; } } /*! * @brief Deinitializes i2c interface * * @return void. */ void bmi2_interface_deinit(struct bmi2_dev *bma) { struct i2c_device* device = (bma->intf_ptr); i2c_close(device->bus); }