#define BNO055_UART_SUCCESS 0x01
+/* This makes the code non-reentrant,
+but the Bosch BNO055_driver is already non-reentrant */
+static struct termios old_tty;
+
s8 BNO055_tty_init(int fd,struct bno055_t *bno055)
{
struct termios tty;
int status;
if(tcgetattr(fd,&tty)) return BNO055_ERROR;
+ old_tty=tty;
if(cfsetspeed(&tty,B115200)) return BNO055_OUT_OF_RANGE;
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;
- /* nRESET low*/
+ /* nRESET low */
status=TIOCM_DTR | TIOCM_RTS;
ioctl(fd,TIOCMBIS,&status);
usleep(1);
return BNO055_SUCCESS;
}
+s8 BNO055_tty_close(struct bno055_t *bno055)
+{
+ if(tcsetattr(bno055->dev_addr,TCSANOW,&old_tty)) return BNO055_ERROR;
+
+ bno055->bus_write=NULL;
+ bno055->bus_read=NULL;
+ bno055->delay_msec=NULL;
+ bno055->dev_addr=0;
+
+ return BNO055_SUCCESS;
+}
+
s8 BNO055_tty_write(u8 dev_addr,u8 reg_addr,u8 *reg_data,u8 cnt)
{
u8 array[BUFFER_LEN];
u8 i=0;
int n=0;
+ int c=0;
tcflush(dev_addr,TCIOFLUSH);
array[0]=BNO055_UART_START;
array[3]=cnt;
for (i=0;i < cnt;i++) array[i+4]=*(reg_data+i);
- n=0;
- while(n < cnt+4) if((n+=write(dev_addr,array+n,cnt+4-n))==-1) return BNO055_ERROR;
+ for(n=0;n < cnt+4;n+=c)
+ if((c=write(dev_addr,array+n,cnt+4-n))==-1) return BNO055_ERROR;
+
+ for(n=0;n < 2; n+=c)
+ if((c=read(dev_addr,array+n,2-n)) <= 0) return BNO055_ERROR;
- // Add a timeout? KISS approach for now...
- n=0;
- while(n < 2) if((n+=read(dev_addr,array+n,2-n))==-1) return BNO055_ERROR;
if(array[0] != BNO055_UART_ACK || array[1] != BNO055_UART_SUCCESS) return BNO055_ERROR;
-
+
return BNO055_SUCCESS;
}
u8 array[BUFFER_LEN];
int n=0;
int length=0;
+ int c=0;
tcflush(dev_addr,TCIOFLUSH);
array[0]=BNO055_UART_START;
array[2]=reg_addr;
array[3]=cnt;
- n=0;
- while(n < 4) if((n+=write(dev_addr,array+n,4-n))==-1) return BNO055_ERROR;
+ for(n=0;n < 4;n+=c)
+ if((c=write(dev_addr,array+n,4-n))==-1) return BNO055_ERROR;
- // Add a timeout? KISS approach for now...
- n=0;
- while(n < 2) if((n+=read(dev_addr,array+n,2-n))==-1) return BNO055_ERROR;
+ for(n=0;n < 2; n+=c)
+ if((c=read(dev_addr,array+n,2-n)) <= 0) return BNO055_ERROR;
switch(array[0])
{
case BNO055_UART_REPLY:
length=array[1];
if(length > BUFFER_LEN) length=BUFFER_LEN;
- n=0;
- while(n < length && n < cnt) if((n+=read(dev_addr,reg_data+n,length-n))==-1) return BNO055_ERROR;
+ for(n=0;n < length && n < cnt;n+=c)
+ if((c=read(dev_addr,reg_data+n,length-n)) <= 0) return BNO055_ERROR;
break;
case BNO055_UART_ACK:
+/******************************************************************************
+ BNO055 Test
+ Copyright (C) 2019 Walter Fetter Lages <w.fetter@ieee.org>
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see
+ <http://www.gnu.org/licenses/>.
+
+*******************************************************************************/
+
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
printf("Temperature=%G degrees Celsius\n",d_temp);
else fprintf(stderr,"Error while reading temperature in degrees Celsius.\n");
+ BNO055_tty_close(&bno055);
close(fd);
return 0;
}