Dump SPI from input to pc.serial

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
cbayley
Date:
Mon Jan 30 23:08:52 2012 +0000
Commit message:

Changed in this revision

MODSERIAL.lib Show annotated file Show diff for this revision Revisions of this file
MySPI/MySPI.c Show annotated file Show diff for this revision Revisions of this file
MySPI/MySPI.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
tinyQ.c Show annotated file Show diff for this revision Revisions of this file
tinyQ.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 1ca47c273d0f MODSERIAL.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MODSERIAL.lib	Mon Jan 30 23:08:52 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/AjK/code/MODSERIAL/#af2af4c61c2f
diff -r 000000000000 -r 1ca47c273d0f MySPI/MySPI.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MySPI/MySPI.c	Mon Jan 30 23:08:52 2012 +0000
@@ -0,0 +1,8 @@
+#include "mbed.h"
+#include "MySPI.h"
+
+void MySPI::puts(char *s)
+{
+    while (*s)
+        this->write(*s++);
+}
\ No newline at end of file
diff -r 000000000000 -r 1ca47c273d0f MySPI/MySPI.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MySPI/MySPI.h	Mon Jan 30 23:08:52 2012 +0000
@@ -0,0 +1,13 @@
+#include "mbed.h"
+
+#ifndef MY_SPI_H
+#define MY_SPI_H
+
+class MySPI : public SPI
+{
+public:
+    void puts(char * s);
+    //void putc(char c);
+};
+
+#endif
diff -r 000000000000 -r 1ca47c273d0f main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Jan 30 23:08:52 2012 +0000
@@ -0,0 +1,66 @@
+// Relay bytes received as SPI slave to PC-USB-Serial
+
+#include "mbed.h"
+#include "MODSERIAL.h"
+
+//SPISlave    slave(p5, NC, p7, p8);
+SPISlave    slave(p11, NC, p13, p14);
+MODSERIAL   pc(USBTX,NC,1024,0);
+//Serial    pc(USBTX,NC);
+DigitalOut  ledTX(LED1);
+DigitalOut  ledRX(LED2);
+DigitalOut  ledOV(LED4);
+Timeout     toRXLed;
+
+
+#define fSPI (5000000)
+#define BAUD (115200)
+
+
+void offRXLed(void)
+{
+    ledRX = 0;
+}
+
+void onRX(void)
+{
+    ledRX = 1;
+    toRXLed.attach(offRXLed,0.1);
+}
+
+// This function is called when a character goes from the TX buffer
+// to the Uart THR FIFO register.
+void txCallback(MODSERIAL_IRQ_INFO *q) {
+    ledTX = 1;
+}
+
+// This function is called when TX buffer goes empty
+void txEmpty(MODSERIAL_IRQ_INFO *q) {
+    ledTX = 0;
+}   
+
+// This function is called when TX buffer is Overrun
+void txOVR(MODSERIAL_IRQ_INFO *q) {
+    ledOV = 1;
+}
+
+  
+int main()
+{
+    pc.baud(BAUD);
+    slave.frequency(fSPI);
+    pc.attach(&txEmpty,    MODSERIAL::TxEmpty);
+    pc.attach(&txCallback,    MODSERIAL::TxIrq);
+    pc.attach(&txOVR,    MODSERIAL::TxOvIrq);
+    
+    pc.printf("\n==== SPI 2 USB ready... ===\n");
+
+    while(1)
+    {
+        while ( !slave.receive()  );
+        {
+            pc.putc( slave.read() );   // Read byte from master
+            onRX();
+        }
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r 1ca47c273d0f mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Jan 30 23:08:52 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912
diff -r 000000000000 -r 1ca47c273d0f tinyQ.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tinyQ.c	Mon Jan 30 23:08:52 2012 +0000
@@ -0,0 +1,57 @@
+#include "string.h"
+#include "tinyQ.h"
+
+
+
+/**
+ * \brief Push an Accessibility Event onto our queue for sending to the iphone
+ * \param q pointer to our tinyQ_t
+ * \param c the Event to send to the iphone
+ * \return null if Q full,  1 on success
+ * \sa Qpop(), Qjump()
+ * \ingroup tinyQ
+ */
+int Qpush( tinyQ_t* q, char c )
+{
+   if ( q->fill == Q_SIZE )
+    return 0;
+    
+   __disable_irq();
+   q->fill++;
+   q->data[q->head++] = c;
+   __enable_irq();
+   
+   return 1;
+}
+
+
+/**
+ * \brief Pop an Accessibility Event off our queue for sending to the iphone
+ * \param q pointer to our tinyQ_t
+ * \param c pointer to receive the Event
+ * \return null if Q empty, 1 on success
+ * \sa Qpop(), Qjump()
+ * \ingroup tinyQ
+ */
+ int Qpop( tinyQ_t* q, char * c)
+{
+   if (! q->fill )
+      return 0;
+      
+   __disable_irq();
+   q->fill--;
+   *c = q->data[q->tail++];
+   __enable_irq();
+   
+   return 1;
+}
+
+int Qfree( tinyQ_t* q )
+{
+    return (Q_SIZE - q->fill);
+}
+
+void Qinit( tinyQ_t * q )
+{
+    memset( q, 0, sizeof(q) );
+}
diff -r 000000000000 -r 1ca47c273d0f tinyQ.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tinyQ.h	Mon Jan 30 23:08:52 2012 +0000
@@ -0,0 +1,44 @@
+/** \defgroup tinyQ Tiny Queue
+ * Tiny Queues implements a 4 byte queue for iPhone events with just 1 byte of management overhead.
+ * Head and Tail pointers both inherently wrap at the end of the buffer and no bounds checking is 
+ * necesary IFF the buffer size is set as a power of 2 equal to the width of the pointers !!
+ * i.e head is 2 bits wide so the buffer must be 2^2 = 4 bytes deep.
+ * @{
+ */
+/** NOTE: Q_SIZE MUST be == 2 ^ widthof( tinyQ_t.head ) */
+#define Q_SIZE 256
+
+
+
+/** a tinyQ tracks a 4 byte queue with just 1 byte overhead */
+typedef struct
+{
+    unsigned int head:8;  ///< the bit depth MUST be that power of 2 that is the Q_SIZE
+    unsigned int tail:8;  ///< the bit depth MUST be that power of 2 that is the Q_SIZE
+    unsigned int fill:9;  ///< Must be 1 bit bigger than the head and tail pointers
+    char data[Q_SIZE];     ///< NOTE: Q_SIZE MUST be == 2 ^ widthof( tinyQ_t.head )
+}tinyQ_t;
+
+
+/**
+ * \brief Push an Accessibility Event onto our queue for sending to the iphone
+ * \param q pointer to our tinyQ_t
+ * \param c the Event to send to the iphone
+ * \return null if Q full,  1 on success
+ * \sa Qpop(), Qjump()
+ * \ingroup tinyQ
+ */
+int Qpush( tinyQ_t* q, char c );
+
+/**
+ * \brief Pop an Accessibility Event off our queue for sending to the iphone
+ * \param q pointer to our tinyQ_t
+ * \param c pointer to receive the Event
+ * \return null if Q empty, 1 on success
+ * \sa Qpop(), Qjump()
+ * \ingroup tinyQ
+ */
+int Qpop( tinyQ_t* q, char * c);
+
+int Qfree( tinyQ_t* q );
+void Qinit( tinyQ_t* q );
\ No newline at end of file