4

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.

Gianni B.
  • 2,691
  • 18
  • 31
Leo K.
  • 111
  • 1
  • 1
  • 6
  • I solved it, see http://www.at91.com/forum/viewtopic.php/f,29/t,21332/sid,6c5bc3ec666c266be7d6794344d0639d/ – Leo K. Jan 17 '13 at 17:03

3 Answers3

7

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 -
Leo K.
  • 111
  • 1
  • 1
  • 6
  • 1
    +1 Very helpful when I couldn't find any example of UART initialization, either [online](http://asf.atmel.com/docs/3.11.0/sam3x/html/group__sam__drivers__uart__group.html) or in AtmelStudio. – Bob Stein Jun 19 '14 at 17:03
1

The problem is that the UART on this board Sam4S Explained Pro is not mapped to PB2A and PB3A. The UART is mapped to PB2A and PA22A. It took me a long day to figure this out because it's wrong in the documentation.

jetcode
  • 11
  • 1
0

You might also like this page: It contains the setup for SAM4S uart and lots of other examples. ASF Source Code Document It uses USART0, and i know you want to use USART1. But still a good reference.

Hope this helps

Mattis Asp
  • 993
  • 3
  • 14
  • 29
  • Just coming here, it's worth noting that `UART` is **NOT** `USART` *in this case*. The SAM4S has 2 UART peripherals, and 2 USART peripherals. They have slightly different APIs, and are routed to different pins. – Fake Name Oct 05 '16 at 00:55