Contiki 3.x
rs232.h
1 /*
2  * Copyright (c) 2005, 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  * This file is part of the Contiki operating system.
30  *
31  * Author: Adam Dunkels <adam@sics.se>
32  * Simon Barner <barner@in.tum.de>
33  *
34  */
35 
36 #ifndef RS232_H_
37 #define RS232_H_
38 
39 #include <avr/pgmspace.h>
40 #include "contiki-conf.h"
41 
42 #if defined (__AVR_ATmega128__)
43 #include "dev/rs232_atmega128.h"
44 #elif defined (__AVR_ATmega1281__)
45 #include "dev/rs232_atmega1281.h"
46 #elif defined (__AVR_ATmega1284P__)
47 #include "dev/rs232_atmega1284.h"
48 #elif defined (__AVR_AT90USB1287__)
49 #include "dev/rs232_at90usb1287.h"
50 #elif defined (__AVR_ATmega128RFA1__)
52 #elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega328P__)
53 #include "dev/rs232_atmega644.h"
54 #elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega8515__) \
55  || defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__)
56 #include "dev/rs232_atmega32.h"
57 #else
58 #error "Please implement a rs232 header for your MCU (or set the MCU type \
59 in contiki-conf.h)."
60 #endif
61 
62 /******************************************************************************/
63 /*** Baud rates */
64 /******************************************************************************/
65 #define BAUD_RATE(x) (F_CPU/16/x-1)
66 
67 /**
68  * \brief Initialize the RS232 module
69  *
70  * This function is called from the boot up code to
71  * initalize the RS232 module.
72  * \param port The RS232 port to be used.
73  * \param bd The baud rate of the connection.
74  * \param ffmt The frame format of the connection, i.e. parity mode,
75  * number of stop and data bits, ...
76  */
77 void
78 rs232_init (uint8_t port, uint8_t bd, uint8_t ffmt);
79 
80 /**
81  * \brief Set an input handler for incoming RS232 data
82  * \param port The RS232 port to be used.
83  * \param f A pointer to a byte input handler
84  *
85  * This function sets the input handler for incoming RS232
86  * data. The input handler function is called for every
87  * incoming data byte. The function is called from the
88  * RS232 interrupt handler, so care must be taken when
89  * implementing the input handler to avoid race
90  * conditions.
91  *
92  * The return value of the input handler affects the sleep
93  * mode of the CPU: if the input handler returns non-zero
94  * (true), the CPU is awakened to let other processing
95  * take place. If the input handler returns zero, the CPU
96  * is kept sleeping.
97  */
98 void
99 rs232_set_input(uint8_t port, int (* f)(unsigned char));
100 
101 
102 /**
103  * \brief Print a text string from program memory on RS232
104  * \param port The RS232 port to be used.
105  * \param buf A pointer to the string that is to be printed
106  *
107  * This function prints a string from program memory to
108  * RS232. The string must be terminated by a null
109  * byte. The RS232 module must be correctly initalized and
110  * configured for this function to work.
111  */
112 void
113 rs232_print(uint8_t port, char *buf);
114 
115 /**
116  * \brief Print a formated string on RS232
117  * \param port The RS232 port to be used.
118  * \param fmt The format string that is used to construct the string
119  * from a variable number of arguments.
120  *
121  * This function prints a formated string to RS232. Note
122  * that this function used snprintf internally and thus cuts
123  * the resulting string after RS232_PRINTF_BUFFER_LENGTH - 1
124  * bytes. You can override this buffer lenght with the
125  * RS232_CONF_PRINTF_BUFFER_LENGTH define. The RS232 module
126  * must becorrectly initalized and configured for this
127  * function to work.
128  */
129 void
130 rs232_printf(uint8_t port, const char *fmt, ...);
131 
132 /**
133  * \brief Print a character on RS232
134  * \param port The RS232 port to be used.
135  * \param c The character to be printed
136  *
137  * This function prints a character to RS232. The RS232
138  * module must be correctly initalized and configured for
139  * this function to work.
140  */
141 void
142 rs232_send(uint8_t port, unsigned char c);
143 
144 /**
145  * \brief Redirects stdout to a given RS232 port
146  * \param port The RS232 port to be used.
147  *
148  * This function redirects the stdout channel to a given
149  * RS232 port. Note that this modfies the global variable
150  * stdout. If you want to restore the previous behaviour, it
151  * is your responsibility to backup to old value. The RS232
152  * module must be correctly initalized and configured for
153  * the redirection to work.
154  */
155 void
156 rs232_redirect_stdout (uint8_t port);
157 
158 #endif /* RS232_H_ */
void rs232_init(void)
Initialize the RS232 module.
Definition: rs232.c:50
AVR specific definitions for the rs232 port.
AVR specific definitions for the rs232 port.
void rs232_set_input(int(*f)(unsigned char))
Set an input handler for incoming RS232 data.
Definition: rs232.c:55
AVR specific definitions for the rs232 port.
AVR specific definitions for the rs232 port.
AVR specific definitions for the rs232 port.
void rs232_print(char *text)
Print a text string on RS232.
Definition: rs232.c:65
AVR specific definitions for the rs232 port.
AVR specific definitions for the rs232 port.
void rs232_send(uint8_t port, unsigned char c)
Print a character on RS232.
Definition: rs232.c:325