twi_slave_example.c File Reference


Detailed Description

TWI slave 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_slave_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_MEM_ADDR   1
#define TWI_MEM_DATA   2
#define TWI_MEM_IDLE   0
#define TWI_MEM_SIZE   20
 Defines & Variables to manage a virtual TWI memory.
#define TWI_SPEED   50000
#define VIRTUALMEM_ADDR_START   0x123456

Functions

int main (void)
 Main function.
static void twi_slave_rx (U8 u8_value)
 Manage the received data on TWI.
static void twi_slave_stop (void)
 Manage stop transfer reception on TWI.
static U8 twi_slave_tx (void)
 Transmit a data on TWI.

Variables

U8 s_memory [TWI_MEM_SIZE] = {0}
U8 s_status_cmd = TWI_MEM_IDLE
U32 s_u32_addr
U8 s_u8_addr_pos
const U8 test_pattern []


Define Documentation

#define EEPROM_ADDR_LGT   3

Definition at line 120 of file twi_slave_example.c.

Referenced by twi_slave_rx().

#define EEPROM_ADDRESS   0x50

Definition at line 119 of file twi_slave_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 127 of file twi_slave_example.c.

#define TWI_MEM_ADDR   1

Definition at line 144 of file twi_slave_example.c.

Referenced by twi_slave_rx().

#define TWI_MEM_DATA   2

Definition at line 145 of file twi_slave_example.c.

Referenced by twi_slave_rx(), and twi_slave_tx().

#define TWI_MEM_IDLE   0

Definition at line 143 of file twi_slave_example.c.

Referenced by twi_slave_rx(), and twi_slave_stop().

#define TWI_MEM_SIZE   20

Defines & Variables to manage a virtual TWI memory.

Definition at line 142 of file twi_slave_example.c.

Referenced by twi_slave_rx(), and twi_slave_tx().

#define TWI_SPEED   50000

Definition at line 122 of file twi_slave_example.c.

Referenced by main().

#define VIRTUALMEM_ADDR_START   0x123456

Definition at line 121 of file twi_slave_example.c.

Referenced by twi_slave_rx(), and twi_slave_tx().


Function Documentation

int main ( void   ) 

Main function.

Definition at line 218 of file twi_slave_example.c.

References twi_options_t::chip, EEPROM_ADDRESS, twi_options_t::pba_hz, twi_slave_fct_t::rx, twi_options_t::speed, twi_slave_fct_t::stop, twi_slave_fct, twi_slave_init(), twi_slave_rx(), twi_slave_stop(), twi_slave_tx(), TWI_SPEED, TWI_SUCCESS, and twi_slave_fct_t::tx.

00219 {
00220   static const gpio_map_t TWI_GPIO_MAP =
00221   {
00222     {AVR32_TWI_SDA_0_0_PIN, AVR32_TWI_SDA_0_0_FUNCTION},
00223     {AVR32_TWI_SCL_0_0_PIN, AVR32_TWI_SCL_0_0_FUNCTION}
00224   };
00225   twi_options_t opt;
00226   twi_slave_fct_t twi_slave_fct;
00227   int status;
00228 
00229   // Switch to oscillator 0
00230   pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);
00231 
00232   // Init debug serial line
00233   init_dbg_rs232(FOSC0);
00234 
00235   // Display a header to user
00236   print_dbg("\x0C\r\nTWI Example\r\nSlave!\r\n");
00237 
00238   // TWI gpio pins configuration
00239   gpio_enable_module(TWI_GPIO_MAP, sizeof(TWI_GPIO_MAP) / sizeof(TWI_GPIO_MAP[0]));
00240 
00241   // options settings
00242   opt.pba_hz = FOSC0;
00243   opt.speed = TWI_SPEED;
00244   opt.chip = EEPROM_ADDRESS;
00245 
00246   // initialize TWI driver with options
00247   twi_slave_fct.rx = &twi_slave_rx;
00248   twi_slave_fct.tx = &twi_slave_tx;
00249   twi_slave_fct.stop = &twi_slave_stop;
00250   status = twi_slave_init(&AVR32_TWI, &opt, &twi_slave_fct );
00251   // check init result
00252   if (status == TWI_SUCCESS)
00253   {
00254     // display test result to user
00255     print_dbg("Slave start:\tPASS\r\n");
00256   }
00257   else
00258   {
00259     // display test result to user
00260     print_dbg("slave start:\tFAIL\r\n");
00261   }
00262 
00263   while(1);
00264 }

