PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
esos_pic24_rs232.h
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
3  * All rights reserved.
4  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
5  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
6  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation for any purpose, without fee, and without written agreement is
10  * hereby granted, provided that the above copyright notice, the following
11  * two paragraphs and the authors appear in all copies of this software.
12  *
13  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
15  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
16  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
23  *
24  * Please maintain this header in its entirety when copying/modifying
25  * these files.
26  *
27  *
28  */
29 
30 /** \file
31  * \brief This file contains macros, prototypes, and definitions for
32  * Microchip PIC24 Family specific communications on ESOS
33  */
34 
35 
36 #ifndef _ESOS_PIC24_RS232_H
37 #define _ESOS_PIC24_RS232_H
38 
39 /* I N C L U D E S **********************************************************/
40 #include "esos.h"
41 #include "esos_comm.h"
42 #include "esos_pic24.h"
43 
44 /* D E F I N I T I O N S ****************************************************/
45 
46 // use the same definitions as the non-ESOS pic24_uart.h/.c library but
47 // prepend with our __ESOS_ tag
48 #define __ESOS_UART1_TX_INTERRUPT
49 #define __ESOS_UART1_RX_INTERRUPT
50 #define __ESOS_UART1_TX_INTERRUPT_PRIORITY 5
51 #define __ESOS_UART1_RX_INTERRUPT_PRIORITY 5
52 
53 /* E X T E R N S ************************************************************/
54 
55 /* M A C R O S **************************************************************/
56 
57 /* P U B L I C P R O T O T Y P E S *****************************************/
58 void __esos_hw_signal_start_tx(void);
59 void __esos_hw_signal_stop_tx(void);
60 
61 // Documentation for this file. If the \file tag isn't present,
62 // this file won't be documented.
63 /** \file
64  * This file contains routines which configure and
65  * use the UARTs on the PIC. See \ref pic24_serial.h
66  * for higher-level routines, which should typically
67  * be called by the user rather than these routines.
68  */
69 
70 /* ########################################################################### */
71 /** Chose a default baud rate for the UART, used by
72  * \ref configUART1 to set up the UART.
73  */
74 #define DEFAULT_BAUDRATE 57600
75 
76 /** Configure the UART's baud rate, based on \ref FCY.
77  * Note that the value computed is truncated, not
78  * rounded, since this is done using integer
79  * arithmetic. That is, BRG = truncate(FCY/16/baud - 1),
80  * giving an actual baud rate of FCY/16/(reg + 1).
81  *
82  * NOTE: this code sets BRGH=0 (16 clocks for each bit).
83  * Be careful about using BRGH=1 - this uses only four clock
84  * periods to sample each bit and can be very intolerant of
85  * baud rate %error - you may see framing errors.
86  *
87  * \param baudRate Desired baud rate.
88  */
89 static inline void CONFIG_BAUDRATE_UART1(uint32 baudRate) {
90  uint32 brg = (FCY/baudRate/16) - 1;
91  ASSERT(brg <= 0xFFFF);
92  U1MODEbits.BRGH = 0;
93  U1BRG = brg;
94 }
95 
96 
97 /**@{ \name Constants for the UxMODE.PDSEL bitfield
98  * Use with \ref CONFIG_PDSEL_UART1.
99  */
100 #define UXMODE_PDSEL_8DATA_NOPARITY 0
101 #define UXMODE_PDSEL_8DATA_EVENPARITY 1
102 #define UXMODE_PDSEL_8DATA_ODDPARITY 2
103 #define UXMODE_PDSEL_9DATA_NOPARITY 3
104 ///@}
105 
106 /** Select the parity and number of data bits for the UART.
107  * Use constants UXMODE_PDSEL_8DATA_NOPARITY and following.
108  * \param u8_pdsel Parity and number of data bits.
109  */
110 inline static void CONFIG_PDSEL_UART1(uint8 u8_pdsel) {
111  ASSERT(u8_pdsel <= UXMODE_PDSEL_9DATA_NOPARITY);
112  U1MODEbits.PDSEL = u8_pdsel;
113 }
114 
115 /** Select the number of stop bits for this UART. Valid values
116  * are 1 or 2.
117  * \param u8_numStopbits Number of stop bits.
118  */
119 inline static void CONFIG_STOPBITS_UART1(uint8 u8_numStopbits) {
120  ASSERT(u8_numStopbits <= 2);
121  U1MODEbits.STSEL = u8_numStopbits - 1;
122 }
123 
124 /** Enable RX, TX for UART. */
125 static inline void ENABLE_UART1() {
126  U1MODEbits.UEN = 0b00; // UxTX and UxRX pins are enabled and used; no flow control pins
127  U1MODEbits.UARTEN = 1; // enable UART RX/TX
128  U1STAbits.UTXEN = 1; //Enable the transmitter
129 }
130 
131 /** Determine if a character is available in the UART's
132  * receive buffer.
133  * \return True (1) if character is available, 0 if not.
134  */
135 #define IS_CHAR_READY_UART1() U1STAbits.URXDA
136 
137 /** Determine if a the transmit buffer is full.
138  * \return True (1) if the transmit buffer if full,
139  * false (0) if not.
140  */
141 #define IS_TRANSMIT_BUFFER_FULL_UART1() U1STAbits.UTXBF
142 
143 /** Determines if all characters placed in the UART have been sent.
144  * Returns 1 if the last transmission has completed, or 0 if a transmission
145  * is in progress or queued in the transmit buffer.
146  * \return True (1) if the last transmission has completed, 0 if not.
147  */
148 #define IS_TRANSMIT_COMPLETE_UART1() U1STAbits.TRMT
149 
150 // communications commands used outside of ESOS tasks (like user_init routine)
151 // these routines/macros should almost never be used.
152 void __esos_hw_PutUint8(uint8 u8_c);
153 void __esos_hw_PutString(uint8* psz_in);
154 uint8 __esos_hw_GetUint8(void);
155 
156 
157 void configUART1(uint32 u32_baudRate);
158 void __esos_hw_InitCommSystem(void);
159 
160 
161 /** Waits until all characters placed in the UART have been sent. */
163  while (!IS_TRANSMIT_COMPLETE_UART1())
164  doHeartbeat();
165 }
166 
167 
168 
169 #endif // end ESOS_PIC24_RS232_H