It is modified accordingly to work with sparkfun dmp library under mbed platform

Dependents:   MPU9250-dmp-bluepill MPU9250-dmp

Fork of MotionDriver_6_1 by Prosper Van

Committer:
mbedoguz
Date:
Mon Aug 14 07:36:07 2017 +0000
Revision:
6:7469a85601f1
Parent:
5:e4e36c17ba70
get_ms now returns the counter.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedoguz 1:a6c3f8680fe0 1 #include "mdcompat.h"
mbedoguz 1:a6c3f8680fe0 2 #include "mbed.h" // must be out of the scope of extern "C"
mbedoguz 2:e7e761471df9 3 I2C imu9250(I2C_SDA,I2C_SCL);
mbedoguz 4:f395d9cc57c4 4 Ticker stamper;
mbedoguz 4:f395d9cc57c4 5 unsigned long stamper_count=0;
mbedoguz 4:f395d9cc57c4 6 void counter(){
mbedoguz 4:f395d9cc57c4 7 stamper_count++;
mbedoguz 4:f395d9cc57c4 8 }
mbedoguz 4:f395d9cc57c4 9 void stamper_init(void){
mbedoguz 4:f395d9cc57c4 10 stamper.attach(&counter,1);
mbedoguz 4:f395d9cc57c4 11 }
mbedoguz 1:a6c3f8680fe0 12 void imu_init(void){
mbedoguz 1:a6c3f8680fe0 13 imu9250.frequency(400000);
mbedoguz 1:a6c3f8680fe0 14 }
mbedoguz 1:a6c3f8680fe0 15 unsigned short constrain(
mbedoguz 1:a6c3f8680fe0 16 unsigned short x,
mbedoguz 1:a6c3f8680fe0 17 unsigned short a,
mbedoguz 1:a6c3f8680fe0 18 unsigned short b) {unsigned short result;
mbedoguz 1:a6c3f8680fe0 19 result= (x<a) ? a:x;
mbedoguz 1:a6c3f8680fe0 20 result= (x>b) ? b:x;
mbedoguz 1:a6c3f8680fe0 21 return result;
mbedoguz 1:a6c3f8680fe0 22 }
mbedoguz 1:a6c3f8680fe0 23
mbedoguz 1:a6c3f8680fe0 24 extern "C" {
mbedoguz 1:a6c3f8680fe0 25
mbedoguz 1:a6c3f8680fe0 26 int mbed_i2c_write(
mbedoguz 1:a6c3f8680fe0 27 unsigned char slave_addr,
mbedoguz 1:a6c3f8680fe0 28 unsigned char reg_addr,
mbedoguz 1:a6c3f8680fe0 29 unsigned char length,
mbedoguz 4:f395d9cc57c4 30 unsigned char *data) {
mbedoguz 5:e4e36c17ba70 31
mbedoguz 5:e4e36c17ba70 32 unsigned char buffer[length+1];
mbedoguz 5:e4e36c17ba70 33 buffer[0]=reg_addr;
mbedoguz 5:e4e36c17ba70 34 for(int i=0;i<length;i++){
mbedoguz 5:e4e36c17ba70 35 buffer[i+1]=data[i];
mbedoguz 5:e4e36c17ba70 36 }
mbedoguz 4:f395d9cc57c4 37 const char* x=(const char*)buffer;//
mbedoguz 1:a6c3f8680fe0 38
mbedoguz 5:e4e36c17ba70 39 imu9250.write((int)slave_addr<<1,x,length+1,0);//sending register adress first to indicate which register we are writing and a write register(0)
mbedoguz 1:a6c3f8680fe0 40 return 0;
mbedoguz 1:a6c3f8680fe0 41 }
mbedoguz 1:a6c3f8680fe0 42
mbedoguz 1:a6c3f8680fe0 43 int mbed_i2c_read(
mbedoguz 1:a6c3f8680fe0 44 unsigned char slave_addr,
mbedoguz 1:a6c3f8680fe0 45 unsigned char reg_addr,
mbedoguz 1:a6c3f8680fe0 46 unsigned char length,
mbedoguz 4:f395d9cc57c4 47 unsigned char *data) {const char RA[]={reg_addr};
mbedoguz 5:e4e36c17ba70 48 unsigned char buffer[length];
mbedoguz 5:e4e36c17ba70 49 for(int i=0;i<length;i++){
mbedoguz 5:e4e36c17ba70 50 buffer[i]=data[i];
mbedoguz 5:e4e36c17ba70 51 }
mbedoguz 3:8f2d21735748 52 imu9250.write((int)slave_addr<<1,RA, 1, 1); // no stop
mbedoguz 5:e4e36c17ba70 53 imu9250.read((int)slave_addr<<1,(char*)data, length, 0);
mbedoguz 2:e7e761471df9 54 return 0;
mbedoguz 1:a6c3f8680fe0 55 }
mbedoguz 1:a6c3f8680fe0 56
mbedoguz 1:a6c3f8680fe0 57 int delay_ms(
mbedoguz 1:a6c3f8680fe0 58 unsigned long num_ms) {
mbedoguz 4:f395d9cc57c4 59 wait_ms(num_ms);
mbedoguz 1:a6c3f8680fe0 60 return 0;
mbedoguz 1:a6c3f8680fe0 61 }
mbedoguz 1:a6c3f8680fe0 62
mbedoguz 1:a6c3f8680fe0 63 int get_ms(
mbedoguz 4:f395d9cc57c4 64 unsigned long *count=(unsigned long*)malloc(sizeof(unsigned long))) {
mbedoguz 4:f395d9cc57c4 65 *count=stamper_count;
mbedoguz 6:7469a85601f1 66 return (int)stamper_count;
mbedoguz 1:a6c3f8680fe0 67 }
mbedoguz 1:a6c3f8680fe0 68
mbedoguz 1:a6c3f8680fe0 69 int reg_int_cb(
mbedoguz 1:a6c3f8680fe0 70 void (*cb)(void),
mbedoguz 1:a6c3f8680fe0 71 unsigned char port,
mbedoguz 1:a6c3f8680fe0 72 unsigned char pin) {
mbedoguz 1:a6c3f8680fe0 73 return 0;
mbedoguz 1:a6c3f8680fe0 74 }
mbedoguz 1:a6c3f8680fe0 75
mbedoguz 1:a6c3f8680fe0 76 long labs(long x) {
mbedoguz 1:a6c3f8680fe0 77 return x > 0 ? x : -x;
mbedoguz 1:a6c3f8680fe0 78 }
mbedoguz 1:a6c3f8680fe0 79
mbedoguz 1:a6c3f8680fe0 80 float fabsf(float x) {
mbedoguz 1:a6c3f8680fe0 81 return x > 0 ? x : -x;
mbedoguz 1:a6c3f8680fe0 82 }
mbedoguz 1:a6c3f8680fe0 83
mbedoguz 1:a6c3f8680fe0 84 int min(int a, int b) {
mbedoguz 1:a6c3f8680fe0 85 return a > b ? b : a;
mbedoguz 1:a6c3f8680fe0 86 }
mbedoguz 1:a6c3f8680fe0 87
mbedoguz 1:a6c3f8680fe0 88 };