MAX3100, an external serial device to add additional serial ports via SPI

Dependents:   FLIGHT_CONTROL_AND_COMMUNICATIONS_SYSTEM

Files at this revision

API Documentation at this revision

Comitter:
AjK
Date:
Fri Aug 03 12:28:27 2012 +0000
Parent:
1:46c8c60e744a
Commit message:
Add example4.h and ISR user callback code

Changed in this revision

ChangeLog.h Show annotated file Show diff for this revision Revisions of this file
MAX3100.cpp Show annotated file Show diff for this revision Revisions of this file
MAX3100.h Show annotated file Show diff for this revision Revisions of this file
example4.h Show annotated file Show diff for this revision Revisions of this file
diff -r 46c8c60e744a -r 2a49171453d5 ChangeLog.h
--- a/ChangeLog.h	Mon Jan 17 01:14:16 2011 +0000
+++ b/ChangeLog.h	Fri Aug 03 12:28:27 2012 +0000
@@ -22,6 +22,9 @@
 
 /*
 
+1.2     03/08/2012
+        Added ability to make a user callback when an IRQ is fired.
+        
 1.1     17/01/2011
         Added this file.
         Altered the way interrupts are disabled. Previously __disable_irq()/__enable_irq()
diff -r 46c8c60e744a -r 2a49171453d5 MAX3100.cpp
--- a/MAX3100.cpp	Mon Jan 17 01:14:16 2011 +0000
+++ b/MAX3100.cpp	Fri Aug 03 12:28:27 2012 +0000
@@ -37,6 +37,10 @@
     _cs_obj      = NULL;
     _cs_method   = NULL;
     
+    _isr_user_function = NULL;
+    _isr_user_obj      = NULL;
+    _isr_user_method   = NULL;
+    
     if (cs != NC) {    
         _cs = new DigitalOut(cs);
         _cs->write( 1 );
@@ -254,7 +258,23 @@
         rx_buffer[rx_buffer_in++] = (char)(data & 0xFF);
         if (rx_buffer_in >= MAX3100_RX_BUFFER_SIZE) rx_buffer_in = 0;
         if (rx_buffer_in == rx_buffer_out) rx_buffer_full = true;
-    }        
+    }
+    
+    int isrType = MAX3100::ISR;     
+    
+    if ( data & MAX3100_CONF_R ) {
+        isrType |= MAX3100::ISR_RX;     
+    }
+    
+    if ( tx_ready ) {
+        isrType |= MAX3100::ISR_TX;     
+    }
+    
+    
+    if (_isr_user_function != NULL) (*_isr_user_function)( isrType );
+    else {
+       if (_isr_user_obj && _isr_user_method) (_isr_user_obj->*_isr_user_method)( isrType );
+    }   
 }
 
 void 
diff -r 46c8c60e744a -r 2a49171453d5 MAX3100.h
--- a/MAX3100.h	Mon Jan 17 01:14:16 2011 +0000
+++ b/MAX3100.h	Fri Aug 03 12:28:27 2012 +0000
@@ -143,6 +143,13 @@
     MAX3100Dummy  *_cs_obj;
     void (MAX3100Dummy::*_cs_method)(int, int);
 
+    // C style callback function pointer for user isr callback.    
+    void (*_isr_user_function)(int); 
+
+    // C++ style callback method pointer for external CS control
+    MAX3100Dummy  *_isr_user_obj;
+    void (MAX3100Dummy::*_isr_user_method)(int);
+    
     // Internal CS control.
     void cs_value(int);
     
@@ -154,6 +161,12 @@
 
 public:
     
+    static const int ISR    = 0;
+    
+    static const int ISR_RX = 1;
+    
+    static const int ISR_TX = 2;
+    
     /** Constructor
      */
     MAX3100() { error( "No pins supplied to constructor" ); }
@@ -319,6 +332,21 @@
         _cs_obj = (MAX3100Dummy *)item; _cs_method = (void (MAX3100Dummy::*)(int, int))method; 
     }
     
