|
|
@ -1,5 +1,7 @@ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "wmp.h" |
|
|
|
#include "i2c.h" |
|
|
|
#include "i2c.h" |
|
|
|
|
|
|
|
#include "timer.h" |
|
|
|
|
|
|
|
|
|
|
|
#define UARTBASE 0xE000C000 |
|
|
|
#define UARTBASE 0xE000C000 |
|
|
|
|
|
|
|
|
|
|
@ -23,20 +25,6 @@ |
|
|
|
#define U0THRE ((UREG(LSR) & (1<<5))) /* UART0 transmitter holding register is empty */ |
|
|
|
#define U0THRE ((UREG(LSR) & (1<<5))) /* UART0 transmitter holding register is empty */ |
|
|
|
#define U0DR ((UREG(LSR) & (1<<0))) /* UART0 data ready */ |
|
|
|
#define U0DR ((UREG(LSR) & (1<<0))) /* UART0 data ready */ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define I2CBASE 0xE001C000 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define I2CONSET 0x00 |
|
|
|
|
|
|
|
#define I2STAT 0x04 |
|
|
|
|
|
|
|
#define I2DAT 0x08 |
|
|
|
|
|
|
|
#define I2ADR 0x0c |
|
|
|
|
|
|
|
#define I2SCLH 0x10 |
|
|
|
|
|
|
|
#define I2SCLL 0x14 |
|
|
|
|
|
|
|
#define I2CONCLR 0x18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define IREG(x) (((volatile unsigned char *)I2CBASE)[x]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define PINSEL0 (*((volatile unsigned char *) 0xE002C000)) |
|
|
|
#define PINSEL0 (*((volatile unsigned char *) 0xE002C000)) |
|
|
|
|
|
|
|
|
|
|
|
void init_uart(void) |
|
|
|
void init_uart(void) |
|
|
@ -109,10 +97,142 @@ void reply(char *str) |
|
|
|
|
|
|
|
|
|
|
|
unsigned int count = 0; |
|
|
|
unsigned int count = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void minmax_sample(void) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
int count; |
|
|
|
|
|
|
|
int fast_roll_min, fast_roll_max; |
|
|
|
|
|
|
|
int fast_pitch_min, fast_pitch_max; |
|
|
|
|
|
|
|
int fast_yaw_min, fast_yaw_max; |
|
|
|
|
|
|
|
int slow_roll_min, slow_roll_max; |
|
|
|
|
|
|
|
int slow_pitch_min, slow_pitch_max; |
|
|
|
|
|
|
|
int slow_yaw_min, slow_yaw_max; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
putstr("Sampling min/max values\r\n"); |
|
|
|
|
|
|
|
if (!wmp_sample()) { |
|
|
|
|
|
|
|
putstr("\r\nRead error\r\n"); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fast_roll_min = fast_roll_max = wmp_roll; |
|
|
|
|
|
|
|
fast_pitch_min = fast_pitch_max = wmp_pitch; |
|
|
|
|
|
|
|
fast_yaw_min = fast_yaw_max = wmp_yaw; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
slow_roll_min = slow_roll_max = wmp_roll; |
|
|
|
|
|
|
|
slow_pitch_min = slow_pitch_max = wmp_pitch; |
|
|
|
|
|
|
|
slow_yaw_min = slow_yaw_max = wmp_yaw; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
count = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (1) { |
|
|
|
|
|
|
|
if (!wmp_sample()) { |
|
|
|
|
|
|
|
putstr("\r\nRead error\r\n"); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (wmp_roll_fast) { |
|
|
|
|
|
|
|
if (wmp_roll < fast_roll_min) |
|
|
|
|
|
|
|
fast_roll_min = wmp_roll; |
|
|
|
|
|
|
|
if (wmp_roll > fast_roll_max) |
|
|
|
|
|
|
|
fast_roll_max = wmp_roll; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
if (wmp_roll < slow_roll_min) |
|
|
|
|
|
|
|
slow_roll_min = wmp_roll; |
|
|
|
|
|
|
|
if (wmp_roll > slow_roll_max) |
|
|
|
|
|
|
|
slow_roll_max = wmp_roll; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (wmp_pitch_fast) { |
|
|
|
|
|
|
|
if (wmp_pitch < fast_pitch_min) |
|
|
|
|
|
|
|
fast_pitch_min = wmp_pitch; |
|
|
|
|
|
|
|
if (wmp_pitch > fast_pitch_max) |
|
|
|
|
|
|
|
fast_pitch_max = wmp_pitch; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
if (wmp_pitch < slow_pitch_min) |
|
|
|
|
|
|
|
slow_pitch_min = wmp_pitch; |
|
|
|
|
|
|
|
if (wmp_pitch > slow_pitch_max) |
|
|
|
|
|
|
|
slow_pitch_max = wmp_pitch; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (wmp_yaw_fast) { |
|
|
|
|
|
|
|
if (wmp_yaw < fast_yaw_min) |
|
|
|
|
|
|
|
fast_yaw_min = wmp_yaw; |
|
|
|
|
|
|
|
if (wmp_yaw > fast_yaw_max) |
|
|
|
|
|
|
|
fast_yaw_max = wmp_yaw; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
if (wmp_yaw < slow_yaw_min) |
|
|
|
|
|
|
|
slow_yaw_min = wmp_yaw; |
|
|
|
|
|
|
|
if (wmp_yaw > slow_yaw_max) |
|
|
|
|
|
|
|
slow_yaw_max = wmp_yaw; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
count++; |
|
|
|
|
|
|
|
if (count > 1000) { |
|
|
|
|
|
|
|
putstr("("); |
|
|
|
|
|
|
|
puthex(slow_roll_min); |
|
|
|
|
|
|
|
putstr(", "); |
|
|
|
|
|
|
|
puthex(slow_pitch_min); |
|
|
|
|
|
|
|
putstr(", "); |
|
|
|
|
|
|
|
puthex(slow_yaw_min); |
|
|
|
|
|
|
|
putstr(") ("); |
|
|
|
|
|
|
|
puthex(slow_roll_max); |
|
|
|
|
|
|
|
putstr(", "); |
|
|
|
|
|
|
|
puthex(slow_pitch_max); |
|
|
|
|
|
|
|
putstr(", "); |
|
|
|
|
|
|
|
puthex(slow_yaw_max); |
|
|
|
|
|
|
|
putstr(") ("); |
|
|
|
|
|
|
|
puthex(fast_roll_min); |
|
|
|
|
|
|
|
putstr(", "); |
|
|
|
|
|
|
|
puthex(fast_pitch_min); |
|
|
|
|
|
|
|
putstr(", "); |
|
|
|
|
|
|
|
puthex(fast_yaw_min); |
|
|
|
|
|
|
|
putstr(") ("); |
|
|
|
|
|
|
|
puthex(fast_roll_max); |
|
|
|
|
|
|
|
putstr(", "); |
|
|
|
|
|
|
|
puthex(fast_pitch_max); |
|
|
|
|
|
|
|
putstr(", "); |
|
|
|
|
|
|
|
puthex(fast_yaw_max); |
|
|
|
|
|
|
|
putstr(") \r"); |
|
|
|
|
|
|
|
count = 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
timer_delay_ms(2); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void average_sample(void) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
int i; |
|
|
|
|
|
|
|
int roll_total; |
|
|
|
|
|
|
|
int pitch_total; |
|
|
|
|
|
|
|
int yaw_total; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
putstr("Sampling average values\r\n"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
roll_total = 0; |
|
|
|
|
|
|
|
pitch_total = 0; |
|
|
|
|
|
|
|
yaw_total = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < 0x1000; i++) { |
|
|
|
|
|
|
|
if (!wmp_sample()) { |
|
|
|
|
|
|
|
putstr("\r\nRead error\r\n"); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
roll_total += wmp_roll; |
|
|
|
|
|
|
|
pitch_total += wmp_pitch; |
|
|
|
|
|
|
|
yaw_total += wmp_yaw; |
|
|
|
|
|
|
|
timer_delay_ms(2); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
putstr("("); |
|
|
|
|
|
|
|
puthex(roll_total); |
|
|
|
|
|
|
|
putstr(", "); |
|
|
|
|
|
|
|
puthex(pitch_total); |
|
|
|
|
|
|
|
putstr(", "); |
|
|
|
|
|
|
|
puthex(yaw_total); |
|
|
|
|
|
|
|
putstr(")\r\n"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int main(void) { |
|
|
|
int main(void) { |
|
|
|
|
|
|
|
int i; |
|
|
|
|
|
|
|
int data; |
|
|
|
init_uart(); |
|
|
|
init_uart(); |
|
|
|
init_i2c(); |
|
|
|
init_i2c(); |
|
|
|
init_pins(); |
|
|
|
init_pins(); |
|
|
|
|
|
|
|
init_timer(); |
|
|
|
putstr("Your entire life has been a mathematical error... a mathematical error I'm about to correct!\r\n"); |
|
|
|
putstr("Your entire life has been a mathematical error... a mathematical error I'm about to correct!\r\n"); |
|
|
|
|
|
|
|
|
|
|
|
while (1) { |
|
|
|
while (1) { |
|
|
@ -140,17 +260,15 @@ int main(void) { |
|
|
|
case '?': |
|
|
|
case '?': |
|
|
|
reply("Help is not available. Try a psychiatrist."); |
|
|
|
reply("Help is not available. Try a psychiatrist."); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case 'R': |
|
|
|
|
|
|
|
putstr("I2C register is: "); |
|
|
|
|
|
|
|
puthex(i2c_conreg()); |
|
|
|
|
|
|
|
reply("."); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case 'T': |
|
|
|
case 'T': |
|
|
|
putstr("I2C status was: "); |
|
|
|
putstr("I2C status was: "); |
|
|
|
puthex(i2cstat); |
|
|
|
puthex(i2cstat); |
|
|
|
putstr(" I2C status is: "); |
|
|
|
putstr(" I2C status is: "); |
|
|
|
puthex(i2c_statreg()); |
|
|
|
puthex(i2c_statreg()); |
|
|
|
reply("."); |
|
|
|
reply("."); |
|
|
|
|
|
|
|
putstr("I2C register is: "); |
|
|
|
|
|
|
|
puthex(i2c_conreg()); |
|
|
|
|
|
|
|
reply("."); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case 'S': |
|
|
|
case 'S': |
|
|
|
putstr("Sending START... "); |
|
|
|
putstr("Sending START... "); |
|
|
@ -159,29 +277,59 @@ int main(void) { |
|
|
|
else |
|
|
|
else |
|
|
|
reply("FAIL"); |
|
|
|
reply("FAIL"); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case 'D': |
|
|
|
case 'O': |
|
|
|
putstr("Sending address... "); |
|
|
|
putstr("Sending STOP... "); |
|
|
|
if (i2c_send_address(0x53, TRUE)) |
|
|
|
i2c_send_stop(); |
|
|
|
reply("OK"); |
|
|
|
reply("sent"); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case 'I': |
|
|
|
|
|
|
|
putstr("Initialising WMP... "); |
|
|
|
|
|
|
|
if (wmp_init()) |
|
|
|
|
|
|
|
reply("done"); |
|
|
|
else |
|
|
|
else |
|
|
|
reply("FAIL"); |
|
|
|
reply("FAIL"); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case 'B': |
|
|
|
case 'M': |
|
|
|
putstr("Sending bytes... "); |
|
|
|
putstr("Reading from WMP... "); |
|
|
|
if (i2c_send_data(0xfe)) |
|
|
|
if (wmp_sample()) { |
|
|
|
reply("OK"); |
|
|
|
putstr("("); |
|
|
|
else |
|
|
|
puthex(wmp_roll); |
|
|
|
|
|
|
|
putstr(", "); |
|
|
|
|
|
|
|
puthex(wmp_pitch); |
|
|
|
|
|
|
|
putstr(", "); |
|
|
|
|
|
|
|
puthex(wmp_yaw); |
|
|
|
|
|
|
|
reply(")."); |
|
|
|
|
|
|
|
} else |
|
|
|
reply("FAIL"); |
|
|
|
reply("FAIL"); |
|
|
|
|
|
|
|
break; |
|
|
|
if (i2c_send_data(0x04)) |
|
|
|
case 'L': |
|
|
|
reply("OK"); |
|
|
|
minmax_sample(); |
|
|
|
else |
|
|
|
break; |
|
|
|
|
|
|
|
case 'V': |
|
|
|
|
|
|
|
average_sample(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case 'D': |
|
|
|
|
|
|
|
putstr("Reading calibration data... "); |
|
|
|
|
|
|
|
if (wmp_read_calibration_data()) { |
|
|
|
|
|
|
|
putstr("\r\n"); |
|
|
|
|
|
|
|
for (i = 0; i < 0x10 ; i++) { |
|
|
|
|
|
|
|
puthex(wmp_calibration_data[i]); |
|
|
|
|
|
|
|
putstr(" "); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
putstr("\r\n"); |
|
|
|
|
|
|
|
for (i = 0x10; i < 0x20 ; i++) { |
|
|
|
|
|
|
|
puthex(wmp_calibration_data[i]); |
|
|
|
|
|
|
|
putstr(" "); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
putstr("\r\n"); |
|
|
|
|
|
|
|
} else { |
|
|
|
reply("FAIL"); |
|
|
|
reply("FAIL"); |
|
|
|
|
|
|
|
} |
|
|
|
break; |
|
|
|
break; |
|
|
|
case 'O': |
|
|
|
case 'N': |
|
|
|
putstr("Sending STOP... "); |
|
|
|
putstr("The time is "); |
|
|
|
i2c_send_stop(); |
|
|
|
puthex(timer_read()); |
|
|
|
reply("sent"); |
|
|
|
reply("."); |
|
|
|
break; |
|
|
|
break; |
|
|
|
default: |
|
|
|
default: |
|
|
|
reply("Unrecognised command."); |
|
|
|
reply("Unrecognised command."); |
|
|
|