twi_example.c

Go to the documentation of this file.
00001 /*This file has been prepared for Doxygen automatic documentation generation.*/
00025 /* ************************************************************************
00026 
00027 Copyright (c) 2006, Atmel Corporation All rights reserved.
00028 
00029 Redistribution and use in source and binary forms, with or without
00030 modification, are permitted provided that the following conditions are met:
00031 
00032 1. Redistributions of source code must retain the above copyright notice,
00033 this list of conditions and the
00034 following disclaimer.
00035 
00036 2. Redistributions in binary form must reproduce the above copyright notice,
00037 this list of conditions and the following disclaimer in the documentation
00038 and/or other materials provided with the distribution.
00039 
00040 3. The name of ATMEL may not be used to endorse or promote products
00041 derived from this software without specific prior written permission.
00042 
00043 THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS
00044 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00045 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00046 PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY
00047 DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
00048 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00049 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00050 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00051 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00052 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00053 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
00054 WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00055 
00056 POSSIBILITY OF SUCH DAMAGE.
00057 
00058 ************************************************************************ */
00059 
00060 #include "twi.h"
00061 
00062 /*
00063    NOTE: This example communicates with an external twi device with address 0x42.
00064    Keep in mind that external pull-ups on the twi data and clock lines are needed.
00065    Values around 5k should do nicely.
00066 */
00067 
00068 #define CPU_HZ  20000000
00069 
00070 /* This is a work around for the IAR EWAVR32, version 2.10B */
00071 #ifdef __ICCAVR32__
00072         #define AVR32_TWI_SCL_0_PIN      7
00073         #define AVR32_TWI_SCL_0_FUNCTION 0
00074         #define AVR32_TWI_SDA_0_PIN      6
00075         #define AVR32_TWI_SDA_0_FUNCTION 0
00076 #endif
00077 
00078 #define WRITE 0 /* set to 1 for writing */
00079 #define READ  0 /* set to 1 for reading */
00080 #define SUCCESS  0
00081 #define ERROR -1
00082 
00083 typedef unsigned char avr32_piomap_t[][2];
00084 
00085 /* prototype */
00086 int pio_enable_module(avr32_piomap_t piomap, unsigned int size);
00087 
00088 
00089 int main ( void )
00090 {
00091   struct twi_options_t opt;
00092   struct twi_package_t package;
00093   
00094   int bytes;
00095   char buf_out[32] = "testing_testing\0";
00096   char buf_in[32];
00097   
00098   
00099   avr32_piomap_t twi_piomap = {                                 \
00100     {AVR32_TWI_SCL_0_PIN, AVR32_TWI_SCL_0_FUNCTION},            \
00101     {AVR32_TWI_SDA_0_PIN, AVR32_TWI_SDA_0_FUNCTION}             \
00102   };
00103   
00104   pio_enable_module(twi_piomap, 2);
00105   
00106   // TWI - 200kpbs, own address 0x42 (NOT USED)
00107   opt.cpu_hz = CPU_HZ;
00108   opt.speed = 200000;
00109   opt.twi_addr = 0x42;
00110   
00111   if( twi_init(&opt) != TWI_SUCCESS )
00112     return ERROR;
00113   
00114   /* master write */
00115   if (WRITE){
00116     package.chip = 0x34;                // Chip address for receiver
00117     package.addr = package.chip; // Address/command within the remote device
00118     package.addr_length = 0;         // Address/command length in bytes
00119     package.length = 4;     // Number of bytes in the data package
00120     package.buffer = buf_out;       // The data package itself
00121     
00122     bytes = twi_write(&package);
00123     if(bytes != package.length)
00124       return ERROR;
00125   }
00126   
00127   /* master read */
00128   if (READ){
00129     package.chip = 0x34;    // Chip address for receiver
00130     package.addr = package.chip; // Address/command within the remote device
00131     package.addr_length = 0;        // Address/command length in bytes
00132     package.length = 13;    // Number of bytes in the data package
00133     package.buffer = buf_in;        // The data package itself
00134     
00135     bytes = twi_read(&package);
00136     if(bytes != package.length)
00137       return ERROR;
00138   }
00139   
00140   return SUCCESS;
00141 }
00142 
00143 
00153 int pio_enable_module(avr32_piomap_t piomap, unsigned int size)
00154 {
00155   int i;
00156   volatile struct avr32_pio_t *pio;
00157 
00158   /* get the base address for the port */
00159   switch (**piomap/32) {
00160 
00161   case 0:
00162     pio = &AVR32_PIOA;
00163     break;
00164   case 1:
00165     pio = &AVR32_PIOB;
00166     break;
00167   case 2:
00168     pio = &AVR32_PIOC;
00169     break;
00170   case 3:
00171     pio = &AVR32_PIOD;
00172     break;
00173   case 4:
00174     pio = &AVR32_PIOE;
00175     break;
00176   default :
00177     return ERROR;
00178 
00179   }
00180 
00181   for(i=0; i<size; i++){
00182 
00183     pio->pdr |= ( 1<<( (**piomap) % 32) );
00184     pio->pudr |= ( 1<<( (**piomap) % 32) );
00185 
00186     switch( *(*piomap+1) ){    
00187     case 0:
00188       pio->asr |= ( 1<<( (**piomap) % 32) );
00189       break;
00190     case 1:
00191       pio->bsr |= ( 1<<( (**piomap) % 32) );
00192       break;
00193     default:
00194       return ERROR;
00195     }
00196 
00197     ++piomap;
00198 
00199   }
00200   
00201   return SUCCESS;
00202 }

Generated on Wed Sep 26 10:45:42 2007 for AVR32107 Using TWI as a Master on the AVR32 by  doxygen 1.5.3