static void twi_slave_rx ( U8  u8_value  )  [static]

Manage the received data on TWI.

Definition at line 154 of file twi_slave_example.c.

References EEPROM_ADDR_LGT, s_memory, s_status_cmd, s_u32_addr, s_u8_addr_pos, TWI_MEM_ADDR, TWI_MEM_DATA, TWI_MEM_IDLE, TWI_MEM_SIZE, and VIRTUALMEM_ADDR_START.

Referenced by main().

00155 {
00156 
00157    switch( s_status_cmd )
00158    {
00159    case TWI_MEM_IDLE:
00160       s_u8_addr_pos = EEPROM_ADDR_LGT; // Init before receiving the target address.
00161       s_u32_addr = 0;
00162       // No break to continue on next case
00163 
00164    case TWI_MEM_ADDR:
00165       s_u8_addr_pos--;
00166       // Receiving the Nth Byte that makes the address (MSB first).
00167       s_u32_addr += ((U32)u8_value << (s_u8_addr_pos*8));
00168       if( 0 == s_u8_addr_pos )
00169       {  // the address is completely received => switch to data mode.
00170          s_status_cmd = TWI_MEM_DATA;
00171       }else{
00172          s_status_cmd = TWI_MEM_ADDR;
00173       }
00174       break;
00175 
00176    case TWI_MEM_DATA:      // We are receiving data
00177       // Check that we're still in the range of the virtual mem
00178       if( TWI_MEM_SIZE > (s_u32_addr-VIRTUALMEM_ADDR_START) )
00179       {
00180          s_memory[s_u32_addr-VIRTUALMEM_ADDR_START] = u8_value;
00181       }
00182       s_u32_addr++;  // Update to next position
00183       break;
00184    }
00185 }

static void twi_slave_stop ( void   )  [static]

Manage stop transfer reception on TWI.

Definition at line 210 of file twi_slave_example.c.

References s_status_cmd, and TWI_MEM_IDLE.

Referenced by main().

00211 {
00212    s_status_cmd = TWI_MEM_IDLE;
00213 }

static U8 twi_slave_tx ( void   )  [static]

Transmit a data on TWI.

Definition at line 189 of file twi_slave_example.c.

References s_memory, s_status_cmd, s_u32_addr, TWI_MEM_DATA, TWI_MEM_SIZE, and VIRTUALMEM_ADDR_START.

Referenced by main().

00190 {
00191    U8 u8_value;
00192 
00193    // This callback is called after a read request from the TWI master, for each
00194    // Byte to transmit.
00195    s_status_cmd = TWI_MEM_DATA;
00196    // Check that we're still in the range of the virtual mem
00197    if( TWI_MEM_SIZE > (s_u32_addr-VIRTUALMEM_ADDR_START) )
00198    {
00199       u8_value = s_memory[s_u32_addr-VIRTUALMEM_ADDR_START];
00200    }else{
00201       u8_value = 0xFF;
00202    }
00203    s_u32_addr++;  // Update to next position
00204    return u8_value;
00205 }


Variable Documentation

U8 s_memory[TWI_MEM_SIZE] = {0}

Definition at line 149 of file twi_slave_example.c.

Referenced by twi_slave_rx(), and twi_slave_tx().

U8 s_status_cmd = TWI_MEM_IDLE

Definition at line 146 of file twi_slave_example.c.

Referenced by twi_slave_rx(), twi_slave_stop(), and twi_slave_tx().

Definition at line 148 of file twi_slave_example.c.

Referenced by twi_slave_rx(), and twi_slave_tx().

Definition at line 147 of file twi_slave_example.c.

Referenced by twi_slave_rx().

const U8 test_pattern[]

Initial value:

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

Definition at line 128 of file twi_slave_example.c.


Generated on Fri Feb 19 02:27:10 2010 for AVR32 UC3 - TWI Driver - Slave Mode by  doxygen 1.5.5