modify to nucleo stm32F411
Dependencies: RingBuffer
Dependents: MX_control Program_BEAR_Protocol SwitchMode BEAR_Motion ... more
Fork of iSerial by
iSerial.cpp
- Committer:
- ykuroda
- Date:
- 2012-09-03
- Revision:
- 5:d83fc550ccbc
- Parent:
- 4:b38ef9675d39
- Child:
- 6:8d4b95b90c3b
File content as of revision 5:d83fc550ccbc:
// // 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++ // #include <stdarg.h> #include "mbed.h" #include "RingBuffer.h" #include "iSerial.h" //DigitalOut led1(LED1); void iSerial::enable_uart_irq(void) { switch(tx){ case USBTX: #if defined(TARGET_LPC1768) NVIC_EnableIRQ(UART2_IRQn); #elif defined(TARGET_LPC11U24) NVIC_EnableIRQ(UART_IRQn); #endif // led1 = !led1; break; case p9: #if defined(TARGET_LPC1768) NVIC_EnableIRQ(UART1_IRQn); #elif defined(TARGET_LPC11U24) NVIC_EnableIRQ(UART_IRQn); #endif break; #if defined(TARGET_LPC1768) case p13: NVIC_EnableIRQ(UART3_IRQn); break; case p28: NVIC_EnableIRQ(UART0_IRQn); break; #endif } } /* * Interrupt Function */ void iSerial::rx_handler(void) { if(Serial::readable()){ rxbuf.save(Serial::getc()); } enable_uart_irq(); } void iSerial::tx_handler(void) { if(Serial::writeable()){ if(txbuf.check()){ Serial::putc( txbuf.read() ); } } enable_uart_irq(); } iSerial::iSerial(PinName _tx, PinName _rx, const char *_name, int _txbufsize, int _rxbufsize) : Serial(_tx, _rx, _name), 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()); c = rxbuf.read(); return c; } void iSerial::putc(short ch) { if(txbuf.check()==0 && Serial::writeable()){ Serial::putc(ch); } else { while(txbuf.full()); 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; }