system
July 26, 2013, 5:58am
1
I'm using ATMEL AVR studio 6 to program for the Arduino due and following the manual from Atmel: http://www.atmel.com/Images/doc11057.pdf
I'm trying to figure out how to communicate to the SAM3X8E via TX0, in this case I want the due to send something via the TX line at 9600 baud:
#include <asf.h>
#define UART_CR *(unsigned volatile char *) (0x400E0800) //Control Register
#define UART_IER *(unsigned volatile char *) (0x400E0008) //Interrupt Enable Register
#define UART_IDR *(unsigned volatile char *) (0x400E000C) //Interrupt Disable Register
#define UART_BRGR *(unsigned volatile char *) (0x400E0820) //Baud Rate Generator Register
#define UART_MR *(unsigned volatile char *) (0x400E0804) //Mode Register
#define UART_THR *(unsigned volatile char *) (0x400E081C) //Transmit Holding Register
#define UART_IMR (0x400E0010) //Interrupt Mask Register
#define UART_SR (0x400E0014) // Status Register
void UART_Init(void){
//Control Register, RX disabled, TX enabled, Status reset.
UART_CR = 0x160;
//Baud rate, 9600, 84Mhz.
UART_BRGR = 0x222;
//Normal mode.
UART_MR = 0x00;
}
void UART_Out(void){
UART_THR = 0x09;
}
int main (void)
{
board_init();
UART_Init();
UART_Out();
}
Unfortunately, I can't seem to get anything working
system
August 4, 2013, 3:46pm
2
Before we deal with the software... Communicate TO the Due on TX? If you're sending something to the Due, you'll be receiving it on the Rx line, not the TX line.
Most (all?) peripherals on an ARM have to have their clocks enabled before they will work. I assume the UART would be the same.
I haven't programmed on the SAM but for an LPC this is required to get the GPIO ports to work
// Enable AHB clock for GPIO ports
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<30) | (1<<31);
I'm sure there will be something similar.
Rob
AdderD
August 5, 2013, 12:07pm
4
Heh, it always amazes me the # of people who come on the board to ask questions that obviously indicate that they aren't using the Arduino framework at all. Anyway, looking at the Arduino core library source would help here:
Here is the code that Arduino uses to initialize a UART:
// Configure PMC
pmc_enable_periph_clk( _dwId ) ;
// Disable PDC channel
_pUart->UART_PTCR = UART_PTCR_RXTDIS | UART_PTCR_TXTDIS ;
// Reset and disable receiver and transmitter
_pUart->UART_CR = UART_CR_RSTRX | UART_CR_RSTTX | UART_CR_RXDIS | UART_CR_TXDIS ;
// Configure mode
_pUart->UART_MR = UART_MR_PAR_NO | UART_MR_CHMODE_NORMAL ;
// Configure baudrate (asynchronous, no oversampling)
_pUart->UART_BRGR = (SystemCoreClock / dwBaudRate) >> 4 ;
// Configure interrupts
_pUart->UART_IDR = 0xFFFFFFFF;
_pUart->UART_IER = UART_IER_RXRDY | UART_IER_OVRE | UART_IER_FRAME;
// Enable UART interrupt in NVIC
NVIC_EnableIRQ(_dwIrq);
// Enable receiver and transmitter
_pUart->UART_CR = UART_CR_RXEN | UART_CR_TXEN ;
You'll notice that you do need to enable the clock first.
To enable the peripheral clock for UART, use this line of code
PMC->PMC_PCER0 |= PMC_PCER0_PID8;
PCER0 or Peripheral clock enable register 0
PID8 or Peripheral Identifier 8 which is UART.
Refer to the datasheet for more info.