MAX3100, an external serial device to add additional serial ports via SPI
example2.h@0:055897ab699b, 2011-01-16 (annotated)
- Committer:
- AjK
- Date:
- Sun Jan 16 18:27:44 2011 +0000
- Revision:
- 0:055897ab699b
- Child:
- 1:46c8c60e744a
1.0 Initial release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AjK | 0:055897ab699b | 1 | /* |
AjK | 0:055897ab699b | 2 | Copyright (c) 2011 Andy Kirkham |
AjK | 0:055897ab699b | 3 | |
AjK | 0:055897ab699b | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
AjK | 0:055897ab699b | 5 | of this software and associated documentation files (the "Software"), to deal |
AjK | 0:055897ab699b | 6 | in the Software without restriction, including without limitation the rights |
AjK | 0:055897ab699b | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
AjK | 0:055897ab699b | 8 | copies of the Software, and to permit persons to whom the Software is |
AjK | 0:055897ab699b | 9 | furnished to do so, subject to the following conditions: |
AjK | 0:055897ab699b | 10 | |
AjK | 0:055897ab699b | 11 | The above copyright notice and this permission notice shall be included in |
AjK | 0:055897ab699b | 12 | all copies or substantial portions of the Software. |
AjK | 0:055897ab699b | 13 | |
AjK | 0:055897ab699b | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
AjK | 0:055897ab699b | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
AjK | 0:055897ab699b | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
AjK | 0:055897ab699b | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
AjK | 0:055897ab699b | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
AjK | 0:055897ab699b | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
AjK | 0:055897ab699b | 20 | THE SOFTWARE. |
AjK | 0:055897ab699b | 21 | */ |
AjK | 0:055897ab699b | 22 | |
AjK | 0:055897ab699b | 23 | #ifdef MAX3100_EXAMPLE_COMPILE |
AjK | 0:055897ab699b | 24 | |
AjK | 0:055897ab699b | 25 | /* |
AjK | 0:055897ab699b | 26 | * Connecting up the MAX3100 for this test program. Note, to form a "loopback" |
AjK | 0:055897ab699b | 27 | * the MAX3100 TX pin (13) is connected to the RX pin (12). Don't forget thwe Xtal |
AjK | 0:055897ab699b | 28 | * and power pins that are not shown here. Although I do PullUp mode on the IRQ pin |
AjK | 0:055897ab699b | 29 | * I still needed a real external pull up resistor on the IRQ lines. You may need to |
AjK | 0:055897ab699b | 30 | * add two pullups to teh two interrupt lines at p9 and p11. |
AjK | 0:055897ab699b | 31 | * ____________ |
AjK | 0:055897ab699b | 32 | * / \ U1 |
AjK | 0:055897ab699b | 33 | * Mbed MOSI p5 |-------*---------------> 1| Din TX | 13 ------\ |
AjK | 0:055897ab699b | 34 | * MISO p6 |-----*-|---------------> 2| Dout | | |
AjK | 0:055897ab699b | 35 | * SCLK p7 |---*-|-|---------------> 3| Sclk RX | 12 ------/ |
AjK | 0:055897ab699b | 36 | * p8 |---|-|-|---------------> 4| CS | |
AjK | 0:055897ab699b | 37 | * p9 |---|-|-|---------------> 5| IRQ | MAX3100 |
AjK | 0:055897ab699b | 38 | * | | | +5v ---> 6| SHTD | Xtal and PWR not shown. |
AjK | 0:055897ab699b | 39 | * | | | \____________/ |
AjK | 0:055897ab699b | 40 | * | | | ____________ |
AjK | 0:055897ab699b | 41 | * | | | / \ U2 |
AjK | 0:055897ab699b | 42 | * | | \---------------> 1| Din TX | 13 ------\ |
AjK | 0:055897ab699b | 43 | * | \-----------------> 2| Dout | | |
AjK | 0:055897ab699b | 44 | * \-------------------> 3| Sclk RX | 12 ------/ |
AjK | 0:055897ab699b | 45 | * p10 |-----------------------> 4| CS | |
AjK | 0:055897ab699b | 46 | * p11 |-----------------------> 5| IRQ | MAX3100 |
AjK | 0:055897ab699b | 47 | * +5v ---> 6| SHTD | Xtal and PWR not shown. |
AjK | 0:055897ab699b | 48 | * \____________/ |
AjK | 0:055897ab699b | 49 | * |
AjK | 0:055897ab699b | 50 | * This example shows two MAX3100 sharing a single SPI bus. Each device, however, |
AjK | 0:055897ab699b | 51 | * still has it's own CS and IRQ signals. |
AjK | 0:055897ab699b | 52 | */ |
AjK | 0:055897ab699b | 53 | |
AjK | 0:055897ab699b | 54 | #include "mbed.h" |
AjK | 0:055897ab699b | 55 | #include "MAX3100.h" |
AjK | 0:055897ab699b | 56 | |
AjK | 0:055897ab699b | 57 | Serial pc(USBTX, USBRX); |
AjK | 0:055897ab699b | 58 | SPI spi(p5, p6, p7); |
AjK | 0:055897ab699b | 59 | MAX3100 *max1; |
AjK | 0:055897ab699b | 60 | MAX3100 *max2; |
AjK | 0:055897ab699b | 61 | |
AjK | 0:055897ab699b | 62 | int main() { |
AjK | 0:055897ab699b | 63 | |
AjK | 0:055897ab699b | 64 | // Set the PC USB serial baud rate. |
AjK | 0:055897ab699b | 65 | pc.baud(115200); |
AjK | 0:055897ab699b | 66 | |
AjK | 0:055897ab699b | 67 | // Format the SPI interface. |
AjK | 0:055897ab699b | 68 | spi.format(16, 0); |
AjK | 0:055897ab699b | 69 | spi.frequency(MAX3100_SPI_FREQ); |
AjK | 0:055897ab699b | 70 | |
AjK | 0:055897ab699b | 71 | // Create the two MAX3100 objects which share the SPI bus. |
AjK | 0:055897ab699b | 72 | max1 = new MAX3100(&spi, p8, p9); |
AjK | 0:055897ab699b | 73 | max2 = new MAX3100(&spi, p10, p11); |
AjK | 0:055897ab699b | 74 | |
AjK | 0:055897ab699b | 75 | // Enable the interrupts. |
AjK | 0:055897ab699b | 76 | max1->enableRxIrq(); |
AjK | 0:055897ab699b | 77 | max1->enableTxIrq(); |
AjK | 0:055897ab699b | 78 | max2->enableRxIrq(); |
AjK | 0:055897ab699b | 79 | max2->enableTxIrq(); |
AjK | 0:055897ab699b | 80 | |
AjK | 0:055897ab699b | 81 | max1->printf("\nHello World.\n"); |
AjK | 0:055897ab699b | 82 | max2->printf("\nHello World.\n"); |
AjK | 0:055897ab699b | 83 | |
AjK | 0:055897ab699b | 84 | // Any byte received on the "USB serial port" is sent to both MAX3100 devices. |
AjK | 0:055897ab699b | 85 | // Any byte received by a MAX3100 device is sent to the "USB serial port". |
AjK | 0:055897ab699b | 86 | while (1) { |
AjK | 0:055897ab699b | 87 | if (pc.readable()) { |
AjK | 0:055897ab699b | 88 | int c = pc.getc(); |
AjK | 0:055897ab699b | 89 | while (!max1->writable()); |
AjK | 0:055897ab699b | 90 | max1->putc(c); |
AjK | 0:055897ab699b | 91 | while (!max2->writable()); |
AjK | 0:055897ab699b | 92 | max2->putc(c); |
AjK | 0:055897ab699b | 93 | } |
AjK | 0:055897ab699b | 94 | if (max1->readable()) { |
AjK | 0:055897ab699b | 95 | pc.putc( max1->getc() ); |
AjK | 0:055897ab699b | 96 | } |
AjK | 0:055897ab699b | 97 | if (max2->readable()) { |
AjK | 0:055897ab699b | 98 | pc.putc( max2->getc() ); |
AjK | 0:055897ab699b | 99 | } |
AjK | 0:055897ab699b | 100 | } |
AjK | 0:055897ab699b | 101 | } |
AjK | 0:055897ab699b | 102 | |
AjK | 0:055897ab699b | 103 | #endif |