uisng UART with PDC in SAM4S

Discussion around product based on ARM Cortex M4 core.

Moderators: nferre, ncollot

sijeo.philip
Posts: 2
Joined: Sun Oct 26, 2014 6:52 pm

uisng UART with PDC in SAM4S

Wed Feb 04, 2015 6:18 pm

Hi, I am trying to use UART in SAM4s using PDC.. I am getting continuous interrupts, without receiving or transmitting any bytes/messages, not sure what I have missed. Can anyone help me to find out what I have missed... Below are the Initialization routine and Transmit Used

Code: Select all

#include "UART_RELAY.H"
#include <SAM4S.H>

extern struct RX_Frame RX_UART_FRAME;
//Initialize the UART peripheral UART1
void Uart_Init(void)
{
uint32_t priority;	
uint32_t priorityGroup;	
REG_PMC_WPMR = 0x504D4300; // DISABLE PMC WRITE PROTECTION
	 
	 //ENABLING PORT UART1 PERIPHERAL CLOCK 
 REG_PMC_PCER0 = 0x00000200; // PERIPHERAL ID 9	
 REG_UART1_CR = 0x00000050;  //ENABLE UART1 RECIEVER AND TRANSMITTER
 REG_UART1_MR = (4UL<<9); // NO PARITY AND NORMAL MODE
 REG_UART1_BRGR = 52; // SETTING BAUD RATE TO 115200;
	
	REG_PMC_WPMR = 0x504D4301; // ENABLE PMC WRITE PROTECTION
	REG_UART1_IER = 0x00001000; // ENABLE RXBUFF INTERRUPTS
	REG_UART1_RCR = 8;
	REG_UART1_RPR = (uint32_t)&RX_UART_FRAME;
	REG_UART1_PTCR = 0x00000001; //RXEN IS SET 
	__disable_irq();
	priorityGroup = NVIC_GetPriorityGrouping();
	priority = NVIC_EncodePriority(priorityGroup, 3, 6);
	NVIC_SetPriority(UART1_IRQn, priority);
	NVIC_EnableIRQ(UART1_IRQn);
	__enable_irq();
}

//Transmit Commansd to the other controller.
void SendUARTCommand(unsigned char* data, unsigned short dataCount)
{
REG_UART1_TCR = dataCount;
REG_UART1_TPR = (uint32_t)data;
REG_UART1_IER = 0x00000800; // ENABLE TXBUFE INTERRUPTS	
REG_UART1_PTCR = 0x00000100; //TXEN is SET;	
}
Below is the Interrupt Service Routine Used

Code: Select all

void UART1_IRQHandler(void)
{
	if((REG_UART1_SR&0x00001000)==0x00001000)
	{
		if(RX_UART_FRAME.Message_Type == PARAM_MEASURE_MODE)
		{
			RX_UART_FRAME.Message_Type=0;
			REG_UART1_RCR = 48;
			REG_UART1_RPR = (uint32_t)&RX_UART_FRAME.Measure_Values[0];
			Reading_Measure_Values = false;
			Read_Done_Flag=true;
		}
		else
		if(RX_UART_FRAME.Message_Type==STATUS_UPDATE_MODE)
		{
			STATUS_LED = RX_UART_FRAME.Status;
			StatusLED_Update_Flag = true;
			if((STATUS_LED&MOTORRUN_LED)==MOTORRUN_LED)
       SET_LEDS(motrorunning_led);
     else
       CLEAR_LEDS(motrorunning_led);
		 
     if((STATUS_LED&MOTORSTOP_LED)==MOTORSTOP_LED)
       SET_LEDS(motorstopped_led);
     else
       CLEAR_LEDS(motorstopped_led);
		 
     if((STATUS_LED&SUCCESS_START_LED)==SUCCESS_START_LED)
       SET_LEDS(sucessstart_led);
     else
       CLEAR_LEDS(sucessstart_led);
		 
     if((STATUS_LED&TIME_BTW_START_LED)==TIME_BTW_START_LED)
       SET_LEDS(timebetween2start_led);
     else
       CLEAR_LEDS(timebetween2start_led);
		 
     if((STATUS_LED&START_NUMBER_LED)==START_NUMBER_LED)
       SET_LEDS(startnumber_led);
     else
       CLEAR_LEDS(startnumber_led);
		 
     if((STATUS_LED&FAULT_START_LED)==FAULT_START_LED)
       START_LED_ON;
     else
      START_LED_OFF;
		 
		 REG_UART1_RCR = 8;
		 REG_UART1_RPR = (uint32_t)&RX_UART_FRAME;
		}
		else
		if(RX_UART_FRAME.Message_Type==FAULT_TRIP_MODE)
		{
		 FAULT_TYPE = RX_UART_FRAME.Status;
     fault = true;	
		 REG_UART1_RCR = 8;
		 REG_UART1_RPR = (uint32_t)&RX_UART_FRAME;
		}
		else
		if(RX_UART_FRAME.Message_Type==0)
		{
			REG_UART1_RCR = 8;
			REG_UART1_RPR = (uint32_t)&RX_UART_FRAME;
		}
		
	}// END OF RECIEVE
	else
	 if((REG_UART1_SR&0x00000800)==0x00000800)
	 {
		REG_UART1_PTCR = 0x00000200; //TXDIS is SET;	 
	 }// END OF TRANSMIT
//NVIC_ClearPendingIRQ(UART1_IRQn);
}	
It would be really helpful if anybody could give me a sample code to understand how to use a UART with PDC , I am using Keil Compiler for my application
sijeo.philip
Posts: 2
Joined: Sun Oct 26, 2014 6:52 pm

