/* File name: comm.h
* Author: Richard Wall
* Date: June 12, 2012
*/
#ifndef __COMM_H__
#define __COMM_H__
#define BACKSPACE 0x08
#define NO_PARITY 0
#define ODD_PARITY 1
#define EVEN_PARITY 2
#endif
void initialize_uart1(unsigned int bit_rate, int parity);
void _mon_putc(char c); // Called by system to implement "printf" functions
int putcU1( int c); // Sends single character to UART
int getcU1( char *ch); // Gets single character from UART
int putsU1( const char *s); // Send string to UART1
int getstrU1( char *s, unsigned int len ); // Gets string from UART
// End of comm.h
/* File name: comm.c
* Author: Richard Wall
* Date: June 12, 2013
*
* This code also provides access to the "printf" function
* on UART Serial Port 1
*
* ChipKITPRO MX7 requires UART crossover cable
* Connector Pin Pmod Pin Function
* 2 4 MX7 Rx
* 3 3 MX7 Tx
* 5 5 Gnd
* 6 6 Vcc
*/
#include <plib.h>
#include <stdio.h> // required for printf
#include "comm.h"
#include "chipKIT_PRO_MX7.h" // has info regarding the PB clock
/* initialize_comm FUNCTION DESCRIPTION ************************************
SYNTAX: void initialize_comm(unsigned int bit_rate, int parity);
KEYWORDS: UART, initialization, parity
DESCRIPTION: Initializes UART1 comm port for specified bit rate using
the assigned parity
PARAMETER 1: bit_rate: the communications bit rate
PARAMETER 2: parity: NO_PARITY, ODD_PARITY, or EVEN_PARITY
RETURN VALUE: None
NOTES: 9 bit mode MARK or SPACE parity is not supported
END DESCRIPTION **********************************************************/
void initialize_uart1(unsigned int bit_rate, int parity)
{
unsigned int brg;
brg = (unsigned short)(( (float)FPB / ( (float)4 * (float) bit_rate))-
(float)0.5); // provides rounding for greatest accuracy
switch(parity)
{
case NO_PARITY:
OpenUART1( (UART_EN | UART_BRGH_FOUR | UART_NO_PAR_8BIT),
(UART_RX_ENABLE | UART_TX_ENABLE) , brg );
break;
case ODD_PARITY:
OpenUART1( (UART_EN | UART_BRGH_FOUR | UART_ODD_PAR_8BIT),
(UART_RX_ENABLE | UART_TX_ENABLE) , BRG );
break;
case EVEN_PARITY:
OpenUART1( (UART_EN | UART_BRGH_FOUR | UART_EVEN_PAR_8BIT),
(UART_RX_ENABLE | UART_TX_ENABLE) , BRG );
break;
}
printf("\n\rChipKIT PRO MX7 Serial Port 1 ready\n\r");
}
/* _mon_putc FUNCTION DESCRIPTION ******************************************
SYNTAX: void _mon_putc( char c);
KEYWORDS: printf, console, monitor
DESCRIPTION: Sets up serial port to function to serves as the system console for the XC32 library printf function.
This function is used only by library function code.
PARAMETER 1: c: The character to send to monitor
RETURN VALUE: None
NOTES: This function will block until space is available
in the transmit buffer
END DESCRIPTION **********************************************************/
void _mon_putc(char c)
{
while(BusyUART1());
WriteUART1((unsigned int) c);
} // end of _mon_putc
/* putcU1 FUNCTION DESCRIPTION ********************************************
SYNTAX: int putcU1( int c);
KEYWORDS: UART, character
DESCRIPTION: Waits while UART1 is busy (buffer full) and then sends a
single byte to UART1.
PARAMETER: c: character to send
RETURN VALUE: character sent
NOTES: This function will block until space is available in the
transmit buffer. The data type for the parameter, "c" is
int but only the least significant 8 bits of the parameter
are sent out the serial port.
END DESCRIPTION **********************************************************/
int putcU1( int c)
{
while(BusyUART1());
WriteUART1((unsigned int) c);
return c;
} // end of putcU1
/* getcU1 FUNCTION DESCRIPTION ********************************************
SYNTAX: int getcU1( char *ch_ptr);
KEYWORDS: character, get
DESCRIPTION: Checks for a new character to arrive to the UART1 serial
port.
PARAMETER 1: ch_ptr: pointer to character
RETURN VALUE: TRUE = new character received
FALSE = No new character
NOTES: This function does not block for new character not ready
END DESCRIPTION **********************************************************/
int getcU1( char *ch_ptr)
{
if( !DataRdyUART1()) // wait for new char to arrive
return FALSE; // Return new data not available flag
else
{
*ch_ptr = ReadUART1();// read the char from receive buffer
return TRUE; // Return new data available flag
}
}// end of getcU1
/* putsU1 FUNCTION DESCRIPTION ********************************************
SYNTAX: int putsU1( const char *s);
KEYWORDS: UART, string
DESCRIPTION: Sends a NULL terminates text string to UART1 with
CR and LF appended
PARAMETER 1: pointer to text string
RETURN VALUE: Logical TRUE
NOTES: This function will block until space is available
in the transmit buffer
END DESCRIPTION **********************************************************/
int putsU1( const char *s)
{
putsUART1(s); // Uses XC32 peripheral library function
putcUART1( '\r'); // Uses XC32 peripheral library function
putcUART1( '\n'); // Uses XC32 peripheral library function
return 1;
} // putsU1
/* getstrU1 FUNCTION DESCRIPTION ******************************************
SYNTAX: int getstrU1( char *s, unsigned int len );
KEYWORDS: string, get, UART
DESCRIPTION: This function assembles a line of text until the number of
characters assembled exceed the buffer length or an ASCII
CR control character is received. This function echos each
received character back to the UART. It also implements a
destructive backspace. ASCII LF control characters are
filtered out. The returned string has the CR character
removed and a NULL character appended to terminate the text
string.
PARAMETER 1: s: pointer to string
PARAMETER 2: len: maximum string length
RETURN VALUE: TRUE = signals the line of text is complete and ready to
process. FALSE = signals the line of text has not receives
the .
FALSE = waiting for end of line
NOTES: It is presumed that the buffer pointer or the buffer length
does not change after the initial call asking to
receive a new line of text. This function does not block
for no character received. A timeout can be added to this
function to free resource. There is no way to restart
the function after the first call until a EOL has been received. Hence this function has denial of service security risks.
END DESCRIPTION **********************************************************/
int getstrU1( char *s, unsigned int len )
{
static int eol = TRUE; // End of input string flag
static unsigned int buf_len;
static char *p1; // copy #1 of the the buffer pointer
static char *p2; // copy #2 of the the buffer pointer
char ch; // Received new character
if(eol) // Initial call to function - new line
{ // Make two copies of pointer - one for
p1 = s; // receiving characters and one for marking
p2 = s; // the starting address of the string. The
eol = FALSE; // second copy is needed for backspacing.
buf_len = len-1; // Save maximum number of characters that can be
// received.
}
if(!(getcU1(&ch))) // Check for character received
{
return FALSE; // Bail out if not
}
else
{
*p1 = ch; // Save new character in string buffer
putcU1( *p1); // echo character
switch(ch) // Test for control characters
{
case BACKSPACE:
if ( p1>p2)
{
putcU1( ' ' ); // overwrite the last character
putcU1( BACKSPACE );
buf_len++;
p1--; // back off the pointer
}
break;
case '\r': // end of line, end loop
putcU1( '\n' ); // add line feed
eol = TRUE; // Mark end of line
break;
case '\n': // line feed, ignore it
break;
default:
p1++; // increment buffer pointer
buf_len--; // decrement length counter
}
}
if( (buf_len == 0) || eol ) // Check for buffer full or end of line
{
*p1 = '\0'; // add null terminate the string
EOL = TRUE; // Set EOL flag to TRUE
}
return EOL; // Return with EOL flag set to FALSE
} // end of getstrU1
// End of comm.c