# twi_master_example.c File Reference

## Detailed Description

TWI example driver for AVR32 AP700x.

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

- Compiler: IAR EWAVR32 and GNU GCC for AVR32
- Supported devices: All AVR32 devices with a TWI module can be used.
- AppNote:

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 "intc.h"`
`#include "twi.h"`
`#include "pm_at32ap7000.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 pattern send and received.
#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 121 of file twi_master_example.c.

Referenced by main().

#define EEPROM_ADDRESS 0x50

Definition at line 120 of file twi_master_example.c.

Referenced by main().

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

Constants to define the pattern send and received.

Definition at line 128 of file twi_master_example.c.

Referenced by main().

#define TWI_SPEED 50000

Definition at line 123 of file twi_master_example.c.

Referenced by main().

#define VIRTUALMEM_ADDR_START 0x123456

Definition at line 122 of file twi_master_example.c.

Referenced by main().

## Function Documentation

int main | ( | void |  | ) |

Main function.

Definition at line 144 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.

    00145 {
    00146   static const gpio_map_t TWI_GPIO_MAP =
    00147   {
    00148 #if BOARD == EVK1100
    00149     {AVR32_TWI_SDA_0_0_PIN, AVR32_TWI_SDA_0_0_FUNCTION},
    00150     {AVR32_TWI_SCL_0_0_PIN, AVR32_TWI_SCL_0_0_FUNCTION}
    00151 #elif BOARD == EVK1101
    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 == STK1000
    00155     {AVR32_TWI_SDA_0_PIN, AVR32_TWI_SDA_0_FUNCTION},
    00156     {AVR32_TWI_SCL_0_PIN, AVR32_TWI_SCL_0_FUNCTION}
    00157 #else
    00158 #  error The TWI configuration to use in this example is missing.
    00159 #endif
    00160   };
    00161   twi_options_t opt;
    00162   twi_package_t packet, packet_received;
    00163   int status, i;
    00164
    00165   char data_received[PATTERN_TEST_LENGTH] = {0};
    00166
    00167 #if BOARD == EVK1100 || BOARD == EVK1101
    00168
    00169   // Switch to oscillator 0
    00170   pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);
    00171
    00172 #elif BOARD == STK1000
    00173   // Reset PM. Makes sure we get the expected clocking after a soft reset (e.g.: JTAG reset)
    00174   pm_reset();
    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 }

## Variable Documentation

const U8 test_pattern[]

Initial value:

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

Definition at line 129 of file twi_master_example.c.

Referenced by main().