Atmel SAM4S Xplained UART

Leo K. picture Leo K. · Jan 11, 2013 · Viewed 10k times · Source

I've a SAM4s Xplained and want to use the UART1 but can't find a example or help. I tried on my own but it doesn't work.

Here is my code so far:

conf_uart.h

#include "asf.h" //uart.h etc. included here
#include <sam4s_xplained.h>

#define UART_SERIAL_BAUDRATE        9600
#define UART_SERIAL_CHANNEL_MODE   UART_MR_CHMODE_AUTOMATIC //UART_MR_CHMODE_NORMAL
#define UART_SERIAL_MCK            240000000UL //CHIP_FREQ_CPU_MAX (tired both!)
#define UART_SERIAL_MODE         UART_MR_PAR_NO

void uart_custom_init(void);

conf_uart.c

#include "conf_uart.h"

uint8_t received_byte;

void uart_custom_init(void) {
    sysclk_init();

    const sam_uart_opt_t uart_console_settings = {
       UART_SERIAL_BAUDRATE,
       UART_SERIAL_CHANNEL_MODE,
       UART_SERIAL_MCK,
       UART_SERIAL_MODE
    };

    uart_init(UART1,&uart_console_settings);      //Init UART1

    uart_enable_rx(UART1);                     //Enable RX (receiving)
    uart_enable_tx(UART1);                     //Enable TX (transmitting)
    uart_enable(UART1);                     //Enable UART1

    uart_enable_interrupt(UART1,UART_IER_RXRDY);   //Interrupt reading ready
    NVIC_EnableIRQ(UART1_IRQn);

}

void UART1_Handler() {
   uint32_t dw_status = uart_get_status(UART1);

   if(dw_status & UART_SR_RXRDY) {
      uint8_t received_byte;
      uart_read(UART1, &received_byte);
      uart_write(UART1, received_byte);
   }
}

I tried different things. As Example I activated the automatic echo, but I don't recieve anything on my Computer and I don't receive anything at my SAM4s.

Answer

Leo K. picture Leo K. · Jan 17, 2013

Here is my working code: Offline

conf_uart.h

#include "asf.h" //uart.h etc. included here

#define UART_SERIAL_BAUDRATE        9600
#define UART_SERIAL_CHANNEL_MODE   UART_MR_CHMODE_NORMAL
#define UART_SERIAL_MODE         UART_MR_PAR_NO

/* =============== UART1 =============== */ //(UART0 is defined but not UART1)
#define PINS_UART1          (PIO_PB2A_URXD1 | PIO_PB3A_UTXD1)
#define PINS_UART1_FLAGS    (PIO_PERIPH_A | PIO_DEFAULT)
#define PINS_UART1_MASK     (PIO_PB2A_URXD1 | PIO_PB3A_UTXD1)
#define PINS_UART1_PIO      PIOB
#define PINS_UART1_ID       ID_PIOB
#define PINS_UART1_TYPE     PIO_PERIPH_A
#define PINS_UART1_ATTR     PIO_DEFAULT

void uart_custom_init(void);
void sendViaUart(uint8_t data);

conf_uart.cpp

#include "conf_uart.h"

void uart_custom_init(void) {
    sysclk_init();

     // set the pins to use the uart peripheral
    pio_configure(PINS_UART1_PIO, PINS_UART1_TYPE, PINS_UART1_MASK, PINS_UART1_ATTR);

    //enable the uart peripherial clock
    pmc_enable_periph_clk(ID_UART1);

    const sam_uart_opt_t uart1_settings =
      { sysclk_get_cpu_hz(), UART_SERIAL_BAUDRATE, UART_SERIAL_MODE };

    uart_init(UART1,&uart1_settings);      //Init UART1 and enable Rx and Tx

    uart_enable_interrupt(UART1,UART_IER_RXRDY);   //Interrupt reading ready
    NVIC_EnableIRQ(UART1_IRQn);

}

void sendViaUart(uint8_t data) {
   while (!(UART1->UART_SR & UART_SR_TXRDY));
   uart_write(UART1, data);
}

void UART1_Handler() {
   uint32_t dw_status = uart_get_status(UART1);

   if(dw_status & UART_SR_RXRDY) {
      uint8_t received_byte;
      uart_read(UART1, &received_byte);
      sendViaUart(received_byte);
   }
}

best regards Leo

  • closed -