⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usart.c

📁 avr32100.zip Embedded demo
💻 C
📖 第 1 页 / 共 2 页
字号:
/*This file has been prepared for Doxygen automatic documentation generation.*//*! \file ********************************************************************* * * \brief USART driver library. * * This file contains basic drivers for the AVR32 USART, with support for all * all modes, settings and clock speeds. * * - Compiler:           IAR EWAAVR32 and GNU GCC for AVR32 * - Supported devices:  All AVR32 devices with a USART module can be used. * - AppNote:            AVR32100 - Communication with the AVR32 USART * * \author               Atmel Corporation: http://www.atmel.com \n *                       Support email: avr32@atmel.com * * $Name:  $ * $Revision: 527 $ * $RCSfile: usart.c,v $ * $Date: 2007-07-24 14:15:23 +0200 (Tue, 24 Jul 2007) $ **************************************************************************** *//* ************************************************************************Copyright (c) 2006, Atmel Corporation All rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions are met:1. Redistributions of source code must retain the above copyright notice,this list of conditions and thefollowing disclaimer.2. Redistributions in binary form must reproduce the above copyright notice,this list of conditions and the following disclaimer in the documentationand/or other materials provided with the distribution.3. The name of ATMEL may not be used to endorse or promote productsderived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESSOR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THEIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR APARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLYDISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIALDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OFSUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANYTHEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANYWAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THEPOSSIBILITY OF SUCH DAMAGE.\* ************************************************************************ */#include "usart.h"/* * Description: Check if the usart is in multidrop * Arguments:   *usart: Base address of the usart * Returns:     1 if the usart is in multidrop mode, otherwise 0 */static int usart_mode_is_multidrop(volatile struct avr32_usart_t * usart){  return ( (usart->mr & 0x00000600) >> AVR32_USART_MR_PAR_OFFSET );}void usart_reset( volatile struct avr32_usart_t * usart){	/* Disable all usart interrupts, interrupts needed should be set	   explicitly on every reset */	usart->idr = 0xFFFFffff;	/* Reset mode and other registers that could cause unpredictable	   behaviour after reset */	usart->mr = 0;	usart->rtor = 0;	usart->ttgr = 0;	/* Shutdown RX and TX (will be reenabled when setup	   is completed successfully), reset status bits and turn	   off DTR and RTS */	usart->cr = (1 << AVR32_USART_CR_RSTRX_OFFSET) |	            (1 << AVR32_USART_CR_RSTTX_OFFSET) |	            (1 << AVR32_USART_CR_RSTSTA_OFFSET) |	            (1 << AVR32_USART_CR_RSTIT_OFFSET) |	            (1 << AVR32_USART_CR_RSTNACK_OFFSET) |	            (1 << AVR32_USART_CR_DTRDIS_OFFSET) |	            (1 << AVR32_USART_CR_RTSDIS_OFFSET);}/*! * This function will calculate a clock divider(CD) and fractional * part(FP) that gets the usart as close to a wanted baudrate as * possible * \param *usart   Base address of the usart * \param baudrate Wanted baudrate * \param cpu_hz  Frequency of the selected clock * \return USART_SUCCESS or USART_INVALID_INPUT if wanted baudrate is *            impossible with given clockspeed */static int usart_set_baudrate( volatile struct avr32_usart_t * usart, unsigned int baudrate, long cpu_hz){	int cd; /* Clock divider */	/*	 *             ** BAUDRATE CALCULATION **	 *	 *                 Selected Clock                       Selected Clock	 *     baudrate = ----------------   or     baudrate = ----------------	 *                             16 x CD                                        8 x CD	 *	 *       (with 16x oversampling)              (with 8x oversampling)	 */	if ( baudrate < (cpu_hz/16)  ){		/* Use 8x oversampling */		usart->mr |= (1<<AVR32_USART_MR_OVER_OFFSET);		cd = cpu_hz / (8*baudrate);		if (cd < 2) {			return USART_INVALID_INPUT;		}		usart->brgr = (cd << AVR32_USART_BRGR_CD_OFFSET);	} else {		/* Use 16x oversampling */		usart->mr &= ~(1<<AVR32_USART_MR_OVER_OFFSET);		cd =  cpu_hz / (16*baudrate);		if (cd > 65535) {		  /* Baudrate is too low */		  return USART_INVALID_INPUT;		}	}	usart->brgr = (cd << AVR32_USART_BRGR_CD_OFFSET);	return USART_SUCCESS;} /*---------------------------------------------------------------------------+ |                                                                            | |                           INITIALIZATION FUNCTIONS                         | |                                                                            | +---------------------------------------------------------------------------*/int usart_init_rs232(volatile struct avr32_usart_t * usart, struct usart_options_t * opt, long cpu_hz){	int retval;	/* Reset the usart and shutdown RX and TX */	usart_reset(usart);	/* Control input values */	if (opt == 0) /* Null pointer */		return USART_INVALID_INPUT;	if (opt->charlength < 5 || opt->charlength > 9)		return USART_INVALID_INPUT;	if (opt->paritytype > 7)		return USART_INVALID_INPUT;	if (opt->stopbits > 2+255)		return USART_INVALID_INPUT;	if (opt->channelmode > 3)		return USART_INVALID_INPUT;	if ((retval = usart_set_baudrate(usart, opt->baudrate, cpu_hz)) != \	     USART_SUCCESS)		return retval;	if (opt->charlength == 9) {		/* Charlength set to 9 bits; MODE9 dominates CHRL */		usart->mr |= (1<<AVR32_USART_MR_MODE9_OFFSET);	} else {		/* CHRL gives the charlength( - 5) when USART_MODE9=0 */		usart->mr |=			((opt->charlength-5) << AVR32_USART_MR_CHRL_OFFSET);	}	usart->mr |= (opt->channelmode << AVR32_USART_MR_CHMODE_OFFSET) |	             (opt->paritytype << AVR32_USART_MR_PAR_OFFSET);	if (opt->stopbits > 2)	{		/* Set two stop bits */		usart->mr |= (2 << AVR32_USART_MR_NBSTOP_OFFSET);		/* And a timeguard period gives the rest */		usart->ttgr = (opt->stopbits-2);	}	else		/* Insert 1, 1.5 or 2 stop bits */		usart->mr |= (opt->stopbits << AVR32_USART_MR_NBSTOP_OFFSET);	/* Setup complete; enable communication */	/* Enable input and output */	usart->cr |= (1<<AVR32_USART_CR_TXEN_OFFSET) |	             (1<<AVR32_USART_CR_RXEN_OFFSET);	return USART_SUCCESS;}/* * Description: This function is meant to be run after rs232_init(). *              It sets up the usart to use handshaking in its communication. * Arguments:   *usart:    Base address of the usart *              *opt:      Options needed to set up RS232 communcation (see usart_options_t) *				cpu_hz:    The clock frequency of the usart module *              software_handshaking: *                         1= Use software handshaking *                         0= Use hardware handshaking (requires extra wiring) *              xon_char:  Character sent from receiver to transmitter when more *                         data can be sent. (Software handshaking only) *              xoff_char: Sent from recv. to trans. when recv. buffers are full (sw) * Returns:     USART_SUCCESS or USART_INVALID_INPUT */int usart_init_handshaking(volatile struct avr32_usart_t * usart, struct usart_options_t * opt,                           long cpu_hz, int software_handshaking,                           char xon_char, char xoff_char){	int retval;	/* First: Setup standard RS323 */	if ((retval = usart_init_rs232(usart, opt, cpu_hz)) != USART_SUCCESS)		return retval;	if (software_handshaking == 0)	{		/* Clear previous mode */		usart-> mr &= ~(0xf << AVR32_USART_MR_MODE_OFFSET);		/* Hardware handshaking */		usart-> mr |= (USART_MODE_HW_HSH << AVR32_USART_MR_MODE_OFFSET);	}	else if (software_handshaking == 1)	{		/* Clear previous mode */		usart-> mr &= ~(0xf << AVR32_USART_MR_MODE_OFFSET);		/* Software handshaking */		usart-> mr |= (USART_MODE_SW_HSH << AVR32_USART_MR_MODE_OFFSET);	}	else		return USART_INVALID_INPUT;	return USART_SUCCESS;}/* * Description: Setup the usart to use the IrDA protocol * Arguments:   *usart:      Base address of the usart *              *opt:        Options needed to set up RS232 communcation (see usart_options_t) *				cpu_hz:		 The module's clock frequency *              irda_filter: Counter used to seperate received ones from zeros * Returns:     USART_SUCCESS or USART_INVALID_INPUT */int usart_init_IrDA(volatile struct avr32_usart_t * usart, struct usart_options_t * opt,                    long cpu_hz, unsigned char irda_filter){	int retval;	/* First: Setup standard RS323 */	if ((retval = usart_init_rs232(usart, opt, cpu_hz)) != USART_SUCCESS)		return retval;	/* Set IrDA counter */	usart->ifr = irda_filter;	/* Activate "low-pass filtering" of input */	usart->mr |= (1 << AVR32_USART_MR_FILTER_OFFSET);	return USART_SUCCESS;}/* * Description: Setup the usart to use the Modem protocol, activating special inputs/outputs * Arguments:   *usart: Base address of the usart *              *opt:   Options needed to set up RS232 communcation (see usart_options_t)*				cpu_hz:		 The module's clock frequency

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -