hisi3516上调试mpu6500

一、平台: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);                       
 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值