U5STASET = 0x00001400;
// Enable Transmit and Receive
//priority 2 sub priority 3 - 00011111 bits
//<26:28> sub bits <25:24>
IPC12SET = 0x0B000000;//0000 1011 0000 0000
0000 0000 0000 0000
IEC2SET = 0x00000400;
IFS2CLR = 0x00000400;
//flush receive buffer
USART5_RxTail = 0x00;
USART5_RxHead = 0x00;
if ( tmphead == USART5_RxTail )
{
// ERROR! Receive buffer overflow
// Your error handler code goes here
}
USART5_RxBuf[tmphead] = data; // store
received data in buffer
IFS2CLR = 0x00000400; //0000 0000 0000
0000 0000 0100 0000 0000
}
Each USART interrupt handler is identical with the
exception of the IFS2CLR interrupt flag bit mask. You can
see this in the USART4 interrupt handler code:
A Bit of Redirection
The FTDI USB port will most likely be used to pass
information between a PC and the PIC32MX575F512H.
The ST3232 is present to make sure we can talk to legacy
RS-232 equipped gadgets. If the hardware is used to
communicate via a LAN or the Internet, we will be
“speaking” through the ACKme Wi-Fi more often than not.
//***********************************************
//* UART 4 interrupt handler
//* it is set at priority level 2 with software
//* context saving
//***********************************************
void __ISR(_UART_4_VECTOR, IPL2SOFT)
IntUart4Handler(void)
{
BYTE data,tmphead,tmptail;
So, to make it a bit easier to pass data to the Wi-Fi
module, we will redirect the STDOUT to USART5. This
redirection allows us to use the printf statement to send
data to the USART5 portal. It’s really easy to do. Here’s
the redirection code:
data = U4RXREG;
// read the received data
// calculate buffer index
tmphead = ( USART4_RxHead + 1 ) &
USART4_RX_BUFFER_MASK;
USART4_RxHead = tmphead;
// store new index
//***********************************************
//* UART5 REDIRECTOR
//***********************************************
void _mon_putc(char c)
{
U5TXREG = c;
while(U5STAbits.TRMT == 0);
}
if ( tmphead == USART4_RxTail )
{
// ERROR! Receive buffer overflow
}
The redirection can be applied to any of our USARTs
in a mutually exclusive manner by simply changing the
USART4_RxBuf[tmphead] = data;
// store received data in buffer
IFS2CLR = 0x00000010;
}
USART number inside of the mon_putc function.
USART Character Handlers
USART Interrupt Handlers
We must also have a way of retrieving the buffered
data. That is done with the USART recvchar functions:
Putting the received data into the ring buffers is
accomplished with our USART receive interrupt handlers.
Each USART has one. Here’s the USART5 interrupt
handler:
//***********************************************
//* UART 5 interrupt handler
//* it is set at priority level 2 with software
//* context saving
//***********************************************
void __ISR(_UART_5_VECTOR, IPL2SOFT)
IntUart5Handler(void)
{
BYTE data,tmphead,tmptail;
BYTE recvchar5(void)
{
BYTE tmptail;
// wait for incoming data
while ( USART5_RxHead == USART5_RxTail );
// calculate buffer index
tmptail = ( USART5_RxTail + 1 ) &
USART5_RX_BUFFER_MASK;
USART5_RxTail = tmptail;
// store new index
return USART5_RxBuf[tmptail];
// return data
data = U5RXREG;
// read the received data
// calculate buffer index
tmphead = ( USART5_RxHead + 1 ) &
USART5_RX_BUFFER_MASK;
USART5_RxHead = tmphead;
// store new index
Again, all of the USART recvchar functions are
identical and delineated simply by the USART number:
BYTE recvchar1(void)
{
BYTE tmptail;
// wait for incomming data
while ( USART1_RxHead == USART1_RxTail );
// calculate buffer index
tmptail = ( USART1_RxTail + 1 ) &
USART1_RX_BUFFER_MASK;
USART1_RxTail = tmptail;
72 September 2015