/*
* \Brief: TTY initilization routine
- * \Return: status of the initialization
- * \param fd: tty device file descriptor
+ * \Return: 0 on success, -1 on failure and set errno to indicate the error
+ * \param dev_name: tty device name
* \param bno055: pointer to a bno055 structure
*/
-extern s8 BNO055_tty_init(int fd,struct bno055_t *bno055);
+extern int BNO055_tty_init(const char *dev_name,struct bno055_t *bno055);
/*
* \Brief: TTY close routine
- * \Return: status of the close
+ * \Return: 0 on success, -1 on failure and set errno to indicate the error
* \param bno055: pointer to a bno055 structure
*/
-extern s8 BNO055_tty_close(struct bno055_t *bno055);
+extern int BNO055_tty_close(struct bno055_t *bno055);
#ifdef __cplusplus
*******************************************************************************/
#include <errno.h>
+#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
but the Bosch BNO055_driver is already non-reentrant */
static struct termios old_tty;
-s8 BNO055_tty_init(int fd,struct bno055_t *bno055)
+int BNO055_tty_init(const char *dev_name,struct bno055_t *bno055)
{
struct termios tty;
int status;
+ int fd;
- if(tcgetattr(fd,&tty)) return BNO055_ERROR;
+ if((fd=open(dev_name,O_RDWR | O_NOCTTY))==-1) return -1;
+ if(tcgetattr(fd,&tty)) return -1;
old_tty=tty;
- if(cfsetspeed(&tty,B115200)) return BNO055_OUT_OF_RANGE;
+ if(cfsetspeed(&tty,B115200)) return -1;
cfmakeraw(&tty);
tty.c_cflag&=~CRTSCTS;
tty.c_cflag|=CLOCAL;
tty.c_cc[VMIN]=0;
tty.c_cc[VTIME]=10; /* 1 s timeout */
- if(tcsetattr(fd,TCSANOW,&tty)) return BNO055_ERROR;
+ if(tcsetattr(fd,TCSANOW,&tty)) return -1;
/* nRESET low */
status=TIOCM_DTR | TIOCM_RTS;
- ioctl(fd,TIOCMBIS,&status);
+ if(ioctl(fd,TIOCMBIS,&status)) return -1;
usleep(1);
/* nRESET high */
status=TIOCM_DTR | TIOCM_RTS;
- ioctl(fd,TIOCMBIC,&status);
+ if(ioctl(fd,TIOCMBIC,&status)) return -1;
/* Wait BNO055 Power on Reset time */
usleep(650000);
bno055->delay_msec=BNO055_delay;
bno055->dev_addr=fd;
- return BNO055_SUCCESS;
+ return 0;
}
-s8 BNO055_tty_close(struct bno055_t *bno055)
+int BNO055_tty_close(struct bno055_t *bno055)
{
- if(tcsetattr(bno055->dev_addr,TCSANOW,&old_tty)) return BNO055_ERROR;
+ int fd=bno055->dev_addr;
+
+ if(tcsetattr(fd,TCSANOW,&old_tty)) return -1;
bno055->bus_write=NULL;
bno055->bus_read=NULL;
bno055->delay_msec=NULL;
bno055->dev_addr=0;
+
+ if(close(fd)) return -1;
- return BNO055_SUCCESS;
+ return 0;
}
s8 BNO055_tty_write(u8 dev_addr,u8 reg_addr,u8 *reg_data,u8 cnt)
*******************************************************************************/
#include <errno.h>
-#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
int opt;
- int fd;
struct bno055_t bno055;
struct bno055_accel_t accel;
case 't':
/* TTY interface */
- if((fd=open(optarg,O_RDWR | O_NOCTTY))==-1)
+ if(BNO055_tty_init(optarg,&bno055))
{
- perror(argv[0]);
+ perror("Error while initializing tty interface");
return -errno;
}
- if(BNO055_tty_init(fd,&bno055) != BNO055_SUCCESS)
- fprintf(stderr,"Error while initializing tty interface.\n");
break;
default:
else fprintf(stderr,"Error while reading temperature in degrees Celsius.\n");
BNO055_tty_close(&bno055);
- close(fd);
+
return 0;
}