twi_master_example.c File Reference


Detailed Description

TWI master example driver for AVR32 UC3.

This file provides an example for the TWI on AVR32 UC3 devices.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file twi_master_example.c.

#include <avr32/io.h>
#include "board.h"
#include "print_funcs.h"
#include "gpio.h"
#include "pm.h"
#include "intc.h"
#include "twi.h"

Go to the source code of this file.

Defines

#define EEPROM_ADDR_LGT   3
#define EEPROM_ADDRESS   0x50
#define PATTERN_TEST_LENGTH   (sizeof(test_pattern)/sizeof(U8))
 Constants to define the sent and received pattern.
#define TWI_SPEED   50000
#define VIRTUALMEM_ADDR_START   0x123456

Functions

int main (void)
 Main function.

Variables

const U8 test_pattern []


Define Documentation

#define EEPROM_ADDR_LGT   3

Definition at line 124 of file twi_master_example.c.

Referenced by main().

#define EEPROM_ADDRESS   0x50

Definition at line 123 of file twi_master_example.c.

Referenced by main().

#define PATTERN_TEST_LENGTH   (sizeof(test_pattern)/sizeof(U8))

Constants to define the sent and received pattern.

Definition at line 131 of file twi_master_example.c.

Referenced by main().

#define TWI_SPEED   50000

Definition at line 126 of file twi_master_example.c.

Referenced by main().

#define VIRTUALMEM_ADDR_START   0x123456

Definition at line 125 of file twi_master_example.c.

Referenced by main().


Function Documentation

int main ( void   ) 

Main function.

Definition at line 147 of file twi_master_example.c.

References twi_package_t::addr, twi_package_t::addr_length, twi_package_t::buffer, twi_package_t::chip, twi_options_t::chip, EEPROM_ADDR_LGT, EEPROM_ADDRESS, twi_package_t::length, PATTERN_TEST_LENGTH, twi_options_t::pba_hz, twi_options_t::speed, test_pattern, twi_master_init(), twi_master_read(), twi_master_write(), TWI_SPEED, TWI_SUCCESS, and VIRTUALMEM_ADDR_START.

00148 {
00149   static const gpio_map_t TWI_GPIO_MAP =
00150   {
00151 #if BOARD == EVK1100
00152     {AVR32_TWI_SDA_0_0_PIN, AVR32_TWI_SDA_0_0_FUNCTION},
00153     {AVR32_TWI_SCL_0_0_PIN, AVR32_TWI_SCL_0_0_FUNCTION}
00154 #elif BOARD == EVK1101
00155     {AVR32_TWI_SDA_0_0_PIN, AVR32_TWI_SDA_0_0_FUNCTION},
00156     {AVR32_TWI_SCL_0_0_PIN, AVR32_TWI_SCL_0_0_FUNCTION}
00157 #elif BOARD == STK1000
00158     {AVR32_TWI_SDA_0_PIN, AVR32_TWI_SDA_0_FUNCTION},
00159     {AVR32_TWI_SCL_0_PIN, AVR32_TWI_SCL_0_FUNCTION}
00160 #else
00161 #  error The TWI configuration to use in this example is missing.
00162 #endif
00163   };
00164   twi_options_t opt;
00165   twi_package_t packet, packet_received;
00166   int status, i;
00167 
00168   char data_received[PATTERN_TEST_LENGTH] = {0};
00169 
00170 #if BOARD == EVK1100 || BOARD == EVK1101
00171 
00172   // Switch to oscillator 0
00173   pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);
00174 
00175 #endif
00176 
00177   // Init debug serial line
00178   init_dbg_rs232(FOSC0);
00179 
00180   // Display a header to user
00181   print_dbg("\x0C\r\nTWI Example\r\nMaster!\r\n");
00182 
00183   // TWI gpio pins configuration
00184   gpio_enable_module(TWI_GPIO_MAP, sizeof(TWI_GPIO_MAP) / sizeof(TWI_GPIO_MAP[0]));
00185 
00186   // options settings
00187   opt.pba_hz = FOSC0;
00188   opt.speed = TWI_SPEED;
00189   opt.chip = EEPROM_ADDRESS;
00190 
00191   // initialize TWI driver with options
00192   status = twi_master_init(&AVR32_TWI, &opt);
00193   // check init result
00194   if (status == TWI_SUCCESS)
00195   {
00196     // display test result to user
00197     print_dbg("Probe test:\tPASS\r\n");
00198   }
00199   else
00200   {
00201     // display test result to user
00202     print_dbg("Probe test:\tFAIL\r\n");
00203   }
00204 
00205   // TWI chip address to communicate with
00206   packet.chip = EEPROM_ADDRESS;
00207   // TWI address/commands to issue to the other chip (node)
00208   packet.addr = VIRTUALMEM_ADDR_START;
00209   // Length of the TWI data address segment (1-3 bytes)
00210   packet.addr_length = EEPROM_ADDR_LGT;
00211   // Where to find the data to be written
00212   packet.buffer = (void*) test_pattern;
00213   // How many bytes do we want to write
00214   packet.length = PATTERN_TEST_LENGTH;
00215 
00216   // perform a write access
00217   status = twi_master_write(&AVR32_TWI, &packet);
00218 
00219   // check write result
00220   if (status == TWI_SUCCESS)
00221   {
00222     // display test result to user
00223     print_dbg("Write test:\tPASS\r\n");
00224   }
00225   else
00226   {
00227     // display test result to user
00228     print_dbg("Write test:\tFAIL\r\n");
00229   }
00230 
00231   // TWI chip address to communicate with
00232   packet_received.chip = EEPROM_ADDRESS ;
00233   // Length of the TWI data address segment (1-3 bytes)
00234   packet_received.addr_length = EEPROM_ADDR_LGT;
00235   // How many bytes do we want to write
00236   packet_received.length = PATTERN_TEST_LENGTH;
00237   // TWI address/commands to issue to the other chip (node)
00238   packet_received.addr = VIRTUALMEM_ADDR_START;
00239   // Where to find the data to be written
00240   packet_received.buffer = data_received;
00241 
00242   // perform a read access
00243   status = twi_master_read(&AVR32_TWI, &packet_received);
00244 
00245   // check read result
00246   if (status == TWI_SUCCESS)
00247   {
00248     // display test result to user
00249     print_dbg("Read Test:\tPASS\r\n");
00250   }
00251   else
00252   {
00253     // display test result to user
00254     print_dbg("Read test:\tFAIL\r\n");
00255   }
00256 
00257   // check received data against sent data
00258   for (i = 0 ; i < PATTERN_TEST_LENGTH; i++)
00259   {
00260     if (data_received[i] != test_pattern[i])
00261     {
00262       // a char isn't consistent
00263       print_dbg("Check Read:\tFAIL\r\n");
00264       // Error
00265       while(1);
00266     }
00267   }
00268 
00269   // everything was OK
00270   print_dbg("Check Read:\tPASS\r\n");
00271 
00272   while(1);
00273 }
00274 }


Variable Documentation

const U8 test_pattern[]

Initial value:

  {
   0xAA,
   0x55,
   0xA5,
   0x5A,
   0x77,
   0x99}

Definition at line 132 of file twi_master_example.c.

Referenced by main().


Generated on Fri Feb 19 02:27:08 2010 for AVR32 - TWI Driver - Single-Master Mode by  doxygen 1.5.5