summaryrefslogtreecommitdiffstats
path: root/lib/msp430/TimerSerial.h
blob: 58dc66016a07d9228431fb619af902c5f1661375 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
  TimerSerial.h - Timer based serial library for MSP430
  Copyright (c) 2012 Robert Wessels.  All right reserved.
  Modeled after Nicholas Zambetti's HardwareSerial.
  and
  msp430softserial by Rick Kimball
  https://github.com/RickKimball/msp430softserial/

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#ifndef TimerSerial_h
#define TimerSerial_h

#include <inttypes.h>
#include <Stream.h>

#define TX_PIN BIT1	// TXD on P1.1
#define RX_PIN BIT2	// RXD on P1.2

// running at < 3MHz requires a lower baud rate
#ifndef TIMERSERIAL_BAUD
#if F_CPU > 1000000
    #define TIMERSERIAL_BAUD 9600
#else
    #define TIMERSERIAL_BAUD 4800
#endif
#endif

#if defined(__MSP430G2231__)
 #define NEEDS_BUFF_PTR 1 // sadly, the g2231 seems to have a problem if we don't use the original structure
 struct ring_buffer_ts; // forward declaration
#else
 #define NEEDS_BUFF_PTR 0 // everything else is happy to run fully optimized
#endif

class TimerSerial : public Stream
{
public:
    TimerSerial(void);

    void begin(unsigned long baud = TIMERSERIAL_BAUD);
    void end(void);

    virtual size_t write(uint8_t byte);
    virtual int read(void);
    virtual int available(void);
    virtual void flush(void);
    virtual int peek(void);

    using Print::write;

private:

#if NEEDS_BUFF_PTR
    ring_buffer_ts *_rx_buffer; // gcc seems to get confused on the g2231 without this
    ring_buffer_ts *_tx_buffer;
#endif

};

#if !defined(__MSP430_HAS_USCI__) && !defined(__MSP430_HAS_EUSCI_A0__)
extern TimerSerial Serial;
#endif
extern void serialEventRun(void) __attribute__((weak));
#endif