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/

Committer:
tylerjw
Date:
Mon Dec 17 22:35:51 2012 +0000
Revision:
4:d3122119f92b
Parent:
3:a4a21e18acd1
naming conventions and documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tylerjw 0:707b9f3904dd 1 /*
tylerjw 4:d3122119f92b 2 * @file buffered_serial.h
tylerjw 4:d3122119f92b 3 * @author Tyler Weaver
tylerjw 4:d3122119f92b 4 *
tylerjw 4:d3122119f92b 5 * @section LICENSE
tylerjw 4:d3122119f92b 6 *
tylerjw 4:d3122119f92b 7 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
tylerjw 4:d3122119f92b 8 * and associated documentation files (the "Software"), to deal in the Software without restriction,
tylerjw 4:d3122119f92b 9 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
tylerjw 4:d3122119f92b 10 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
tylerjw 4:d3122119f92b 11 * furnished to do so, subject to the following conditions:
tylerjw 4:d3122119f92b 12 *
tylerjw 4:d3122119f92b 13 * The above copyright notice and this permission notice shall be included in all copies or
tylerjw 4:d3122119f92b 14 * substantial portions of the Software.
tylerjw 4:d3122119f92b 15 *
tylerjw 4:d3122119f92b 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
tylerjw 4:d3122119f92b 17 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
tylerjw 4:d3122119f92b 18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
tylerjw 4:d3122119f92b 19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
tylerjw 4:d3122119f92b 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
tylerjw 4:d3122119f92b 21 *
tylerjw 4:d3122119f92b 22 * @section DESCRIPTION
tylerjw 4:d3122119f92b 23 *
tylerjw 4:d3122119f92b 24 * Buffered serial UART1 - Setup to work with UART1 (p13,p14)
tylerjw 4:d3122119f92b 25 *
tylerjw 4:d3122119f92b 26 * Uses RTOS to block current thread.
tylerjw 4:d3122119f92b 27 */
tylerjw 0:707b9f3904dd 28
tylerjw 0:707b9f3904dd 29 #ifndef BUFFERED_SERIAL_H
tylerjw 0:707b9f3904dd 30 #define BUFFERED_SERIAL_H
tylerjw 0:707b9f3904dd 31
tylerjw 0:707b9f3904dd 32 #define BUFFER_SIZE 255
tylerjw 0:707b9f3904dd 33 #define LINE_SIZE 80
tylerjw 0:707b9f3904dd 34 #define NEXT(x) ((x+1)&BUFFER_SIZE)
tylerjw 4:d3122119f92b 35 #define IS_TX_FULL (((tx_in_ + 1) & BUFFER_SIZE) == tx_out_)
tylerjw 4:d3122119f92b 36 #define IS_RX_FULL (((rx_in_ + 1) & BUFFER_SIZE) == rx_out_)
tylerjw 4:d3122119f92b 37 #define IS_RX_EMPTY (rx_in_ == rx_out_)
tylerjw 0:707b9f3904dd 38
tylerjw 0:707b9f3904dd 39 #include "mbed.h"
tylerjw 0:707b9f3904dd 40 #include "rtos.h"
tylerjw 4:d3122119f92b 41 /** Buffered serial UART1 - Setup to work with UART1 (p13,p14)
tylerjw 4:d3122119f92b 42 *
tylerjw 4:d3122119f92b 43 * Uses RTOS to block current thread.
tylerjw 4:d3122119f92b 44 */
tylerjw 0:707b9f3904dd 45 class BufferedSerial : public Serial
tylerjw 0:707b9f3904dd 46 {
tylerjw 0:707b9f3904dd 47 public:
tylerjw 4:d3122119f92b 48 /** Default Constructor
tylerjw 4:d3122119f92b 49 * Initialize UART1 - Serial(p13,p14)
tylerjw 4:d3122119f92b 50 * Initialize Semaphores
tylerjw 4:d3122119f92b 51 * Attach Serial Interrupts
tylerjw 4:d3122119f92b 52 */
tylerjw 4:d3122119f92b 53 BufferedSerial();
tylerjw 0:707b9f3904dd 54
tylerjw 4:d3122119f92b 55 /** Put cstring in buffer/output
tylerjw 4:d3122119f92b 56 *
tylerjw 4:d3122119f92b 57 * @param cstring to put in buffer for printing (max length = 80 characters)
tylerjw 4:d3122119f92b 58 */
tylerjw 4:d3122119f92b 59 void put_line(char*);
tylerjw 4:d3122119f92b 60
tylerjw 4:d3122119f92b 61 /** Gets a cstring from the buffer/input
tylerjw 4:d3122119f92b 62 *
tylerjw 4:d3122119f92b 63 * @param buffer cstring to put line from buffer in (ends at '\n' or 80 characters)
tylerjw 4:d3122119f92b 64 */
tylerjw 4:d3122119f92b 65 void get_line(char*);
tylerjw 0:707b9f3904dd 66
tylerjw 0:707b9f3904dd 67 private:
tylerjw 0:707b9f3904dd 68 void Tx_interrupt();
tylerjw 0:707b9f3904dd 69 void Rx_interrupt();
tylerjw 0:707b9f3904dd 70
tylerjw 0:707b9f3904dd 71 // for disabling the irq
tylerjw 0:707b9f3904dd 72 IRQn device_irqn;
tylerjw 0:707b9f3904dd 73
tylerjw 0:707b9f3904dd 74 // Circular buffers for serial TX and RX data - used by interrupt routines
tylerjw 0:707b9f3904dd 75 // might need to increase buffer size for high baud rates
tylerjw 4:d3122119f92b 76 char tx_buffer_[BUFFER_SIZE];
tylerjw 4:d3122119f92b 77 char rx_buffer_[BUFFER_SIZE];
tylerjw 0:707b9f3904dd 78 // Circular buffer pointers
tylerjw 0:707b9f3904dd 79 // volatile makes read-modify-write atomic
tylerjw 4:d3122119f92b 80 volatile int tx_in_;
tylerjw 4:d3122119f92b 81 volatile int tx_out_;
tylerjw 4:d3122119f92b 82 volatile int rx_in_;
tylerjw 4:d3122119f92b 83 volatile int rx_out_;
tylerjw 0:707b9f3904dd 84 // Line buffers for sprintf and sscanf
tylerjw 4:d3122119f92b 85 char tx_line_[LINE_SIZE];
tylerjw 4:d3122119f92b 86 char rx_line_[LINE_SIZE];
tylerjw 0:707b9f3904dd 87
tylerjw 3:a4a21e18acd1 88 //DigitalOut led1; // debug
tylerjw 3:a4a21e18acd1 89 //DigitalOut led2;
tylerjw 0:707b9f3904dd 90
tylerjw 4:d3122119f92b 91 Semaphore rx_sem_;
tylerjw 4:d3122119f92b 92 Semaphore tx_sem_;
tylerjw 0:707b9f3904dd 93 };
tylerjw 0:707b9f3904dd 94
tylerjw 0:707b9f3904dd 95 #endif