UART1 buffered serial driver, requires RTOS

Dependents:   Serial_interrupts_buffered HARP2 HARP3

Buffered serial UART1 - Setup to work with UART1 (p13,p14)

Uses RTOS to block current thread.

Reference: http://mbed.org/users/tylerjw/notebook/buffered-serial-with-rtos/

buffered_serial.h

Committer:
tylerjw
Date:
2012-12-13
Revision:
3:a4a21e18acd1
Parent:
0:707b9f3904dd
Child:
4:d3122119f92b

File content as of revision 3:a4a21e18acd1:

/*
    Buffered serial 1 - Setup to work with UART1 (p13,p14)
*/

#ifndef BUFFERED_SERIAL_H
#define BUFFERED_SERIAL_H

#define BUFFER_SIZE     255
#define LINE_SIZE       80
#define NEXT(x)         ((x+1)&BUFFER_SIZE)
#define IS_TX_FULL      (((tx_in + 1) & BUFFER_SIZE) == tx_out)
#define IS_RX_FULL      (((rx_in + 1) & BUFFER_SIZE) == rx_out)
#define IS_RX_EMPTY     (rx_in == rx_out)

#include "mbed.h"
#include "rtos.h"

class BufferedSerial : public Serial
{
public:
    BufferedSerial(PinName tx, PinName rx);

    void send_line(char*);
    void read_line(char*);

private:
    void Tx_interrupt();
    void Rx_interrupt();

// for disabling the irq
    IRQn device_irqn;
    
// Circular buffers for serial TX and RX data - used by interrupt routines
// might need to increase buffer size for high baud rates
    char tx_buffer[BUFFER_SIZE];
    char rx_buffer[BUFFER_SIZE];
// Circular buffer pointers
// volatile makes read-modify-write atomic
    volatile int tx_in;
    volatile int tx_out;
    volatile int rx_in;
    volatile int rx_out;
// Line buffers for sprintf and sscanf
    char tx_line[LINE_SIZE];
    char rx_line[LINE_SIZE];

    //DigitalOut led1; // debug
    //DigitalOut led2;
    
    Semaphore rx_sem;
    Semaphore tx_sem;
};

#endif