Atmel SAM4S Xplained UART

时间:2013-01-11 14:21:08

标签: arm uart

我有一个SAM4s Xplained,想要使用UART1,但无法找到示例或帮助。我试着靠自己但是没办法。

到目前为止,这是我的代码:

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);
   }
}

我尝试了不同的东西。作为示例,我激活了自动回音,但是我没有在我的计算机上收到任何东西,我在SAM4上没有收到任何东西。

3 个答案:

答案 0 :(得分:6)

这是我的工作代码: 离线

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);
   }
}
最好的问候 利奥

  • 关闭 -

答案 1 :(得分:1)

问题是这块板上的UART Sam4S Explained Pro没有映射到PB2A和PB3A。 UART映射到PB2A和PA22A。我花了很长时间来弄清楚这一点,因为它在文档中是错误的。

答案 2 :(得分:0)

您可能也喜欢这个页面: 它包含SAM4S uart的设置和许多其他示例。 ASF Source Code Document它使用USART0,我知道你想使用USART1。但仍然是一个很好的参考。

希望这有帮助