Contiki-NG
Toggle main menu visibility
Loading...
Searching...
No Matches
uart1.c
1
/*
2
* Copyright (c) 2010, Swedish Institute of Computer Science
3
* All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
13
* 3. Neither the name of the Institute nor the names of its contributors
14
* may be used to endorse or promote products derived from this software
15
* without specific prior written permission.
16
*
17
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
* SUCH DAMAGE.
28
*
29
*/
30
31
/*
32
* Machine dependent MSP430X UART1 code.
33
*/
34
#include "contiki.h"
35
#include <stdlib.h>
36
#include "
dev/uart1.h
"
37
#include "dev/watchdog.h"
38
#include "
lib/ringbuf.h
"
39
#include "isr_compat.h"
40
41
static
int (*uart1_input_handler)(
unsigned
char
c);
42
43
static
volatile
uint8_t transmitting;
44
45
#ifdef UART1_CONF_TX_WITH_INTERRUPT
46
#define TX_WITH_INTERRUPT UART1_CONF_TX_WITH_INTERRUPT
47
#else
/* UART1_CONF_TX_WITH_INTERRUPT */
48
#define TX_WITH_INTERRUPT 1
49
#endif
/* UART1_CONF_TX_WITH_INTERRUPT */
50
51
#if TX_WITH_INTERRUPT
52
#define TXBUFSIZE 64
53
54
static
struct
ringbuf
txbuf;
55
static
uint8_t txbuf_data[TXBUFSIZE];
56
#endif
/* TX_WITH_INTERRUPT */
57
58
/*---------------------------------------------------------------------------*/
59
uint8_t
60
uart1_active(
void
)
61
{
62
return
(UCA0STAT & UCBUSY) | transmitting;
63
}
64
/*---------------------------------------------------------------------------*/
65
void
66
uart1_set_input(
int
(*input)(
unsigned
char
c))
67
{
68
uart1_input_handler = input;
69
}
70
/*---------------------------------------------------------------------------*/
71
void
72
uart1_writeb(
unsigned
char
c)
73
{
74
/* watchdog_periodic(); */
75
#if TX_WITH_INTERRUPT
76
77
/* Put the outgoing byte on the transmission buffer. If the buffer
78
is full, we just keep on trying to put the byte into the buffer
79
until it is possible to put it there. */
80
while
(
ringbuf_put
(&txbuf, c) == 0);
81
82
/* If there is no transmission going, we need to start it by putting
83
the first byte into the UART. */
84
if
(transmitting == 0) {
85
transmitting = 1;
86
UCA0TXBUF =
ringbuf_get
(&txbuf);
87
}
88
89
#else
/* TX_WITH_INTERRUPT */
90
91
/* Loop until the transmission buffer is available. */
92
while
(!(IFG2 & UCA0TXIFG));
93
94
/* Transmit the data. */
95
UCA0TXBUF = c;
96
#endif
/* TX_WITH_INTERRUPT */
97
}
98
/*---------------------------------------------------------------------------*/
99
/**
100
* Initalize the RS232 port.
101
*
102
*/
103
void
104
uart1_init
(
unsigned
long
ubr)
105
{
106
/* RS232 */
107
P3SEL |= 0x30;
/* P3.4,5 = USCI_A0 TXD/RXD */
108
UCA0CTL1 |= UCSSEL_2;
/* CLK = SMCLK */
109
UCA0BR0 = 0x45;
/* 8MHz/115200 = 69 = 0x45 */
110
UCA0BR1 = 0x00;
111
UCA0MCTL = UCBRS2;
/* Modulation UCBRSx = 4 */
112
UCA0CTL1 &= ~UCSWRST;
/* Initialize USCI state machine */
113
114
transmitting = 0;
115
116
}
117
/*---------------------------------------------------------------------------*/
118
ISR(USCIAB1RX, uart1_rx_interrupt)
119
{
120
uint8_t c;
121
122
/* Check status register for receive errors. */
123
if
(UCA0STAT & UCRXERR) {
124
c = UCA0RXBUF;
/* Clear error flags by forcing a dummy read. */
125
}
else
{
126
c = UCA0RXBUF;
127
if
(uart1_input_handler != NULL) {
128
if
(uart1_input_handler(c)) {
129
LPM4_EXIT;
130
}
131
}
132
}
133
}
134
/*---------------------------------------------------------------------------*/
135
#if TX_WITH_INTERRUPT
136
ISR(USCIAB1TX, uart1_tx_interrupt)
137
{
138
if
(IFG2 & UCA0TXIFG) {
139
if
(
ringbuf_elements
(&txbuf) == 0) {
140
transmitting = 0;
141
}
else
{
142
UCA0TXBUF =
ringbuf_get
(&txbuf);
143
}
144
}
145
}
146
#endif
/* TX_WITH_INTERRUPT */
147
/*---------------------------------------------------------------------------*/
ringbuf_get
int ringbuf_get(struct ringbuf *r)
Get a byte from the ring buffer.
Definition
ringbuf.c:71
ringbuf_put
int ringbuf_put(struct ringbuf *r, uint8_t c)
Insert a byte into the ring buffer.
Definition
ringbuf.c:44
ringbuf_elements
int ringbuf_elements(struct ringbuf *r)
Get the number of elements currently in the ring buffer.
Definition
ringbuf.c:110
ringbuf.h
Header file for the ring buffer library.
ringbuf
Structure that holds the state of a ring buffer.
Definition
ringbuf.h:68
uart1.h
A brief description of what this file is.
uart1_init
void uart1_init(unsigned long ubr)
Initalize the RS232 port.
Definition
uart1.c:142
arch
cpu
msp430
f2xxx
uart1.c
Generated on
for Contiki-NG by
1.17.0