Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of DDRO_Farrari by
serialIO.cpp
- Committer:
- josewang
- Date:
- 2014-02-11
- Revision:
- 6:d772ad205bc7
File content as of revision 6:d772ad205bc7:
#include "mbed.h"
#include "pinout.h"
#include "serialIO.h"
// buffer for TX and RX
char tx_buffer[buffer_size];
char rx_buffer[buffer_size];
// buffer for sscanf and sprintf
char tx_line[80];
char rx_line[80];
// index marking the begin/end of rx_buffer and tx_buffer
volatile int tx_out = 0;
volatile int tx_in = 0;
volatile int rx_out = 0;
volatile int rx_in = 0;
// Copy tx line buffer to large tx buffer for tx interrupt routine
void send_line() {
int i;
char temp_char;
bool empty;
i = 0;
// Start Critical Section - don't interrupt while changing global buffer variables
NVIC_DisableIRQ(USB_IRQn);
empty = (tx_in == tx_out);
while ((i==0) || (tx_line[i-1] != '\n')) {
// Wait if buffer full
if (((tx_in + 1) % buffer_size) == tx_out) {
// End Critical Section - need to let interrupt routine empty buffer by sending
NVIC_EnableIRQ(USB_IRQn);
while (((tx_in + 1) % buffer_size) == tx_out) {
}
// Start Critical Section - don't interrupt while changing global buffer variables
NVIC_DisableIRQ(USB_IRQn);
}
tx_buffer[tx_in] = tx_line[i];
i++;
tx_in = (tx_in + 1) % buffer_size;
}
if (pc.writeable() && (empty)) {
temp_char = tx_buffer[tx_out];
tx_out = (tx_out + 1) % buffer_size;
// Send first character to start tx interrupts, if stopped
pc.putc(temp_char);
}
// End Critical Section
NVIC_EnableIRQ(USB_IRQn);
return;
}
// Interupt Routine to write out data to serial port
void Tx_interrupt() {
// Loop to fill more than one character in UART's transmit FIFO buffer
// Stop if buffer empty
while ((pc.writeable()) && (tx_in != tx_out)) {
pc.putc(tx_buffer[tx_out]);
tx_out = (tx_out + 1) % buffer_size;
}
return;
}
// Read a line from the large rx buffer from rx interrupt routine
void read_line() {
int i;
i = 0;
// Start Critical Section - don't interrupt while changing global buffer variables
NVIC_DisableIRQ(USB_IRQn);
// Loop reading rx buffer characters until end of line character
while ((i==0) || (rx_line[i-1] != '\r')) {
// Wait if buffer empty
if (rx_in == rx_out) {
// End Critical Section - need to allow rx interrupt to get new characters for buffer
NVIC_EnableIRQ(USB_IRQn);
while (rx_in == rx_out) {
}
// Start Critical Section - don't interrupt while changing global buffer variables
NVIC_DisableIRQ(USB_IRQn);
}
rx_line[i] = rx_buffer[rx_out];
i++;
rx_out = (rx_out + 1) % buffer_size;
}
// End Critical Section
NVIC_EnableIRQ(USB_IRQn);
rx_line[i-1] = 0;
return;
}
// Interupt Routine to read in data from serial port
void Rx_interrupt() {
// Loop just in case more than one character is in UART's receive FIFO buffer
// Stop if buffer full
while ((pc.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
rx_buffer[rx_in] = pc.getc();
// Uncomment to Echo to USB serial to watch data flow
// monitor_device.putc(rx_buffer[rx_in]);
rx_in = (rx_in + 1) % buffer_size;
}
return;
}