+    /** attach_isr_user
+     * is controlling the chip CS line.
+     * @param function A C function pointer
+     */
+    void attach_isr_user(void (*function)(int) = 0) { _isr_user_function = function; }
+    
+    /** attach_isr_user
+     * @param object An object that conatins the callback method.
+     * @param method The method within the object to call.
+     */
+    template<class T> 
+    void attach_isr_user(T* item, void (T::*method)(int)) { 
+        _isr_user_obj = (MAX3100Dummy *)item; _isr_user_method = (void (MAX3100Dummy::*)(int))method; 
+    }
+    
 };
 
 }; // namespace AjK ends.
diff -r 46c8c60e744a -r 2a49171453d5 example4.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example4.h	Fri Aug 03 12:28:27 2012 +0000
@@ -0,0 +1,94 @@
+/*
+    Copyright (c) 2011 Andy Kirkham
+ 
+    Permission is hereby granted, free of charge, to any person obtaining a copy
+    of this software and associated documentation files (the "Software"), to deal
+    in the Software without restriction, including without limitation the rights
+    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+    copies of the Software, and to permit persons to whom the Software is
+    furnished to do so, subject to the following conditions:
+ 
+    The above copyright notice and this permission notice shall be included in
+    all copies or substantial portions of the Software.
+ 
+    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+    THE SOFTWARE.
+*/
+
+#ifdef MAX3100_EXAMPLE4_COMPILE
+
+/*
+ * This is the same as example1.h but shows how to attach a usre defined callback
+ * function that is called by the ISR.
+ *
+ * Connecting up the MAX3100 for this test program. Note, to form a "loopback"
+ * the MAX3100 TX pin (13) is connected to the RX pin (12). Don't forget thwe Xtal
+ * and power pins that are not shown here. Although I do PullUp mode on the IRQ pin
+ * I still needed a real external pull up resistor on the IRQ line. You may need one
+ * also.
+ *                                    ____________
+ *                                   /            \
+ * Mbed MOSI p5 |-----------------> 1| Din     TX | 13 ------\
+ *      MISO p6 |-----------------> 2| Dout       |          |
+ *      SCLK p7 |-----------------> 3| Sclk    RX | 12 ------/
+ *           p8 |-----------------> 4| CS         |
+ *           p9 |-----------------> 5| IRQ        | MAX3100
+ *                      +5v ------> 6| SHTD       | Xtal and PWR not shown.
+ *                                   \____________/
+ */
+
+#include "mbed.h"
+#include "MAX3100.h"
+
+Serial  pc(USBTX, USBRX);
+MAX3100 max(p5, p6, p7, p8, p9);
+
+// Used to count RX interrupts. Must be declared as
+// volatile as it's used in MyIsrCallback() which is 
+// in ISR context and main() which is user context.
+// See http://mbed.org/users/AjK/notebook/regarding-interrupts-use-and-blocking/
+volatile int isrCounter;
+
+// User function called when an IRQ from the MAX3100 is activated.
+void MyIsrCallback( int isrType ) {
+    // Only count RX IRQs.
+    if ( isrType & MAX3100::ISR_RX ) {
+        isrCounter++;
+    }
+}
+
+int main() {
+    
+    isrCounter = 0;
+    
+    // Set the PC USB serial baud rate.
+    pc.baud(115200);
+    
+    max.attach_isr_user( MyIsrCallback );
+    
+    max.enableRxIrq();
+    max.enableTxIrq();
+    
+    max.printf("\nHello World.\n");
+    
+    // Any byte received on the "USB serial port" is sent to the MAX3100 device.
+    // Any byte received by the MAX3100 device is sent to the "USB serial port".
+    while (1) {
+        if (pc.readable()) {
+            int c = pc.getc();
+            max.putc(c);            
+        }
+        
+        if ( isrCounter > 0 ) {
+            pc.putc( max.getc() );
+            isrCounter--;
+        }
+    }    
+}
+
+#endif