twi_example.c File Reference


Detailed Description

An example of usage of the TWI driver library.

This file contains an example on how to use the TWI driver library. It sets up the TWI in master mode. Depending on the global definitions READ and WRITE, it will try to read and write data to a TWI slave. See documentation or comments in this file for further information

Author:
Atmel Corporation: http://www.atmel.com
Support email: avr32@atmel.com
$Name$
Revision
$RCSfile$
Date

Definition in file twi_example.c.

#include "twi.h"

Include dependency graph for twi_example.c:

Go to the source code of this file.

Defines

#define CPU_HZ   20000000
#define ERROR   -1
#define READ   0
#define SUCCESS   0
#define WRITE   0

Typedefs

typedef unsigned char avr32_piomap_t [][2]

Functions

int main (void)
int pio_enable_module (avr32_piomap_t piomap, unsigned int size)
 Set the pins under module control.


Define Documentation

#define CPU_HZ   20000000

Definition at line 68 of file twi_example.c.

Referenced by main().

#define ERROR   -1

Definition at line 81 of file twi_example.c.

Referenced by main(), and pio_enable_module().

#define READ   0

Definition at line 79 of file twi_example.c.

Referenced by main().

#define SUCCESS   0

Definition at line 80 of file twi_example.c.

Referenced by main(), and pio_enable_module().

#define WRITE   0

Definition at line 78 of file twi_example.c.

Referenced by main().


Typedef Documentation

typedef unsigned char avr32_piomap_t[][2]

Definition at line 83 of file twi_example.c.


Function Documentation

int main ( void   ) 

Definition at line 89 of file twi_example.c.

References twi_package_t::addr, twi_package_t::addr_length, twi_package_t::buffer, twi_package_t::chip, CPU_HZ, twi_options_t::cpu_hz, ERROR, twi_package_t::length, pio_enable_module(), READ, twi_options_t::speed, SUCCESS, twi_options_t::twi_addr, twi_init(), twi_read(), TWI_SUCCESS, twi_write(), and WRITE.

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 }

Here is the call graph for this function:

int pio_enable_module ( avr32_piomap_t  piomap,
unsigned int  size 
)

Set the pins under module control.

Parameters:
piomap a map describing how the setup of the PIO
size number of elements in the map
Returns:
Status or error code
Return values:
SUCCESS on success
ERROR on bad values in piomap

Definition at line 153 of file twi_example.c.

References ERROR, and SUCCESS.

Referenced by main().

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:44 2007 for AVR32107 Using TWI as a Master on the AVR32 by  doxygen 1.5.3