Basic implementation of Xbus message parsing and generation for embedded processors. The code has no dependencies and should also work for other MCU architectures than ARM provided a C99 compiler is available.

Dependents:   MTi-1_example_LPC1768 MTi-1_rikbeun MTi-1_example MTi-1_example

For an example of using the Xbus library to communicate with an MTi-1 series device using a full-duplex UART see:

Import programMTi-1_example

Example of using Xbus library to communicate with an MTi-1 series device using a full-duplex UART connection.

Committer:
tjerkhofmeijer
Date:
Fri Oct 02 16:22:33 2015 +0200
Revision:
1:c24f69a2eff4
Parent:
0:eb25b1785ee4
Xbus library is updated to support MTi 1-series' I2C and SPI interfaces

Who changed what in which revision?

UserRevisionLine numberNew contents of line
alexandery 0:eb25b1785ee4 1 /*!
alexandery 0:eb25b1785ee4 2 * \file
alexandery 0:eb25b1785ee4 3 * \copyright Copyright (C) Xsens Technologies B.V., 2015.
alexandery 0:eb25b1785ee4 4 *
alexandery 0:eb25b1785ee4 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
alexandery 0:eb25b1785ee4 6 * use this file except in compliance with the License. You may obtain a copy
alexandery 0:eb25b1785ee4 7 * of the License at
alexandery 0:eb25b1785ee4 8 *
alexandery 0:eb25b1785ee4 9 * http://www.apache.org/licenses/LICENSE-2.0
alexandery 0:eb25b1785ee4 10 *
alexandery 0:eb25b1785ee4 11 * Unless required by applicable law or agreed to in writing, software
alexandery 0:eb25b1785ee4 12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
alexandery 0:eb25b1785ee4 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
alexandery 0:eb25b1785ee4 14 * License for the specific language governing permissions and limitations
alexandery 0:eb25b1785ee4 15 * under the License.
alexandery 0:eb25b1785ee4 16 */
alexandery 0:eb25b1785ee4 17
alexandery 0:eb25b1785ee4 18 #include "xbusutility.h"
alexandery 0:eb25b1785ee4 19
alexandery 0:eb25b1785ee4 20 /*!
alexandery 0:eb25b1785ee4 21 * \name Xbus message reading utility functions.
alexandery 0:eb25b1785ee4 22 * Xbus messages use big-endian representations for multibyte data.
alexandery 0:eb25b1785ee4 23 * These functions help in reading data from an Xbus message and converting
alexandery 0:eb25b1785ee4 24 * it to a native type.
alexandery 0:eb25b1785ee4 25 *
alexandery 0:eb25b1785ee4 26 * The functions are intended to scan over a message as follows:
alexandery 0:eb25b1785ee4 27 * \code
alexandery 0:eb25b1785ee4 28 * void readValues(XbusMessage const* message)
alexandery 0:eb25b1785ee4 29 * {
alexandery 0:eb25b1785ee4 30 * uint16_t v1;
alexandery 0:eb25b1785ee4 31 * uint8_t v2;
alexandery 0:eb25b1785ee4 32 * uint32_t v3;
alexandery 0:eb25b1785ee4 33 * uint8_t* dptr = message->data;
alexandery 0:eb25b1785ee4 34 * dptr = XbusUtility_readU16(&v1, dptr);
alexandery 0:eb25b1785ee4 35 * dptr = XbusUtility_readU8(&v2, dptr);
alexandery 0:eb25b1785ee4 36 * dptr = XbusUtility_readU32(&v3, dptr);
alexandery 0:eb25b1785ee4 37 * }
alexandery 0:eb25b1785ee4 38 * \endcode
alexandery 0:eb25b1785ee4 39 * \{
alexandery 0:eb25b1785ee4 40 */
alexandery 0:eb25b1785ee4 41 /*!
alexandery 0:eb25b1785ee4 42 * \brief Read a uint8_t value from an Xbus message.
alexandery 0:eb25b1785ee4 43 */
alexandery 0:eb25b1785ee4 44 uint8_t const* XbusUtility_readU8(uint8_t* out, uint8_t const* in)
alexandery 0:eb25b1785ee4 45 {
alexandery 0:eb25b1785ee4 46 *out = *in;
alexandery 0:eb25b1785ee4 47 return ++in;
alexandery 0:eb25b1785ee4 48 }
alexandery 0:eb25b1785ee4 49
alexandery 0:eb25b1785ee4 50 /*! \brief Read a uint16_t value from an Xbus message. */
alexandery 0:eb25b1785ee4 51 uint8_t const* XbusUtility_readU16(uint16_t* out, uint8_t const* in)
alexandery 0:eb25b1785ee4 52 {
alexandery 0:eb25b1785ee4 53 *out = (in[0] << 8) | in[1];
alexandery 0:eb25b1785ee4 54 return in + sizeof(uint16_t);
alexandery 0:eb25b1785ee4 55 }
alexandery 0:eb25b1785ee4 56
alexandery 0:eb25b1785ee4 57 /*! \brief Read a uint32_t value from an Xbus message. */
alexandery 0:eb25b1785ee4 58 uint8_t const* XbusUtility_readU32(uint32_t* out, uint8_t const* in)
alexandery 0:eb25b1785ee4 59 {
alexandery 0:eb25b1785ee4 60 *out = (in[0] << 24) | (in[1] << 16) | (in[2] << 8) | in[3];
alexandery 0:eb25b1785ee4 61 return in + sizeof(uint32_t);
alexandery 0:eb25b1785ee4 62 }
alexandery 0:eb25b1785ee4 63 /*! \} */
alexandery 0:eb25b1785ee4 64
alexandery 0:eb25b1785ee4 65 /*!
alexandery 0:eb25b1785ee4 66 * \name Xbus message writing utility functions.
alexandery 0:eb25b1785ee4 67 * These functions aid in writing native values to big-endian xbus message
alexandery 0:eb25b1785ee4 68 * payloads. See corresponding reading functions for further details.
alexandery 0:eb25b1785ee4 69 * \{
alexandery 0:eb25b1785ee4 70 */
alexandery 0:eb25b1785ee4 71 /*! \brief Write a uint8_t value to an Xbus message. */
alexandery 0:eb25b1785ee4 72 uint8_t* XbusUtility_writeU8(uint8_t* out, uint8_t in)
alexandery 0:eb25b1785ee4 73 {
alexandery 0:eb25b1785ee4 74 *out++ = in;
alexandery 0:eb25b1785ee4 75 return out;
alexandery 0:eb25b1785ee4 76 }
alexandery 0:eb25b1785ee4 77
alexandery 0:eb25b1785ee4 78 /*! \brief Write a uint16_t value to an Xbus message. */
alexandery 0:eb25b1785ee4 79 uint8_t* XbusUtility_writeU16(uint8_t* out, uint16_t in)
alexandery 0:eb25b1785ee4 80 {
alexandery 0:eb25b1785ee4 81 *out++ = (in >> 8) & 0xFF;
alexandery 0:eb25b1785ee4 82 *out++ = in & 0xFF;
alexandery 0:eb25b1785ee4 83 return out;
alexandery 0:eb25b1785ee4 84 }
alexandery 0:eb25b1785ee4 85
alexandery 0:eb25b1785ee4 86 /*! \brief Write a uint32_t value to an Xbus message. */
alexandery 0:eb25b1785ee4 87 uint8_t* XbusUtility_writeU32(uint8_t* out, uint32_t in)
alexandery 0:eb25b1785ee4 88 {
alexandery 0:eb25b1785ee4 89 *out++ = (in >> 24) & 0xFF;
alexandery 0:eb25b1785ee4 90 *out++ = (in >> 16) & 0xFF;
alexandery 0:eb25b1785ee4 91 *out++ = (in >> 8) & 0xFF;
alexandery 0:eb25b1785ee4 92 *out++ = in & 0xFF;
alexandery 0:eb25b1785ee4 93 return out;
alexandery 0:eb25b1785ee4 94 }
alexandery 0:eb25b1785ee4 95 /*! \} */