Add LPC1768
Dependencies: mbed-rtos mbed Xbus
Fork of MTi-1_example by
main.cpp@71:b9b97b6d3a8e, 2017-01-12 (annotated)
- Committer:
- dakh131
- Date:
- Thu Jan 12 12:15:42 2017 +0000
- Revision:
- 71:b9b97b6d3a8e
- Parent:
- 69:6d6dbeefd196
- Parent:
- 68:00b3b39c60a3
Merged LPC1768
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Alex Young |
36:21198d933917 | 1 | /*! |
Alex Young |
36:21198d933917 | 2 | * \file |
Alex Young |
61:b9d3e7e5ba0c | 3 | * \copyright Copyright (C) Xsens Technologies B.V., 2015. |
Alex Young |
61:b9d3e7e5ba0c | 4 | * |
Alex Young |
61:b9d3e7e5ba0c | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
Alex Young |
61:b9d3e7e5ba0c | 6 | * use this file except in compliance with the License. You may obtain a copy |
Alex Young |
61:b9d3e7e5ba0c | 7 | * of the License at |
Alex Young |
36:21198d933917 | 8 | * |
Alex Young |
61:b9d3e7e5ba0c | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Alex Young |
36:21198d933917 | 10 | * |
Alex Young |
61:b9d3e7e5ba0c | 11 | * Unless required by applicable law or agreed to in writing, software |
Alex Young |
61:b9d3e7e5ba0c | 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
Alex Young |
61:b9d3e7e5ba0c | 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
Alex Young |
61:b9d3e7e5ba0c | 14 | * License for the specific language governing permissions and limitations |
Alex Young |
61:b9d3e7e5ba0c | 15 | * under the License. |
Alex Young |
61:b9d3e7e5ba0c | 16 | * |
Alex Young |
54:2e9bb1390c9c | 17 | * \page Overview Firmware overview |
Alex Young |
54:2e9bb1390c9c | 18 | * |
Alex Young |
54:2e9bb1390c9c | 19 | * Example firmware for communicating with an Xsens MTi-1 series motion |
Alex Young |
54:2e9bb1390c9c | 20 | * tracker (MT). |
Alex Young |
54:2e9bb1390c9c | 21 | * |
Alex Young |
54:2e9bb1390c9c | 22 | * The firmware uses the mbed-rtos library to provide RTOS features such as |
Alex Young |
54:2e9bb1390c9c | 23 | * memory pools and queues. A single thread (main) is used with reception of |
tjerkhofmeijer | 64:8a0f00a064bb | 24 | * data from the motion tracker. |
Alex Young |
54:2e9bb1390c9c | 25 | * |
Alex Young |
54:2e9bb1390c9c | 26 | * \section Hardware setup |
Alex Young |
54:2e9bb1390c9c | 27 | * The firmware has been tested with a ST Nucleo F302R8 development board. |
Alex Young |
54:2e9bb1390c9c | 28 | * The Nucleo board should be connected to the MTi1 development board using the |
Alex Young |
54:2e9bb1390c9c | 29 | * Arduino compatible headers on the Nucleo board as follows: |
Alex Young |
54:2e9bb1390c9c | 30 | * |
tjerkhofmeijer | 64:8a0f00a064bb | 31 | * | Nucleo pin | MTi1 func. | MTi1 dev. pin | Used for PSEL | |
tjerkhofmeijer | 64:8a0f00a064bb | 32 | * |------------|-------------|---------------|---------------| |
tjerkhofmeijer | 64:8a0f00a064bb | 33 | * | 5V | VDD | P300-1 | Any | |
tjerkhofmeijer | 64:8a0f00a064bb | 34 | * | IORef | VDDIO | P300-2 | Any | |
tjerkhofmeijer | 64:8a0f00a064bb | 35 | * | GND | GND | P300-3 | Any | |
tjerkhofmeijer | 64:8a0f00a064bb | 36 | * | D2 | nRST | P300-5 | Any | |
tjerkhofmeijer | 64:8a0f00a064bb | 37 | * | SCL/D15 | UART_TX/SCL | P300-9 | UART / I2C | |
tjerkhofmeijer | 64:8a0f00a064bb | 38 | * | SDA/D14 | UART_RX/SDA | P300-11 | UART / I2C | |
tjerkhofmeijer | 64:8a0f00a064bb | 39 | * | D3 | DRDY | P300-15 | SPI / I2C | |
tjerkhofmeijer | 64:8a0f00a064bb | 40 | * | SCK/D13 | SCK/ADD0 | P300-17 | SPI / I2C | |
tjerkhofmeijer | 64:8a0f00a064bb | 41 | * | MISO/D12 | MISO/ADD1 | P300-19 | SPI / I2C | |
tjerkhofmeijer | 64:8a0f00a064bb | 42 | * | MOSI/D11 | MOSI/ADD2 | P300-21 | SPI / I2C | |
tjerkhofmeijer | 64:8a0f00a064bb | 43 | * | CS/D10 | nCS | P300-23 | SPI | |
Alex Young |
54:2e9bb1390c9c | 44 | * |
Alex Young |
54:2e9bb1390c9c | 45 | * Communication with the host PC is achieved using the built-in USB serial |
tjerkhofmeijer | 64:8a0f00a064bb | 46 | * bridge of the Nucleo board. Communication with the MT is achieved through |
tjerkhofmeijer | 64:8a0f00a064bb | 47 | * either the UART, I2C or SPI interface. The active interface is chosen |
tjerkhofmeijer | 64:8a0f00a064bb | 48 | * on the MT's side by use of the PSEL0 and PSEL1 switch on the MTi1 |
tjerkhofmeijer | 64:8a0f00a064bb | 49 | * development board. This example needs to be built with the matching |
tjerkhofmeijer | 64:8a0f00a064bb | 50 | * MTI_USES_xxxx_INTERFACE define set (see below) |
Alex Young |
54:2e9bb1390c9c | 51 | * |
Alex Young |
54:2e9bb1390c9c | 52 | * \subsection Porting |
tjerkhofmeijer | 64:8a0f00a064bb | 53 | * To port to a different mbed platform the following pin definitions need |
tjerkhofmeijer | 64:8a0f00a064bb | 54 | * to be updated. |
tjerkhofmeijer | 64:8a0f00a064bb | 55 | * In all cases: the reset line pin |
tjerkhofmeijer | 64:8a0f00a064bb | 56 | * For UART: the serial Rx/Tx lines UART_TX and UART_RX |
tjerkhofmeijer | 64:8a0f00a064bb | 57 | * For I2C: the SCL,SDA,DRDY and address lines |
tjerkhofmeijer | 64:8a0f00a064bb | 58 | * For SPI: The SCK,MISO,MOSI,nCS and DRDY lines |
Alex Young |
54:2e9bb1390c9c | 59 | * |
Alex Young |
54:2e9bb1390c9c | 60 | * \section Firmware Operation |
Alex Young |
54:2e9bb1390c9c | 61 | * The firmware starts by initializing the serial ports used to communicate |
Alex Young |
54:2e9bb1390c9c | 62 | * with the host PC and with the MT. During the initialization the MT is held |
Alex Young |
54:2e9bb1390c9c | 63 | * in reset using the nRST input. |
Alex Young |
54:2e9bb1390c9c | 64 | * |
Alex Young |
54:2e9bb1390c9c | 65 | * Once the firmware is ready to communicate with the MT the reset line is |
Alex Young |
54:2e9bb1390c9c | 66 | * released and the firmware waits for a wakeup message from the MT. If this is |
Alex Young |
54:2e9bb1390c9c | 67 | * not received within 1 second the firmware will try to restore communication |
Alex Young |
54:2e9bb1390c9c | 68 | * with the MT using a special restore communication procedure. |
Alex Young |
54:2e9bb1390c9c | 69 | * |
Alex Young |
54:2e9bb1390c9c | 70 | * When the MT is ready for communication the firmware requests the device ID |
Alex Young |
54:2e9bb1390c9c | 71 | * of the MT, and based on this determines which type of MTi is connected. |
Alex Young |
54:2e9bb1390c9c | 72 | * If the MT is an MTi-1 then it will be configured to send inertial and |
tjerkhofmeijer | 56:041d3d9c300a | 73 | * magnetic measurement data. MTi-2 and MTi-3 devices have onboard orientation |
Alex Young |
54:2e9bb1390c9c | 74 | * estimation and will therefore be configured to provide quaternion output. |
Alex Young |
36:21198d933917 | 75 | */ |
Alex Young |
36:21198d933917 | 76 | |
Alex Young |
4:98f063b2e6da | 77 | #include "mbed.h" |
Alex Young |
25:01356fb59467 | 78 | #include "rtos.h" |
Alex Young |
4:98f063b2e6da | 79 | #include "xbusparser.h" |
Alex Young |
11:8593ba137917 | 80 | #include "xbusmessage.h" |
Alex Young |
40:b77a8c10c76d | 81 | #include "xsdeviceid.h" |
tjerkhofmeijer | 64:8a0f00a064bb | 82 | #include "xbusdef.h" |
tjerkhofmeijer | 64:8a0f00a064bb | 83 | |
tjerkhofmeijer | 64:8a0f00a064bb | 84 | // Select communication interface to use for MTi |
tjerkhofmeijer | 64:8a0f00a064bb | 85 | #define MTI_USES_I2C_INTERFACE |
tjerkhofmeijer | 64:8a0f00a064bb | 86 | |
tjerkhofmeijer | 64:8a0f00a064bb | 87 | #if !(defined(MTI_USES_I2C_INTERFACE) || defined(MTI_USES_SPI_INTERFACE) || defined(MTI_USES_UART_INTERFACE)) |
tjerkhofmeijer | 64:8a0f00a064bb | 88 | #error "Must select communication interface by defining one of: MTI_USES_I2C_INTERFACE, MTI_USES_SPI_INTERFACE or MTI_USES_UART_INTERFACE" |
tjerkhofmeijer | 64:8a0f00a064bb | 89 | #endif |
Alex Young |
4:98f063b2e6da | 90 | |
dakh131 | 68:00b3b39c60a3 | 91 | // Define Target (David) |
dakh131 | 68:00b3b39c60a3 | 92 | //#define TARGET_MBED_LPC1768 |
dakh131 | 68:00b3b39c60a3 | 93 | |
Alex Young |
59:f9166c19451f | 94 | #if defined(TARGET_NUCLEO_F302R8) |
tjerkhofmeijer | 64:8a0f00a064bb | 95 | |
tjerkhofmeijer | 66:f12dec1c0c3d | 96 | #define PC_TX PA_2 |
tjerkhofmeijer | 66:f12dec1c0c3d | 97 | #define PC_RX PA_3 |
tjerkhofmeijer | 66:f12dec1c0c3d | 98 | #define MT_TX PB_9 |
tjerkhofmeijer | 66:f12dec1c0c3d | 99 | #define MT_RX PB_8 |
tjerkhofmeijer | 66:f12dec1c0c3d | 100 | #define MT_SDA PB_9 |
tjerkhofmeijer | 66:f12dec1c0c3d | 101 | #define MT_SCL PB_8 |
tjerkhofmeijer | 66:f12dec1c0c3d | 102 | #define MT_ADD0 PB_13 |
tjerkhofmeijer | 66:f12dec1c0c3d | 103 | #define MT_ADD1 PB_14 |
tjerkhofmeijer | 66:f12dec1c0c3d | 104 | #define MT_ADD2 PB_15 |
tjerkhofmeijer | 66:f12dec1c0c3d | 105 | #define MT_MOSI PB_15 |
tjerkhofmeijer | 66:f12dec1c0c3d | 106 | #define MT_MISO PB_14 |
tjerkhofmeijer | 66:f12dec1c0c3d | 107 | #define MT_SCLK PB_13 |
tjerkhofmeijer | 66:f12dec1c0c3d | 108 | #define MT_nCS PB_6 |
tjerkhofmeijer | 66:f12dec1c0c3d | 109 | #define MT_NRESET PA_10 |
tjerkhofmeijer | 66:f12dec1c0c3d | 110 | #define MT_DRDY PB_3 |
tjerkhofmeijer | 64:8a0f00a064bb | 111 | |
tjerkhofmeijer | 69:6d6dbeefd196 | 112 | #elif defined(TARGET_NUCLEO_F401RE) |
tjerkhofmeijer | 69:6d6dbeefd196 | 113 | |
tjerkhofmeijer | 69:6d6dbeefd196 | 114 | #define PC_TX PA_2 |
tjerkhofmeijer | 69:6d6dbeefd196 | 115 | #define PC_RX PA_3 |
tjerkhofmeijer | 69:6d6dbeefd196 | 116 | #define MT_TX PA_10 |
tjerkhofmeijer | 69:6d6dbeefd196 | 117 | #define MT_RX PA_9 |
tjerkhofmeijer | 69:6d6dbeefd196 | 118 | #define MT_SDA PB_9 |
tjerkhofmeijer | 69:6d6dbeefd196 | 119 | #define MT_SCL PB_8 |
tjerkhofmeijer | 69:6d6dbeefd196 | 120 | #define MT_ADD0 PB_13 |
tjerkhofmeijer | 69:6d6dbeefd196 | 121 | #define MT_ADD1 PB_14 |
tjerkhofmeijer | 69:6d6dbeefd196 | 122 | #define MT_ADD2 PB_15 |
tjerkhofmeijer | 69:6d6dbeefd196 | 123 | #define MT_MOSI PB_15 |
tjerkhofmeijer | 69:6d6dbeefd196 | 124 | #define MT_MISO PB_14 |
tjerkhofmeijer | 69:6d6dbeefd196 | 125 | #define MT_SCLK PB_13 |
tjerkhofmeijer | 69:6d6dbeefd196 | 126 | #define MT_nCS PB_6 |
tjerkhofmeijer | 69:6d6dbeefd196 | 127 | #define MT_NRESET PC_9 |
tjerkhofmeijer | 69:6d6dbeefd196 | 128 | #define MT_DRDY PB_3 |
tjerkhofmeijer | 69:6d6dbeefd196 | 129 | |
Alex Young |
59:f9166c19451f | 130 | #elif defined(TARGET_KL46Z) |
tjerkhofmeijer | 64:8a0f00a064bb | 131 | |
tjerkhofmeijer | 66:f12dec1c0c3d | 132 | #define PC_TX USBTX |
tjerkhofmeijer | 66:f12dec1c0c3d | 133 | #define PC_RX USBRX |
tjerkhofmeijer | 66:f12dec1c0c3d | 134 | #define MT_TX PTE0 |
tjerkhofmeijer | 66:f12dec1c0c3d | 135 | #define MT_RX PTE1 |
tjerkhofmeijer | 66:f12dec1c0c3d | 136 | #define MT_SDA PTE0 |
tjerkhofmeijer | 66:f12dec1c0c3d | 137 | #define MT_SCL PTE1 |
tjerkhofmeijer | 66:f12dec1c0c3d | 138 | #define MT_ADD0 PTD5 |
tjerkhofmeijer | 66:f12dec1c0c3d | 139 | #define MT_ADD1 PTD7 |
tjerkhofmeijer | 66:f12dec1c0c3d | 140 | #define MT_ADD2 PTD6 |
tjerkhofmeijer | 66:f12dec1c0c3d | 141 | #define MT_MOSI PTD6 |
tjerkhofmeijer | 66:f12dec1c0c3d | 142 | #define MT_MISO PTD7 |
tjerkhofmeijer | 66:f12dec1c0c3d | 143 | #define MT_SCLK PTD5 |
tjerkhofmeijer | 66:f12dec1c0c3d | 144 | #define MT_nCS PTD4 |
tjerkhofmeijer | 66:f12dec1c0c3d | 145 | #define MT_NRESET PTD3 |
tjerkhofmeijer | 66:f12dec1c0c3d | 146 | #define MT_DRDY PTD2 |
tjerkhofmeijer | 64:8a0f00a064bb | 147 | |
Alex Young |
60:ab9dad3560d3 | 148 | #elif defined(TARGET_LPC4088) |
tjerkhofmeijer | 64:8a0f00a064bb | 149 | |
tjerkhofmeijer | 66:f12dec1c0c3d | 150 | #define PC_TX USBTX |
tjerkhofmeijer | 66:f12dec1c0c3d | 151 | #define PC_RX USBRX |
tjerkhofmeijer | 66:f12dec1c0c3d | 152 | #define MT_TX p9 |
tjerkhofmeijer | 66:f12dec1c0c3d | 153 | #define MT_RX p10 |
tjerkhofmeijer | 66:f12dec1c0c3d | 154 | #define MT_SDA p9 |
tjerkhofmeijer | 66:f12dec1c0c3d | 155 | #define MT_SCL p10 |
tjerkhofmeijer | 66:f12dec1c0c3d | 156 | #define MT_ADD0 p13 |
tjerkhofmeijer | 66:f12dec1c0c3d | 157 | #define MT_ADD1 p12 |
tjerkhofmeijer | 66:f12dec1c0c3d | 158 | #define MT_ADD2 p11 |
tjerkhofmeijer | 66:f12dec1c0c3d | 159 | #define MT_MOSI p11 |
tjerkhofmeijer | 66:f12dec1c0c3d | 160 | #define MT_MISO p12 |
tjerkhofmeijer | 66:f12dec1c0c3d | 161 | #define MT_SCLK p13 |
tjerkhofmeijer | 66:f12dec1c0c3d | 162 | #define MT_nCS p14 |
tjerkhofmeijer | 66:f12dec1c0c3d | 163 | #define MT_NRESET p8 |
tjerkhofmeijer | 66:f12dec1c0c3d | 164 | #define MT_DRDY p15 |
tjerkhofmeijer | 64:8a0f00a064bb | 165 | |
dakh131 | 68:00b3b39c60a3 | 166 | #elif defined(TARGET_MBED_LPC1768) |
dakh131 | 68:00b3b39c60a3 | 167 | |
dakh131 | 68:00b3b39c60a3 | 168 | #define PC_TX USBTX |
dakh131 | 68:00b3b39c60a3 | 169 | #define PC_RX USBRX |
dakh131 | 68:00b3b39c60a3 | 170 | #define MT_TX p9 |
dakh131 | 68:00b3b39c60a3 | 171 | #define MT_RX p10 |
dakh131 | 68:00b3b39c60a3 | 172 | #define MT_SDA p9 |
dakh131 | 68:00b3b39c60a3 | 173 | #define MT_SCL p10 |
dakh131 | 68:00b3b39c60a3 | 174 | #define MT_ADD0 p13 |
dakh131 | 68:00b3b39c60a3 | 175 | #define MT_ADD1 p12 |
dakh131 | 68:00b3b39c60a3 | 176 | #define MT_ADD2 p11 |
dakh131 | 68:00b3b39c60a3 | 177 | #define MT_MOSI p11 |
dakh131 | 68:00b3b39c60a3 | 178 | #define MT_MISO p12 |
dakh131 | 68:00b3b39c60a3 | 179 | #define MT_SCLK p13 |
dakh131 | 68:00b3b39c60a3 | 180 | #define MT_nCS p14 |
dakh131 | 68:00b3b39c60a3 | 181 | #define MT_NRESET p8 |
dakh131 | 68:00b3b39c60a3 | 182 | #define MT_DRDY p15 |
Alex Young |
57:c3c85ebb7375 | 183 | #else |
tjerkhofmeijer | 66:f12dec1c0c3d | 184 | |
Alex Young |
57:c3c85ebb7375 | 185 | #error "Support for selected mbed platform has not been added." |
tjerkhofmeijer | 66:f12dec1c0c3d | 186 | |
Alex Young |
57:c3c85ebb7375 | 187 | #endif |
Alex Young |
57:c3c85ebb7375 | 188 | |
Alex Young |
57:c3c85ebb7375 | 189 | |
Alex Young |
44:b3980e8ac074 | 190 | /*! |
Alex Young |
53:3891f4259901 | 191 | * \brief Baudrate used to communicate with host PC. |
Alex Young |
53:3891f4259901 | 192 | */ |
Alex Young |
53:3891f4259901 | 193 | #define PC_UART_BAUDRATE (921600) |
Alex Young |
53:3891f4259901 | 194 | |
Alex Young |
53:3891f4259901 | 195 | /*! |
Alex Young |
44:b3980e8ac074 | 196 | * \brief The number of items to hold in the memory pools. |
Alex Young |
44:b3980e8ac074 | 197 | */ |
Alex Young |
25:01356fb59467 | 198 | #define MEMORY_POOL_SIZE (4) |
Alex Young |
44:b3980e8ac074 | 199 | /*! |
Alex Young |
44:b3980e8ac074 | 200 | * \brief The size of the queue used for device responses. |
Alex Young |
44:b3980e8ac074 | 201 | * This is set to one as in typical Xbus operation each command receives a |
Alex Young |
44:b3980e8ac074 | 202 | * response before the next command is sent. |
Alex Young |
44:b3980e8ac074 | 203 | */ |
Alex Young |
26:665d3624f9ab | 204 | #define RESPONSE_QUEUE_SIZE (1) |
Alex Young |
44:b3980e8ac074 | 205 | /*! |
Alex Young |
44:b3980e8ac074 | 206 | * \brief The size of the queue used for data messages. |
Alex Young |
44:b3980e8ac074 | 207 | * This is set to two to allow some overlap between printing received data to |
Alex Young |
44:b3980e8ac074 | 208 | * the PC serial port and the reception of the subsequent data packet. In |
Alex Young |
44:b3980e8ac074 | 209 | * more complex applications it might be necessary to increase this if |
Alex Young |
44:b3980e8ac074 | 210 | * message processing might occasionally require more time than normal. |
Alex Young |
44:b3980e8ac074 | 211 | */ |
Alex Young |
43:470c019246e4 | 212 | #define DATA_QUEUE_SIZE (2) |
Alex Young |
44:b3980e8ac074 | 213 | /*! |
Alex Young |
49:38ecfbff5391 | 214 | * \brief The maximum size of an xbus message supported by the application. |
Alex Young |
44:b3980e8ac074 | 215 | * This is the size of the message buffers in the message data memory pool. |
Alex Young |
44:b3980e8ac074 | 216 | */ |
Alex Young |
25:01356fb59467 | 217 | #define MAX_XBUS_DATA_SIZE (128) |
Alex Young |
25:01356fb59467 | 218 | |
Alex Young |
44:b3980e8ac074 | 219 | /*! \brief Serial port for communication with the host PC. */ |
Alex Young |
57:c3c85ebb7375 | 220 | static Serial pc(PC_TX, PC_RX); |
tjerkhofmeijer | 64:8a0f00a064bb | 221 | |
tjerkhofmeijer | 64:8a0f00a064bb | 222 | #if defined(MTI_USES_I2C_INTERFACE) |
tjerkhofmeijer | 64:8a0f00a064bb | 223 | /*! |
tjerkhofmeijer | 64:8a0f00a064bb | 224 | * \brief I2C master used for communication with the MT. |
tjerkhofmeijer | 64:8a0f00a064bb | 225 | */ |
tjerkhofmeijer | 64:8a0f00a064bb | 226 | static I2C mt(MT_SDA, MT_SCL); |
tjerkhofmeijer | 64:8a0f00a064bb | 227 | static DigitalOut add0(MT_ADD0); |
tjerkhofmeijer | 64:8a0f00a064bb | 228 | static DigitalOut add1(MT_ADD1); |
tjerkhofmeijer | 64:8a0f00a064bb | 229 | static DigitalOut add2(MT_ADD2); |
tjerkhofmeijer | 64:8a0f00a064bb | 230 | |
tjerkhofmeijer | 64:8a0f00a064bb | 231 | #elif defined(MTI_USES_SPI_INTERFACE) |
tjerkhofmeijer | 64:8a0f00a064bb | 232 | /*! \brief SPI master used for communication with the MT. */ |
tjerkhofmeijer | 64:8a0f00a064bb | 233 | static SPI mt(MT_MOSI, MT_MISO, MT_SCLK); |
tjerkhofmeijer | 64:8a0f00a064bb | 234 | |
tjerkhofmeijer | 64:8a0f00a064bb | 235 | /*! \brief Chip select line for the MT. */ |
tjerkhofmeijer | 64:8a0f00a064bb | 236 | static DigitalOut cs(MT_nCS, 1); |
tjerkhofmeijer | 64:8a0f00a064bb | 237 | |
tjerkhofmeijer | 64:8a0f00a064bb | 238 | #elif defined(MTI_USES_UART_INTERFACE) |
Alex Young |
58:db60ef0a0d16 | 239 | /*! |
Alex Young |
58:db60ef0a0d16 | 240 | * \brief Serial port for communication with the MT. |
Alex Young |
58:db60ef0a0d16 | 241 | * |
Alex Young |
58:db60ef0a0d16 | 242 | * We use a RawSerial port as the Stream inteface used by the regular |
Alex Young |
58:db60ef0a0d16 | 243 | * Serial class can have problems with the RTOS when using interrupts. |
Alex Young |
58:db60ef0a0d16 | 244 | */ |
Alex Young |
58:db60ef0a0d16 | 245 | static RawSerial mt(MT_TX, MT_RX); |
tjerkhofmeijer | 64:8a0f00a064bb | 246 | #endif |
tjerkhofmeijer | 64:8a0f00a064bb | 247 | |
tjerkhofmeijer | 64:8a0f00a064bb | 248 | #if defined(MTI_USES_I2C_INTERFACE) || defined(MTI_USES_SPI_INTERFACE) |
tjerkhofmeijer | 64:8a0f00a064bb | 249 | /*! |
tjerkhofmeijer | 64:8a0f00a064bb | 250 | * \brief Interrput line used by MT to signal that data is available. |
tjerkhofmeijer | 64:8a0f00a064bb | 251 | */ |
tjerkhofmeijer | 64:8a0f00a064bb | 252 | static InterruptIn drdy(MT_DRDY); |
tjerkhofmeijer | 64:8a0f00a064bb | 253 | #endif |
tjerkhofmeijer | 64:8a0f00a064bb | 254 | |
Alex Young |
35:7e519b88c610 | 255 | /*! |
Alex Young |
35:7e519b88c610 | 256 | * \brief MT reset line. |
Alex Young |
35:7e519b88c610 | 257 | * |
Alex Young |
35:7e519b88c610 | 258 | * MT is held in reset on startup. |
Alex Young |
35:7e519b88c610 | 259 | */ |
Alex Young |
57:c3c85ebb7375 | 260 | static DigitalOut mtReset(MT_NRESET, 0); |
Alex Young |
44:b3980e8ac074 | 261 | /*! \brief XbusParser used to parse incoming Xbus messages from the MT. */ |
Alex Young |
4:98f063b2e6da | 262 | static XbusParser* xbusParser; |
Alex Young |
25:01356fb59467 | 263 | |
Alex Young |
44:b3980e8ac074 | 264 | /*! |
Alex Young |
44:b3980e8ac074 | 265 | * \brief Memory pool used for storing Xbus messages when passing them |
Alex Young |
44:b3980e8ac074 | 266 | * to the main thread. |
Alex Young |
44:b3980e8ac074 | 267 | */ |
Alex Young |
25:01356fb59467 | 268 | MemoryPool<XbusMessage, MEMORY_POOL_SIZE> g_messagePool; |
Alex Young |
44:b3980e8ac074 | 269 | /*! |
Alex Young |
44:b3980e8ac074 | 270 | * \brief Memory pool used for storing the payload of Xbus messages. |
Alex Young |
44:b3980e8ac074 | 271 | */ |
Alex Young |
25:01356fb59467 | 272 | MemoryPool<uint8_t[MAX_XBUS_DATA_SIZE], MEMORY_POOL_SIZE> g_messageDataPool; |
Alex Young |
44:b3980e8ac074 | 273 | /*! |
Alex Young |
44:b3980e8ac074 | 274 | * \brief Queue used to pass data messages to the main thread for processing. |
Alex Young |
44:b3980e8ac074 | 275 | */ |
Alex Young |
44:b3980e8ac074 | 276 | Queue<XbusMessage, DATA_QUEUE_SIZE> g_dataQueue; |
Alex Young |
44:b3980e8ac074 | 277 | /*! |
Alex Young |
44:b3980e8ac074 | 278 | * \brief Queue used for passing all other messages to the main thread for processing. |
Alex Young |
44:b3980e8ac074 | 279 | */ |
Alex Young |
26:665d3624f9ab | 280 | Queue<XbusMessage, RESPONSE_QUEUE_SIZE> g_responseQueue; |
Alex Young |
4:98f063b2e6da | 281 | |
Alex Young |
44:b3980e8ac074 | 282 | /*! |
Alex Young |
44:b3980e8ac074 | 283 | * \brief Allocate message data buffer from the message data pool. |
Alex Young |
44:b3980e8ac074 | 284 | */ |
Alex Young |
25:01356fb59467 | 285 | static void* allocateMessageData(size_t bufSize) |
Alex Young |
4:98f063b2e6da | 286 | { |
Alex Young |
25:01356fb59467 | 287 | return bufSize < MAX_XBUS_DATA_SIZE ? g_messageDataPool.alloc() : NULL; |
Alex Young |
25:01356fb59467 | 288 | } |
Alex Young |
25:01356fb59467 | 289 | |
Alex Young |
44:b3980e8ac074 | 290 | /*! |
Alex Young |
44:b3980e8ac074 | 291 | * \brief Deallocate message data previously allocated from the message |
Alex Young |
44:b3980e8ac074 | 292 | * data pool. |
Alex Young |
44:b3980e8ac074 | 293 | */ |
Alex Young |
25:01356fb59467 | 294 | static void deallocateMessageData(void const* buffer) |
Alex Young |
25:01356fb59467 | 295 | { |
Alex Young |
25:01356fb59467 | 296 | g_messageDataPool.free((uint8_t(*)[MAX_XBUS_DATA_SIZE])buffer); |
Alex Young |
4:98f063b2e6da | 297 | } |
Alex Young |
4:98f063b2e6da | 298 | |
tjerkhofmeijer | 64:8a0f00a064bb | 299 | #if defined(MTI_USES_I2C_INTERFACE) |
tjerkhofmeijer | 64:8a0f00a064bb | 300 | #define MTI_I2C_ADDRESS (0x1D << 1) |
tjerkhofmeijer | 64:8a0f00a064bb | 301 | static void readData(uint8_t pipe, uint16_t dataLength) |
tjerkhofmeijer | 64:8a0f00a064bb | 302 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 303 | const int preambleLength = 2; |
tjerkhofmeijer | 64:8a0f00a064bb | 304 | uint8_t* buf = (uint8_t*)allocateMessageData(dataLength+preambleLength); |
tjerkhofmeijer | 64:8a0f00a064bb | 305 | if (buf) |
tjerkhofmeijer | 64:8a0f00a064bb | 306 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 307 | buf[0] = XBUS_PREAMBLE; |
tjerkhofmeijer | 64:8a0f00a064bb | 308 | buf[1] = XBUS_MASTERDEVICE; |
tjerkhofmeijer | 64:8a0f00a064bb | 309 | mt.write(MTI_I2C_ADDRESS, (char*)&pipe, sizeof(pipe), true); |
tjerkhofmeijer | 64:8a0f00a064bb | 310 | mt.read(MTI_I2C_ADDRESS, (char*)buf+preambleLength, dataLength); |
tjerkhofmeijer | 64:8a0f00a064bb | 311 | XbusParser_parseBuffer(xbusParser, buf, dataLength+preambleLength); |
tjerkhofmeijer | 64:8a0f00a064bb | 312 | deallocateMessageData(buf); |
tjerkhofmeijer | 64:8a0f00a064bb | 313 | } |
tjerkhofmeijer | 64:8a0f00a064bb | 314 | } |
tjerkhofmeijer | 64:8a0f00a064bb | 315 | static void mtInterruptHandler(void) |
tjerkhofmeijer | 64:8a0f00a064bb | 316 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 317 | while (true) |
tjerkhofmeijer | 64:8a0f00a064bb | 318 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 319 | uint8_t opcode = XBUS_PIPE_STATUS; |
tjerkhofmeijer | 64:8a0f00a064bb | 320 | uint8_t status[4]; |
tjerkhofmeijer | 64:8a0f00a064bb | 321 | mt.write(MTI_I2C_ADDRESS, (char*)&opcode, sizeof(opcode), true); |
tjerkhofmeijer | 64:8a0f00a064bb | 322 | mt.read(MTI_I2C_ADDRESS, (char*)status, sizeof(status)); |
tjerkhofmeijer | 64:8a0f00a064bb | 323 | |
tjerkhofmeijer | 64:8a0f00a064bb | 324 | uint16_t notificationSize = status[0] | (status[1] << 8); |
tjerkhofmeijer | 69:6d6dbeefd196 | 325 | uint16_t measurementSize = status[2] | (status[3] << 8); |
tjerkhofmeijer | 64:8a0f00a064bb | 326 | |
tjerkhofmeijer | 64:8a0f00a064bb | 327 | if (notificationSize) |
tjerkhofmeijer | 64:8a0f00a064bb | 328 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 329 | readData(XBUS_NOTIFICATION_PIPE, notificationSize); |
tjerkhofmeijer | 64:8a0f00a064bb | 330 | } |
tjerkhofmeijer | 64:8a0f00a064bb | 331 | else if (measurementSize) |
tjerkhofmeijer | 64:8a0f00a064bb | 332 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 333 | readData(XBUS_MEASUREMENT_PIPE, measurementSize); |
tjerkhofmeijer | 64:8a0f00a064bb | 334 | } |
tjerkhofmeijer | 64:8a0f00a064bb | 335 | else |
tjerkhofmeijer | 64:8a0f00a064bb | 336 | break; // No more data available to read. |
tjerkhofmeijer | 64:8a0f00a064bb | 337 | } |
tjerkhofmeijer | 64:8a0f00a064bb | 338 | } |
tjerkhofmeijer | 64:8a0f00a064bb | 339 | |
tjerkhofmeijer | 64:8a0f00a064bb | 340 | static void configureMtCommunicationInterface(void) |
tjerkhofmeijer | 64:8a0f00a064bb | 341 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 342 | mt.frequency(400000); |
tjerkhofmeijer | 64:8a0f00a064bb | 343 | //Use the addX pins to configure I2C address 0x1D |
tjerkhofmeijer | 64:8a0f00a064bb | 344 | add0.write(0); |
tjerkhofmeijer | 64:8a0f00a064bb | 345 | add1.write(0); |
tjerkhofmeijer | 64:8a0f00a064bb | 346 | add2.write(0); |
tjerkhofmeijer | 64:8a0f00a064bb | 347 | drdy.rise(&mtInterruptHandler); |
tjerkhofmeijer | 64:8a0f00a064bb | 348 | } |
tjerkhofmeijer | 64:8a0f00a064bb | 349 | |
tjerkhofmeijer | 64:8a0f00a064bb | 350 | /*! |
tjerkhofmeijer | 64:8a0f00a064bb | 351 | * \brief Send a message to the MT |
tjerkhofmeijer | 64:8a0f00a064bb | 352 | * |
tjerkhofmeijer | 64:8a0f00a064bb | 353 | * This function formats the message data and writes this to the MT I2C |
tjerkhofmeijer | 64:8a0f00a064bb | 354 | * interface. It does not wait for any response. |
tjerkhofmeijer | 64:8a0f00a064bb | 355 | */ |
tjerkhofmeijer | 64:8a0f00a064bb | 356 | static void sendMessage(XbusMessage const* m) |
tjerkhofmeijer | 64:8a0f00a064bb | 357 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 358 | uint8_t buf[64]; |
tjerkhofmeijer | 64:8a0f00a064bb | 359 | size_t rawLength = XbusMessage_format(buf, m, XLLF_I2c); |
tjerkhofmeijer | 64:8a0f00a064bb | 360 | mt.write(MTI_I2C_ADDRESS, (char*)buf, rawLength); |
tjerkhofmeijer | 64:8a0f00a064bb | 361 | } |
tjerkhofmeijer | 64:8a0f00a064bb | 362 | #elif defined(MTI_USES_SPI_INTERFACE) |
tjerkhofmeijer | 64:8a0f00a064bb | 363 | static void sendOpcode(uint8_t opcode) |
tjerkhofmeijer | 64:8a0f00a064bb | 364 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 365 | mt.write(opcode); |
tjerkhofmeijer | 64:8a0f00a064bb | 366 | for (int filler = 0; filler < 3; ++filler) |
tjerkhofmeijer | 64:8a0f00a064bb | 367 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 368 | mt.write(filler); |
tjerkhofmeijer | 64:8a0f00a064bb | 369 | } |
tjerkhofmeijer | 64:8a0f00a064bb | 370 | } |
tjerkhofmeijer | 64:8a0f00a064bb | 371 | |
tjerkhofmeijer | 64:8a0f00a064bb | 372 | static void readData(uint8_t pipe, uint16_t dataLength) |
tjerkhofmeijer | 64:8a0f00a064bb | 373 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 374 | const int preambleLength = 2; |
tjerkhofmeijer | 64:8a0f00a064bb | 375 | uint8_t* buf = (uint8_t*)allocateMessageData(dataLength+preambleLength); |
tjerkhofmeijer | 64:8a0f00a064bb | 376 | if (buf) |
tjerkhofmeijer | 64:8a0f00a064bb | 377 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 378 | uint8_t* dptr = buf; |
tjerkhofmeijer | 64:8a0f00a064bb | 379 | *dptr++ = XBUS_PREAMBLE; |
tjerkhofmeijer | 64:8a0f00a064bb | 380 | *dptr++ = XBUS_MASTERDEVICE; |
tjerkhofmeijer | 64:8a0f00a064bb | 381 | cs = 0; |
tjerkhofmeijer | 64:8a0f00a064bb | 382 | sendOpcode(pipe); |
tjerkhofmeijer | 64:8a0f00a064bb | 383 | for (int i = 0; i < dataLength; ++i) |
tjerkhofmeijer | 64:8a0f00a064bb | 384 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 385 | *dptr++ = mt.write(0); |
tjerkhofmeijer | 64:8a0f00a064bb | 386 | } |
tjerkhofmeijer | 64:8a0f00a064bb | 387 | cs = 1; |
tjerkhofmeijer | 64:8a0f00a064bb | 388 | XbusParser_parseBuffer(xbusParser, buf, dptr - buf); |
tjerkhofmeijer | 64:8a0f00a064bb | 389 | deallocateMessageData(buf); |
tjerkhofmeijer | 64:8a0f00a064bb | 390 | } |
tjerkhofmeijer | 64:8a0f00a064bb | 391 | } |
tjerkhofmeijer | 64:8a0f00a064bb | 392 | static void mtInterruptHandler(void) |
tjerkhofmeijer | 64:8a0f00a064bb | 393 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 394 | while (true) |
tjerkhofmeijer | 64:8a0f00a064bb | 395 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 396 | cs = 0; |
tjerkhofmeijer | 64:8a0f00a064bb | 397 | sendOpcode(XBUS_PIPE_STATUS); |
tjerkhofmeijer | 64:8a0f00a064bb | 398 | uint8_t status[4]; |
tjerkhofmeijer | 64:8a0f00a064bb | 399 | for (int i = 0; i < sizeof(status); ++i) |
tjerkhofmeijer | 64:8a0f00a064bb | 400 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 401 | status[i] = mt.write(0); |
tjerkhofmeijer | 64:8a0f00a064bb | 402 | } |
tjerkhofmeijer | 64:8a0f00a064bb | 403 | cs = 1; |
tjerkhofmeijer | 64:8a0f00a064bb | 404 | |
tjerkhofmeijer | 64:8a0f00a064bb | 405 | uint16_t notificationSize = status[0] | (status[1] << 8); |
tjerkhofmeijer | 64:8a0f00a064bb | 406 | uint16_t measurementSize = status[2] | (status[3] <<8); |
tjerkhofmeijer | 64:8a0f00a064bb | 407 | |
tjerkhofmeijer | 64:8a0f00a064bb | 408 | if (notificationSize) |
tjerkhofmeijer | 64:8a0f00a064bb | 409 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 410 | readData(XBUS_NOTIFICATION_PIPE, notificationSize); |
tjerkhofmeijer | 64:8a0f00a064bb | 411 | } |
tjerkhofmeijer | 64:8a0f00a064bb | 412 | else if (measurementSize) |
tjerkhofmeijer | 64:8a0f00a064bb | 413 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 414 | readData(XBUS_MEASUREMENT_PIPE, measurementSize); |
tjerkhofmeijer | 64:8a0f00a064bb | 415 | } |
tjerkhofmeijer | 64:8a0f00a064bb | 416 | else |
tjerkhofmeijer | 64:8a0f00a064bb | 417 | break; // No more data available to read. |
tjerkhofmeijer | 64:8a0f00a064bb | 418 | } |
tjerkhofmeijer | 64:8a0f00a064bb | 419 | } |
tjerkhofmeijer | 64:8a0f00a064bb | 420 | |
tjerkhofmeijer | 64:8a0f00a064bb | 421 | static void configureMtCommunicationInterface(void) |
tjerkhofmeijer | 64:8a0f00a064bb | 422 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 423 | mt.frequency(1000000); |
tjerkhofmeijer | 64:8a0f00a064bb | 424 | mt.format(8, 3); |
tjerkhofmeijer | 64:8a0f00a064bb | 425 | drdy.rise(&mtInterruptHandler); |
tjerkhofmeijer | 64:8a0f00a064bb | 426 | } |
tjerkhofmeijer | 64:8a0f00a064bb | 427 | |
tjerkhofmeijer | 64:8a0f00a064bb | 428 | /*! |
tjerkhofmeijer | 64:8a0f00a064bb | 429 | * \brief Send a message to the MT |
tjerkhofmeijer | 64:8a0f00a064bb | 430 | * |
tjerkhofmeijer | 64:8a0f00a064bb | 431 | * This function formats the message data and writes this to the MT SPI |
tjerkhofmeijer | 64:8a0f00a064bb | 432 | * interface. It does not wait for any response. |
tjerkhofmeijer | 64:8a0f00a064bb | 433 | */ |
tjerkhofmeijer | 64:8a0f00a064bb | 434 | static void sendMessage(XbusMessage const* m) |
tjerkhofmeijer | 64:8a0f00a064bb | 435 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 436 | uint8_t buf[64]; |
tjerkhofmeijer | 64:8a0f00a064bb | 437 | size_t rawLength = XbusMessage_format(buf, m, XLLF_Spi); |
tjerkhofmeijer | 64:8a0f00a064bb | 438 | cs = 0; |
tjerkhofmeijer | 64:8a0f00a064bb | 439 | for (int i = 0; i < rawLength; ++i) |
tjerkhofmeijer | 64:8a0f00a064bb | 440 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 441 | mt.write(buf[i]); |
tjerkhofmeijer | 64:8a0f00a064bb | 442 | } |
tjerkhofmeijer | 64:8a0f00a064bb | 443 | cs = 1; |
tjerkhofmeijer | 64:8a0f00a064bb | 444 | } |
tjerkhofmeijer | 64:8a0f00a064bb | 445 | #elif defined(MTI_USES_UART_INTERFACE) |
Alex Young |
44:b3980e8ac074 | 446 | /*! |
Alex Young |
44:b3980e8ac074 | 447 | * \brief RX Interrupt handler for the MT serial port. |
Alex Young |
44:b3980e8ac074 | 448 | * |
Alex Young |
44:b3980e8ac074 | 449 | * Passes received data to an XbusParser to extract messages. |
Alex Young |
44:b3980e8ac074 | 450 | */ |
Alex Young |
4:98f063b2e6da | 451 | static void mtLowLevelHandler(void) |
Alex Young |
4:98f063b2e6da | 452 | { |
Alex Young |
4:98f063b2e6da | 453 | while (mt.readable()) |
Alex Young |
4:98f063b2e6da | 454 | { |
Alex Young |
4:98f063b2e6da | 455 | XbusParser_parseByte(xbusParser, mt.getc()); |
Alex Young |
4:98f063b2e6da | 456 | } |
Alex Young |
4:98f063b2e6da | 457 | } |
Alex Young |
4:98f063b2e6da | 458 | |
Alex Young |
44:b3980e8ac074 | 459 | /*! |
tjerkhofmeijer | 64:8a0f00a064bb | 460 | * \brief Configure the serial port used for communication with the |
tjerkhofmeijer | 64:8a0f00a064bb | 461 | * motion tracker. |
tjerkhofmeijer | 64:8a0f00a064bb | 462 | */ |
tjerkhofmeijer | 64:8a0f00a064bb | 463 | static void configureMtCommunicationInterface(void) |
tjerkhofmeijer | 64:8a0f00a064bb | 464 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 465 | mt.baud(115200); |
tjerkhofmeijer | 64:8a0f00a064bb | 466 | mt.format(8, Serial::None, 1); |
tjerkhofmeijer | 64:8a0f00a064bb | 467 | mt.attach(mtLowLevelHandler, Serial::RxIrq); |
tjerkhofmeijer | 64:8a0f00a064bb | 468 | } |
tjerkhofmeijer | 64:8a0f00a064bb | 469 | |
tjerkhofmeijer | 64:8a0f00a064bb | 470 | /*! |
Alex Young |
44:b3980e8ac074 | 471 | * \brief Send a message to the MT |
Alex Young |
44:b3980e8ac074 | 472 | * |
Alex Young |
44:b3980e8ac074 | 473 | * This function formats the message data and writes this to the MT serial |
Alex Young |
44:b3980e8ac074 | 474 | * port. It does not wait for any response. |
Alex Young |
44:b3980e8ac074 | 475 | */ |
Alex Young |
34:3d7a6519a256 | 476 | static void sendMessage(XbusMessage const* m) |
Alex Young |
11:8593ba137917 | 477 | { |
Alex Young |
26:665d3624f9ab | 478 | uint8_t buf[64]; |
tjerkhofmeijer | 64:8a0f00a064bb | 479 | size_t rawLength = XbusMessage_format(buf, m, XLLF_Uart); |
Alex Young |
11:8593ba137917 | 480 | for (size_t i = 0; i < rawLength; ++i) |
Alex Young |
11:8593ba137917 | 481 | { |
Alex Young |
11:8593ba137917 | 482 | mt.putc(buf[i]); |
Alex Young |
11:8593ba137917 | 483 | } |
Alex Young |
34:3d7a6519a256 | 484 | } |
tjerkhofmeijer | 64:8a0f00a064bb | 485 | #endif |
tjerkhofmeijer | 64:8a0f00a064bb | 486 | |
Alex Young |
34:3d7a6519a256 | 487 | |
Alex Young |
44:b3980e8ac074 | 488 | /*! |
Alex Young |
44:b3980e8ac074 | 489 | * \brief Send a message to the MT and wait for a response. |
Alex Young |
44:b3980e8ac074 | 490 | * \returns Response message from the MT, or NULL is no response received |
Alex Young |
44:b3980e8ac074 | 491 | * within 500ms. |
Alex Young |
44:b3980e8ac074 | 492 | * |
Alex Young |
44:b3980e8ac074 | 493 | * Blocking behaviour is implemented by waiting for a response to be written |
Alex Young |
44:b3980e8ac074 | 494 | * to the response queue by the XbusParser. |
Alex Young |
44:b3980e8ac074 | 495 | */ |
Alex Young |
34:3d7a6519a256 | 496 | static XbusMessage const* doTransaction(XbusMessage const* m) |
Alex Young |
34:3d7a6519a256 | 497 | { |
Alex Young |
34:3d7a6519a256 | 498 | sendMessage(m); |
Alex Young |
26:665d3624f9ab | 499 | |
Alex Young |
26:665d3624f9ab | 500 | osEvent ev = g_responseQueue.get(500); |
Alex Young |
26:665d3624f9ab | 501 | return ev.status == osEventMessage ? (XbusMessage*)ev.value.p : NULL; |
Alex Young |
26:665d3624f9ab | 502 | } |
Alex Young |
26:665d3624f9ab | 503 | |
Alex Young |
31:ce1ea9ae861e | 504 | /*! |
Alex Young |
31:ce1ea9ae861e | 505 | * \brief RAII object to manage message memory deallocation. |
Alex Young |
31:ce1ea9ae861e | 506 | * |
Alex Young |
49:38ecfbff5391 | 507 | * Will automatically free the memory used by an XbusMessage when going out |
Alex Young |
31:ce1ea9ae861e | 508 | * of scope. |
Alex Young |
31:ce1ea9ae861e | 509 | */ |
Alex Young |
31:ce1ea9ae861e | 510 | class XbusMessageMemoryManager |
Alex Young |
26:665d3624f9ab | 511 | { |
Alex Young |
31:ce1ea9ae861e | 512 | public: |
Alex Young |
31:ce1ea9ae861e | 513 | XbusMessageMemoryManager(XbusMessage const* message) |
Alex Young |
31:ce1ea9ae861e | 514 | : m_message(message) |
Alex Young |
31:ce1ea9ae861e | 515 | { |
Alex Young |
31:ce1ea9ae861e | 516 | } |
Alex Young |
31:ce1ea9ae861e | 517 | |
Alex Young |
31:ce1ea9ae861e | 518 | ~XbusMessageMemoryManager() |
Alex Young |
31:ce1ea9ae861e | 519 | { |
Alex Young |
31:ce1ea9ae861e | 520 | if (m_message) |
Alex Young |
31:ce1ea9ae861e | 521 | { |
Alex Young |
31:ce1ea9ae861e | 522 | if (m_message->data) |
Alex Young |
31:ce1ea9ae861e | 523 | deallocateMessageData(m_message->data); |
Alex Young |
31:ce1ea9ae861e | 524 | g_messagePool.free(const_cast<XbusMessage*>(m_message)); |
Alex Young |
31:ce1ea9ae861e | 525 | } |
Alex Young |
31:ce1ea9ae861e | 526 | } |
Alex Young |
31:ce1ea9ae861e | 527 | |
Alex Young |
31:ce1ea9ae861e | 528 | private: |
Alex Young |
31:ce1ea9ae861e | 529 | XbusMessage const* m_message; |
Alex Young |
31:ce1ea9ae861e | 530 | }; |
Alex Young |
26:665d3624f9ab | 531 | |
Alex Young |
44:b3980e8ac074 | 532 | /*! |
Alex Young |
44:b3980e8ac074 | 533 | * \brief Dump information from a message to the PC serial port. |
Alex Young |
44:b3980e8ac074 | 534 | */ |
Alex Young |
29:d9310e7b58b5 | 535 | static void dumpResponse(XbusMessage const* response) |
Alex Young |
29:d9310e7b58b5 | 536 | { |
Alex Young |
29:d9310e7b58b5 | 537 | switch (response->mid) |
Alex Young |
29:d9310e7b58b5 | 538 | { |
Alex Young |
29:d9310e7b58b5 | 539 | case XMID_GotoConfigAck: |
Alex Young |
52:e2197b38c029 | 540 | pc.printf("Device went to config mode.\r\n"); |
Alex Young |
29:d9310e7b58b5 | 541 | break; |
Alex Young |
29:d9310e7b58b5 | 542 | |
Alex Young |
29:d9310e7b58b5 | 543 | case XMID_Error: |
Alex Young |
29:d9310e7b58b5 | 544 | pc.printf("Device error!"); |
Alex Young |
29:d9310e7b58b5 | 545 | break; |
Alex Young |
29:d9310e7b58b5 | 546 | |
Alex Young |
29:d9310e7b58b5 | 547 | default: |
Alex Young |
52:e2197b38c029 | 548 | pc.printf("Received response MID=%X, length=%d\r\n", response->mid, response->length); |
Alex Young |
29:d9310e7b58b5 | 549 | break; |
Alex Young |
29:d9310e7b58b5 | 550 | } |
Alex Young |
29:d9310e7b58b5 | 551 | } |
Alex Young |
29:d9310e7b58b5 | 552 | |
Alex Young |
44:b3980e8ac074 | 553 | /*! |
Alex Young |
44:b3980e8ac074 | 554 | * \brief Send a command to the MT and wait for a response. |
Alex Young |
44:b3980e8ac074 | 555 | * \param cmdId The XsMessageId of the command to send. |
Alex Young |
44:b3980e8ac074 | 556 | * |
Alex Young |
44:b3980e8ac074 | 557 | * Commands are simple messages without and payload data. |
Alex Young |
44:b3980e8ac074 | 558 | */ |
Alex Young |
26:665d3624f9ab | 559 | static void sendCommand(XsMessageId cmdId) |
Alex Young |
26:665d3624f9ab | 560 | { |
Alex Young |
26:665d3624f9ab | 561 | XbusMessage m = {cmdId}; |
Alex Young |
26:665d3624f9ab | 562 | XbusMessage const* response = doTransaction(&m); |
Alex Young |
31:ce1ea9ae861e | 563 | XbusMessageMemoryManager janitor(response); |
Alex Young |
26:665d3624f9ab | 564 | |
Alex Young |
26:665d3624f9ab | 565 | if (response) |
Alex Young |
26:665d3624f9ab | 566 | { |
Alex Young |
29:d9310e7b58b5 | 567 | dumpResponse(response); |
Alex Young |
26:665d3624f9ab | 568 | } |
Alex Young |
26:665d3624f9ab | 569 | else |
Alex Young |
26:665d3624f9ab | 570 | { |
Alex Young |
52:e2197b38c029 | 571 | pc.printf("Timeout waiting for response.\r\n"); |
Alex Young |
26:665d3624f9ab | 572 | } |
Alex Young |
11:8593ba137917 | 573 | } |
Alex Young |
11:8593ba137917 | 574 | |
Alex Young |
44:b3980e8ac074 | 575 | /*! |
Alex Young |
44:b3980e8ac074 | 576 | * \brief Handle a command from the PC |
Alex Young |
44:b3980e8ac074 | 577 | * |
Alex Young |
44:b3980e8ac074 | 578 | * The example application supports single character commands from the host |
Alex Young |
44:b3980e8ac074 | 579 | * PC to switch between configuration and measurement modes. |
Alex Young |
44:b3980e8ac074 | 580 | */ |
Alex Young |
11:8593ba137917 | 581 | static void handlePcCommand(char cmd) |
Alex Young |
11:8593ba137917 | 582 | { |
Alex Young |
11:8593ba137917 | 583 | switch (cmd) |
Alex Young |
11:8593ba137917 | 584 | { |
Alex Young |
11:8593ba137917 | 585 | case 'c': |
Alex Young |
11:8593ba137917 | 586 | sendCommand(XMID_GotoConfig); |
Alex Young |
11:8593ba137917 | 587 | break; |
Alex Young |
11:8593ba137917 | 588 | |
Alex Young |
11:8593ba137917 | 589 | case 'm': |
Alex Young |
11:8593ba137917 | 590 | sendCommand(XMID_GotoMeasurement); |
Alex Young |
11:8593ba137917 | 591 | break; |
Alex Young |
11:8593ba137917 | 592 | } |
Alex Young |
11:8593ba137917 | 593 | } |
Alex Young |
11:8593ba137917 | 594 | |
Alex Young |
44:b3980e8ac074 | 595 | /*! |
Alex Young |
44:b3980e8ac074 | 596 | * \brief XbusParser callback function to handle received messages. |
Alex Young |
44:b3980e8ac074 | 597 | * \param message Pointer to the last received message. |
Alex Young |
44:b3980e8ac074 | 598 | * |
Alex Young |
44:b3980e8ac074 | 599 | * In this example received messages are copied into one of two message |
Alex Young |
44:b3980e8ac074 | 600 | * queues for later handling by the main thread. Data messages are put |
Alex Young |
49:38ecfbff5391 | 601 | * in one queue, while all other responses are placed in the second queue. |
Alex Young |
44:b3980e8ac074 | 602 | * This is done so that data and other messages can be handled separately |
Alex Young |
44:b3980e8ac074 | 603 | * by the application code. |
Alex Young |
44:b3980e8ac074 | 604 | */ |
Alex Young |
24:2cc49dc854e3 | 605 | static void mtMessageHandler(struct XbusMessage const* message) |
Alex Young |
4:98f063b2e6da | 606 | { |
Alex Young |
43:470c019246e4 | 607 | XbusMessage* m = g_messagePool.alloc(); |
Alex Young |
43:470c019246e4 | 608 | if (m) |
Alex Young |
7:c913a7cd5231 | 609 | { |
Alex Young |
43:470c019246e4 | 610 | memcpy(m, message, sizeof(XbusMessage)); |
Alex Young |
43:470c019246e4 | 611 | if (message->mid == XMID_MtData2) |
Alex Young |
43:470c019246e4 | 612 | { |
Alex Young |
43:470c019246e4 | 613 | g_dataQueue.put(m); |
Alex Young |
43:470c019246e4 | 614 | } |
Alex Young |
43:470c019246e4 | 615 | else |
Alex Young |
43:470c019246e4 | 616 | { |
Alex Young |
43:470c019246e4 | 617 | g_responseQueue.put(m); |
Alex Young |
43:470c019246e4 | 618 | } |
Alex Young |
7:c913a7cd5231 | 619 | } |
Alex Young |
43:470c019246e4 | 620 | else if (message->data) |
Alex Young |
7:c913a7cd5231 | 621 | { |
Alex Young |
43:470c019246e4 | 622 | deallocateMessageData(message->data); |
Alex Young |
25:01356fb59467 | 623 | } |
Alex Young |
4:98f063b2e6da | 624 | } |
Alex Young |
4:98f063b2e6da | 625 | |
Alex Young |
44:b3980e8ac074 | 626 | /*! |
tjerkhofmeijer | 64:8a0f00a064bb | 627 | * \brief Configure the serial port used to communicate with the host PC. |
Alex Young |
44:b3980e8ac074 | 628 | */ |
tjerkhofmeijer | 64:8a0f00a064bb | 629 | static void configurePcInterface(void) |
Alex Young |
4:98f063b2e6da | 630 | { |
Alex Young |
53:3891f4259901 | 631 | pc.baud(PC_UART_BAUDRATE); |
Alex Young |
55:9a2d6f947f0d | 632 | pc.format(8, Serial::None, 1); |
Alex Young |
4:98f063b2e6da | 633 | } |
Alex Young |
4:98f063b2e6da | 634 | |
Alex Young |
44:b3980e8ac074 | 635 | /*! |
Alex Young |
44:b3980e8ac074 | 636 | * \brief Read the device ID of the motion tracker. |
Alex Young |
44:b3980e8ac074 | 637 | */ |
Alex Young |
29:d9310e7b58b5 | 638 | static uint32_t readDeviceId(void) |
Alex Young |
29:d9310e7b58b5 | 639 | { |
Alex Young |
29:d9310e7b58b5 | 640 | XbusMessage reqDid = {XMID_ReqDid}; |
Alex Young |
29:d9310e7b58b5 | 641 | XbusMessage const* didRsp = doTransaction(&reqDid); |
Alex Young |
31:ce1ea9ae861e | 642 | XbusMessageMemoryManager janitor(didRsp); |
Alex Young |
29:d9310e7b58b5 | 643 | uint32_t deviceId = 0; |
Alex Young |
29:d9310e7b58b5 | 644 | if (didRsp) |
Alex Young |
29:d9310e7b58b5 | 645 | { |
Alex Young |
29:d9310e7b58b5 | 646 | if (didRsp->mid == XMID_DeviceId) |
Alex Young |
29:d9310e7b58b5 | 647 | { |
Alex Young |
29:d9310e7b58b5 | 648 | deviceId = *(uint32_t*)didRsp->data; |
Alex Young |
29:d9310e7b58b5 | 649 | } |
Alex Young |
29:d9310e7b58b5 | 650 | } |
Alex Young |
29:d9310e7b58b5 | 651 | return deviceId; |
Alex Young |
29:d9310e7b58b5 | 652 | } |
Alex Young |
29:d9310e7b58b5 | 653 | |
Alex Young |
44:b3980e8ac074 | 654 | /*! |
Alex Young |
44:b3980e8ac074 | 655 | * \brief Sets MT output configuration. |
Alex Young |
44:b3980e8ac074 | 656 | * \param conf Pointer to an array of OutputConfiguration elements. |
Alex Young |
44:b3980e8ac074 | 657 | * \param elements The number of elements in the configuration array. |
Alex Young |
44:b3980e8ac074 | 658 | * |
Alex Young |
44:b3980e8ac074 | 659 | * The response from the device indicates the actual values that will |
Alex Young |
44:b3980e8ac074 | 660 | * be used by the motion tracker. These may differ from the requested |
Alex Young |
44:b3980e8ac074 | 661 | * parameters as the motion tracker validates the requested parameters |
Alex Young |
44:b3980e8ac074 | 662 | * before applying them. |
Alex Young |
44:b3980e8ac074 | 663 | */ |
Alex Young |
32:fafe0f42d82b | 664 | static bool setOutputConfiguration(OutputConfiguration const* conf, uint8_t elements) |
Alex Young |
29:d9310e7b58b5 | 665 | { |
Alex Young |
32:fafe0f42d82b | 666 | XbusMessage outputConfMsg = {XMID_SetOutputConfig, elements, (void*)conf}; |
Alex Young |
32:fafe0f42d82b | 667 | XbusMessage const* outputConfRsp = doTransaction(&outputConfMsg); |
Alex Young |
32:fafe0f42d82b | 668 | XbusMessageMemoryManager janitor(outputConfRsp); |
Alex Young |
32:fafe0f42d82b | 669 | if (outputConfRsp) |
Alex Young |
29:d9310e7b58b5 | 670 | { |
Alex Young |
32:fafe0f42d82b | 671 | if (outputConfRsp->mid == XMID_OutputConfig) |
Alex Young |
29:d9310e7b58b5 | 672 | { |
Alex Young |
52:e2197b38c029 | 673 | pc.printf("Output configuration set to:\r\n"); |
Alex Young |
32:fafe0f42d82b | 674 | OutputConfiguration* conf = (OutputConfiguration*)outputConfRsp->data; |
Alex Young |
32:fafe0f42d82b | 675 | for (int i = 0; i < outputConfRsp->length; ++i) |
Alex Young |
32:fafe0f42d82b | 676 | { |
Alex Young |
52:e2197b38c029 | 677 | pc.printf("\t%s: %d Hz\r\n", XbusMessage_dataDescription(conf->dtype), conf->freq); |
Alex Young |
32:fafe0f42d82b | 678 | ++conf; |
Alex Young |
32:fafe0f42d82b | 679 | } |
Alex Young |
32:fafe0f42d82b | 680 | return true; |
Alex Young |
29:d9310e7b58b5 | 681 | } |
Alex Young |
29:d9310e7b58b5 | 682 | else |
Alex Young |
29:d9310e7b58b5 | 683 | { |
Alex Young |
32:fafe0f42d82b | 684 | dumpResponse(outputConfRsp); |
Alex Young |
29:d9310e7b58b5 | 685 | } |
Alex Young |
32:fafe0f42d82b | 686 | } |
Alex Young |
32:fafe0f42d82b | 687 | else |
Alex Young |
32:fafe0f42d82b | 688 | { |
Alex Young |
52:e2197b38c029 | 689 | pc.printf("Failed to set output configuration.\r\n"); |
Alex Young |
32:fafe0f42d82b | 690 | } |
Alex Young |
32:fafe0f42d82b | 691 | return false; |
Alex Young |
32:fafe0f42d82b | 692 | } |
Alex Young |
29:d9310e7b58b5 | 693 | |
Alex Young |
44:b3980e8ac074 | 694 | /*! |
Alex Young |
44:b3980e8ac074 | 695 | * \brief Sets the motion tracker output configuration based on the function |
Alex Young |
44:b3980e8ac074 | 696 | * of the attached device. |
Alex Young |
44:b3980e8ac074 | 697 | * |
Alex Young |
44:b3980e8ac074 | 698 | * The output configuration depends on the type of MTi-1 device connected. |
Alex Young |
49:38ecfbff5391 | 699 | * An MTI-1 (IMU) device does not have an onboard orientation filter so |
Alex Young |
44:b3980e8ac074 | 700 | * cannot output quaternion data, only inertial and magnetic measurement |
Alex Young |
44:b3980e8ac074 | 701 | * data. |
Alex Young |
44:b3980e8ac074 | 702 | * MTi-2 and MTi-3 devices have an onboard filter so can send quaternions. |
Alex Young |
44:b3980e8ac074 | 703 | */ |
Alex Young |
32:fafe0f42d82b | 704 | static bool configureMotionTracker(void) |
Alex Young |
32:fafe0f42d82b | 705 | { |
Alex Young |
32:fafe0f42d82b | 706 | uint32_t deviceId = readDeviceId(); |
Alex Young |
32:fafe0f42d82b | 707 | |
Alex Young |
32:fafe0f42d82b | 708 | if (deviceId) |
Alex Young |
32:fafe0f42d82b | 709 | { |
Alex Young |
52:e2197b38c029 | 710 | pc.printf("Found device with ID: %08X.\r\n", deviceId); |
Alex Young |
40:b77a8c10c76d | 711 | if (!XsDeviceId_isMtMk4_X(deviceId)) |
Alex Young |
40:b77a8c10c76d | 712 | { |
Alex Young |
52:e2197b38c029 | 713 | pc.printf("Device is not an MTi-1 series.\r\n"); |
Alex Young |
40:b77a8c10c76d | 714 | return false; |
Alex Young |
40:b77a8c10c76d | 715 | } |
Alex Young |
32:fafe0f42d82b | 716 | |
Alex Young |
40:b77a8c10c76d | 717 | DeviceFunction function = XsDeviceId_getFunction(deviceId); |
Alex Young |
52:e2197b38c029 | 718 | pc.printf("Device is an MTi-%d: %s.\r\n", function, XsDeviceId_functionDescription(function)); |
Alex Young |
40:b77a8c10c76d | 719 | |
Alex Young |
40:b77a8c10c76d | 720 | if (function == DF_IMU) |
Alex Young |
29:d9310e7b58b5 | 721 | { |
Alex Young |
32:fafe0f42d82b | 722 | OutputConfiguration conf[] = { |
Alex Young |
32:fafe0f42d82b | 723 | {XDI_PacketCounter, 65535}, |
Alex Young |
32:fafe0f42d82b | 724 | {XDI_SampleTimeFine, 65535}, |
Alex Young |
32:fafe0f42d82b | 725 | {XDI_Acceleration, 100}, |
Alex Young |
32:fafe0f42d82b | 726 | {XDI_RateOfTurn, 100}, |
Alex Young |
32:fafe0f42d82b | 727 | {XDI_MagneticField, 100} |
Alex Young |
32:fafe0f42d82b | 728 | }; |
Alex Young |
32:fafe0f42d82b | 729 | return setOutputConfiguration(conf, |
Alex Young |
32:fafe0f42d82b | 730 | sizeof(conf) / sizeof(OutputConfiguration)); |
Alex Young |
29:d9310e7b58b5 | 731 | } |
Alex Young |
29:d9310e7b58b5 | 732 | else |
Alex Young |
29:d9310e7b58b5 | 733 | { |
Alex Young |
32:fafe0f42d82b | 734 | OutputConfiguration conf[] = { |
Alex Young |
32:fafe0f42d82b | 735 | {XDI_PacketCounter, 65535}, |
Alex Young |
32:fafe0f42d82b | 736 | {XDI_SampleTimeFine, 65535}, |
Alex Young |
32:fafe0f42d82b | 737 | {XDI_Quaternion, 100}, |
Alex Young |
32:fafe0f42d82b | 738 | {XDI_StatusWord, 65535} |
Alex Young |
32:fafe0f42d82b | 739 | }; |
Alex Young |
32:fafe0f42d82b | 740 | return setOutputConfiguration(conf, |
Alex Young |
32:fafe0f42d82b | 741 | sizeof(conf) / sizeof(OutputConfiguration)); |
Alex Young |
29:d9310e7b58b5 | 742 | } |
Alex Young |
29:d9310e7b58b5 | 743 | } |
Alex Young |
32:fafe0f42d82b | 744 | |
Alex Young |
32:fafe0f42d82b | 745 | return false; |
Alex Young |
29:d9310e7b58b5 | 746 | } |
Alex Young |
29:d9310e7b58b5 | 747 | |
Alex Young |
35:7e519b88c610 | 748 | /*! |
Alex Young |
35:7e519b88c610 | 749 | * \brief Wait for a wakeup message from the MTi. |
Alex Young |
37:3e87bf647c68 | 750 | * \param timeout Time to wait to receive the wakeup message. |
Alex Young |
37:3e87bf647c68 | 751 | * \return true if wakeup received within timeout, else false. |
Alex Young |
35:7e519b88c610 | 752 | * |
Alex Young |
49:38ecfbff5391 | 753 | * The MTi sends an XMID_Wakeup message once it has completed its bootup |
Alex Young |
49:38ecfbff5391 | 754 | * procedure. If this is acknowledged by an XMID_WakeupAck message then the MTi |
Alex Young |
35:7e519b88c610 | 755 | * will stay in configuration mode. Otherwise it will automatically enter |
Alex Young |
35:7e519b88c610 | 756 | * measurement mode with the stored output configuration. |
Alex Young |
35:7e519b88c610 | 757 | */ |
Alex Young |
37:3e87bf647c68 | 758 | bool waitForWakeup(uint32_t timeout) |
Alex Young |
35:7e519b88c610 | 759 | { |
Alex Young |
37:3e87bf647c68 | 760 | osEvent ev = g_responseQueue.get(timeout); |
Alex Young |
35:7e519b88c610 | 761 | if (ev.status == osEventMessage) |
Alex Young |
35:7e519b88c610 | 762 | { |
Alex Young |
35:7e519b88c610 | 763 | XbusMessage const* m = (XbusMessage const*)ev.value.p; |
Alex Young |
35:7e519b88c610 | 764 | XbusMessageMemoryManager janitor(m); |
Alex Young |
35:7e519b88c610 | 765 | return m->mid == XMID_Wakeup; |
Alex Young |
35:7e519b88c610 | 766 | } |
Alex Young |
35:7e519b88c610 | 767 | return false; |
Alex Young |
35:7e519b88c610 | 768 | } |
Alex Young |
35:7e519b88c610 | 769 | |
Alex Young |
35:7e519b88c610 | 770 | /*! |
Alex Young |
37:3e87bf647c68 | 771 | * \brief Send wakeup acknowledge message to MTi. |
Alex Young |
37:3e87bf647c68 | 772 | * |
Alex Young |
37:3e87bf647c68 | 773 | * Sending a wakeup acknowledge will cause the device to stay in configuration |
Alex Young |
37:3e87bf647c68 | 774 | * mode instead of automatically transitioning to measurement mode with the |
Alex Young |
37:3e87bf647c68 | 775 | * stored output configuration. |
Alex Young |
37:3e87bf647c68 | 776 | */ |
Alex Young |
37:3e87bf647c68 | 777 | void sendWakeupAck(void) |
Alex Young |
37:3e87bf647c68 | 778 | { |
Alex Young |
37:3e87bf647c68 | 779 | XbusMessage ack = {XMID_WakeupAck}; |
Alex Young |
37:3e87bf647c68 | 780 | sendMessage(&ack); |
Alex Young |
52:e2197b38c029 | 781 | pc.printf("Device ready for operation.\r\n"); |
Alex Young |
37:3e87bf647c68 | 782 | } |
Alex Young |
37:3e87bf647c68 | 783 | |
tjerkhofmeijer | 64:8a0f00a064bb | 784 | #ifdef MTI_USES_UART_INTERFACE |
Alex Young |
37:3e87bf647c68 | 785 | /*! |
Alex Young |
37:3e87bf647c68 | 786 | * \brief Restore communication with the MTi. |
Alex Young |
37:3e87bf647c68 | 787 | * |
Alex Young |
37:3e87bf647c68 | 788 | * On bootup the MTi will listen for a magic byte to signal that it should |
Alex Young |
37:3e87bf647c68 | 789 | * return to default baudrate and output configuration. This can be used to |
Alex Young |
37:3e87bf647c68 | 790 | * recover from a bad or unknown configuration. |
Alex Young |
37:3e87bf647c68 | 791 | */ |
Alex Young |
37:3e87bf647c68 | 792 | void restoreCommunication(void) |
Alex Young |
37:3e87bf647c68 | 793 | { |
Alex Young |
37:3e87bf647c68 | 794 | pc.printf("Restoring communication with device... "); |
Alex Young |
37:3e87bf647c68 | 795 | mtReset = 0; |
Alex Young |
37:3e87bf647c68 | 796 | Thread::wait(1); |
Alex Young |
37:3e87bf647c68 | 797 | mtReset = 1; |
Alex Young |
37:3e87bf647c68 | 798 | |
Alex Young |
37:3e87bf647c68 | 799 | do |
Alex Young |
37:3e87bf647c68 | 800 | { |
Alex Young |
37:3e87bf647c68 | 801 | mt.putc(0xDE); |
Alex Young |
37:3e87bf647c68 | 802 | } |
Alex Young |
37:3e87bf647c68 | 803 | while (!waitForWakeup(1)); |
Alex Young |
52:e2197b38c029 | 804 | pc.printf("done\r\n"); |
Alex Young |
37:3e87bf647c68 | 805 | |
Alex Young |
37:3e87bf647c68 | 806 | sendWakeupAck(); |
Alex Young |
37:3e87bf647c68 | 807 | } |
tjerkhofmeijer | 64:8a0f00a064bb | 808 | #endif |
Alex Young |
37:3e87bf647c68 | 809 | |
Alex Young |
37:3e87bf647c68 | 810 | /*! |
Alex Young |
35:7e519b88c610 | 811 | * \brief Releases the MTi reset line and waits for a wakeup message. |
Alex Young |
37:3e87bf647c68 | 812 | * |
Alex Young |
37:3e87bf647c68 | 813 | * If no wakeup message is received within 1 second the restore communications |
Alex Young |
37:3e87bf647c68 | 814 | * procedure is done to reset the MTi to default baudrate and output configuration. |
Alex Young |
35:7e519b88c610 | 815 | */ |
tjerkhofmeijer | 64:8a0f00a064bb | 816 | static bool wakeupMotionTracker(void) |
Alex Young |
35:7e519b88c610 | 817 | { |
Alex Young |
35:7e519b88c610 | 818 | mtReset.write(1); // Release MT from reset. |
Alex Young |
37:3e87bf647c68 | 819 | if (waitForWakeup(1000)) |
Alex Young |
35:7e519b88c610 | 820 | { |
Alex Young |
37:3e87bf647c68 | 821 | sendWakeupAck(); |
Alex Young |
37:3e87bf647c68 | 822 | } |
Alex Young |
37:3e87bf647c68 | 823 | else |
Alex Young |
37:3e87bf647c68 | 824 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 825 | #ifdef MTI_USES_UART_INTERFACE |
Alex Young |
37:3e87bf647c68 | 826 | restoreCommunication(); |
tjerkhofmeijer | 64:8a0f00a064bb | 827 | #else |
tjerkhofmeijer | 64:8a0f00a064bb | 828 | pc.printf("Failed to communicate with MTi device\r\n"); |
tjerkhofmeijer | 64:8a0f00a064bb | 829 | return true; |
tjerkhofmeijer | 64:8a0f00a064bb | 830 | #endif |
Alex Young |
35:7e519b88c610 | 831 | } |
tjerkhofmeijer | 64:8a0f00a064bb | 832 | return true; |
Alex Young |
35:7e519b88c610 | 833 | } |
Alex Young |
35:7e519b88c610 | 834 | |
Alex Young |
38:d8d410d1662c | 835 | static void printIntroMessage(void) |
Alex Young |
38:d8d410d1662c | 836 | { |
Alex Young |
52:e2197b38c029 | 837 | pc.printf("\r\n\r\n\r\n\r\n\r\n"); |
Alex Young |
52:e2197b38c029 | 838 | pc.printf("MTi-1 series embedded example firmware.\r\n"); |
Alex Young |
38:d8d410d1662c | 839 | } |
Alex Young |
38:d8d410d1662c | 840 | |
Alex Young |
38:d8d410d1662c | 841 | static void printUsageInstructions(void) |
Alex Young |
38:d8d410d1662c | 842 | { |
Alex Young |
52:e2197b38c029 | 843 | pc.printf("\r\n"); |
Alex Young |
52:e2197b38c029 | 844 | pc.printf("Press 'm' to start measuring and 'c' to return to config mode.\r\n"); |
Alex Young |
38:d8d410d1662c | 845 | } |
Alex Young |
38:d8d410d1662c | 846 | |
Alex Young |
44:b3980e8ac074 | 847 | /*! |
Alex Young |
44:b3980e8ac074 | 848 | * \brief Output the contents of a data message to the PC serial port. |
Alex Young |
44:b3980e8ac074 | 849 | */ |
Alex Young |
43:470c019246e4 | 850 | static void printMessageData(struct XbusMessage const* message) |
Alex Young |
43:470c019246e4 | 851 | { |
Alex Young |
43:470c019246e4 | 852 | if (!message) |
Alex Young |
43:470c019246e4 | 853 | return; |
Alex Young |
43:470c019246e4 | 854 | |
Alex Young |
43:470c019246e4 | 855 | pc.printf("MTData2:"); |
Alex Young |
43:470c019246e4 | 856 | uint16_t counter; |
Alex Young |
43:470c019246e4 | 857 | if (XbusMessage_getDataItem(&counter, XDI_PacketCounter, message)) |
Alex Young |
43:470c019246e4 | 858 | { |
Alex Young |
43:470c019246e4 | 859 | pc.printf(" Packet counter: %5d", counter); |
Alex Young |
43:470c019246e4 | 860 | } |
Alex Young |
43:470c019246e4 | 861 | float ori[4]; |
Alex Young |
43:470c019246e4 | 862 | if (XbusMessage_getDataItem(ori, XDI_Quaternion, message)) |
Alex Young |
43:470c019246e4 | 863 | { |
Alex Young |
43:470c019246e4 | 864 | pc.printf(" Orientation: (% .3f, % .3f, % .3f, % .3f)", ori[0], ori[1], |
Alex Young |
43:470c019246e4 | 865 | ori[2], ori[3]); |
Alex Young |
43:470c019246e4 | 866 | } |
Alex Young |
43:470c019246e4 | 867 | float acc[3]; |
Alex Young |
43:470c019246e4 | 868 | if (XbusMessage_getDataItem(acc, XDI_Acceleration, message)) |
Alex Young |
43:470c019246e4 | 869 | { |
Alex Young |
43:470c019246e4 | 870 | pc.printf(" Acceleration: (% .3f, % .3f, % .3f)", acc[0], acc[1], acc[2]); |
Alex Young |
43:470c019246e4 | 871 | } |
Alex Young |
43:470c019246e4 | 872 | float gyr[3]; |
Alex Young |
43:470c019246e4 | 873 | if (XbusMessage_getDataItem(gyr, XDI_RateOfTurn, message)) |
Alex Young |
43:470c019246e4 | 874 | { |
Alex Young |
43:470c019246e4 | 875 | pc.printf(" Rate Of Turn: (% .3f, % .3f, % .3f)", gyr[0], gyr[1], gyr[2]); |
Alex Young |
43:470c019246e4 | 876 | } |
Alex Young |
43:470c019246e4 | 877 | float mag[3]; |
Alex Young |
43:470c019246e4 | 878 | if (XbusMessage_getDataItem(mag, XDI_MagneticField, message)) |
Alex Young |
43:470c019246e4 | 879 | { |
Alex Young |
43:470c019246e4 | 880 | pc.printf(" Magnetic Field: (% .3f, % .3f, % .3f)", mag[0], mag[1], mag[2]); |
Alex Young |
43:470c019246e4 | 881 | } |
Alex Young |
43:470c019246e4 | 882 | uint32_t status; |
Alex Young |
43:470c019246e4 | 883 | if (XbusMessage_getDataItem(&status, XDI_StatusWord, message)) |
Alex Young |
43:470c019246e4 | 884 | { |
Alex Young |
43:470c019246e4 | 885 | pc.printf(" Status:%X", status); |
Alex Young |
43:470c019246e4 | 886 | } |
Alex Young |
52:e2197b38c029 | 887 | pc.printf("\r\n"); |
Alex Young |
43:470c019246e4 | 888 | } |
Alex Young |
43:470c019246e4 | 889 | |
Alex Young |
2:b3e402dc11ca | 890 | int main(void) |
Alex Young |
2:b3e402dc11ca | 891 | { |
Alex Young |
4:98f063b2e6da | 892 | XbusParserCallback xbusCallback = {}; |
Alex Young |
25:01356fb59467 | 893 | xbusCallback.allocateBuffer = allocateMessageData; |
Alex Young |
25:01356fb59467 | 894 | xbusCallback.deallocateBuffer = deallocateMessageData; |
Alex Young |
24:2cc49dc854e3 | 895 | xbusCallback.handleMessage = mtMessageHandler; |
Alex Young |
4:98f063b2e6da | 896 | |
Alex Young |
4:98f063b2e6da | 897 | xbusParser = XbusParser_create(&xbusCallback); |
tjerkhofmeijer | 64:8a0f00a064bb | 898 | configurePcInterface(); |
tjerkhofmeijer | 64:8a0f00a064bb | 899 | configureMtCommunicationInterface(); |
tjerkhofmeijer | 69:6d6dbeefd196 | 900 | |
Alex Young |
38:d8d410d1662c | 901 | printIntroMessage(); |
tjerkhofmeijer | 64:8a0f00a064bb | 902 | if (wakeupMotionTracker()) |
Alex Young |
5:abc52dd88be2 | 903 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 904 | if (configureMotionTracker()) |
Alex Young |
26:665d3624f9ab | 905 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 906 | printUsageInstructions(); |
tjerkhofmeijer | 64:8a0f00a064bb | 907 | for (;;) |
Alex Young |
29:d9310e7b58b5 | 908 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 909 | while (pc.readable()) |
tjerkhofmeijer | 64:8a0f00a064bb | 910 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 911 | handlePcCommand(pc.getc()); |
tjerkhofmeijer | 64:8a0f00a064bb | 912 | } |
Alex Young |
43:470c019246e4 | 913 | |
tjerkhofmeijer | 64:8a0f00a064bb | 914 | osEvent ev = g_dataQueue.get(10); |
tjerkhofmeijer | 64:8a0f00a064bb | 915 | if (ev.status == osEventMessage) |
tjerkhofmeijer | 64:8a0f00a064bb | 916 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 917 | XbusMessage const* data = (XbusMessage const*)ev.value.p; |
tjerkhofmeijer | 64:8a0f00a064bb | 918 | XbusMessageMemoryManager janitor(data); |
tjerkhofmeijer | 64:8a0f00a064bb | 919 | printMessageData(data); |
tjerkhofmeijer | 64:8a0f00a064bb | 920 | } |
Alex Young |
43:470c019246e4 | 921 | } |
Alex Young |
26:665d3624f9ab | 922 | } |
tjerkhofmeijer | 64:8a0f00a064bb | 923 | else |
tjerkhofmeijer | 64:8a0f00a064bb | 924 | { |
tjerkhofmeijer | 64:8a0f00a064bb | 925 | pc.printf("Failed to configure motion tracker.\r\n"); |
tjerkhofmeijer | 64:8a0f00a064bb | 926 | return -1; |
tjerkhofmeijer | 64:8a0f00a064bb | 927 | } |
Alex Young |
29:d9310e7b58b5 | 928 | } |
Alex Young |
4:98f063b2e6da | 929 | } |