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

Dependents:   FLIGHT_CONTROL_AND_COMMUNICATIONS_SYSTEM

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers example4.h Source File

example4.h

00001 /*
00002     Copyright (c) 2011 Andy Kirkham
00003  
00004     Permission is hereby granted, free of charge, to any person obtaining a copy
00005     of this software and associated documentation files (the "Software"), to deal
00006     in the Software without restriction, including without limitation the rights
00007     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008     copies of the Software, and to permit persons to whom the Software is
00009     furnished to do so, subject to the following conditions:
00010  
00011     The above copyright notice and this permission notice shall be included in
00012     all copies or substantial portions of the Software.
00013  
00014     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020     THE SOFTWARE.
00021 */
00022 
00023 #ifdef MAX3100_EXAMPLE4_COMPILE
00024 
00025 /*
00026  * This is the same as example1.h but shows how to attach a usre defined callback
00027  * function that is called by the ISR.
00028  *
00029  * Connecting up the MAX3100 for this test program. Note, to form a "loopback"
00030  * the MAX3100 TX pin (13) is connected to the RX pin (12). Don't forget thwe Xtal
00031  * and power pins that are not shown here. Although I do PullUp mode on the IRQ pin
00032  * I still needed a real external pull up resistor on the IRQ line. You may need one
00033  * also.
00034  *                                    ____________
00035  *                                   /            \
00036  * Mbed MOSI p5 |-----------------> 1| Din     TX | 13 ------\
00037  *      MISO p6 |-----------------> 2| Dout       |          |
00038  *      SCLK p7 |-----------------> 3| Sclk    RX | 12 ------/
00039  *           p8 |-----------------> 4| CS         |
00040  *           p9 |-----------------> 5| IRQ        | MAX3100
00041  *                      +5v ------> 6| SHTD       | Xtal and PWR not shown.
00042  *                                   \____________/
00043  */
00044 
00045 #include "mbed.h"
00046 #include "MAX3100.h"
00047 
00048 Serial  pc(USBTX, USBRX);
00049 MAX3100 max(p5, p6, p7, p8, p9);
00050 
00051 // Used to count RX interrupts. Must be declared as
00052 // volatile as it's used in MyIsrCallback() which is 
00053 // in ISR context and main() which is user context.
00054 // See http://mbed.org/users/AjK/notebook/regarding-interrupts-use-and-blocking/
00055 volatile int isrCounter;
00056 
00057 // User function called when an IRQ from the MAX3100 is activated.
00058 void MyIsrCallback( int isrType ) {
00059     // Only count RX IRQs.
00060     if ( isrType & MAX3100::ISR_RX ) {
00061         isrCounter++;
00062     }
00063 }
00064 
00065 int main() {
00066     
00067     isrCounter = 0;
00068     
00069     // Set the PC USB serial baud rate.
00070     pc.baud(115200);
00071     
00072     max.attach_isr_user( MyIsrCallback );
00073     
00074     max.enableRxIrq();
00075     max.enableTxIrq();
00076     
00077     max.printf("\nHello World.\n");
00078     
00079     // Any byte received on the "USB serial port" is sent to the MAX3100 device.
00080     // Any byte received by the MAX3100 device is sent to the "USB serial port".
00081     while (1) {
00082         if (pc.readable()) {
00083             int c = pc.getc();
00084             max.putc(c);            
00085         }
00086         
00087         if ( isrCounter > 0 ) {
00088             pc.putc( max.getc() );
00089             isrCounter--;
00090         }
00091     }    
00092 }
00093 
00094 #endif