- Main Page
- Related Pages
- Data Structures
- Files

# twi_master_example.c

Go to the documentation of this file.

    00001 /*This file has been prepared for Doxygen automatic documentation generation.*/
    00109 #include <avr32/io.h>
    00110 #include "board.h"
    00111 #include "print_funcs.h"
    00112 #include "gpio.h"
    00113 #include "intc.h"
    00114 #include "twi.h"
    00115 #include "pm_at32ap7000.h"
    00116
    00117
    00118 //------------------  C O N F I G U R A T I O N S  -------------------
    00119
    00120 #define EEPROM_ADDRESS        0x50        // EEPROM's TWI address
    00121 #define EEPROM_ADDR_LGT       3           // Address length of the EEPROM memory
    00122 #define VIRTUALMEM_ADDR_START 0x123456    // Address of the virtual mem in the EEPROM
    00123 #define TWI_SPEED             50000       // Speed of TWI
    00124
    00125 //------------------  D E F I N I T I O N S  -------------------
    00126
    00128 #define  PATTERN_TEST_LENGTH        (sizeof(test_pattern)/sizeof(U8))
    00129 const U8 test_pattern[] =  {
    00130    0xAA,
    00131    0x55,
    00132    0xA5,
    00133    0x5A,
    00134    0x77,
    00135    0x99};
    00136
    00137
    00138 //--------------------------------------------------------------------------------------------------------------
    00139 //----------------------------------------- T W I   M A S T E R ------------------------------------------------
    00140 //--------------------------------------------------------------------------------------------------------------
    00141
    00144 int main(void)
    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 }