Contiki-Inga 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 // This is MCU specific, no general XMega file here
58 #elif defined (__AVR_ATxmega256A3__) || defined (__AVR_ATxmega256A3B__)
59 #include "dev/rs232_atxmega256a3.h"
60 #else
61 #error "Please implement a rs232 header for your MCU (or set the MCU type \
62 in contiki-conf.h)."
63 #endif
64 
65 /******************************************************************************/
66 /*** Baud rates */
67 /******************************************************************************/
68 #define BAUD_RATE(x) (F_CPU/16/x-1)
69 
70 /**
71  * \brief Initialize the RS232 module
72  *
73  * This function is called from the boot up code to
74  * initalize the RS232 module.
75  * \param port The RS232 port to be used.
76  * \param bd The baud rate of the connection.
77  * \param ffmt The frame format of the connection, i.e. parity mode,
78  * number of stop and data bits, ...
79  */
80 void
81 rs232_init (uint8_t port, uint8_t bd, uint8_t ffmt);
82 
83 /**
84  * \brief Set an input handler for incoming RS232 data
85  * \param port The RS232 port to be used.
86  * \param f A pointer to a byte input handler
87  *
88  * This function sets the input handler for incoming RS232
89  * data. The input handler function is called for every
90  * incoming data byte. The function is called from the
91  * RS232 interrupt handler, so care must be taken when
92  * implementing the input handler to avoid race
93  * conditions.
94  *
95  * The return value of the input handler affects the sleep
96  * mode of the CPU: if the input handler returns non-zero
97  * (true), the CPU is awakened to let other processing
98  * take place. If the input handler returns zero, the CPU
99  * is kept sleeping.
100  */
101 void
102 rs232_set_input(uint8_t port, int (* f)(unsigned char));
103 
104 
105 /**
106  * \brief Print a text string from program memory on RS232
107  * \param port The RS232 port to be used.
108  * \param buf A pointer to the string that is to be printed
109  *
110  * This function prints a string from program memory to
111  * RS232. The string must be terminated by a null
112  * byte. The RS232 module must be correctly initalized and
113  * configured for this function to work.
114  */
115 void
116 rs232_print(uint8_t port, char *buf);
117 
118 /**
119  * \brief Print a formated string on RS232
120  * \param port The RS232 port to be used.
121  * \param fmt The format string that is used to construct the string
122  * from a variable number of arguments.
123  *
124  * This function prints a formated string to RS232. Note
125  * that this function used snprintf internally and thus cuts
126  * the resulting string after RS232_PRINTF_BUFFER_LENGTH - 1
127  * bytes. You can override this buffer lenght with the
128  * RS232_CONF_PRINTF_BUFFER_LENGTH define. The RS232 module
129  * must becorrectly initalized and configured for this
130  * function to work.
131  */
132 void
133 rs232_printf(uint8_t port, const char *fmt, ...);
134 
135 /**
136  * \brief Print a character on RS232
137  * \param port The RS232 port to be used.
138  * \param c The character to be printed
139  *
140  * This function prints a character to RS232. The RS232
141  * module must be correctly initalized and configured for
142  * this function to work.
143  */
144 void
145 rs232_send(uint8_t port, unsigned char c);
146 
147 /**
148  * \brief Redirects stdout to a given RS232 port
149  * \param port The RS232 port to be used.
150  *
151  * This function redirects the stdout channel to a given
152  * RS232 port. Note that this modfies the global variable
153  * stdout. If you want to restore the previous behaviour, it
154  * is your responsibility to backup to old value. The RS232
155  * module must be correctly initalized and configured for
156  * the redirection to work.
157  */
158 void
159 rs232_redirect_stdout (uint8_t port);
160 
161 void
162 
163 s232_set_baud(uint16_t bd);
164 
165 #endif /* RS232_H_ */