PIC24 Support Libraries
esos_pic24_rs232.c
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 
31 /**
32  * \addtogroup ESOS_UART_Service
33  * @{
34  */
35 
36 
37 // Documentation for this file. If the \file tag isn't present,
38 // this file won't be documented.
39 /** \file
40  * To do.
41  */
42 
43 /*** I N C L U D E S *************************************************/
44 #include "esos_pic24_rs232.h"
45 
46 /*** G L O B A L S *************************************************/
47 
48 /*** T H E C O D E *************************************************/
49 #define __ESOS_HW_SIGNAL_START_TX() _U1TXIE = 1
50 #define __ESOS_HW_SIGNAL_STOP_TX() _U1TXIE = 0
51 
52 /*********************************************************
53  * Public functions intended to be called by other files *
54  *********************************************************/
55 inline void __esos_hw_signal_start_tx(void) {
56  __ESOS_HW_SIGNAL_START_TX();
57 }
58 
59 inline void __esos_hw_signal_stop_tx(void) {
60  __ESOS_HW_SIGNAL_STOP_TX();
61 }
62 
63 
64 
65 /* ########################################################################### */
66 void _ISRFAST _U1TXInterrupt (void) {
67  if (__st_TxBuffer.u16_Head == __st_TxBuffer.u16_Tail) {
68  //empty TX buffer, disable the interrupt, do not clear the flag
69  __ESOS_HW_SIGNAL_STOP_TX();
70  } else {
71  //at least one free spot in the TX buffer!
72  __st_TxBuffer.u16_Tail++; //increment tail pointer
73  if (__st_TxBuffer.u16_Tail == ESOS_SERIAL_IN_EP_SIZE)
74  __st_TxBuffer.u16_Tail = 0; //wrap if needed
75  _U1TXIF = 0; //clear the interrupt flag
76  //transfer character from software buffer to transmit buffer
77  U1TXREG = __st_TxBuffer.pau8_Data[__st_TxBuffer.u16_Tail];
78  }
79 }
80 
81 void _ISRFAST _U1RXInterrupt (void) {
82  int8_t u8_c;
83 
84  _U1RXIF = 0; //clear the UART RX interrupt bit
85 
86  // This fcn is found in pic24_uart.c which we've replaced with
87  // our own ESOS versions.
88  u8_c = U1RXREG; //read character
89  __st_RxBuffer.u16_Head++; //increment head pointer
90  if (__st_RxBuffer.u16_Head == ESOS_SERIAL_OUT_EP_SIZE)
91  __st_RxBuffer.u16_Head = 0; //wrap if needed
92 
93  __st_RxBuffer.pau8_Data[__st_RxBuffer.u16_Head] = u8_c; //place in buffer
94 }
95 
96 /** Configure the UART. Settings chosen:
97  * - TX is on RP11
98  * - RX is on RP10
99  * - Format is 8 data bits, no parity, 1 stop bit
100  * - CTS, RTS, and BCLK not used
101  *
102  * \param u32_baudRate The baud rate to use.
103  */
104 void __esos_configUART1(uint32_t u32_baudRate) {
105  /************************* UART config ********************/
106  //Pin mapping macros in pic24_ports.h
107 
108 #if (HARDWARE_PLATFORM == EMBEDDED_C1)
109 # warning Building configUART1() for the Rev.C1 Embedded Systems target.
111  CONFIG_U1RX_TO_RP(RB12_RP); // U1RX <- RB12
113  CONFIG_U1TX_TO_RP(RC8_RP); // U1TX -> RC8
114 #elif (HARDWARE_PLATFORM == EMBEDDED_F14)
115 # warning Building configUART1() for the Rev.F14 Embedded Systems target.
116  CONFIG_RB10_AS_DIG_INPUT(); // RX pin must be digital input
117  CONFIG_U1RX_TO_RP(RB10_RP); // U1RX <- RB10
118  CONFIG_RF0_AS_DIG_OUTPUT(); // TX pin must be digital output
119  CONFIG_U1TX_TO_RP(RF0_RP); // U1TX -> RF0??? (Use RF0 for now.)
120 #else
122  CONFIG_U1RX_TO_RP(RB10_RP); //U1RX <- RP10
124  CONFIG_U1TX_TO_RP(RB11_RP); //U1TX -> RP11
125 #endif
126 
127  //UART macros defined in "pic24_uart.h"
128  CONFIG_BAUDRATE_UART1(u32_baudRate); //baud rate
129  CONFIG_PDSEL_UART1(UXMODE_PDSEL_8DATA_NOPARITY); // 8-bit data, no parity
130  CONFIG_STOPBITS_UART1(1); // 1 Stop bit
131 
132  // ESOS comm system uses UART1_RX IRQs
133  _U1RXIF = 0; //clear the flag
134  _U1RXIP = __ESOS_UART1_RX_INTERRUPT_PRIORITY; //choose a priority
135  _U1RXIE = 1; //enable the interrupt
136 
137  // ESOS comm system uses UART1_RX IRQs
138  // do not clear the U1TXIF flag!
139  _U1RXIP = __ESOS_UART1_TX_INTERRUPT_PRIORITY; //choose a priority
140  //do not enable the interrupt until we try to write to the UART
141 
142  ENABLE_UART1(); //enable the UART
143 }
144 
145 /* ########################################################################### */
146 
147 
148 /******************************************************************************
149  * Function: void _esos_hw_InitSerialUart( void )
150  *
151  * PreCondition: None
152  *
153  * Input: None
154  *
155  * Output: ptr to ESOS_COMM_BUFF_DSC structure with initialized ptrs
156  *
157  * Side Effects: Turns on USART hardware
158  *
159  *****************************************************************************/
160 void __esos_hw_InitCommSystem(void) {
161  // use the MSSTATE PIC24 routines to init the RS232 comm subsystem
162  // 8N1 @ 56k7 baud (DEFAULT_BAUDRATE) for now
164 
165 } // end __esos_hw_InitCommSystem()
166 
167 
168 /******************************************************************************
169  * Function: uint8_t esos_GetCommSystemMaxInDataLen(void)
170  *
171  * PreCondition: None.
172  *
173  * Input: None
174  *
175  * Output: the maximum number of uint8_ts that the comm system will
176  * receive in a single buffer transfer from the host -- OR --
177  * in the case of single uint8_t xfers (like RS232), the maximum
178  * number of uint8_ts that can be RX-ed before the buffers
179  * overflow
180  *
181  * Side Effects: None
182  *
183  * Overview: A way for a run-time determination of the maximum buffer
184  * size that the user can can expect. This number is
185  * actually hard-coded in the USB CDC header file, but this
186  * method will allow the user code to be more generic, if
187  * it chooses to be.
188  *
189  *****************************************************************************/
191  return ESOS_SERIAL_OUT_EP_SIZE;
192 } //end esos_GetCommSystemMaxInDataLen()
193 
194 /******************************************************************************
195  * Function: uint8_t esos_GetCommSystemMaxOutDataLen(void)
196  *
197  * PreCondition: None.
198  *
199  * Input: None
200  *
201  * Output: the maximum number of uint8_ts that the comm system will
202  * transfer back to the host in a single buffer -- OR --
203  * in the case of singe uint8_t xfers (like RS232), the maximum
204  * number of uint8_ts in the output buffer before overflow
205  *
206  * Side Effects: None
207  *
208  * Overview: A way for a run-time determination of the maximum buffer
209  * size that the user can can send efficiently. The USB system
210  * will send a bigger buffer than getUSBCdcTxMax() size, but
211  * will do so in several smaller getUSBCdcTxMax()-sized chunks.
212  *
213  * This number is actually hard-coded in the USB CDC header file,
214  * but this method will allow the user code to be more generic,
215  * if it chooses to be.
216  *
217  *****************************************************************************/
219  return ESOS_SERIAL_IN_EP_SIZE;
220 } //end esos_GetCommSystemMaxOutDataLen()
221 
222 /******************************************************************************
223  * Function: uint8_t _esos_hw_GetUartVersion(void)
224  *
225  * PreCondition: None.
226  *
227  * Input: None
228  *
229  * Output: Return the version number of the MSU Bulk CDC driver firmware
230  * currently running.
231  * The most-significant bit denotes we're running USB
232  * The most-significant nibble is the major revision number
233  * The least-significant nibble is the minor revision number
234  *
235  * Side Effects: None
236  *
237  *****************************************************************************/
238 uint8_t _esos_hw_GetSerialUartVersion(void) {
239  return ESOS_COMM_SYS_SERIAL_REV;
240 } //end _esos_hw_GetUartVersion()
241 
242 /** @} */
#define CONFIG_RC8_AS_DIG_OUTPUT()
This file contains macros, prototypes, and definitions for Microchip PIC24 Family specific communicat...
#define RB12_RP
static void CONFIG_PDSEL_UART1(uint8_t u8_pdsel)
#define CONFIG_RB12_AS_DIG_INPUT()
static void ENABLE_UART1()
#define RC8_RP
uint8_t esos_GetCommSystemMaxInDataLen(void)
#define CONFIG_U1RX_TO_RP(pin)
Definition: pic24_ports.h:287
#define CONFIG_RF0_AS_DIG_OUTPUT()
void __esos_configUART1(uint32_t u32_baudRate)
static void CONFIG_BAUDRATE_UART1(uint32_t baudRate)
#define RB11_RP
#define CONFIG_RB11_AS_DIG_OUTPUT()
uint8_t esos_GetCommSystemMaxOutDataLen(void)
#define RF0_RP
#define CONFIG_RB10_AS_DIG_INPUT()
static void CONFIG_STOPBITS_UART1(uint8_t u8_numStopbits)
#define RB10_RP
#define CONFIG_U1TX_TO_RP(Rxy_RP)
Definition: pic24_ports.h:443
#define DEFAULT_BAUDRATE
unsigned char uint8_t
An abbreviation for an 8-bit unsigned integer.
Definition: dataXferImpl.h:194