// UART1.c - UART1 for PIC32 either polled or interrupt driven
// to use for another UART
// 1. copy this file uart2.c (to uart4.c say) and change references to uart2 to uart4
// 2. copy uart2.h (to uart4.h say) and edit the defines to suit UART4
#include "UART1.h"
#ifdef UART_RX_INTERRUPT
// buffer and pointers for received data under interrupts
#define BUFFER 100
static unsigned char UARTx(rxBuffer)[BUFFER]={0}, * UARTx(rxIn)=0, * UARTx(rxOut)=0;
static int UARTx(number)=0; // number of characters in buffer
int UARTx(error)=0; // number of buffer overflows
#endif
//******************************************************************************
// Constants
//******************************************************************************
#define BRG_DIV2 16
#define BRGH2 0
// initialize UART
// Input: baud rate
// Output: 1 for OK, 0 if baud rate error > 3%
int UARTx(Init)(const long int BAUDRATE)
{
int error;
// This initialization assumes 36MHz Fpb clock. If it changes,
// you will have to modify baud rate initializer.
UARTConfigure(UART1, UART_ENABLE_PINS_TX_RX_ONLY);
UARTSetFifoMode(UART1, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY);
UARTSetLineControl(UART1, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
UARTSetDataRate(UART1, GetPeripheralClock(), BAUDRATE);
UARTEnable(UART1, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));
#ifdef UART_RX_INTERRUPT
// Configure UART1 RX Interrupt
INTEnable(INT_SOURCE_UART_RX(UART1), INT_ENABLED);
INTSetVectorPriority(INT_VECTOR_UART(UART1), INT_PRIORITY_LEVEL_2);
INTSetVectorSubPriority(INT_VECTOR_UART(UART1), INT_SUB_PRIORITY_LEVEL_0);
// configure for multi-vectored mode
INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
// enable interrupts
INTEnableInterrupts();
UARTx(rxIn)= UARTx(rxOut)= UARTx(rxBuffer);
UARTx(number)=UARTx(error)=0;
#endif
error = (int) (100 *( (long int) UARTGetDataRate(UART1, GetPeripheralClock())- BAUDRATE)/BAUDRATE);
if (error > 2)
{ printf("\n\n" UARTNAME " baud rate shold be %ld is %lu error %d\n", BAUDRATE, UARTGetDataRate(UART1, GetPeripheralClock()), error); return -1; }
printf(""UARTNAME " baud rate %ld set up\n", BAUDRATE);
return 0;
}
#ifdef UART_RX_INTERRUPT
// UART 2 interrupt handler - it is set at priority level 2
//void __ISR(_UART1_VECTOR, ipl2) IntUart2Handler(void)
void UART_RX_INTERRUPT
{
// Is this an RX interrupt?
if(INTGetFlag(INT_SOURCE_UART_RX(UART1)))
{
// Clear the RX interrupt Flag
INTClearFlag(INT_SOURCE_UART_RX(UART1));
*UARTx(rxIn)++ = UARTGetDataByte(UART1); // read character into buffer
if(UARTx(rxIn) >= UARTx(rxBuffer)+BUFFER) UARTx(rxIn)=UARTx(rxBuffer); // check for end of buffer
if(UARTx(number)++>=BUFFER) { UARTx(rxIn)= UARTx(rxOut)= UARTx(rxBuffer); UARTx(number)=0; UARTx(error)++;}
// Toggle LED to indicate UART activity
//mPORTAToggleBits(BIT_7);
}
}
#endif
// Function: UART flush - flush input data
void UARTx(flush)(void)
{
#ifdef UART_RX_INTERRUPT
UARTx(rxIn)= UARTx(rxOut)= UARTx(rxBuffer);
UARTx(number)=UARTx(error)=0;
#endif
}
//Function:UART CharReady - return true if there is a new byte in UART reception buffer.
char UARTx(CharReady)()
{
#ifdef UART_RX_INTERRUPT
return UARTx(rxIn) != UARTx(rxOut); // pointers different - if so characters in buffer
#else
return UARTReceivedDataIsAvailable (UART1); // none zero if UART has received data
#endif
}
//Function:UART GetChar : waits for a byte to be received. It then returns that byte.
char UARTx(GetChar)()
{
char Temp;
#ifdef UART_RX_INTERRUPT
while(UARTx(rxIn) == UARTx(rxOut)) Nop(); // wait for character
Temp = *UARTx(rxOut)++; // get character
UARTx(number)--;
if(UARTx(rxOut) == UARTx(rxBuffer)+BUFFER) UARTx(rxOut)=UARTx(rxBuffer); // check for end of buffer
#else
//while(! _UARTx(RXIF)); // wait for data
Temp= UARTGetDataByte(UART1); // read character into buffer
// _UARTx(RXIF) = 0; // clear interrupt flag
#endif
return Temp;
}
// Function:UART GetString - read a string until \n received or length reached
void UARTx(GetString)(char *str , int length)
{
char ch=0;
while(ch != '\n')
if(UARTx(CharReady)())
{
ch=*str=UARTx(GetChar)();
str++;
*str=0;
if(--length<1) return;
}
}
// Function:UART PutChar - This routine writes a character to the transmit FIFO
void UARTx(PutChar)(const char ch )
{
while(!UARTTransmitterIsReady(UART1));
UARTSendDataByte(UART1, ch); // transmit character
}
//Function:UART PrintString - prints a string of characters to the UART.
void UARTx(PrintString)(const char *str )
{
char c;
while( (c = *str++) )
UARTx(PutChar)(c);
}
//Function:UART PrintBytes - prints byte array of specified length to the UART.
void UARTx(PrintBytes)(const unsigned char *str, int length )
{
int i;
for(i=0;i<length;i++)
UARTx(PutChar)(str[i]);
}
// UART1.h for PIC32 either polled or interrupt driven
#include "hardware.h"
// undefine any previous definitions
#undef UARTNAME
#undef UARTx
#undef UART_RX_INTERRUPT
// definitions for this UART
#define UARTNAME "\nUART1" // UART name as a string
#define UARTx(text) UART1##text
// if not using interrupts comment out next line
// defines UART 1 interrupt handler - it is set at priority level 2
#define UART_RX_INTERRUPT __ISR(_UART1_VECTOR, ipl2) IntUart1Handler(void)
//extern int UARTx(number); // number of characters in buffer
extern int UARTx(error); // number of buffer overflows
// initialize UART
// Input: baud rate
// Output: 1 for OK, 0 if baud rate error > 3%
int UARTx(Init)(const long int BAUDRATE);
//Function:UART CharReady - return true if there is a new byte in UART reception buffer.
char UARTx(CharReady)();
//Function:UART PrintString - prints a string of characters to the UART.
void UARTx(PrintString)(const char *str );
//Function:UART GetChar : waits for a byte to be received. It then returns that byte.
char UARTx(GetChar)();
// Function:UART GetString - read a string until \n received or length reached
void UARTx(GetString)(char *str , int length) ;
//Function:UART PrintBytes - prints byte array of specified length to the UART.
void UARTx(PrintBytes)(const unsigned char *str, int length );
// Function:UART PutChar - This routine writes a character to the transmit FIFO
void UARTx(PutChar)(const char ch );
// Function: UART findString
// check for character string received
// if ASCII character add it to string data
// if \n return number of characters in string
// if timeout and no characters received return -1 else number of characters in string
int UARTx(findString)(char *find, char *data, const int length, const int timeout);
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?