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