Can't get UART/USART working on a SAM3S4B

Discussion around product based on ARM Cortex M3 core.

Moderators: nferre, ncollot

henryneedhelp
Posts: 5
Joined: Wed Mar 18, 2015 6:57 pm

Can't get UART/USART working on a SAM3S4B

Wed Mar 18, 2015 7:33 pm

I have spent two days full time trying to get this most basic of functions working but can't get any data in our out of any of the 4 UARTS on this device. I expect that it is some kind of register config issue but have been though all the documentation I can find and can't see anything I am missing.
I have
Enabled the Peripheral clock PMC->PMC_PCER0 = (1<<UART1);
Disabled the GPIO control of these pins
PIOA->PIO_PDR = PIO_PA5A_RXD0 | PIO_PA6A_TXD0;
Reset the UART Control reg - UART_CR= UART_CR_RSTRX | UART_CR_RSTTX | UART_CR_RXDIS | UART_CR_TXDIS;
Set the Mode register UART_MR = UART_MR_PAR_NO;
Set the baud rate register UART_BRGR = BRGR
Enabled transmits and received UART_CR = UART_CR_RXEN | UART_CR_TXEN

I then try to send data one byte at a time waiting for TXEMPTY each time

while((m_pUART0->UART_SR & UART_SR_TXEMPTY) == 0);
m_pUART0->UART_THR = out;

but nothing comes out, I have timed the delay between SR_TXEMPTY  changing and it is exactly what you expect from a 9600 baud transmit but there is nothing on the pin. When I re enable the GPIO on the same pin there is no problem toggling the pin high or low but when its in Peripheral A more it is low. I have tried enabling the pull up and it goes to about 3v so it seems the peripheral is tri state. 

If anyone has any ideas of what could be cause this I would be massively grateful! I am about to switch to another micro family and I don't know what else to try.
henryneedhelp
Posts: 5
Joined: Wed Mar 18, 2015 6:57 pm

Re: Can't get UART/USART working on a SAM3S4B

Wed Mar 18, 2015 7:36 pm

Code: Select all

void UART_Initialize(void)
{
	const unsigned int BRGR = SystemCoreClock / (9600 * 16);
	
	// Disable write protect for config
	PMC->PMC_WPMR = 0x504D4300;             
	
	// enable UART 0 clock
	PMC->PMC_PCER0 = (1 << ID_UART0);

	// Disable GPIO use
	PIOA->PIO_PDR = PIO_PA5A_RXD0 | PIO_PA6A_TXD0; 		
	
	// Enable write protect
	PMC->PMC_WPMR = 0x504D4301;             




	// Reset and disable receiver & transmitter
	m_pUART0->UART_CR = UART_CR_RSTRX | UART_CR_RSTTX | UART_CR_RXDIS | UART_CR_TXDIS;
	
	m_pUART0->UART_MR = UART_MR_PAR_NO; // No parity normal mode
	
	// Set baud rate generator register
	m_pUART0->UART_BRGR = BRGR; // MCK / 115200 * 16 (inc 1.6% error)
	
	// disable DMA transfer UART_PTCR
	m_pUART0->UART_PTCR = UART_PTCR_RXTDIS | UART_PTCR_TXTDIS;
	
	// Enable Tx and Rx
	m_pUART0->UART_CR = UART_CR_RXEN | UART_CR_TXEN;
}

void SendByte(unsigned char out)
{
	// wait until holding register is ready
	while((m_pUART0->UART_SR & UART_SR_TXEMPTY) == 0);
	
	// then load with next byte
	m_pUART0->UART_THR = out;	
}
blue_z
Location: USA
Posts: 2037
Joined: Thu Apr 19, 2007 10:15 pm

Re: Can't get UART/USART working on a SAM3S4B

Wed Mar 18, 2015 9:02 pm

henryneedhelp wrote:... but there is nothing on the pin.
That's not a technical or specific description of what's happening.
If you cannot measure a high-logic voltage on the transmit pin as the UART idles, then you need to verify power connections, the peripheral clocks, pin configurations and Tx enable.

Verify the config by dumping registers.
henryneedhelp wrote:Disabled the GPIO control of these pins
PIOA->PIO_PDR = PIO_PA5A_RXD0 | PIO_PA6A_TXD0;
That's not sufficient to select the peripheral multiplexing on the pins.
If UART0 is the peripheral A function, then you need to specify that for all UART pins using the PIO_ABCDSR1 and PIO_ABCDSR2 registers.
IOW you may have been configuring a peripheral device that is disconnected from the board and probing pins that are disconnected.

