Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nexpaq 0:6c56fb4bc5f0 1 /*
nexpaq 0:6c56fb4bc5f0 2 * To run this test program, link p9 to p10 so the Serial loops
nexpaq 0:6c56fb4bc5f0 3 * back and receives characters it sends.
nexpaq 0:6c56fb4bc5f0 4 */
nexpaq 0:6c56fb4bc5f0 5 #include "mbed.h"
nexpaq 0:6c56fb4bc5f0 6 #include "MODSERIAL.h"
nexpaq 0:6c56fb4bc5f0 7
nexpaq 0:6c56fb4bc5f0 8 DigitalOut led1(LED1);
nexpaq 0:6c56fb4bc5f0 9 DigitalOut led2(LED2);
nexpaq 0:6c56fb4bc5f0 10 DigitalOut led3(LED3);
nexpaq 0:6c56fb4bc5f0 11 DigitalOut led4(LED4);
nexpaq 0:6c56fb4bc5f0 12
nexpaq 0:6c56fb4bc5f0 13 MODSERIAL pc(USBTX, USBRX);
nexpaq 0:6c56fb4bc5f0 14
nexpaq 0:6c56fb4bc5f0 15 /*
nexpaq 0:6c56fb4bc5f0 16 * As experiement, you can define MODSERIAL as show here and see what
nexpaq 0:6c56fb4bc5f0 17 * effects it has on the LEDs.
nexpaq 0:6c56fb4bc5f0 18 *
nexpaq 0:6c56fb4bc5f0 19 * MODSERIAL uart(TX_PIN, RX_PIN, 512);
nexpaq 0:6c56fb4bc5f0 20 * With this, the 512 characters sent can straight into the buffer
nexpaq 0:6c56fb4bc5f0 21 * vary quickly. This means LED1 is only on briefly as the TX buffer
nexpaq 0:6c56fb4bc5f0 22 * fills.
nexpaq 0:6c56fb4bc5f0 23 *
nexpaq 0:6c56fb4bc5f0 24 * MODSERIAL uart(TX_PIN, RX_PIN, 32);
nexpaq 0:6c56fb4bc5f0 25 * With this, the buffer is smaller than the default 256 bytes and
nexpaq 0:6c56fb4bc5f0 26 * therefore LED1 stays on much longer while the system waits for
nexpaq 0:6c56fb4bc5f0 27 * room in the TX buffer.
nexpaq 0:6c56fb4bc5f0 28 */
nexpaq 0:6c56fb4bc5f0 29 MODSERIAL uart(p9, p10);
nexpaq 0:6c56fb4bc5f0 30
nexpaq 0:6c56fb4bc5f0 31 // This function is called when a character goes from the TX buffer
nexpaq 0:6c56fb4bc5f0 32 // to the Uart THR FIFO register.
nexpaq 0:6c56fb4bc5f0 33 void txCallback(MODSERIAL_IRQ_INFO *q) {
nexpaq 0:6c56fb4bc5f0 34 led2 = !led2;
nexpaq 0:6c56fb4bc5f0 35 }
nexpaq 0:6c56fb4bc5f0 36
nexpaq 0:6c56fb4bc5f0 37 // This function is called when TX buffer goes empty
nexpaq 0:6c56fb4bc5f0 38 void txEmpty(MODSERIAL_IRQ_INFO *q) {
nexpaq 0:6c56fb4bc5f0 39 led2 = 0;
nexpaq 0:6c56fb4bc5f0 40 pc.puts(" Done. ");
nexpaq 0:6c56fb4bc5f0 41 }
nexpaq 0:6c56fb4bc5f0 42
nexpaq 0:6c56fb4bc5f0 43 // This function is called when a character goes into the RX buffer.
nexpaq 0:6c56fb4bc5f0 44 void rxCallback(MODSERIAL_IRQ_INFO *q) {
nexpaq 0:6c56fb4bc5f0 45 led3 = !led3;
nexpaq 0:6c56fb4bc5f0 46 pc.putc(uart.getc());
nexpaq 0:6c56fb4bc5f0 47 }
nexpaq 0:6c56fb4bc5f0 48
nexpaq 0:6c56fb4bc5f0 49 int main() {
nexpaq 0:6c56fb4bc5f0 50 int c = 'A';
nexpaq 0:6c56fb4bc5f0 51
nexpaq 0:6c56fb4bc5f0 52 // Ensure the baud rate for the PC "USB" serial is much
nexpaq 0:6c56fb4bc5f0 53 // higher than "uart" baud rate below. (default: 9600)
nexpaq 0:6c56fb4bc5f0 54 // pc.baud(9600);
nexpaq 0:6c56fb4bc5f0 55
nexpaq 0:6c56fb4bc5f0 56 // Use a deliberatly slow baud to fill up the TX buffer
nexpaq 0:6c56fb4bc5f0 57 uart.baud(1200);
nexpaq 0:6c56fb4bc5f0 58
nexpaq 0:6c56fb4bc5f0 59 uart.attach(&txCallback, MODSERIAL::ModTxIrq);
nexpaq 0:6c56fb4bc5f0 60 uart.attach(&rxCallback, MODSERIAL::ModRxIrq);
nexpaq 0:6c56fb4bc5f0 61 uart.attach(&txEmpty, MODSERIAL::ModTxEmpty);
nexpaq 0:6c56fb4bc5f0 62
nexpaq 0:6c56fb4bc5f0 63 // Loop sending characters. We send 512
nexpaq 0:6c56fb4bc5f0 64 // which is twice the default TX/RX buffer size.
nexpaq 0:6c56fb4bc5f0 65
nexpaq 0:6c56fb4bc5f0 66 led1 = 1; // Show start of sending with LED1.
nexpaq 0:6c56fb4bc5f0 67
nexpaq 0:6c56fb4bc5f0 68 for (int loop = 0; loop < 512; loop++) {
nexpaq 0:6c56fb4bc5f0 69 uart.printf("%c", c);
nexpaq 0:6c56fb4bc5f0 70 c++;
nexpaq 0:6c56fb4bc5f0 71 if (c > 'Z') c = 'A';
nexpaq 0:6c56fb4bc5f0 72 }
nexpaq 0:6c56fb4bc5f0 73
nexpaq 0:6c56fb4bc5f0 74 led1 = 0; // Show the end of sending by switching off LED1.
nexpaq 0:6c56fb4bc5f0 75
nexpaq 0:6c56fb4bc5f0 76 // End program. Flash LED4. Notice how LED 2 and 3 continue
nexpaq 0:6c56fb4bc5f0 77 // to flash for a short period while the interrupt system
nexpaq 0:6c56fb4bc5f0 78 // continues to send the characters left in the TX buffer.
nexpaq 0:6c56fb4bc5f0 79
nexpaq 0:6c56fb4bc5f0 80 while(1) {
nexpaq 0:6c56fb4bc5f0 81 led4 = !led4;
nexpaq 0:6c56fb4bc5f0 82 wait(0.25);
nexpaq 0:6c56fb4bc5f0 83 }
nexpaq 0:6c56fb4bc5f0 84 }
nexpaq 0:6c56fb4bc5f0 85
nexpaq 0:6c56fb4bc5f0 86 /*
nexpaq 0:6c56fb4bc5f0 87 * Notes. Here is the sort of output you can expect on your PC/Mac/Linux host
nexpaq 0:6c56fb4bc5f0 88 * machine that is connected to the "pc" USB serial port.
nexpaq 0:6c56fb4bc5f0 89 *
nexpaq 0:6c56fb4bc5f0 90 * ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUV
nexpaq 0:6c56fb4bc5f0 91 * WXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQR
nexpaq 0:6c56fb4bc5f0 92 * STUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMN
nexpaq 0:6c56fb4bc5f0 93 * OPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJ
nexpaq 0:6c56fb4bc5f0 94 * KLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEF
nexpaq 0:6c56fb4bc5f0 95 * GHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZAB
nexpaq 0:6c56fb4bc5f0 96 * CDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQ Done. R
nexpaq 0:6c56fb4bc5f0 97 *
nexpaq 0:6c56fb4bc5f0 98 * Of interest is that last "R" character after the system has said "Done."
nexpaq 0:6c56fb4bc5f0 99 * This comes from the fact that the TxEmpty callback is made when the TX buffer
nexpaq 0:6c56fb4bc5f0 100 * becomes empty. MODSERIAL makes use of the fact that the Uarts built into the
nexpaq 0:6c56fb4bc5f0 101 * LPC17xx device use a 16 byte FIFO on both RX and TX channels. This means that
nexpaq 0:6c56fb4bc5f0 102 * when the TxEmpty callback is made, the TX buffer is empty, but that just means
nexpaq 0:6c56fb4bc5f0 103 * the "last few characters" were written to the TX FIFO. So although the TX
nexpaq 0:6c56fb4bc5f0 104 * buffer has gone empty, the Uart's transmit system is still sending any remaining
nexpaq 0:6c56fb4bc5f0 105 * characters from it's TX FIFO. If you want to be truely sure all the characters
nexpaq 0:6c56fb4bc5f0 106 * you have sent have left the Mbed then call txIsBusy(); This function will
nexpaq 0:6c56fb4bc5f0 107 * return true if characters are still being sent. If it returns false after
nexpaq 0:6c56fb4bc5f0 108 * the Tx buffer is empty then all your characters have been sent.
nexpaq 0:6c56fb4bc5f0 109 *
nexpaq 0:6c56fb4bc5f0 110 * In a similar way, when characters are received into the RX FIFO, the entire
nexpaq 0:6c56fb4bc5f0 111 * FIFO contents is moved to the RX buffer, assuming there is room left in the
nexpaq 0:6c56fb4bc5f0 112 * RX buffer. If there is not, any remaining characters are left in the RX FIFO
nexpaq 0:6c56fb4bc5f0 113 * and will be moved to the RX buffer on the next interrupt or when the running
nexpaq 0:6c56fb4bc5f0 114 * program removes a character(s) from the RX buffer with the getc() method.
nexpaq 0:6c56fb4bc5f0 115 */