I'm relatively new to embedded linux so apologies if there's a simple answer to this question or if I'm not providing the necessary information.
And if there's a concept here that I clearly don't understand please point it out as I'm doing all of this as a learning exercise.
I recently need to use GPS receiver neo-6m and 3-axis accelerator adxl345 to collect GPS data and acceleration on Raspberry Pi 3 for the project I currently working on. Individually both work fine. However when my program runs them together only one of them will receive data. Below are my code to setup both of them:
int setup_for_acc_impl(void) {
// Create I2C bus
int file;
char *bus = "/dev/i2c-1";
if ((file = open(bus, O_RDWR)) < 0) {
printf("Failed to open the bus. \n");
exit(1);
}
// Get I2C device, ADXL345 I2C address is 0x53(83)
ioctl(file, I2C_SLAVE, 0x53);
// Select Bandwidth rate register(0x2C)
// Normal mode, Output data rate = 100 Hz(0x0A)
char config[2] = {0};
config[0] = 0x2C;
config[1] = 0x0A;
write(file, config, 2);
// Select Power control register(0x2D)
// Auto-sleep disable(0x08)
config[0] = 0x2D;
config[1] = 0x08;
write(file, config, 2);
// Select Data format register(0x31)
// Self test disabled, 4-wire interface, Full resolution, range = +/-2g(0x08)
config[0] = 0x31;
config[1] = 0x08;
write(file, config, 2);
return file;
}
and set up for gps:
void gps_setup(void){
int uart0_filestream = -1;
uart0_filestream = open(PORTNAME, O_RDWR | O_NOCTTY | O_NDELAY);
if (uart0_filestream == -1)
{
printf("Failed to open the ttyS0. \n");
exit(1);
}
struct termios options;
tcgetattr(uart0_filestream, &options);
options.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
options.c_iflag = IGNPAR;
options.c_oflag = 0;
options.c_lflag = 0;
tcflush(uart0_filestream, TCIFLUSH);
tcsetattr(uart0_filestream, TCSANOW, &options);
}
and how I read data from both of them:
//read data from adxl345,simply read
read(obu->i2c_fd, data, 6) ;
//read data from gps module
void serial_read_for_gps(char *buffer, int len)
{
char c;
char *b = buffer;
int rx_length = -1;
while(1) {
rx_length = read(uart0_filestream, (void*)(&c), 1);
if (rx_length <= 0) {
//wait for messages
printf("didn't receive any data\n");
sleep(1);
} else {
if (c == '\n') {
*b++ = '\0';
break;
}
*b++ = c;
}
}
}
I'm not sure where the problem is coming from, any help would be great.
Thanks