11 #include "../firmware.h"
13 #include "inc/hw_i2c.h"
14 #include "inc/hw_memmap.h"
15 #include "inc/hw_types.h"
17 #include "driverlib/gpio.h"
18 #include "driverlib/i2c.h"
19 #include "driverlib/pin_map.h"
20 #include "driverlib/sysctl.h"
25 #define DEBUG_SERIAL UART_CAMERA // this is the serial port debug messages will come through (UART_CAMERA or UART_GNSS)
26 #define GPS_SERIAL UART_GNSS
27 #define CAM_SERIAL UART_CAMERA
29 #define DEBUG_SERIAL GPS_SERIAL
33 #define SLAVE_ADDRESS 0x68 // this is the MPU-9250A i2c address %1011000
38 #define MPU_GYRO_CONFIG 27
39 #define MPU_ACCEL_CONFIG 28
40 #define MPU_ACCEL_XOUT 59
41 #define MPU_ACCEL_YOUT 61
42 #define MPU_ACCEL_ZOUT 63
43 #define MPU_TEMP_OUT 65
44 #define MPU_GYRO_XOUT 67
45 #define MPU_GYRO_YOUT 69
46 #define MPU_GYRO_ZOUT 71
47 #define MPU_WHO_AM_I 117
48 #define MPU_INT_BYPASS_ENABLE 55
53 #define STRING_BUFFER_LENGTH 20 // rough hack for testing
55 static char string_buffer[STRING_BUFFER_LENGTH];
56 static char string_buffer2[STRING_BUFFER_LENGTH];
58 static void UART_putnum(
unsigned int serialport,
signed long x)
60 itoa(abs(x),string_buffer,16);
61 unsigned int len=strlen(string_buffer);
62 unsigned int targetlen=6-len;
63 strcpy(string_buffer2,
"+00000");
64 strcpy(string_buffer2+targetlen,string_buffer);
65 if (x<0) string_buffer2[0]=
'-';
69 static void UART_putstr(
unsigned int serialport,
char *s1,
signed long x,
char *s2)
72 UART_putnum(serialport,x);
76 #define DISP3(x,y,z) UART_putstr(DEBUG_SERIAL,x,y,z); // x and z are strings, y is a number shown as signed base10 16bit
77 #define DISP2(y,z) UART_putstr(DEBUG_SERIAL,NULL,y,z);
78 #define DISP1(x) UART_puts(DEBUG_SERIAL,x);
91 UART_init(DEBUG_SERIAL, 9600); DISP1(
"\n\n\r I2C Satellite I2C IMU test.\n")
95 #define MAG_PASS_THROUGH_I2C_ADDR 12
99 #define MAG_HZL 7 // offsets in magnetometer, note little endian, so need to be read opposite way round
100 #define MAG_CNTL1 10 // Magnetometer control register
101 #define MAG_STA1 2 // read only gives flags at 1 for DRDY and 2 for DOR (data ready and overload), used in read cycle
106 i2cstring[0]=MPU_INT_BYPASS_ENABLE;
107 char i2cstatus=I2CReceive(SLAVE_ADDRESS,MPU_INT_BYPASS_ENABLE);
108 i2cstring[1]=i2cstatus | MPU_INT_BYPASS_ENABLE;
110 I2CSendString(SLAVE_ADDRESS, i2cstring);
114 i2cstring[0]=MAG_CNTL1;
117 I2CSendString(MAG_PASS_THROUGH_I2C_ADDR, i2cstring);
119 I2CSendString(MAG_PASS_THROUGH_I2C_ADDR, i2cstring);
121 char mag_id=I2CReceive(MAG_PASS_THROUGH_I2C_ADDR,0);
123 DISP3(
"\r I2Cstatus old: ",i2cstatus,
"\n")
124 DISP3("\r I2Cstatus new (After enabling passthrough for Magnetometer): ",I2CReceive(SLAVE_ADDRESS,55),"\n")
125 DISP3("\r Magnetometer ID (test, should equal 72 if found): ",mag_id,"\n")
127 signed short acc_x,acc_y,acc_z,gyr_x,gyr_y,gyr_z,mag_x,mag_y,mag_z,temp;
129 unsigned int wdt_start=20;
132 #define MPUGET(x) I2CReceive16(SLAVE_ADDRESS,x); // wrapper for I2C call to MPU
133 #define MAGGET(x) I2CReceive16r(MAG_PASS_THROUGH_I2C_ADDR, x); // wrapper for I2C call to Magnetometer
138 for (
unsigned int wdt_kicker=wdt_start;wdt_kicker>0;wdt_kicker--)
140 acc_x=MPUGET(MPU_ACCEL_XOUT) acc_y=MPUGET( MPU_ACCEL_YOUT) acc_z=MPUGET(MPU_ACCEL_ZOUT)
141 gyr_x=MPUGET(MPU_GYRO_XOUT) gyr_y=MPUGET(MPU_GYRO_YOUT) gyr_z=MPUGET(MPU_GYRO_ZOUT)
142 temp=MPUGET(MPU_TEMP_OUT)
145 while ((I2CReceive(MAG_PASS_THROUGH_I2C_ADDR,MAG_STA1)&1)!=1) {}
146 mag_x=MAGGET(MAG_HXL) mag_y=MAGGET(MAG_HYL) mag_z=MAGGET(MAG_HZL)
148 I2CSendString(MAG_PASS_THROUGH_I2C_ADDR, i2cstring);
150 DISP3("\r A:(",acc_x,",") DISP2(acc_y,",") DISP2(acc_z,") ")
151 DISP3("G:(",gyr_x,",") DISP2(gyr_y,",") DISP2(gyr_z,") ")
152 DISP3("Temp:(",temp,")")
153 DISP3(" M:(",mag_x,",") DISP2(mag_y,",") DISP2(mag_z,") ")
void UART_init(uint8_t uart_num, uint32_t baudrate)
int main(void)
Runs all module tests.
void UART_puts(uint8_t uart_num, char *str)