Re: uisng UART with PDC in SAM4S

Thu Feb 05, 2015 4:16 pm

Hi, I could see an example in Atmel Studio for using UART peripheral with PDC and did incorporated the following changes in the posted codes... I couldn't spot any difference between the same.

Code: Select all

void Uart_Init(void)
{
uint32_t priority;	
uint32_t priorityGroup;	
REG_PMC_WPMR = 0x504D4300; // DISABLE PMC WRITE PROTECTION
	 
	 //ENABLING PORT UART1 PERIPHERAL CLOCK 
  REG_PMC_PCER0 = 0x00000200; // PERIPHERAL ID 9	
	REG_PMC_WPMR = 0x504D4301; // ENABLE PMC WRITE PROTECTION
	REG_UART1_MR = (4UL<<9); // NO PARITY AND NORMAL MODE
	REG_UART1_BRGR = 52; // SETTING BAUD RATE TO 115200; 
	
	REG_UART1_RCR = 8;
	REG_UART1_RPR = (uint32_t)&RX_UART_FRAME;
	REG_UART1_CR = 0x00000050;  //ENABLE UART1 RECIEVER AND TRANSMITTER
	
	REG_UART1_PTCR = 0x00000101; // RXTEN and TXEN is SET;	
	REG_UART1_IER = 0x00001000; // ENABLE RXBUFF INTERRUPTS
		
 __disable_irq();
	priorityGroup = NVIC_GetPriorityGrouping();
	priority = NVIC_EncodePriority(priorityGroup, 3, 6);
	NVIC_SetPriority(UART1_IRQn, priority);
	NVIC_EnableIRQ(UART1_IRQn);
	__enable_irq();
	
	
	
}

//Transmit Commansd to the other controller.
void SendUARTCommand(unsigned char* data, unsigned short dataCount)
{
REG_UART1_TCR = dataCount;
REG_UART1_TPR = (uint32_t)data;
}
also below is my updated code for the ISR

Code: Select all

void UART1_IRQHandler(void)
{
	if((REG_UART1_SR&0x00001000)==0x00001000)
	{
		if(RX_UART_FRAME.Message_Type == PARAM_MEASURE_MODE)
		{
			RX_UART_FRAME.Message_Type=0;
			REG_UART1_RCR = 48;
			REG_UART1_RPR = (uint32_t)&RX_UART_FRAME.Measure_Values[0];
			Reading_Measure_Values = false;
			Read_Done_Flag=true;
		}
		else
		if(RX_UART_FRAME.Message_Type==STATUS_UPDATE_MODE)
		{
			STATUS_LED = RX_UART_FRAME.Status;
			StatusLED_Update_Flag = true;
			if((STATUS_LED&MOTORRUN_LED)==MOTORRUN_LED)
       SET_LEDS(motrorunning_led);
     else
       CLEAR_LEDS(motrorunning_led);
		 
     if((STATUS_LED&MOTORSTOP_LED)==MOTORSTOP_LED)
       SET_LEDS(motorstopped_led);
     else
       CLEAR_LEDS(motorstopped_led);
		 
     if((STATUS_LED&SUCCESS_START_LED)==SUCCESS_START_LED)
       SET_LEDS(sucessstart_led);
     else
       CLEAR_LEDS(sucessstart_led);
		 
     if((STATUS_LED&TIME_BTW_START_LED)==TIME_BTW_START_LED)
       SET_LEDS(timebetween2start_led);
     else
       CLEAR_LEDS(timebetween2start_led);
		 
     if((STATUS_LED&START_NUMBER_LED)==START_NUMBER_LED)
       SET_LEDS(startnumber_led);
     else
       CLEAR_LEDS(startnumber_led);
		 
     if((STATUS_LED&FAULT_START_LED)==FAULT_START_LED)
       START_LED_ON;
     else
      START_LED_OFF;
		 
		 REG_UART1_RCR = 8;
		 REG_UART1_RPR = (uint32_t)&RX_UART_FRAME;
		}
		else
		if(RX_UART_FRAME.Message_Type==FAULT_TRIP_MODE)
		{
		 FAULT_TYPE = RX_UART_FRAME.Status;
     fault = true;	
		 REG_UART1_RCR = 8;
		 REG_UART1_RPR = (uint32_t)&RX_UART_FRAME;
		}
		else
		if(RX_UART_FRAME.Message_Type==0)
		{
			REG_UART1_RCR = 8;
			REG_UART1_RPR = (uint32_t)&RX_UART_FRAME;
		}
		
	}// END OF RECIEVE

}	
Not sure if I am missing/overlooking anything.. I am unable to see anything on my scope please do let me know if I am missing anything

Return to “SAM4 Cortex-M4 MCU”

Who is online

Users browsing this forum: No registered users and 2 guests