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

Dependents:   FLIGHT_CONTROL_AND_COMMUNICATIONS_SYSTEM

Committer:
AjK
Date:
Fri Aug 03 12:28:27 2012 +0000
Revision:
2:2a49171453d5
Add example4.h and ISR user callback code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AjK 2:2a49171453d5 1 /*
AjK 2:2a49171453d5 2 Copyright (c) 2011 Andy Kirkham
AjK 2:2a49171453d5 3
AjK 2:2a49171453d5 4 Permission is hereby granted, free of charge, to any person obtaining a copy
AjK 2:2a49171453d5 5 of this software and associated documentation files (the "Software"), to deal
AjK 2:2a49171453d5 6 in the Software without restriction, including without limitation the rights
AjK 2:2a49171453d5 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
AjK 2:2a49171453d5 8 copies of the Software, and to permit persons to whom the Software is
AjK 2:2a49171453d5 9 furnished to do so, subject to the following conditions:
AjK 2:2a49171453d5 10
AjK 2:2a49171453d5 11 The above copyright notice and this permission notice shall be included in
AjK 2:2a49171453d5 12 all copies or substantial portions of the Software.
AjK 2:2a49171453d5 13
AjK 2:2a49171453d5 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
AjK 2:2a49171453d5 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
AjK 2:2a49171453d5 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AjK 2:2a49171453d5 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
AjK 2:2a49171453d5 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
AjK 2:2a49171453d5 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
AjK 2:2a49171453d5 20 THE SOFTWARE.
AjK 2:2a49171453d5 21 */
AjK 2:2a49171453d5 22
AjK 2:2a49171453d5 23 #ifdef MAX3100_EXAMPLE4_COMPILE
AjK 2:2a49171453d5 24
AjK 2:2a49171453d5 25 /*
AjK 2:2a49171453d5 26 * This is the same as example1.h but shows how to attach a usre defined callback
AjK 2:2a49171453d5 27 * function that is called by the ISR.
AjK 2:2a49171453d5 28 *
AjK 2:2a49171453d5 29 * Connecting up the MAX3100 for this test program. Note, to form a "loopback"
AjK 2:2a49171453d5 30 * the MAX3100 TX pin (13) is connected to the RX pin (12). Don't forget thwe Xtal
AjK 2:2a49171453d5 31 * and power pins that are not shown here. Although I do PullUp mode on the IRQ pin
AjK 2:2a49171453d5 32 * I still needed a real external pull up resistor on the IRQ line. You may need one
AjK 2:2a49171453d5 33 * also.
AjK 2:2a49171453d5 34 * ____________
AjK 2:2a49171453d5 35 * / \
AjK 2:2a49171453d5 36 * Mbed MOSI p5 |-----------------> 1| Din TX | 13 ------\
AjK 2:2a49171453d5 37 * MISO p6 |-----------------> 2| Dout | |
AjK 2:2a49171453d5 38 * SCLK p7 |-----------------> 3| Sclk RX | 12 ------/
AjK 2:2a49171453d5 39 * p8 |-----------------> 4| CS |
AjK 2:2a49171453d5 40 * p9 |-----------------> 5| IRQ | MAX3100
AjK 2:2a49171453d5 41 * +5v ------> 6| SHTD | Xtal and PWR not shown.
AjK 2:2a49171453d5 42 * \____________/
AjK 2:2a49171453d5 43 */
AjK 2:2a49171453d5 44
AjK 2:2a49171453d5 45 #include "mbed.h"
AjK 2:2a49171453d5 46 #include "MAX3100.h"
AjK 2:2a49171453d5 47
AjK 2:2a49171453d5 48 Serial pc(USBTX, USBRX);
AjK 2:2a49171453d5 49 MAX3100 max(p5, p6, p7, p8, p9);
AjK 2:2a49171453d5 50
AjK 2:2a49171453d5 51 // Used to count RX interrupts. Must be declared as
AjK 2:2a49171453d5 52 // volatile as it's used in MyIsrCallback() which is
AjK 2:2a49171453d5 53 // in ISR context and main() which is user context.
AjK 2:2a49171453d5 54 // See http://mbed.org/users/AjK/notebook/regarding-interrupts-use-and-blocking/
AjK 2:2a49171453d5 55 volatile int isrCounter;
AjK 2:2a49171453d5 56
AjK 2:2a49171453d5 57 // User function called when an IRQ from the MAX3100 is activated.
AjK 2:2a49171453d5 58 void MyIsrCallback( int isrType ) {
AjK 2:2a49171453d5 59 // Only count RX IRQs.
AjK 2:2a49171453d5 60 if ( isrType & MAX3100::ISR_RX ) {
AjK 2:2a49171453d5 61 isrCounter++;
AjK 2:2a49171453d5 62 }
AjK 2:2a49171453d5 63 }
AjK 2:2a49171453d5 64
AjK 2:2a49171453d5 65 int main() {
AjK 2:2a49171453d5 66
AjK 2:2a49171453d5 67 isrCounter = 0;
AjK 2:2a49171453d5 68
AjK 2:2a49171453d5 69 // Set the PC USB serial baud rate.
AjK 2:2a49171453d5 70 pc.baud(115200);
AjK 2:2a49171453d5 71
AjK 2:2a49171453d5 72 max.attach_isr_user( MyIsrCallback );
AjK 2:2a49171453d5 73
AjK 2:2a49171453d5 74 max.enableRxIrq();
AjK 2:2a49171453d5 75 max.enableTxIrq();
AjK 2:2a49171453d5 76
AjK 2:2a49171453d5 77 max.printf("\nHello World.\n");
AjK 2:2a49171453d5 78
AjK 2:2a49171453d5 79 // Any byte received on the "USB serial port" is sent to the MAX3100 device.
AjK 2:2a49171453d5 80 // Any byte received by the MAX3100 device is sent to the "USB serial port".
AjK 2:2a49171453d5 81 while (1) {
AjK 2:2a49171453d5 82 if (pc.readable()) {
AjK 2:2a49171453d5 83 int c = pc.getc();
AjK 2:2a49171453d5 84 max.putc(c);
AjK 2:2a49171453d5 85 }
AjK 2:2a49171453d5 86
AjK 2:2a49171453d5 87 if ( isrCounter > 0 ) {
AjK 2:2a49171453d5 88 pc.putc( max.getc() );
AjK 2:2a49171453d5 89 isrCounter--;
AjK 2:2a49171453d5 90 }
AjK 2:2a49171453d5 91 }
AjK 2:2a49171453d5 92 }
AjK 2:2a49171453d5 93
AjK 2:2a49171453d5 94 #endif