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.
Dependents: Frequency_Counter_w_GPS_1PPS FreqCntr_GPS1PPS_F746F4xx_w_recipro
Fork of iSerial by
iSerial.cpp
- Committer:
- kenjiArai
- Date:
- 2015-01-02
- Revision:
- 9:45be5c5d5101
- Parent:
- 8:20759f992d48
- Child:
- 11:6bea021727a1
File content as of revision 9:45be5c5d5101:
//
// iSerial.cpp ... Serial Driver with Interrupt Rec/Send
//
// 2009.11.13 ... Originally written by Y.Kuroda for Renesas H83664
// 2012.08.31 ... Code convert for mbed in C++
// 2015.01.02 ... Added other mbed code by JH1PJL
//
#include <stdarg.h>
#include "mbed.h"
#include "RingBuffer.h"
#include "iSerial.h"
// Device header files
#include "iSerial_LPC1768.h"
#include "iSerial_LPC11U24.h"
#include "iSerial_NucleoF4xx.h"
#include "iSerial_NucleoL152.h"
/*
* Interrupt Function
*/
void
iSerial::rx_handler(void)
{
while(Serial::readable())
rxbuf.save(Serial::getc());
}
void
iSerial::tx_handler(void)
{
while(Serial::writeable() && txbuf.check())
Serial::putc( txbuf.read() );
}
iSerial::iSerial(PinName _tx, PinName _rx, int _txbufsize, int _rxbufsize)
: Serial(_tx, _rx),
tx(_tx),
rx(_rx),
txbufsize(_txbufsize),
rxbufsize(_rxbufsize),
txbuf(RingBuffer(txbufsize)),
rxbuf(RingBuffer(rxbufsize)),
str(new char [txbufsize])
{
__disable_irq();
attach(this, &iSerial::tx_handler, Serial::TxIrq);
attach(this, &iSerial::rx_handler, Serial::RxIrq);
// format(8,Serial::None,1); // default
// baud(baudrate);
__enable_irq();
enable_uart_irq();
}
iSerial::~iSerial()
{
delete [] str;
}
int
iSerial::readable(void)
{
return rxbuf.check();
}
int
iSerial::getc(void)
{
unsigned short int c;
while(!rxbuf.check()); // wait receiving a character
disable_uart_irq();
c = rxbuf.read();
enable_uart_irq();
return c;
}
void
iSerial::putc(short ch)
{
if(txbuf.check()==0 && Serial::writeable()) {
Serial::putc(ch);
} else {
while(txbuf.full()) {
disable_uart_irq();
tx_handler();
enable_uart_irq();
}
disable_uart_irq();
txbuf.save(ch);
enable_uart_irq();
}
}
short int
iSerial::putstr(const char* s)
{
int i=0;
for(; ; i++) {
if(*s==0) break;
putc(*s++);
}
return i;
}
short int
iSerial::puts(const char* s)
{
short int n = putstr(s);
putc(CR);
putc(LF);
return n;
}
char*
iSerial::printf(const char* format, ...)
{
va_list arg;
va_start(arg,format);
vsprintf(str, format, arg);
va_end(arg);
putstr(str);
return str;
}
void
iSerial::flush(void)
{
while(txbuf.check())
enable_uart_irq();
}
