rikbeuncode
Dependencies: mbed-rtos mbed Xbus
Fork of MTi-1_example by
Revision 26:665d3624f9ab, committed 2015-05-20
- Comitter:
- Alex Young
- Date:
- Wed May 20 14:49:54 2015 +0200
- Parent:
- 25:01356fb59467
- Child:
- 27:eebe5fc884e3
- Commit message:
- Move PC command handling to a thread.
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Wed May 20 13:58:34 2015 +0200
+++ b/main.cpp Wed May 20 14:49:54 2015 +0200
@@ -4,6 +4,7 @@
#include "xbusmessage.h"
#define MEMORY_POOL_SIZE (4)
+#define RESPONSE_QUEUE_SIZE (1)
#define MAX_XBUS_DATA_SIZE (128)
static Serial pc(PA_2, PA_3);
@@ -12,6 +13,7 @@
MemoryPool<XbusMessage, MEMORY_POOL_SIZE> g_messagePool;
MemoryPool<uint8_t[MAX_XBUS_DATA_SIZE], MEMORY_POOL_SIZE> g_messageDataPool;
+Queue<XbusMessage, RESPONSE_QUEUE_SIZE> g_responseQueue;
static void* allocateMessageData(size_t bufSize)
{
@@ -31,15 +33,66 @@
}
}
-static void sendCommand(XsMessageId cmdId)
+XbusMessage const* doTransaction(XbusMessage* m)
{
- uint8_t buf[8];
- XbusMessage m = {cmdId};
- size_t rawLength = XbusMessage_format(buf, &m);
+ uint8_t buf[64];
+ size_t rawLength = XbusMessage_format(buf, m);
for (size_t i = 0; i < rawLength; ++i)
{
mt.putc(buf[i]);
}
+
+ osEvent ev = g_responseQueue.get(500);
+ return ev.status == osEventMessage ? (XbusMessage*)ev.value.p : NULL;
+}
+
+static void freeMessage(XbusMessage const* m)
+{
+ if (m->data)
+ deallocateMessageData(m->data);
+ g_messagePool.free((XbusMessage*)m);
+}
+
+static void sendCommand(XsMessageId cmdId)
+{
+ XbusMessage m = {cmdId};
+ XbusMessage const* response = doTransaction(&m);
+
+ if (response)
+ {
+ switch (response->mid)
+ {
+ case XMID_GotoConfigAck:
+ pc.printf("Device went to config mode\n");
+ break;
+
+ case XMID_DeviceId:
+ pc.printf("Device ID: %08X\n", *(uint32_t*)response->data);
+ break;
+
+ case XMID_OutputConfig:
+ {
+ pc.printf("Output configuration\n");
+ OutputConfiguration* conf = (OutputConfiguration*)response->data;
+ for (int i = 0; i < response->length; ++i)
+ {
+ pc.printf("\t%s: %d Hz\n", XbusMessage_dataDescription(conf->dtype), conf->freq);
+ ++conf;
+ }
+ }
+ break;
+
+
+ default:
+ pc.printf("Received response MID=%X, length=%d\n", response->mid, response->length);
+ break;
+ }
+ freeMessage(response);
+ }
+ else
+ {
+ pc.printf("Timeout waiting for response.\n");
+ }
}
static void handlePcCommand(char cmd)
@@ -64,14 +117,6 @@
}
}
-static void pcHandler(void)
-{
- while (pc.readable())
- {
- handlePcCommand(pc.getc());
- }
-}
-
static void handleDataMessage(struct XbusMessage const* message)
{
pc.printf("MTData2:");
@@ -92,6 +137,7 @@
pc.printf(" Status:%X", status);
}
pc.printf("\n");
+ deallocateMessageData(message->data);
}
static void mtMessageHandler(struct XbusMessage const* message)
@@ -100,27 +146,11 @@
{
handleDataMessage(message);
}
- else if (message->mid == XMID_DeviceId)
- {
- pc.printf("Device ID: %8X\n", *(uint32_t*)message->data);
- }
- else if (message->mid == XMID_OutputConfig)
- {
- pc.printf("Output configuration:\n");
- struct OutputConfiguration* conf = (struct OutputConfiguration*)message->data;
- for (int i = 0; i < message->length; ++i)
- {
- pc.printf("\t%s: %d Hz\n", XbusMessage_dataDescription(conf->dtype), conf->freq);
- ++conf;
- }
- }
else
{
- pc.printf("Received Xbus message. MID=%X, length=%d\n", message->mid, message->length);
- }
- if (message->data)
- {
- deallocateMessageData(message->data);
+ XbusMessage* m = g_messagePool.alloc();
+ memcpy(m, message, sizeof(XbusMessage));
+ g_responseQueue.put(m);
}
}
@@ -128,7 +158,6 @@
{
pc.baud(921600);
pc.format(8, Serial::None, 2);
- pc.attach(pcHandler, Serial::RxIrq);
mt.baud(921600);
mt.format(8, Serial::None, 2);
@@ -147,6 +176,9 @@
for (;;)
{
- sleep();
+ while (pc.readable())
+ {
+ handlePcCommand(pc.getc());
+ }
}
}
