一、平台:hisi3516
imu模块:mpu6500 采集方式:spi 传输到pc:socket
开发板上源码:
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#include <sys/wait.h>
//#define SINGLE_READ
#define SERVER_PORT 8888
#define LENGTH_OF_LISTEN_QUEUE 20
#define BUFFER_SIZE 1024
static const char *device = "/dev/spidev0.0";
static unsigned char mode = SPI_MODE_3 ;
static unsigned char bits = 16;
static uint32_t speed = 12*1000*1000;/* 设置spi传输速度 */
static int g_fd = -1;
struct mpu6500_data {
int16_t accel_raw[3];
int16_t gyro_raw[3];
int16_t temprature;
double timeStampe;
unsigned int index;
};
double gettimestamp()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_usec + tv.tv_sec*1e6;
}
int SPI_Fast_Transfer(unsigned char *tx , unsigned char *rx, unsigned int len)
{
int ret = 0;
struct spi_ioc_transfer mesg[1];
memset(mesg, 0, sizeof(mesg));
mesg[0].tx_buf = (__u64)tx;
mesg[0].len = (__u32)len;
mesg[0].rx_buf = (__u64)rx;
mesg[0].cs_change = 1;
ret = ioctl(g_fd, SPI_IOC_MESSAGE(1), mesg);
if (ret < 0) {
printf("SPI_IOC_MESSAGE error \n");
return -1;
}
}
int sensor_write_register(unsigned char addr, unsigned char data)
{
int ret;
struct spi_ioc_transfer mesg[1];
unsigned char tx_buf[8] = {0};
unsigned char rx_buf[8] = {0};
tx_buf[0] = data;
tx_buf[1] = addr & (~0x80);
memset(mesg, 0, sizeof(mesg));
mesg[0].tx_buf = (__u64)tx_buf;
mesg[0].len = 2;
mesg[0].rx_buf = (__u64)rx_buf;
mesg[0].cs_change = 1;
ret = ioctl(g_fd, SPI_IOC_MESSAGE(1), mesg);
if (ret < 0) {
printf("SPI_IOC_MESSAGE error \n");
return -1;
}
return 0;
}
int sensor_read_register(unsigned char addr)
{
int ret = 0;
struct spi_ioc_transfer mesg[1];
unsigned char tx_buf[8] = {0};
unsigned char rx_buf[8] = {0};
tx_buf[0] = 0;
tx_buf[1] = addr | 0x80;
memset(mesg, 0, sizeof(mesg));
mesg[0].tx_buf = (__u64)tx_buf;
mesg[0].len = 2;
mesg[0].rx_buf = (__u64)rx_buf;
mesg[0].cs_change = 1;
ret = ioctl(g_fd, SPI_IOC_MESSAGE(1), mesg);
if (ret < 0) {
printf("SPI_IOC_MESSAGE error \n");
return -1;
}
return rx_buf[0];
}
/**
*设置imu寄存器
**/
int set_imu_register(void)
{
sensor_write_register(0x56, 0x00);
usleep(1000);
sensor_write_register(0x1A,0x07); //low pass filter [2:0]
usleep(1000);
sensor_write_register(0x1B,0x1B); //GYRO_CONFIG XG_ST YG_ST ZG_ST FS_SEL Resv FCHOICE_B //00为启用
usleep(1000);
sensor_write_register(0x1C,0x18); //ACCEL_CONFIG XA_ST YA_ST ZA_ST FS_SEL Resv
usleep(1000);