我有一个 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 也没有收到任何东西。

最佳答案

这是我的工作代码:
离线

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

此致
狮子座
  • 关闭 -
  • 关于arm - Atmel SAM4S Xplained UART,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14280053/

    10-13 06:37