FXOS8700CQ demo for mbed OS 5

Dependencies:   FXOS8700CQ

Fork of fxos8700cq_example by Thomas Murphy

Committer:
trm
Date:
Wed May 28 17:32:41 2014 +0000
Revision:
0:6c6060a8a2f6
Child:
1:cd2525cbbe4e
Initial commit of complete example of using FXOS8700CQ firmware by Thomas Murphy.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
trm 0:6c6060a8a2f6 1 #include "mbed.h"
trm 0:6c6060a8a2f6 2 #include "FXOS8700CQ.h"
trm 0:6c6060a8a2f6 3
trm 0:6c6060a8a2f6 4 #define DATA_RECORD_TIME_MS 1000
trm 0:6c6060a8a2f6 5
trm 0:6c6060a8a2f6 6 Serial pc(USBTX, USBRX); // Primary output to demonstrate library
trm 0:6c6060a8a2f6 7
trm 0:6c6060a8a2f6 8 // Pin names for FRDM-K64F
trm 0:6c6060a8a2f6 9 FXOS8700CQ fxos(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // SDA, SCL, (addr << 1)
trm 0:6c6060a8a2f6 10
trm 0:6c6060a8a2f6 11 DigitalOut green(LED_GREEN); // waiting light
trm 0:6c6060a8a2f6 12 DigitalOut blue(LED_BLUE); // collection-in-progress light
trm 0:6c6060a8a2f6 13 DigitalOut red(LED_RED); // completed/error ligt
trm 0:6c6060a8a2f6 14
trm 0:6c6060a8a2f6 15 Timer t; // Microsecond timer, 32 bit int, maximum count of ~30 minutes
trm 0:6c6060a8a2f6 16 InterruptIn fxos_int1(PTC6); // unused, common with SW2 on FRDM-K64F
trm 0:6c6060a8a2f6 17 InterruptIn fxos_int2(PTC13); // should just be the Data-Ready interrupt
trm 0:6c6060a8a2f6 18 InterruptIn start_sw(PTA4); // switch SW3
trm 0:6c6060a8a2f6 19
trm 0:6c6060a8a2f6 20 // Interrupt status flags and data
trm 0:6c6060a8a2f6 21 bool fxos_int1_triggered = false;
trm 0:6c6060a8a2f6 22 bool fxos_int2_triggered = false;
trm 0:6c6060a8a2f6 23 uint32_t us_ellapsed = 0;
trm 0:6c6060a8a2f6 24 bool start_sw_triggered = false;
trm 0:6c6060a8a2f6 25
trm 0:6c6060a8a2f6 26 // Storage for the data from the sensor
trm 0:6c6060a8a2f6 27 SRAWDATA accel_data;
trm 0:6c6060a8a2f6 28 SRAWDATA magn_data;
trm 0:6c6060a8a2f6 29
trm 0:6c6060a8a2f6 30 void trigger_fxos_int1(void)
trm 0:6c6060a8a2f6 31 {
trm 0:6c6060a8a2f6 32 fxos_int1_triggered = true;
trm 0:6c6060a8a2f6 33 }
trm 0:6c6060a8a2f6 34
trm 0:6c6060a8a2f6 35 void trigger_fxos_int2(void)
trm 0:6c6060a8a2f6 36 {
trm 0:6c6060a8a2f6 37 fxos_int2_triggered = true;
trm 0:6c6060a8a2f6 38 us_ellapsed = t.read_us();
trm 0:6c6060a8a2f6 39 }
trm 0:6c6060a8a2f6 40
trm 0:6c6060a8a2f6 41 void trigger_start_sw(void)
trm 0:6c6060a8a2f6 42 {
trm 0:6c6060a8a2f6 43 start_sw_triggered = true;
trm 0:6c6060a8a2f6 44 }
trm 0:6c6060a8a2f6 45
trm 0:6c6060a8a2f6 46 void print_reading()
trm 0:6c6060a8a2f6 47 {
trm 0:6c6060a8a2f6 48 pc.printf("%d A X:%5d,Y:%5d,Z:%5d M X:%5d,Y:%5d,Z:%5d\r\n",
trm 0:6c6060a8a2f6 49 us_ellapsed,
trm 0:6c6060a8a2f6 50 accel_data.x, accel_data.y, accel_data.z,
trm 0:6c6060a8a2f6 51 magn_data.x, magn_data.y, magn_data.z);
trm 0:6c6060a8a2f6 52 }
trm 0:6c6060a8a2f6 53
trm 0:6c6060a8a2f6 54 int main(void)
trm 0:6c6060a8a2f6 55 {
trm 0:6c6060a8a2f6 56 // Setup
trm 0:6c6060a8a2f6 57 t.reset();
trm 0:6c6060a8a2f6 58 pc.baud(115200); // Print quickly! 200Hz x line of output data!
trm 0:6c6060a8a2f6 59
trm 0:6c6060a8a2f6 60 // Lights off (FRDM-K64F has active-low LEDs)
trm 0:6c6060a8a2f6 61 green.write(1);
trm 0:6c6060a8a2f6 62 red.write(1);
trm 0:6c6060a8a2f6 63 blue.write(1);
trm 0:6c6060a8a2f6 64
trm 0:6c6060a8a2f6 65 // Diagnostic printing of the FXOS WHOAMI register value
trm 0:6c6060a8a2f6 66 printf("\r\n\nFXOS8700Q Who Am I= %X\r\n", fxos.get_whoami());
trm 0:6c6060a8a2f6 67
trm 0:6c6060a8a2f6 68 // Iterrupt for active-low interrupt line from FXOS
trm 0:6c6060a8a2f6 69 // Configured with only one interrupt on INT2 signaling Data-Ready
trm 0:6c6060a8a2f6 70 fxos_int2.fall(&trigger_fxos_int2);
trm 0:6c6060a8a2f6 71 fxos.enable();
trm 0:6c6060a8a2f6 72
trm 0:6c6060a8a2f6 73 // Interrupt for SW3 button-down state
trm 0:6c6060a8a2f6 74 start_sw.mode(PullUp); // Since the FRDM-K64F doesn't have its SW2/SW3 pull-ups populated
trm 0:6c6060a8a2f6 75 start_sw.fall(&trigger_start_sw);
trm 0:6c6060a8a2f6 76
trm 0:6c6060a8a2f6 77 green.write(0); // ready-green on
trm 0:6c6060a8a2f6 78
trm 0:6c6060a8a2f6 79 // Example data printing
trm 0:6c6060a8a2f6 80 fxos.get_data(&accel_data, &magn_data);
trm 0:6c6060a8a2f6 81 print_reading();
trm 0:6c6060a8a2f6 82
trm 0:6c6060a8a2f6 83 pc.printf("Waiting for data collection trigger on SW3\r\n");
trm 0:6c6060a8a2f6 84
trm 0:6c6060a8a2f6 85 while(true) {
trm 0:6c6060a8a2f6 86 if(start_sw_triggered) {
trm 0:6c6060a8a2f6 87 break;
trm 0:6c6060a8a2f6 88 }
trm 0:6c6060a8a2f6 89 wait_ms(50); // just to slow the loop, fast enough for UX
trm 0:6c6060a8a2f6 90 }
trm 0:6c6060a8a2f6 91
trm 0:6c6060a8a2f6 92 green.write(1); // ready-green off
trm 0:6c6060a8a2f6 93 blue.write(0); // working-blue on
trm 0:6c6060a8a2f6 94
trm 0:6c6060a8a2f6 95 pc.printf("Started data collection. Accelerometer at max %dg.\r\n",
trm 0:6c6060a8a2f6 96 fxos.get_accel_scale());
trm 0:6c6060a8a2f6 97
trm 0:6c6060a8a2f6 98 fxos.get_data(&accel_data, &magn_data); // clear interrupt from device
trm 0:6c6060a8a2f6 99 fxos_int2_triggered = false; // un-trigger
trm 0:6c6060a8a2f6 100
trm 0:6c6060a8a2f6 101 t.start(); // start timer and enter collection loop
trm 0:6c6060a8a2f6 102 while (t.read_ms() <= DATA_RECORD_TIME_MS) {
trm 0:6c6060a8a2f6 103
trm 0:6c6060a8a2f6 104 if(fxos_int2_triggered) {
trm 0:6c6060a8a2f6 105 fxos_int2_triggered = false; // un-trigger
trm 0:6c6060a8a2f6 106 fxos.get_data(&accel_data, &magn_data);
trm 0:6c6060a8a2f6 107 print_reading(); // outpouring of data !!
trm 0:6c6060a8a2f6 108 }
trm 0:6c6060a8a2f6 109
trm 0:6c6060a8a2f6 110 // Continuous polling of interrupt status is not efficient, but...
trm 0:6c6060a8a2f6 111 wait_us(500); // 1/10th the period of the 200Hz sample rate
trm 0:6c6060a8a2f6 112 }
trm 0:6c6060a8a2f6 113
trm 0:6c6060a8a2f6 114 blue.write(1); // working-blue off
trm 0:6c6060a8a2f6 115 red.write(0); // complete-red on
trm 0:6c6060a8a2f6 116
trm 0:6c6060a8a2f6 117 pc.printf("Done. Reset to repeat.\r\n");
trm 0:6c6060a8a2f6 118
trm 0:6c6060a8a2f6 119 while(true) {
trm 0:6c6060a8a2f6 120 pc.putc('.'); // idle dots
trm 0:6c6060a8a2f6 121 wait(1.0);
trm 0:6c6060a8a2f6 122 }
trm 0:6c6060a8a2f6 123 }