4.2.3 EDBG USART管脚初始化 SERCOM3 USART连接到EDBG。Edbg_usart_pin_init函数将初始化管脚PA22和PA23管脚到SERCOM外设功能。 /* EDBG UART (SERCOM3) pin initialization */ void edbg_usart_pin_init(void) { /* PA22 and PA23 set into peripheral function C*/ pin_set_peripheral_function(PINMUX_PA22C_SERCOM3_PAD0); pin_set_peripheral_function(PINMUX_PA23C_SERCOM3_PAD1); } Edbg_usart_pin_init 函数调用pin_set_peripheral_function来分配PA22和PA23I/O线连到SERCOM外设功能。 函数pin_set_peripheral_function()将转换I/O GPIO功能到外设功能并且分配给定的外设功能到管脚。这个函数给定一个32位管脚多路值作为它的数据。32位的多路值在16位MSB部分含有管脚号,在16位LSB部分含有功能号。所以32位多路值对每一个管脚的每一个功能是唯一的。函数首先从管脚号(MSB 16位)指示PORT组,用外设号(LSB 16位)更新PMUX寄存器。 注意:pin_set_peripheral_function只是将分配I/O管脚到声明的功能。功能的单独管脚映射应被外设控制寄存器完成。 4.2.4 EDBG USART初始化 Edbg_usart_init函数将通过控制寄存器,波特率寄存器,设置接收中断使能位,初始化USART功能。 /* EDBG(SERCOM3) UART initialization */ void edbg_usart_init(void) { uint16_t baud_value; baud_value = calculate_baud_value(USART_BAUD_RATE,sys-tem_gclk_chan_get_hz(SERCOM3_GCLK_ID_CORE),USART_SAMPLE_NUM); /* By setting the DORD bit LSB is transmitted first and setting the RXPO bit as 1 corresponding SERCOM PAD[1] will be used for data reception, PAD[0] will be used as TxD pin by setting TXPO bit as 0, 16x over-sampling is selected by setting the SAMPR bit as 0, Generic clock is enabled in all sleep modes by setting RUNSTDBY bit as 1, USART clock mode is selected as USART with internal clock by setting MODE bit into 1. */ SERCOM3->USART.CTRLA.reg = SERCOM_USART_CTRLA_DORD | SERCOM_USART_CTRLA_RXPO(0x1) | SERCOM_USART_CTRLA_TXPO(0x0) | SERCOM_USART_CTRLA_SAMPR(0x0)| SERCOM_USART_CTRLA_RUNSTDBY | SERCOM_USART_CTRLA_MODE_USART_INT_CLK ; /*baud register value corresponds to the device communication baud rate */ SERCOM3->USART.BAUD.reg = baud_value; /* 8-bits size is selected as character size by setting the bit CHSIZE as 0, TXEN bit and RXEN bits are set to enable the Transmitter and receiver*/ SERCOM3->USART.CTRLB.reg = SERCOM_USART_CTRLB_CHSIZE(0x0) | SERCOM_USART_CTRLB_TXEN | SERCOM_USART_CTRLB_RXEN ; /* synchronization busy */ while(SERCOM3->USART.SYNCBUSY.bit.CTRLB); /* SERCOM3 handler enabled */ system_interrupt_enable(SERCOM3_IRQn); /* receive complete interrupt set */ SERCOM3->USART.INTENSET.reg = SERCOM_USART_INTFLAG_RXC; /* SERCOM3 peripheral enabled */ SERCOM3->USART.CTRLA.reg |= SERCOM_USART_CTRLA_ENABLE; /* synchronization busy */ while(SERCOM3->USART.SYNCBUSY.reg & SERCOM_USART_SYNCBUSY_ENABLE); } 在SAM D21 Xplained Pro和PC终端之间通信是用公式计算出来的USART_BAUD_RATE-9600 bps。波特率寄存器的值是通过USART_BAUD_RATE,SERCOM3通用时钟,USART_SAMPLE_MUM来计算的 计算波特率的公式由下列等式给出 FBAUD = ( fREF/ S) (1 – BAUD/65,536) FBAUD = 波特率 fref – SERCOM 通用时钟频率 S – 每位的采样数 BAUD – 波特率寄存器的值
|