rikbeuncode

Dependencies:   mbed-rtos mbed Xbus

Fork of MTi-1_example by Xsens

Files at this revision

API Documentation at this revision

Comitter:
Alex Young
Date:
Wed May 13 17:42:38 2015 +0200
Parent:
9:e4dc986e59f6
Child:
11:8593ba137917
Commit message:
Add basic support for formatting xbus messages.

Changed in this revision

xbus/xbusdef.h Show annotated file Show diff for this revision Revisions of this file
xbus/xbusmessage.c Show annotated file Show diff for this revision Revisions of this file
xbus/xbusmessage.h Show annotated file Show diff for this revision Revisions of this file
xbus/xbusparser.c Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xbus/xbusdef.h	Wed May 13 17:42:38 2015 +0200
@@ -0,0 +1,24 @@
+/*!
+ * \file
+ * \copyright
+ * Copyright (C) Xsens Technologies B.V., 2015.  All rights reserved.
+ *
+ * This source code is intended for use only by Xsens Technologies BV and
+ * those that have explicit written permission to use it from
+ * Xsens Technologies BV.
+ *
+ * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
+ * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
+ * PARTICULAR PURPOSE.
+ */
+
+#ifndef __XBUSDEF_H
+#define __XBUSDEF_H
+
+#define XBUS_PREAMBLE (0xFA)
+#define XBUS_BUS_ID_STANDALONE (0xFF)
+#define XBUS_NO_PAYLOAD (0x00)
+#define XBUS_EXTENDED_LENGTH (0xFF)
+
+#endif // __XBUSDEF_H
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xbus/xbusmessage.c	Wed May 13 17:42:38 2015 +0200
@@ -0,0 +1,56 @@
+/*!
+ * \file
+ * \copyright
+ * Copyright (C) Xsens Technologies B.V., 2015.  All rights reserved.
+ *
+ * This source code is intended for use only by Xsens Technologies BV and
+ * those that have explicit written permission to use it from
+ * Xsens Technologies BV.
+ *
+ * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
+ * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
+ * PARTICULAR PURPOSE.
+ */
+
+#include "xbusmessage.h"
+#include "xbusdef.h"
+
+size_t XbusMessage_format(uint8_t* raw, struct XbusMessage const* message)
+{
+	uint8_t* dptr = raw;
+	*dptr++ = XBUS_PREAMBLE;
+
+	*dptr = XBUS_BUS_ID_STANDALONE;
+	uint8_t checksum = 0;
+	checksum -= *dptr++;
+
+	*dptr = message->mid;
+	checksum -= *dptr++;
+
+	if (message->length < XBUS_EXTENDED_LENGTH)
+	{
+		*dptr = message->length;
+		checksum -= *dptr++;
+	}
+	else
+	{
+		*dptr = XBUS_EXTENDED_LENGTH;
+		checksum -= *dptr++;
+		*dptr = message->length >> 8;
+		checksum -= *dptr++;
+		*dptr = message->length & 0xFF;
+		checksum -= *dptr++;
+	}
+
+	uint8_t* sptr = message->data;
+	for (int i = 0; i < message->length; ++i)
+	{
+		*dptr = *sptr++;
+		checksum -= *dptr++;
+	}
+	*dptr++ = checksum;
+
+	return dptr - raw;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xbus/xbusmessage.h	Wed May 13 17:42:38 2015 +0200
@@ -0,0 +1,47 @@
+/*!
+ * \file
+ * \copyright
+ * Copyright (C) Xsens Technologies B.V., 2015.  All rights reserved.
+ *
+ * This source code is intended for use only by Xsens Technologies BV and
+ * those that have explicit written permission to use it from
+ * Xsens Technologies BV.
+ *
+ * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
+ * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
+ * PARTICULAR PURPOSE.
+ */
+
+#ifndef __XBUSMESSAGE_H
+#define __XBUSMESSAGE_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+enum XsMessageId
+{
+	XMID_GotoConfig         = 0x30,
+	XMID_GotoConfigAck      = 0x31,
+	XMID_GotoMeasurement    = 0x10,
+	XMID_GotoMeasurementAck = 0x11,
+};
+
+struct XbusMessage
+{
+	enum XsMessageId mid;
+	uint16_t length;
+	void* data;
+};
+
+size_t XbusMessage_format(uint8_t* raw, struct XbusMessage const* message);
+
+#ifdef __cplusplus
+}
+#endif // extern "C"
+
+#endif // __XBUSMESSAGE_H
--- a/xbus/xbusparser.c	Wed May 13 17:37:43 2015 +0200
+++ b/xbus/xbusparser.c	Wed May 13 17:42:38 2015 +0200
@@ -14,12 +14,9 @@
  */
 
 #include "xbusparser.h"
+#include "xbusdef.h"
 #include <stdlib.h>
 
-#define XBUS_PREAMBLE (0xFA)
-#define XBUS_NO_PAYLOAD (0x00)
-#define XBUS_EXTENDED_LENGTH (0xFF)
-
 enum XbusParserState
 {
 	XBPS_Preamble,          /*!< \brief Looking for preamble. */