Hugh S / Mbed 2 deprecated imu-daq

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers usb-serial.cpp Source File

usb-serial.cpp

00001 #include "mbed.h"
00002 #include "usb-serial.h"
00003 
00004 volatile unsigned wp, rp;
00005 static const int outbuf_size = 128;
00006 static const unsigned pointer_mask = 0x007fU;
00007 char usb_outbuf[outbuf_size];
00008 static bool overflow;
00009 DigitalOut mydiag(p22);;
00010 
00011 extern "C" static void txReadyISR() __irq {
00012 
00013     mydiag = 1;
00014     // If the output buffer has data, copy the head to the UART
00015     if (rp != wp) {
00016         LPC_UART0->THR = usb_outbuf[rp];
00017         rp = (rp+1) & pointer_mask;
00018     }
00019     
00020     // If the output buffer is empty, disable the transmit buffer empty 
00021     // interrupt
00022     if (rp == wp)
00023        LPC_UART0->IER = 0;
00024 
00025 
00026     mydiag = 0;
00027 }
00028 
00029 void usb_serial_init(void) {
00030     
00031     // init the output FIFO
00032     wp = 0; rp = 0;
00033     overflow = false;
00034 
00035     // init the serial port
00036     Serial usb_serial(USBTX, USBRX);    
00037     usb_serial.baud(/*115200*/230400);
00038     usb_serial.format(8, Serial::None, 1);
00039     LPC_UART0->FCR = 7UL;
00040     NVIC_SetVector(UART0_IRQn, (uint32_t)txReadyISR);
00041     NVIC_EnableIRQ(UART0_IRQn);
00042     LPC_UART0->THR = 0; // this is required to start the interrupts, don't understand why
00043 }
00044 
00045 bool usb_serial_putc(char c) {
00046 
00047     if (overflow == false) {
00048         usb_outbuf[wp] = c;      
00049         LPC_UART0->IER = 0; // ---- critical
00050         wp = (wp+1) & pointer_mask;
00051 
00052         if (wp == rp)
00053             overflow = true;
00054         else
00055             overflow = false;
00056 
00057         LPC_UART0->IER = 2UL;  // ---- \critical
00058     } else {
00059         // reinit the output FIFO
00060         wp = 0; rp = 0;
00061         overflow = false;
00062     }
00063     
00064     return overflow;
00065 }