00001
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
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
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
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
00170 pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);
00171
00172 #elif BOARD == STK1000
00173
00174 pm_reset();
00175 #endif
00176
00177
00178 init_dbg_rs232(FOSC0);
00179
00180
00181 print_dbg("\x0C\r\nTWI Example\r\nMaster!\r\n");
00182
00183
00184 gpio_enable_module(TWI_GPIO_MAP, sizeof(TWI_GPIO_MAP) / sizeof(TWI_GPIO_MAP[0]));
00185
00186
00187 opt.pba_hz = FOSC0;
00188 opt.speed = TWI_SPEED;
00189 opt.chip = EEPROM_ADDRESS;
00190
00191
00192 status = twi_master_init(&AVR32_TWI, &opt);
00193
00194 if (status == TWI_SUCCESS)
00195 {
00196
00197 print_dbg("Probe test:\tPASS\r\n");
00198 }
00199 else
00200 {
00201
00202 print_dbg("Probe test:\tFAIL\r\n");
00203 }
00204
00205
00206 packet.chip = EEPROM_ADDRESS;
00207
00208 packet.addr = VIRTUALMEM_ADDR_START;
00209
00210 packet.addr_length = EEPROM_ADDR_LGT;
00211
00212 packet.buffer = (void*) test_pattern;
00213
00214 packet.length = PATTERN_TEST_LENGTH;
00215
00216
00217 status = twi_master_write(&AVR32_TWI, &packet);
00218
00219
00220 if (status == TWI_SUCCESS)
00221 {
00222
00223 print_dbg("Write test:\tPASS\r\n");
00224 }
00225 else
00226 {
00227
00228 print_dbg("Write test:\tFAIL\r\n");
00229 }
00230
00231
00232 packet_received.chip = EEPROM_ADDRESS ;
00233
00234 packet_received.addr_length = EEPROM_ADDR_LGT;
00235
00236 packet_received.length = PATTERN_TEST_LENGTH;
00237
00238 packet_received.addr = VIRTUALMEM_ADDR_START;
00239
00240 packet_received.buffer = data_received;
00241
00242
00243 status = twi_master_read(&AVR32_TWI, &packet_received);
00244
00245
00246 if (status == TWI_SUCCESS)
00247 {
00248
00249 print_dbg("Read Test:\tPASS\r\n");
00250 }
00251 else
00252 {
00253
00254 print_dbg("Read test:\tFAIL\r\n");
00255 }
00256
00257
00258 for (i = 0 ; i < PATTERN_TEST_LENGTH; i++)
00259 {
00260 if (data_received[i] != test_pattern[i])
00261 {
00262
00263 print_dbg("Check Read:\tFAIL\r\n");
00264
00265 while(1);
00266 }
00267 }
00268
00269
00270 print_dbg("Check Read:\tPASS\r\n");
00271
00272 while(1);
00273 }