pic32/cores/pic32/HardwareSerial.cpp
/* ------------------------------------------------------------ */
/***	HardwareSerial::begin
**
**	Parameters:
**		baudRate		- baud rate to use on port
**      address         - address for RS485 communication
**
**	Return Value:
**		none
**
**	Errors:
**		none
**
**	Description:
**		Initialize the UART for use, setting the baud rate to the
**		requested value, data size of 9-bits, and no parity and 
**      address detection mode
*/
:begin

baudRate                - baud rate to use on port
address         - address for RS485 communication

Return Value:
none

none

Initialize the UART for use, setting the baud rate to the
requested value, data size of 9-bits, and no parity and
address detection mode
: begin baudRate - baud rate to use on port address - address for RS485 communication Return Value : none none Initialize the UART for use setting the baud rate to the requested value data size of 9 - bits and no parity and address detection mode
void HardwareSerial::begin(unsigned long baudRate, uint8_t address) {
	purge();
#if defined(__PIC32_PPS__)
    pinMode(pinTx, INPUT); 
    pinMode(pinRx, INPUT);
    mapPps(pinTx, ppsTx);
    mapPps(pinRx, ppsRx);
#elif __PIC32_PINS__ == 64 && (defined(__PIC32MX5XX__) || defined(__PIC32MX6XX__)  || defined(__PIC32MX7XX__))
    if(uart == ((p32_uart *) _UART5_BASE_ADDRESS))
    {
        AD1PCFGbits.PCFG8 = 1;
        AD1PCFGbits.PCFG14 = 1;
    }
#endif
    setIntVector(vec, isr);
	setIntPriority(vec, ipl, spl);
#if defined(__PIC32MZXX__)
        setIntVector(vec+1, isr);
        setIntVector(vec+2, isr);
        setIntPriority(vec+1, ipl, spl);
        setIntPriority(vec+2, ipl, spl);
#endif
    ifs->clr = bit_rx + bit_tx + bit_err;	
    iec->clr = bit_rx + bit_tx + bit_err;	
    iec->set = bit_rx;						
    uart->uxMode.reg = 0;
    uart->uxSta.reg = 0;
    if (baudRate < LOW_HIGH_BAUD_SPLIT) {
        uart->uxBrg.reg = ((__PIC32_pbClk / 16 / baudRate) - 1);  
        uart->uxMode.set = 0b11 << _UARTMODE_PDSEL;                             
    } else {
        uart->uxBrg.reg = ((__PIC32_pbClk / 4 / baudRate) - 1);
        uart->uxMode.set =  (1 << _UARTMODE_BRGH) + (0b11 << _UARTMODE_PDSEL); 
    }
    uart->uxSta.set = (1 << _UARTSTA_ADM_EN) + (address << _UARTSTA_ADDR) + (1 << _UARTSTA_UTXEN) + (1 << _UARTSTA_URXEN);  
    enableAddressDetection(); 
    uart->uxMode.set = 1 << _UARTMODE_ON; 
}
void HardwareSerial :: begin ( unsigned long baudRate , uint8_t address ) { purge ( ) ; #if defined ( __PIC32_PPS__ ) pinMode ( pinTx , INPUT ) ; pinMode ( pinRx , INPUT ) ; mapPps ( pinTx , ppsTx ) ; mapPps ( pinRx , ppsRx ) ; #elif __PIC32_PINS__ == 64 && ( defined ( __PIC32MX5XX__ ) || defined ( __PIC32MX6XX__ ) || defined ( __PIC32MX7XX__ ) ) if ( uart == ( ( p32_uart * ) _UART5_BASE_ADDRESS ) ) { AD1PCFGbits . PCFG8 = 1 ; AD1PCFGbits . PCFG14 = 1 ; } #endif setIntVector ( vec , isr ) ; setIntPriority ( vec , ipl , spl ) ; #if defined ( __PIC32MZXX__ ) setIntVector ( vec + 1 , isr ) ; setIntVector ( vec + 2 , isr ) ; setIntPriority ( vec + 1 , ipl , spl ) ; setIntPriority ( vec + 2 , ipl , spl ) ; #endif ifs -> clr = bit_rx + bit_tx + bit_err ; iec -> clr = bit_rx + bit_tx + bit_err ; iec -> set = bit_rx ; uart -> uxMode . reg = 0 ; uart -> uxSta . reg = 0 ; if ( baudRate < LOW_HIGH_BAUD_SPLIT ) { uart -> uxBrg . reg = ( ( __PIC32_pbClk / 16 / baudRate ) - 1 ) ; uart -> uxMode . set = 0b11 << _UARTMODE_PDSEL ; } else { uart -> uxBrg . reg = ( ( __PIC32_pbClk / 4 / baudRate ) - 1 ) ; uart -> uxMode . set = ( 1 << _UARTMODE_BRGH ) + ( 0b11 << _UARTMODE_PDSEL ) ; } uart -> uxSta . set = ( 1 << _UARTSTA_ADM_EN ) + ( address << _UARTSTA_ADDR ) + ( 1 << _UARTSTA_UTXEN ) + ( 1 << _UARTSTA_URXEN ) ; enableAddressDetection ( ) ; uart -> uxMode . set = 1 << _UARTMODE_ON ; }
HardwareSerial::begin
HardwareSerial :: begin
/* Initialize the receive buffer.
	*/ // set the pins to digital, just in case they  // are analog pins. The serial controller will not // set these to digital. // let serial controller set as output, keep tri-stated for now. /* Map the UART TX to the appropriate pin.
	*/ /* Map the UART RX to the appropriate pin.
	*/ // the only UART on a non-PPS MX that conflicts with an analog // pin is UART5 on MX 5,6,& 7 64 pin parts only. // see if this is UART5 // RB8 is AN8 & U5RX // RB14 is AN14 & U5TX // set as digital pins /* Set the interrupt privilege level and sub-privilege level
	*/ // MZ has 2 more vectors to worry about // the MZ part works off of offset tables // we must fill in the tx and rx VECs to point // to the ERR VEC so all 3 VECs use the same ISR // and set the priorities for the other 2 vectors. /* Clear the interrupt flags, and set the interrupt enables for the
    ** interrupts used by this UART.
    */ //clear all interrupt flags //disable all interrupts //enable rx interrupts /* Initialize the UART itself.
    **	http://www.chipkit.org/forum/viewtopic.php?f=7&t=213&p=948#p948
    ** Use high baud rate divisor for bauds over LOW_HIGH_BAUD_SPLIT
    */ // calculate actual BAUD generate value. // set to 9 data bits, no parity // calculate actual BAUD generate value. // set to 9 data bits, no parity // set address of RS485 slave, enable transmitter and receiver and auto address detection // enable auto address detection // enable UART module