Hugh Shane
/
imu-daq
Read data from an ADIS16355 IMU and write it to the USB port.
usb-serial.cpp@0:c6ee363ac724, 2011-08-10 (annotated)
- Committer:
- yahugh
- Date:
- Wed Aug 10 21:50:36 2011 +0000
- Revision:
- 0:c6ee363ac724
initial release, still has some problems but might be of interest to others regardless
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yahugh | 0:c6ee363ac724 | 1 | #include "mbed.h" |
yahugh | 0:c6ee363ac724 | 2 | #include "usb-serial.h" |
yahugh | 0:c6ee363ac724 | 3 | |
yahugh | 0:c6ee363ac724 | 4 | volatile unsigned wp, rp; |
yahugh | 0:c6ee363ac724 | 5 | static const int outbuf_size = 128; |
yahugh | 0:c6ee363ac724 | 6 | static const unsigned pointer_mask = 0x007fU; |
yahugh | 0:c6ee363ac724 | 7 | char usb_outbuf[outbuf_size]; |
yahugh | 0:c6ee363ac724 | 8 | static bool overflow; |
yahugh | 0:c6ee363ac724 | 9 | DigitalOut mydiag(p22);; |
yahugh | 0:c6ee363ac724 | 10 | |
yahugh | 0:c6ee363ac724 | 11 | extern "C" static void txReadyISR() __irq { |
yahugh | 0:c6ee363ac724 | 12 | |
yahugh | 0:c6ee363ac724 | 13 | mydiag = 1; |
yahugh | 0:c6ee363ac724 | 14 | // If the output buffer has data, copy the head to the UART |
yahugh | 0:c6ee363ac724 | 15 | if (rp != wp) { |
yahugh | 0:c6ee363ac724 | 16 | LPC_UART0->THR = usb_outbuf[rp]; |
yahugh | 0:c6ee363ac724 | 17 | rp = (rp+1) & pointer_mask; |
yahugh | 0:c6ee363ac724 | 18 | } |
yahugh | 0:c6ee363ac724 | 19 | |
yahugh | 0:c6ee363ac724 | 20 | // If the output buffer is empty, disable the transmit buffer empty |
yahugh | 0:c6ee363ac724 | 21 | // interrupt |
yahugh | 0:c6ee363ac724 | 22 | if (rp == wp) |
yahugh | 0:c6ee363ac724 | 23 | LPC_UART0->IER = 0; |
yahugh | 0:c6ee363ac724 | 24 | |
yahugh | 0:c6ee363ac724 | 25 | |
yahugh | 0:c6ee363ac724 | 26 | mydiag = 0; |
yahugh | 0:c6ee363ac724 | 27 | } |
yahugh | 0:c6ee363ac724 | 28 | |
yahugh | 0:c6ee363ac724 | 29 | void usb_serial_init(void) { |
yahugh | 0:c6ee363ac724 | 30 | |
yahugh | 0:c6ee363ac724 | 31 | // init the output FIFO |
yahugh | 0:c6ee363ac724 | 32 | wp = 0; rp = 0; |
yahugh | 0:c6ee363ac724 | 33 | overflow = false; |
yahugh | 0:c6ee363ac724 | 34 | |
yahugh | 0:c6ee363ac724 | 35 | // init the serial port |
yahugh | 0:c6ee363ac724 | 36 | Serial usb_serial(USBTX, USBRX); |
yahugh | 0:c6ee363ac724 | 37 | usb_serial.baud(/*115200*/230400); |
yahugh | 0:c6ee363ac724 | 38 | usb_serial.format(8, Serial::None, 1); |
yahugh | 0:c6ee363ac724 | 39 | LPC_UART0->FCR = 7UL; |
yahugh | 0:c6ee363ac724 | 40 | NVIC_SetVector(UART0_IRQn, (uint32_t)txReadyISR); |
yahugh | 0:c6ee363ac724 | 41 | NVIC_EnableIRQ(UART0_IRQn); |
yahugh | 0:c6ee363ac724 | 42 | LPC_UART0->THR = 0; // this is required to start the interrupts, don't understand why |
yahugh | 0:c6ee363ac724 | 43 | } |
yahugh | 0:c6ee363ac724 | 44 | |
yahugh | 0:c6ee363ac724 | 45 | bool usb_serial_putc(char c) { |
yahugh | 0:c6ee363ac724 | 46 | |
yahugh | 0:c6ee363ac724 | 47 | if (overflow == false) { |
yahugh | 0:c6ee363ac724 | 48 | usb_outbuf[wp] = c; |
yahugh | 0:c6ee363ac724 | 49 | LPC_UART0->IER = 0; // ---- critical |
yahugh | 0:c6ee363ac724 | 50 | wp = (wp+1) & pointer_mask; |
yahugh | 0:c6ee363ac724 | 51 | |
yahugh | 0:c6ee363ac724 | 52 | if (wp == rp) |
yahugh | 0:c6ee363ac724 | 53 | overflow = true; |
yahugh | 0:c6ee363ac724 | 54 | else |
yahugh | 0:c6ee363ac724 | 55 | overflow = false; |
yahugh | 0:c6ee363ac724 | 56 | |
yahugh | 0:c6ee363ac724 | 57 | LPC_UART0->IER = 2UL; // ---- \critical |
yahugh | 0:c6ee363ac724 | 58 | } else { |
yahugh | 0:c6ee363ac724 | 59 | // reinit the output FIFO |
yahugh | 0:c6ee363ac724 | 60 | wp = 0; rp = 0; |
yahugh | 0:c6ee363ac724 | 61 | overflow = false; |
yahugh | 0:c6ee363ac724 | 62 | } |
yahugh | 0:c6ee363ac724 | 63 | |
yahugh | 0:c6ee363ac724 | 64 | return overflow; |
yahugh | 0:c6ee363ac724 | 65 | } |