📄 usart_example.c
字号:
/*This file has been prepared for Doxygen automatic documentation generation.*//*! \file ********************************************************************* * * \brief Example on usage of the AVR32 USART device library. * * This file contains an example on how to use the AVR32 USART driver. It * initializes the USART and puts out a string. See the documentation or * comments within the file for settings and more info * * - 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: 67 $ * $RCSfile$ * $Date: 2006-05-10 15:00:07 +0200 (Wed, 10 May 2006) $ **************************************************************************** *//* ************************************************************************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 the following 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 products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ************************************************************************ */#include "usart.h"#define SUCCESS 0;#define FAILURE -1;typedef unsigned char avr32_piomap_t[][2];/* used for printing strings */void print(volatile struct avr32_usart_t * usart, char *str);/* enable output on pins */int pio_enable_module(avr32_piomap_t piomap, unsigned int size);/*This example will display a string at the screen. Refer to the struct opt in main for configuration options for your terminal.Please make sure that the correct jumper(s) is/are set on your development board. Consult your hardware reference guide if in doubt.*//* This is a work around for the IAR EWAVR32, version 2.10B */#ifdef __ICCAVR32__#define AVR32_USART1_RXD_0_PIN 17#define AVR32_USART1_RXD_0_FUNCTION 0#define AVR32_USART1_TXD_0_PIN 18#define AVR32_USART1_TXD_0_FUNCTION 0#endif/*! \brief Sets up the USART and print's a string. * * \param; * \return; */int main( void ){ int cpu_hz = 20000000; struct usart_options_t opt; volatile struct avr32_usart_t *usart = &AVR32_USART1; avr32_piomap_t usart_piomap = { \ {AVR32_USART1_RXD_0_PIN, AVR32_USART1_RXD_0_FUNCTION}, \ {AVR32_USART1_TXD_0_PIN, AVR32_USART1_TXD_0_FUNCTION} \ }; // Set options for the USART opt.baudrate = 115200; opt.charlength = 8; opt.paritytype = USART_NO_PARITY; opt.stopbits = USART_1_STOPBIT; opt.channelmode = USART_NORMAL_CHMODE; // Initialize it in RS232 mode usart_init_rs232(usart, &opt, cpu_hz); // Setup pio for USART pio_enable_module(usart_piomap, 2); print(usart, "This is AVR32 saying hello from the STK1000!\n"); return SUCCESS;}/*! \brief Print a string of characters to an usart * * \param *usart The usart to write to * \param *str The string of characters * * \return; */void print(volatile struct avr32_usart_t * usart, char *str){ while (*str != '\0') usart_putchar(usart, *str++);}/*! \brief Set the pins under module control. * * \param piomap a map describing how the setup of the PIO * \param size number of elements in the map * * \return Status or error code * \retval SUCCESS on success * \retval FAILURE on bad values in piomap */int pio_enable_module(avr32_piomap_t piomap, unsigned int size){ int i; volatile struct avr32_pio_t *pio; /* get the base address for the port */ switch (**piomap/32) { case 0: pio = &AVR32_PIOA; break; case 1: pio = &AVR32_PIOB; break; case 2: pio = &AVR32_PIOC; break; case 3: pio = &AVR32_PIOD; break; case 4: pio = &AVR32_PIOE; break; default : return FAILURE; } for(i=0; i<size; i++){ pio->pdr |= ( 1<<( (**piomap) % 32) ); pio->pudr |= ( 1<<( (**piomap) % 32) ); switch( *(*piomap+1) ){ case 0: pio->asr |= ( 1<<( (**piomap) % 32) ); break; case 1: pio->bsr |= ( 1<<( (**piomap) % 32) ); break; default: return FAILURE; } ++piomap; } return SUCCESS;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -