usart_example.c

Go to the documentation of this file.
00001 /*This file has been prepared for Doxygen automatic documentation generation.*/
00023 /* ************************************************************************
00024 
00025 Copyright (c) 2006, Atmel Corporation All rights reserved. 
00026 
00027 Redistribution and use in source and binary forms, with or without
00028 modification, are permitted provided that the following conditions are met:
00029 
00030 1. Redistributions of source code must retain the above copyright notice,
00031 this list of conditions and the 
00032 following disclaimer. 
00033 
00034 2. Redistributions in binary form must reproduce the above copyright notice,
00035 this list of conditions and the following disclaimer in the documentation
00036 and/or other materials provided with the distribution.
00037 
00038 3. The name of ATMEL may not be used to endorse or promote products 
00039 derived from this software without specific prior written permission.  
00040 
00041 THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS 
00042 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
00043 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
00044 PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY 
00045 DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
00046 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
00047 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
00048 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00049 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
00050 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
00051 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 
00052 WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
00053 
00054 POSSIBILITY OF SUCH DAMAGE. 
00055 
00056 ************************************************************************ */
00057 
00058 
00059 #include "usart.h"
00060 
00061 #define SUCCESS 0;
00062 #define FAILURE -1;
00063 
00064 typedef unsigned char avr32_piomap_t[][2];
00065 
00066 /* used for printing strings */
00067 void print(volatile struct avr32_usart_t * usart, char *str);
00068 
00069 /* enable output on pins */
00070 int pio_enable_module(avr32_piomap_t piomap, unsigned int size);
00071 
00072 /*
00073 
00074 This example will display a string at the screen. Refer to the struct opt in main for 
00075 configuration options for your terminal.
00076 
00077 Please make sure that the correct jumper(s) is/are set on your development board. Consult your hardware reference 
00078 guide if in doubt.
00079 
00080 */
00081 
00082 /* This is a work around for the IAR EWAVR32, version 2.10B */
00083 #ifdef __ICCAVR32__
00084 #define AVR32_USART1_RXD_0_PIN      17
00085 #define AVR32_USART1_RXD_0_FUNCTION 0
00086 #define AVR32_USART1_TXD_0_PIN      18
00087 #define AVR32_USART1_TXD_0_FUNCTION 0
00088 #endif
00089 
00090 
00096 int main( void )
00097 {
00098   int cpu_hz = 20000000;
00099   struct usart_options_t opt;
00100 
00101   volatile struct avr32_usart_t *usart = &AVR32_USART1;
00102 
00103   avr32_piomap_t usart_piomap = {                                  \
00104     {AVR32_USART1_RXD_0_PIN, AVR32_USART1_RXD_0_FUNCTION}, \
00105     {AVR32_USART1_TXD_0_PIN, AVR32_USART1_TXD_0_FUNCTION}   \
00106   };
00107 
00108   // Set options for the USART
00109   opt.baudrate = 115200;
00110   opt.charlength = 8;
00111   opt.paritytype = USART_NO_PARITY;
00112   opt.stopbits = USART_1_STOPBIT;
00113   opt.channelmode = USART_NORMAL_CHMODE;
00114 
00115   // Initialize it in RS232 mode
00116   usart_init_rs232(usart, &opt, cpu_hz);
00117 
00118   // Setup pio for USART
00119   pio_enable_module(usart_piomap, 2);
00120 
00121   print(usart, "This is AVR32 saying hello from the STK1000!\n");
00122 
00123   return SUCCESS;
00124 }
00125 
00126 
00134 void print(volatile struct avr32_usart_t * usart, char *str)
00135 {
00136         while (*str != '\0')
00137                 usart_putchar(usart, *str++);
00138 }
00139 
00140 
00150 int pio_enable_module(avr32_piomap_t piomap, unsigned int size)
00151 {
00152   int i;
00153   volatile struct avr32_pio_t *pio;
00154 
00155   /* get the base address for the port */
00156   switch (**piomap/32) {
00157 
00158   case 0:
00159     pio = &AVR32_PIOA;
00160     break;
00161   case 1:
00162     pio = &AVR32_PIOB;
00163     break;
00164   case 2:
00165     pio = &AVR32_PIOC;
00166     break;
00167   case 3:
00168     pio = &AVR32_PIOD;
00169     break;
00170   case 4:
00171     pio = &AVR32_PIOE;
00172     break;
00173   default :
00174     return FAILURE;
00175 
00176   }
00177 
00178   for(i=0; i<size; i++){
00179 
00180     pio->pdr |= ( 1<<( (**piomap) % 32) );
00181     pio->pudr |= ( 1<<( (**piomap) % 32) );
00182 
00183     switch( *(*piomap+1) ){    
00184     case 0:
00185       pio->asr |= ( 1<<( (**piomap) % 32) );
00186       break;
00187     case 1:
00188       pio->bsr |= ( 1<<( (**piomap) % 32) );
00189       break;
00190     default:
00191       return FAILURE;
00192     }
00193 
00194     ++piomap;
00195 
00196   }
00197   
00198   return SUCCESS;
00199 }

Generated on Thu May 10 14:17:56 2007 for AVR321000 Communication with the AVR32 USART by  doxygen 1.5.1