Regards
henryneedhelp
Posts: 5
Joined: Wed Mar 18, 2015 6:57 pm

Re: Can't get UART/USART working on a SAM3S4B

Thu Mar 19, 2015 12:25 pm

Hi blue_z,

Thank you very much for your reply, your help is very welcome

Sorry this was not very detailed about the pin output, I have a scope connected to the Tx output pin and trigger on any state change, when the pin is configured as GPIO setting or clearing the output causes a level change captured on the scope, after configuring the UART the line idles low, if I enable the pull up register it rises. When I enable TXEN and load a byte into THR the busy bit goes high for the same period of time that the transmit should take (1/9600 * 10) but there is no change on the output pin.
henryneedhelp wrote:Disabled the GPIO control of these pins
PIOA->PIO_PDR = PIO_PA5A_RXD0 | PIO_PA6A_TXD0;
As the UART is peripheral A the state of the cosponsoring PIO_ABCDSR1 PIO_ABCDSR bit should be 0 0 which is there default so I don't think I need to change these but I have tried anyway. 
If UART0 is the peripheral A function, then you need to specify that for all UART pins using the PIO_ABCDSR1 and PIO_ABCDSR2 registers.
blue_z wrote:IOW you may have been configuring a peripheral device that is disconnected from the board and probing pins that are disconnected.


I think you are right, the UART seems to be configured and running correctly but doesn't seem to be mapped to the pins, I just can't figure out why if PIO_ABCDSR is selecting Peripheral A and the GPIO is disabled.

I will get a post a dump of the registers.
henryneedhelp
Posts: 5
Joined: Wed Mar 18, 2015 6:57 pm

Re: Can't get UART/USART working on a SAM3S4B

Thu Mar 19, 2015 12:39 pm

I couldn't seem to attach a screen shot but the register values when I break after the UART has been initialised are as follows:

PIOA:
PIOA_PSR   0xFFFFFF9F Bits 5 & 6 clear (PA5_TXD0, PA6_RXD0 GPIO function disabled)
PIOA_MDSR 0x00000000 Bits 5 & 6 clear (Multi drive disabled)
PIOA_ABCDSR[0] 0x00000000 Bits 5 & 6 = 0 0 (Peripheral A selected)
PIOA_ABCDSR[1] 0x00000000




PCM:
PMC_PCSR0 0x0000CB00
PID8 Set - UART1 Enabled
PID9 Set - UART1 Enabled




UART0:
UART0_MR 0x00000800 No Parity, Normal Mode
UART0_SR 0x40001A1A
TXRDY = 1,
ENDRX = 1,
ENDTX = 1,
OVRE = 0,
FRAME = 0,
PARE = 0,
TXEMPTY = 1,
TXBUFF = 1,
RXBUFF 1
UART0_BRGR 0x000001A0 9600 buad at 64Mhz master clk
blue_z
Location: USA
Posts: 2037
Joined: Thu Apr 19, 2007 10:15 pm

Re: Can't get UART/USART working on a SAM3S4B

Thu Mar 19, 2015 8:22 pm

henryneedhelp wrote:the UART seems to be configured and running correctly but doesn't seem to be mapped to the pins
We seem to agree that this fits what you observe.
So focus on the PIO configuration.
henryneedhelp wrote:

Code: Select all

   // Disable write protect for config
   PMC->PMC_WPMR = 0x504D4300; 
You also need to unlock the PIO Write Protect Mode Register. The datasheet indicates that this key (high three bytes) should be 0x50494F (“PIO” in ASCII).
Although I would have expected the PIO Status Register dump to indicate such a problem.

Regards
henryneedhelp
Posts: 5
Joined: Wed Mar 18, 2015 6:57 pm

Re: Can't get UART/USART working on a SAM3S4B

Fri Mar 20, 2015 11:30 am

Thanks for your help, yes I have used the Write protect register (PMC->PMC_WPMR) to unlock the GPIO config registers and am pretty sure this is working. I have order another board as I suspect that this device I am using may be damaged and we see if that makes a difference.

Best regards

Henry

Return to “SAM3 Cortex-M3 MCU”

Who is online

Users browsing this forum: No registered users and 2 guests