VL6180x API
This commit is contained in:
2769
src/vl6180x_api.c
Normal file
2769
src/vl6180x_api.c
Normal file
File diff suppressed because it is too large
Load Diff
54
src/vl6180x_glue.c
Normal file
54
src/vl6180x_glue.c
Normal file
@ -0,0 +1,54 @@
|
||||
#include "vl6180x_glue.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <linux/i2c-dev.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
extern "C" {
|
||||
#include <i2c/smbus.h>
|
||||
}
|
||||
|
||||
#include <ros/ros.h>
|
||||
|
||||
#include "vl6180x_i2c.h"
|
||||
|
||||
static int i2c_dev_file;
|
||||
|
||||
void VL6180x_glue_init(const char *i2c_dev_path) {
|
||||
i2c_dev_file = open(i2c_dev_path, O_RDWR);
|
||||
if (i2c_dev_file < 0) {
|
||||
ROS_FATAL("Couldn't open %s: %s", i2c_dev_path, strerror(errno));
|
||||
return;
|
||||
}
|
||||
|
||||
if (ioctl(i2c_dev_file, I2C_SLAVE, VL6180X_I2C_ADDR) < 0) {
|
||||
close(i2c_dev_file);
|
||||
ROS_FATAL("Couldn't set slave address: %s", strerror(errno));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
int VL6180x_I2CWrite(VL6180xDev_t dev, uint8_t *buff, uint8_t len) {
|
||||
int ret = write(i2c_dev_file, buff, len);
|
||||
if (ret == len) {
|
||||
return 0;
|
||||
} else if (ret < 0) {
|
||||
ROS_ERROR("Error while writing to I2C device: %s", strerror(errno));
|
||||
return -1;
|
||||
} else {
|
||||
ROS_ERROR("Only wrote %d bytes (of %d requested)", ret, len);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int VL6180x_I2CRead(VL6180xDev_t dev, uint8_t *buff, uint8_t len) {
|
||||
int ret = read(i2c_dev_file, buff, len);
|
||||
if (ret == len) {
|
||||
return 0;
|
||||
} else if (ret < 0) {
|
||||
ROS_ERROR("Error while reading from I2C device: %s", strerror(errno));
|
||||
} else {
|
||||
ROS_ERROR("Only read %d bytes (of %d requested)", ret, len);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
245
src/vl6180x_i2c.c
Normal file
245
src/vl6180x_i2c.c
Normal file
@ -0,0 +1,245 @@
|
||||
|
||||
/*******************************************************************************
|
||||
Copyright <20> 2014, STMicroelectronics International N.V.
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of STMicroelectronics nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
|
||||
NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS ARE DISCLAIMED.
|
||||
IN NO EVENT SHALL STMICROELECTRONICS INTERNATIONAL N.V. BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
********************************************************************************/
|
||||
/*
|
||||
* $Date: 2015-07-06 15:44:31 +0200 (Mon, 06 Jul 2015) $
|
||||
* $Revision: 2430 $
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file vl6180x_i2c.c
|
||||
*
|
||||
* Copyright (C) 2014 ST MicroElectronics
|
||||
*
|
||||
* provide variable word size byte/Word/dword VL6180x register access via i2c
|
||||
*
|
||||
*/
|
||||
#include "vl6180x_i2c.h"
|
||||
|
||||
#ifndef I2C_BUFFER_CONFIG
|
||||
#error "I2C_BUFFER_CONFIG not defined"
|
||||
/* TODO you must define value for I2C_BUFFER_CONFIG in configuration or platform h */
|
||||
#endif
|
||||
|
||||
|
||||
#if I2C_BUFFER_CONFIG == 0
|
||||
/* GLOBAL config buffer */
|
||||
uint8_t i2c_global_buffer[VL6180x_MAX_I2C_XFER_SIZE];
|
||||
|
||||
#define DECL_I2C_BUFFER
|
||||
#define VL6180x_GetI2cBuffer(dev, n_byte) i2c_global_buffer
|
||||
|
||||
#elif I2C_BUFFER_CONFIG == 1
|
||||
/* ON STACK */
|
||||
#define DECL_I2C_BUFFER uint8_t LocBuffer[VL6180x_MAX_I2C_XFER_SIZE];
|
||||
#define VL6180x_GetI2cBuffer(dev, n_byte) LocBuffer
|
||||
#elif I2C_BUFFER_CONFIG == 2
|
||||
/* user define buffer type declare DECL_I2C_BUFFER as access via VL6180x_GetI2cBuffer */
|
||||
#define DECL_I2C_BUFFER
|
||||
#else
|
||||
#error "invalid I2C_BUFFER_CONFIG "
|
||||
#endif
|
||||
|
||||
|
||||
int VL6180x_WrByte(VL6180xDev_t dev, uint16_t index, uint8_t data){
|
||||
int status;
|
||||
uint8_t *buffer;
|
||||
DECL_I2C_BUFFER
|
||||
VL6180x_I2C_USER_VAR
|
||||
|
||||
VL6180x_GetI2CAccess(dev);
|
||||
|
||||
buffer=VL6180x_GetI2cBuffer(dev,3);
|
||||
buffer[0]=index>>8;
|
||||
buffer[1]=index&0xFF;
|
||||
buffer[2]=data;
|
||||
|
||||
status=VL6180x_I2CWrite(dev, buffer,(uint8_t)3);
|
||||
VL6180x_DoneI2CAcces(dev);
|
||||
return status;
|
||||
}
|
||||
|
||||
int VL6180x_WrWord(VL6180xDev_t dev, uint16_t index, uint16_t data){
|
||||
int status;
|
||||
DECL_I2C_BUFFER
|
||||
uint8_t *buffer;
|
||||
VL6180x_I2C_USER_VAR
|
||||
|
||||
VL6180x_GetI2CAccess(dev);
|
||||
|
||||
buffer=VL6180x_GetI2cBuffer(dev,4);
|
||||
buffer[0]=index>>8;
|
||||
buffer[1]=index&0xFF;
|
||||
buffer[2]=data>>8;
|
||||
buffer[3]=data&0xFF;
|
||||
|
||||
status=VL6180x_I2CWrite(dev, buffer,(uint8_t)4);
|
||||
VL6180x_DoneI2CAcces(dev);
|
||||
return status;
|
||||
}
|
||||
|
||||
int VL6180x_WrDWord(VL6180xDev_t dev, uint16_t index, uint32_t data){
|
||||
VL6180x_I2C_USER_VAR
|
||||
DECL_I2C_BUFFER
|
||||
int status;
|
||||
uint8_t *buffer;
|
||||
|
||||
|
||||
VL6180x_GetI2CAccess(dev);
|
||||
buffer=VL6180x_GetI2cBuffer(dev,6);
|
||||
buffer[0]=index>>8;
|
||||
buffer[1]=index&0xFF;
|
||||
buffer[2]=data>>24;
|
||||
buffer[3]=(data>>16)&0xFF;
|
||||
buffer[4]=(data>>8)&0xFF;;
|
||||
buffer[5]=data&0xFF;
|
||||
status=VL6180x_I2CWrite(dev, buffer,(uint8_t)6);
|
||||
VL6180x_DoneI2CAcces(dev);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
int VL6180x_UpdateByte(VL6180xDev_t dev, uint16_t index, uint8_t AndData, uint8_t OrData){
|
||||
VL6180x_I2C_USER_VAR
|
||||
int status;
|
||||
uint8_t *buffer;
|
||||
DECL_I2C_BUFFER
|
||||
|
||||
VL6180x_GetI2CAccess(dev);
|
||||
|
||||
buffer=VL6180x_GetI2cBuffer(dev,3);
|
||||
buffer[0]=index>>8;
|
||||
buffer[1]=index&0xFF;
|
||||
|
||||
status=VL6180x_I2CWrite(dev, (uint8_t *)buffer,(uint8_t)2);
|
||||
if( !status ){
|
||||
/* read data direct onto buffer */
|
||||
status=VL6180x_I2CRead(dev, &buffer[2],1);
|
||||
if( !status ){
|
||||
buffer[2]=(buffer[2]&AndData)|OrData;
|
||||
status=VL6180x_I2CWrite(dev, buffer, (uint8_t)3);
|
||||
}
|
||||
}
|
||||
|
||||
VL6180x_DoneI2CAcces(dev);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
int VL6180x_RdByte(VL6180xDev_t dev, uint16_t index, uint8_t *data){
|
||||
VL6180x_I2C_USER_VAR
|
||||
int status;
|
||||
uint8_t *buffer;
|
||||
DECL_I2C_BUFFER
|
||||
|
||||
VL6180x_GetI2CAccess(dev);
|
||||
|
||||
buffer=VL6180x_GetI2cBuffer(dev,2);
|
||||
buffer[0]=index>>8;
|
||||
buffer[1]=index&0xFF;
|
||||
|
||||
status=VL6180x_I2CWrite(dev, buffer, (uint8_t)2);
|
||||
if( !status ){
|
||||
status=VL6180x_I2CRead(dev, buffer,1);
|
||||
if( !status ){
|
||||
*data=buffer[0];
|
||||
}
|
||||
}
|
||||
VL6180x_DoneI2CAcces(dev);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
int VL6180x_RdWord(VL6180xDev_t dev, uint16_t index, uint16_t *data){
|
||||
VL6180x_I2C_USER_VAR
|
||||
int status;
|
||||
uint8_t *buffer;
|
||||
DECL_I2C_BUFFER
|
||||
|
||||
VL6180x_GetI2CAccess(dev);
|
||||
|
||||
buffer=VL6180x_GetI2cBuffer(dev,2);
|
||||
buffer[0]=index>>8;
|
||||
buffer[1]=index&0xFF;
|
||||
|
||||
status=VL6180x_I2CWrite(dev, buffer, (uint8_t)2);
|
||||
if( !status){
|
||||
status=VL6180x_I2CRead(dev, buffer,2);
|
||||
if( !status ){
|
||||
/* VL6180x register are Big endian if cpu is be direct read direct into *data is possible */
|
||||
*data=((uint16_t)buffer[0]<<8)|(uint16_t)buffer[1];
|
||||
}
|
||||
}
|
||||
VL6180x_DoneI2CAcces(dev);
|
||||
return status;
|
||||
}
|
||||
|
||||
int VL6180x_RdDWord(VL6180xDev_t dev, uint16_t index, uint32_t *data){
|
||||
VL6180x_I2C_USER_VAR
|
||||
int status;
|
||||
uint8_t *buffer;
|
||||
DECL_I2C_BUFFER
|
||||
|
||||
VL6180x_GetI2CAccess(dev);
|
||||
buffer=VL6180x_GetI2cBuffer(dev,4);
|
||||
|
||||
buffer[0]=index>>8;
|
||||
buffer[1]=index&0xFF;
|
||||
|
||||
status=VL6180x_I2CWrite(dev, (uint8_t *) buffer, (uint8_t)2);
|
||||
if( !status ){
|
||||
status=VL6180x_I2CRead(dev, buffer,4);
|
||||
if( !status ){
|
||||
/* VL6180x register are Big endian if cpu is be direct read direct into data is possible */
|
||||
*data=((uint32_t)buffer[0]<<24)|((uint32_t)buffer[1]<<16)|((uint32_t)buffer[2]<<8)|((uint32_t)buffer[3]);
|
||||
}
|
||||
}
|
||||
VL6180x_DoneI2CAcces(dev);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
int VL6180x_RdMulti(VL6180xDev_t dev, uint16_t index, uint8_t *data, int nData){
|
||||
VL6180x_I2C_USER_VAR
|
||||
int status;
|
||||
uint8_t *buffer;
|
||||
DECL_I2C_BUFFER
|
||||
|
||||
VL6180x_GetI2CAccess(dev);
|
||||
buffer=VL6180x_GetI2cBuffer(dev,2);
|
||||
|
||||
buffer[0]=index>>8;
|
||||
buffer[1]=index&0xFF;
|
||||
|
||||
status=VL6180x_I2CWrite(dev, (uint8_t *) buffer, (uint8_t)2);
|
||||
if( !status ){
|
||||
status=VL6180x_I2CRead(dev, data, nData);
|
||||
}
|
||||
VL6180x_DoneI2CAcces(dev);
|
||||
return status;
|
||||
}
|
||||
Reference in New Issue
Block a user