📄 lib_usart2.c
字号:
//*----------------------------------------------------------------------------
//* ATMEL Microcontroller Software Support - ROUSSET -
//*----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*----------------------------------------------------------------------------
//* File Name : lib_usart2.c
//* Object : USART2 functions Library.
//*
//* 1.0 25/09/01 : Creation
//*----------------------------------------------------------------------------
#include <stddef.h>
#include "periph\usart2\lib_usart2.h"
#include "periph\pdc2\lib_pdc2.h"
#include "periph\apmc\lib_apmc.h"
//*----------------------------------------------------------------------------
//* Function Name : at91_usart2_open
//* Object : Initialize an USART.
//* Input Parameters : <usart_pt> = the USART to initialize
//* : <mode> = the Mode Register to be programmed
//* : <speed> = the BRDR to be programmed
//* : <timeguard> = the US_TTGR to be programmed
//* Output Parameters : None
//* Functions called : at91_clock_open, at91_pio_close
//*----------------------------------------------------------------------------
void AT91F_US_Open ( const AT91PS_APMC pApmc,
const AT91PS_USART2Desc pUsart ,
u_int mode ,
u_int speed ,
u_int timeguard )
{
u_int *mask_periphA;
u_int *mask_periphB;
//* Enable the clock
at91_apmc_pcer(pApmc, pUsart->periph_id );
//* Define the baud rate divisor register
pUsart->usart_base->US_BRGR = speed ;
//* Write the Timeguard Register
pUsart->usart_base->US_TTGR = timeguard ;
//* reuse of speed and timeguard variables to save periphA and periphB mask
mask_periphA = &speed;
mask_periphB = &timeguard;
*mask_periphA = 0;
*mask_periphB = 0;
//* If External clock used
if (( mode & SCK_USED ) != 0 )
{
if (pUsart->pin_sck & PERIPH_B)
*mask_periphB |= (1 << pUsart->pin_sck);
else
*mask_periphA |= (1 << pUsart->pin_sck);
at91_pio_close (pUsart->pio_base, 1 << (pUsart->pin_sck & 0x00FF));
}
//* Define RXD and TXD as peripheral
if (pUsart->pin_txd & PERIPH_B)
*mask_periphB |= 1 << (pUsart->pin_txd & 0x00FF);
else
*mask_periphA |= 1 << (pUsart->pin_txd & 0x00FF);
if (pUsart->pin_rxd & PERIPH_B)
*mask_periphB |= 1 << (pUsart->pin_rxd & 0x00FF);
else
*mask_periphA |= 1 << (pUsart->pin_rxd & 0x00FF);
at91_pio_close (pUsart->pio_base,
(1 << (pUsart->pin_txd & 0x00FF)) |
(1 << (pUsart->pin_rxd & 0x00FF)) ) ;
//* If RS485 Protocol : Define RTS as peripheral
if (( mode & US_MODE ) == US_MODE_RS485 )
{
if (pUsart->pin_rts & PERIPH_B)
*mask_periphB |= 1 << (pUsart->pin_rts & 0x00FF);
else
*mask_periphA |= 1 << (pUsart->pin_rts & 0x00FF);
at91_pio_close ( pUsart->pio_base, 1 << (pUsart->pin_rts & 0x00FF) ) ;
}
//* If Handshaking Protocol
if (( mode & US_MODE ) == US_MODE_HANDSHAKE )
{
if (pUsart->pin_rts & PERIPH_B)
*mask_periphB |= (1 << pUsart->pin_rts);
else
*mask_periphA |= (1 << pUsart->pin_rts);
if (pUsart->pin_cts & PERIPH_B)
*mask_periphB |= (1 << pUsart->pin_cts);
else
*mask_periphA |= (1 << pUsart->pin_cts);
//* Define RTS and CTS as peripheral
at91_pio_close (pUsart->pio_base,
(1 << (pUsart->pin_rts & 0x00FF) ) |
(1 << (pUsart->pin_cts & 0x00FF) ) ) ;
}
//* If Modem Protocol
if (( mode & US_MODE ) == US_MODE_MODEM )
{
//* Define RTS, DTR, CTS, RI, DSR and DCD as peripheral
at91_pio_close (pUsart->pio_base,
(1 << (pUsart->pin_rts & 0x00FF )) |
(1 << (pUsart->pin_dtr & 0x00FF )) |
(1 << (pUsart->pin_cts & 0x00FF )) |
(1 << (pUsart->pin_ri & 0x00FF )) |
(1 << (pUsart->pin_dsr & 0x00FF )) |
(1 << (pUsart->pin_dcd & 0x00FF )) ) ;
if (pUsart->pin_rts & PERIPH_B) *mask_periphB |= 1 << (pUsart->pin_rts & 0x00FF);else *mask_periphA |= 1 << (pUsart->pin_rts & 0x00FF);
if (pUsart->pin_cts & PERIPH_B) *mask_periphB |= 1 << (pUsart->pin_cts & 0x00FF);else *mask_periphA |= 1 << (pUsart->pin_cts & 0x00FF);
if (pUsart->pin_dtr & PERIPH_B) *mask_periphB |= 1 << (pUsart->pin_dtr & 0x00FF);else *mask_periphA |= 1 << (pUsart->pin_dtr & 0x00FF);
if (pUsart->pin_dsr & PERIPH_B) *mask_periphB |= 1 << (pUsart->pin_dsr & 0x00FF);else *mask_periphA |= 1 << (pUsart->pin_dsr & 0x00FF);
if (pUsart->pin_dcd & PERIPH_B) *mask_periphB |= 1 << (pUsart->pin_dcd & 0x00FF);else *mask_periphA |= 1 << (pUsart->pin_dcd & 0x00FF);
if (pUsart->pin_ri & PERIPH_B) *mask_periphB |= 1 << (pUsart->pin_ri & 0x00FF);else *mask_periphA |= 1 << (pUsart->pin_ri & 0x00FF);
}
at91_pio_set_periphmode(pUsart->pio_base, *mask_periphA, *mask_periphB);
//* Reset receiver and transmitter
pUsart->usart_base->US_CR = US_RSTRX | US_RSTTX | US_RXDIS | US_TXDIS ;
//* Clear Transmit and Receive Counters
pUsart->usart_base->usartPdc2.PDC2_RNCR = 0 ;
pUsart->usart_base->usartPdc2.PDC2_TNCR = 0 ;
pUsart->usart_base->usartPdc2.PDC2_RCR = 0 ;
pUsart->usart_base->usartPdc2.PDC2_TCR = 0 ;
//* Define the USART mode
pUsart->usart_base->US_MR = mode ;
//* Enable receiver and transmitter
pUsart->usart_base->US_CR = US_RXEN | US_TXEN ;
}
//*----------------------------------------------------------------------------
//* Function Name : at91_usart2_close
//* Object : Disable an USART.
//* Input Parameters : <usart_pt> = USART Descriptor pointer
//* Output Parameters : none
//* Functions called : at91_clock_close, at91_pio_open
//*----------------------------------------------------------------------------
void at91_usart2_close (const AT91PS_APMC pApmc, const AT91PS_USART2Desc pUsart )
{
u_int mask_periphB = 0;
u_int mask_periphA = 0;
//* Disable the clock on the Peripheral
//at91_clock_close ( usart_pt->periph_id ) ;
at91_apmc_pcdr(pApmc, pUsart->periph_id);
//* Define all USARTs pins as pio
at91_pio_open (pUsart->pio_base,
(1 << (pUsart->pin_txd & 0x00FF)) |
(1 << (pUsart->pin_rxd & 0x00FF)) |
(1 << (pUsart->pin_rts & 0x00FF)) |
(1 << (pUsart->pin_dtr & 0x00FF)) |
(1 << (pUsart->pin_cts & 0x00FF)) |
(1 << (pUsart->pin_ri & 0x00FF)) |
(1 << (pUsart->pin_dsr & 0x00FF)) |
(1 << (pUsart->pin_dcd & 0x00FF)) |
(1 << (pUsart->pin_sck & 0x00FF)),
RESET_PIO_CONF ) ;
if (pUsart->pin_txd & PERIPH_B) mask_periphB |= 1 << (pUsart->pin_txd & 0x00FF);else mask_periphA |= 1 << (pUsart->pin_txd & 0x00FF);
if (pUsart->pin_rxd & PERIPH_B) mask_periphB |= 1 << (pUsart->pin_rxd & 0x00FF);else mask_periphA |= 1 << (pUsart->pin_rxd & 0x00FF);
if (pUsart->pin_rts & PERIPH_B) mask_periphB |= 1 << (pUsart->pin_rts & 0x00FF);else mask_periphA |= 1 << (pUsart->pin_rts & 0x00FF);
if (pUsart->pin_dtr & PERIPH_B) mask_periphB |= 1 << (pUsart->pin_dtr & 0x00FF);else mask_periphA |= 1 << (pUsart->pin_dtr & 0x00FF);
if (pUsart->pin_cts & PERIPH_B) mask_periphB |= 1 << (pUsart->pin_cts & 0x00FF);else mask_periphA |= 1 << (pUsart->pin_cts & 0x00FF);
if (pUsart->pin_ri & PERIPH_B) mask_periphB |= 1 << (pUsart->pin_ri & 0x00FF);else mask_periphA |= 1 << (pUsart->pin_ri & 0x00FF);
if (pUsart->pin_dsr & PERIPH_B) mask_periphB |= 1 << (pUsart->pin_dsr & 0x00FF);else mask_periphA |= 1 << (pUsart->pin_dsr & 0x00FF);
if (pUsart->pin_dcd & PERIPH_B) mask_periphB |= 1 << (pUsart->pin_dcd & 0x00FF);else mask_periphA |= 1 << (pUsart->pin_dcd & 0x00FF);
if (pUsart->pin_sck & PERIPH_B) mask_periphB |= 1 << (pUsart->pin_sck & 0x00FF);else mask_periphA |= 1 << (pUsart->pin_sck & 0x00FF);
//* Reset p閞iph select register
at91_pio_set_periphmode(pUsart->pio_base, ~mask_periphA, ~mask_periphB);
//* Reset the baud rate divisor register
pUsart->usart_base->US_BRGR = 0 ;
//* Reset the USART mode
pUsart->usart_base->US_MR = 0 ;
//* Reset the Timeguard Register
pUsart->usart_base->US_TTGR = 0;
//* Disable all interrupts
pUsart->usart_base->US_IDR = 0xFFFFFFFF ;
//* Abort the Peripheral Data Transfers
pUsart->usart_base->usartPdc2.PDC2_RNCR = 0 ;
pUsart->usart_base->usartPdc2.PDC2_TNCR = 0 ;
pUsart->usart_base->usartPdc2.PDC2_RCR = 0 ;
pUsart->usart_base->usartPdc2.PDC2_TCR = 0 ;
//* Disable the Peripheral Data Transfers
pUsart->usart_base->usartPdc2.PDC2_PTCR = US_RXTDIS ;
pUsart->usart_base->usartPdc2.PDC2_PTCR = US_TXTDIS ;
//* Disable receiver and transmitter and stop any activity immediately
pUsart->usart_base->US_CR = US_TXDIS | US_RXDIS | US_RSTTX | US_RSTRX ;
}
//*----------------------------------------------------------------------------
//* Function Name : at91_usart2_get_status
//* Object : Read the Status Register of an USART.
//* Input Parameters : <usart_pt> = USART Descriptor pointer
//* Output Parameters : USART Status Register
//* Functions called : none
//*----------------------------------------------------------------------------
u_int at91_usart2_get_status ( const AT91PS_USART2 pUSART )
{
//* Return the Control Status Register Value
return ( pUSART->US_CSR ) ;
}
//*-----------------------------------------------------------------------------
//* Function Name : at91_usart2_send_frame
//* Object : Transmit a complete frame.
//* Input Parameters : <usart_pt> = USART pointer
//* : <pt_buffer> = the address of the transmit buffer
//* : <size_buf> = the maximum number of bytes to be
//* : transmitted
//* : <pt_next_buffer> = the address of the next transmit buffer
//* : <size_next_buf> = the maximum number of bytes to be
//* : transmitted from the next buffer
//* : <timeout> = the inter-character time delay in number
//* : of byte
//* Output Parameters :
//* Functions called : none
//*-----------------------------------------------------------------------------
u_int at91_usart2_send_frame ( AT91PS_USART2 pUSART,
char *pt_buffer,
u_int size_buf,
char *pt_next_buffer,
u_int size_next_buf )
{
if ( pt_buffer != (void *) NULL )
{
if (pUSART->usartPdc2.PDC2_TCR == 0) {
pUSART->usartPdc2.PDC2_TPR = (u_int) pt_buffer;
pUSART->usartPdc2.PDC2_TCR = size_buf;
pUSART->usartPdc2.PDC2_TNPR = (u_int) pt_next_buffer ;
pUSART->usartPdc2.PDC2_TNCR = size_next_buf ;
at91_pdc2_TxEnable(&(pUSART->usartPdc2));
return 2;
}
else if (pUSART->usartPdc2.PDC2_TNCR == 0) {
pUSART->usartPdc2.PDC2_TNPR = (u_int) pt_buffer;
pUSART->usartPdc2.PDC2_TNCR = size_buf;
return 1;
}
}
return 0;
}
//*----------------------------------------------------------------------------
//* Function Name : at91_usart2_trig_cmd
//* Object : Reset the Status Bits of an USART.
//* Input Parameters : <usart_pt> = USART Descriptor pointer
//* : <cmd> = command mask
//* Output Parameters : none
//* Functions called : none
//*----------------------------------------------------------------------------
void at91_usart2_trig_cmd (AT91PS_USART2 pUSART, u_int cmd )
{
//* Write the command in the Control Register
pUSART->US_CR = cmd ;
}
//*-----------------------------------------------------------------------------
//* Function Name : at91_usart2_receive_frame
//* Object : Receive a complete frame.
//* Input Parameters : <usart_pt> = USART pointer
//* : <pt_buffer> = the address of the receive buffer
//* : <max_size> = the maximum number of bytes to be
//* : received
//* : <pt_next_buffer> = the address of the next receive buffer
//* : <max_size_next_buf> = the maximum number of bytes to be
//* : received in the next buffer
//* : <timeout> = the inter-character time delay in number
//* : of byte
//* Output Parameters :
//* Functions called : none
//*-----------------------------------------------------------------------------
u_int at91_usart2_receive_frame ( AT91PS_USART2 pUSART,
char *pt_buffer,
u_int max_size,
char *pt_next_buffer,
u_int max_size_next_buf)
{
if ( pt_buffer != (void *) NULL )
{
if (pUSART->usartPdc2.PDC2_RCR == 0) {
//* Store the address of the buffer
pUSART->usartPdc2.PDC2_RPR = (u_int) pt_buffer ;
//* Store the number of bytes to receive
pUSART->usartPdc2.PDC2_RCR = max_size ;
//* Store the address of the next buffer
pUSART->usartPdc2.PDC2_RNPR = (u_int) pt_next_buffer ;
//* Store the number of bytes to receive in the next buffer
pUSART->usartPdc2.PDC2_RNCR = max_size_next_buf ;
at91_pdc2_RxEnable(&(pUSART->usartPdc2));
return 2;
}
else if ( pUSART->usartPdc2.PDC2_RNCR == 0 ) {
//* Store the address of the next buffer
pUSART->usartPdc2.PDC2_RNPR = (u_int) pt_buffer ;
//* Store the number of bytes to receive in the next buffer
pUSART->usartPdc2.PDC2_RNCR = max_size ;
return 1;
}
return 0;
}
return ( TRUE ) ;
}
//*-----------------------------------------------------------------------------
//* Function Name : at91_usart2_get_CD
//* Object : Receive a complete frame.
//* Input Parameters : <main_clock> = CPU clock
//* : <baud_rate> = UART baudrate
//* Output Parameters :
//* Functions called : none
//*-----------------------------------------------------------------------------
u_int at91_usart2_get_CD ( const u_int main_clock,
const u_int baud_rate)
{
unsigned int baud_value = ((main_clock*10)/(baud_rate * 16));
if ((baud_value % 10) >= 5)
baud_value = (baud_value / 10) + 1;
else
baud_value /= 10;
return baud_value;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -