Configure the UART in MCC
Once you Geneate the MCC code it will have dealt with initialising the UART ready for you to use it.
In your code
#include <definitions.h> //Include all the MCC generated files so their fucntions can be called
volatile uint8_t Uart3RxBuffer[1];
//IN YOUR INITIALSE CODE
//----- SETUP USART 3 -----
//Used for:
//Config done in MCC:
// Setup IO pin assignments:
// PPS Input Pin Configuration > Use PPS Input# > U3RX, > RPF0
// PPS Output Pin Configuration > Use PPS Output# > U3TX > RPF1
// Setup UART:
// Operating mode: None blocking mode
// 115200-8-N-1
// Setup interrupts:
// Enable UART1 Fault Interrupt = Will already be set to Yes
// Enable UART1 Receive Done Interrupt = Will already be set to Yes
// Enable UART1 Transmit Done Interrupt = Will already be set to Yes
// Priority (set for all): 3 //<<<Set as required for your application
// Subpriority (set for all): 0 //<<<Set as required for your application
//UART3_WriteCallbackRegister(UART3_MyTxEventHandler, (uintptr_t)NULL); //Register our callback function to be called when TX is complete (optional)
UART3_ReadCallbackRegister(UART3_MyRxEventHandler, (uintptr_t)NULL); //Register our callback function to be called when data is received or a RX error occurs
//Start receiving
UART3_Read(&Uart3RxBuffer[0], 1); //You can setup to receive multiple bytes, but we want an IRQ callback to us for every byte received
//TX some bytes
uint8_t TxBuffer[] = {0x30, 0x39};
UART3_Write(&TxBuffer[0], sizeof(TxBuffer));
//YOUR FUNCTION TO BE CALLED WHEN TX IS COMPLETE (if wanted)
//*******************************************
//*******************************************
//********** UART3 TX COMPLETE IRQ **********
//*******************************************
//*******************************************
/*
void UART3_MyTxEventHandler(uintptr_t context)
{
//TX HAS COMPLETED
}
*/
//YOUR FUNCTION TO BE CALLED WHEN RX IS COMPLETE
//**********************************
//**********************************
//********** UART3 RX IRQ **********
//**********************************
//**********************************
void UART3_MyRxEventHandler(uintptr_t context)
{
uint8_t Data;
Data = Uart3RxBuffer[0];
if (UART3_ErrorGet() != UART_ERROR_NONE)
{
//A RX ERROR HAS OCCURED
//(It has been cleared already)
//UART_ERROR Values: UART_ERROR_NONE, UART_ERROR_OVERRUN, UART_ERROR_FRAMING, UART_ERROR_PARITY
//Start receiving again ready for next byte
UART3_Read(&Uart3RxBuffer[0], 1);
}
else
{
//WE HAVE A VALID RX
//Start receiving again ready for next byte
UART3_Read(&Uart3RxBuffer[0], 1);
//>>>>> Test code - return the byte + 1
//U3TXREG = Data + 1;
//<<<<<
}
}
Other UART functions
MCC will have created a few useful functions you can call – see the file it generated in: /config/default/peripheral/uart/
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.