PIC24 Support Libraries
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 /** The underlying PIC24 HW libraries choose a default baudrate
72  * appropriate to the chosen MCU model. Only uncomment the line
73  * below if you want ESOS to build the UART with a "non-standard"
74  * baudrate. Used by \ref configUART1 to set up the UART.
75  *
76  * #define DEFAULT_BAUDRATE 9600
77  */
78 
79 /** Configure the UART's baud rate, based on \ref FCY.
80  * Note that the value computed is truncated, not
81  * rounded, since this is done using integer
82  * arithmetic. That is, BRG = truncate(FCY/16/baud - 1),
83  * giving an actual baud rate of FCY/16/(reg + 1).
84  *
85  * NOTE: this code sets BRGH=0 (16 clocks for each bit).
86  * Be careful about using BRGH=1 - this uses only four clock
87  * periods to sample each bit and can be very intolerant of
88  * baud rate %error - you may see framing errors.
89  *
90  * \param baudRate Desired baud rate.
91  */
92 static inline void CONFIG_BAUDRATE_UART1(uint32_t baudRate) {
93  uint32_t brg = (FCY/baudRate/16) - 1;
94  ASSERT(brg <= 0xFFFF);
95  U1MODEbits.BRGH = 0;
96  U1BRG = brg;
97 }
98 
99 
100 /**@{ \name Constants for the UxMODE.PDSEL bitfield
101  * Use with \ref CONFIG_PDSEL_UART1.
102  */
103 #define UXMODE_PDSEL_8DATA_NOPARITY 0
104 #define UXMODE_PDSEL_8DATA_EVENPARITY 1
105 #define UXMODE_PDSEL_8DATA_ODDPARITY 2
106 #define UXMODE_PDSEL_9DATA_NOPARITY 3
107 ///@}
108 
109 /** Select the parity and number of data bits for the UART.
110  * Use constants UXMODE_PDSEL_8DATA_NOPARITY and following.
111  * \param u8_pdsel Parity and number of data bits.
112  */
113 inline static void CONFIG_PDSEL_UART1(uint8_t u8_pdsel) {
114  ASSERT(u8_pdsel <= UXMODE_PDSEL_9DATA_NOPARITY);
115  U1MODEbits.PDSEL = u8_pdsel;
116 }
117 
118 /** Select the number of stop bits for this UART. Valid values
119  * are 1 or 2.
120  * \param u8_numStopbits Number of stop bits.
121  */
122 inline static void CONFIG_STOPBITS_UART1(uint8_t u8_numStopbits) {
123  ASSERT(u8_numStopbits <= 2);
124  U1MODEbits.STSEL = u8_numStopbits - 1;
125 }
126 
127 /** Enable RX, TX for UART. */
128 static inline void ENABLE_UART1() {
129  U1MODEbits.UEN = 0b00; // UxTX and UxRX pins are enabled and used; no flow control pins
130  U1MODEbits.UARTEN = 1; // enable UART RX/TX
131  U1STAbits.UTXEN = 1; //Enable the transmitter
132 }
133 
134 /** Determine if a character is available in the UART's
135  * receive buffer.
136  * \return True (1) if character is available, 0 if not.
137  */
138 #define IS_CHAR_READY_UART1() U1STAbits.URXDA
139 
140 /** Determine if a the transmit buffer is full.
141  * \return True (1) if the transmit buffer if full,
142  * false (0) if not.
143  */
144 #define IS_TRANSMIT_BUFFER_FULL_UART1() U1STAbits.UTXBF
145 
146 /** Determines if all characters placed in the UART have been sent.
147  * Returns 1 if the last transmission has completed, or 0 if a transmission
148  * is in progress or queued in the transmit buffer.
149  * \return True (1) if the last transmission has completed, 0 if not.
150  */
151 #define IS_TRANSMIT_COMPLETE_UART1() U1STAbits.TRMT
152 
153 // communications commands used outside of ESOS tasks (like user_init routine)
154 // these routines/macros should almost never be used.
155 void __esos_hw_PutUint8(uint8_t u8_c);
156 void __esos_hw_PutString(uint8_t* psz_in);
157 uint8_t __esos_hw_GetUint8(void);
158 
159 
160 void __esos_configUART1(uint32_t u32_baudRate);
161 void __esos_hw_InitCommSystem(void);
162 
163 
164 /** Waits until all characters placed in the UART have been sent. */
166  while (!IS_TRANSMIT_COMPLETE_UART1())
167  doHeartbeat();
168 }
169 
170 
171 
172 #endif // end ESOS_PIC24_RS232_H
void doHeartbeat(void)
Definition: pic24_util.c:104
static void CONFIG_PDSEL_UART1(uint8_t u8_pdsel)
static void ENABLE_UART1()
static void WAIT_UNTIL_TRANSMIT_COMPLETE_UART1()
void __esos_configUART1(uint32_t u32_baudRate)
This is the master include file for implementing ESOS on Microchip PIC24 MCUs.
static void CONFIG_BAUDRATE_UART1(uint32_t baudRate)
#define FCY
static void CONFIG_STOPBITS_UART1(uint8_t u8_numStopbits)
#define IS_TRANSMIT_COMPLETE_UART1()
#define ASSERT(test)
unsigned char uint8_t
An abbreviation for an 8-bit unsigned integer.
Definition: dataXferImpl.h:194