我尝试与GPS模块建立UART通信,但仍然遇到一些通信错误。 GPS大约每秒向SAM4E16E micro发送一次数据包,该数据包应触发中断并读入数据。我有两个配置如下的UARTS
conf_uart.h
#include "asf.h" //uart.h etc. included here
#define UART_SERIAL_BAUDRATE 4800
#define UART_SERIAL_CHANNEL_MODE UART_MR_CHMODE_NORMAL
#define UART_SERIAL_MODE UART_MR_PAR_NO
#define UART_SERIAL_STOP_BIT US_MR_NBSTOP_1_BIT
/* =============== UART0 =============== */
#define PINS_UART0 (PIO_PA9A_URXD0 | PIO_PA10A_UTXD0)
#define PINS_UART0_FLAGS (PIO_PERIPH_A | PIO_DEFAULT)
#define PINS_UART0_MASK (PIO_PA9A_URXD0 | PIO_PA9A_URXD0)
#define PINS_UART0_PIO PIOA
#define PINS_UART0_ID ID_PIOA
#define PINS_UART0_TYPE PIO_PERIPH_A
#define PINS_UART0_ATTR PIO_DEFAULT
/* =============== 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
conf_uart.c
#include "conf_uart.h"
void uart_custom_init_1(void) {
// 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);
}
uart_custom_init_0(void)
{
// 1. Enable UART peripheral clock
//2. Enable the required PIO's
pio_configure(PINS_UART0_PIO, PINS_UART0_TYPE, PINS_UART0_MASK, PINS_UART0_ATTR);
pmc_enable_periph_clk(ID_UART0);
// 3. call init
const sam_uart_opt_t uart0_settings =
{ sysclk_get_cpu_hz(), UART_SERIAL_BAUDRATE, UART_SERIAL_MODE };
uart_init(UART0,&uart0_settings); //Init UART1 and enable Rx and Tx
// read on interrupt
uart_enable_interrupt(UART0,UART_IER_RXRDY); //Interrupt reading ready
NVIC_EnableIRQ(UART0_IRQn);
}
void UART1_Handler() {
uint32_t dw_status = uart_get_status(UART1);
if(dw_status & UART_SR_RXRDY) {
uint8_t received_byte1;
uart_read(UART1, &received_byte);
read_buffer[read_buffer_index] = received_byte;
read_buffer_index++;
}
}
void UART0_Handler() {
uint32_t dw_status = uart_get_status(UART0);
if(dw_status & UART_SR_RXRDY)
{
uint8_t received_byte0;
uart_read(UART0, &received_byte);
read_buffer[read_buffer_index] = received_byte;
read_buffer_index++;
}
}
UART1完美运行。 UART0仅每个数据包大约触发一次,甚至不获取数据包中的第一个字节(应为36,接收16)。据我所知,他们是完全相同的。芯片正在工作,我可以看到它在范围内传输。我的想法基本上是新鲜的。