📄 test_i2c.c
字号:
//*--------------------------------------------------------------------------------------
//* ATMEL Microcontroller Software Support - ROUSSET -
//*--------------------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*--------------------------------------------------------------------------------------
//* File Name : test_i2c.c
//* Object : Software I2C
//*
//* 1.0 29/05/00 EL : Creation
//* 2.0 08/09/00 EL : Clean up
//* 2.1 14/01/02 PFi : Clean up and conditional complilation flags for all boards
//* 2.2 04/02/02 PFi : New version of I2C test
//*--------------------------------------------------------------------------------------
//* EB40A Jumper CB13/14 must be closed
//* EB42 Jumper CB13/14 must be closed
//* EB55 Jumper CB13/14 must be closed
//* EB63 Jumper E13 : 1-2-3 must be connected !
#include <stdio.h>
#include "drivers/lib_i2c/lib_i2c.h"
#include "at24c512.h"
#include "periph/stdc/std_c.h"
//* Include files for EB40A
#ifdef AT91R40008
#include "targets/eb40a/eb40a.h"
#include "parts/r40008/lib_r40008.h"
#define TARGET "EB40A"
#endif //AT91R40008
//* Include files for EB42
#ifdef AT91M42800
#include "targets/eb42/eb42.h"
#include "parts/m42800/lib_m42800.h"
#define PIO_DESC PIOB_DESC
#define TARGET "EB42"
#endif //AT91M42800
//* Include files for EB55
#ifdef AT91M55800
#include "targets/eb55/eb55.h"
#include "parts/m55800/lib_m55800.h"
#define PIO_DESC PIOA_DESC
#define TARGET "EB55"
#endif //AT91M55800
//* Include files for EB63
#ifdef AT91M63200
#include "targets/eb63/eb63.h"
#include "parts/m63200/lib_m63200.h"
#define PIO_DESC PIOA_DESC
#define TARGET "EB63"
#endif //AT91M63200
u_char txBuffer[AT24C512_PAGE_LENGTH];
u_char rxBuffer[AT24C512_PAGE_LENGTH];
//*--------------------------------------------------------------------------------------
//* Function Name : main
//* Object : Main function of test I2C
//* Input Parameters : none
//* Output Parameters : True
//*--------------------------------------------------------------------------------------
int main(void)
{
u_int i ;
u_short eeprom_address = 0x0 ;
u_int com_speed = 5000 ;
u_int i2c_speed = ((MCK/4)/com_speed) ; // Communication speed is set to 5KHz
u_int error = 0 ;
printf("--- %s SOFTWARE I2C TEST ---\n", TARGET);
// Open the communication with the two-wire EEPROM
at24_Init( &PIO_DESC, i2c_speed, SDA, SCL ) ;
printf("I2C EEPROM : AT24C512\n");
printf("SPEED Communication set @ %dKHz with %s running @ %dMHz\n", com_speed/1000, TARGET, MCKKHz/1000);
// Initialization of the buffer to transmit
for (i=0; i <=AT24C512_PAGE_LENGTH; i++ )
{
txBuffer[i]=i;
}
// We wait for any previous transfert to end
at24_TransfertEnd();
printf("WRITING %d octets from address %d of the EEPROM\n",AT24C512_PAGE_LENGTH, eeprom_address);
// We write the txBuffer in the EEPROM at address 0
at24_Write(eeprom_address, txBuffer, AT24C512_PAGE_LENGTH, MCK ) ;
// We wait for any previous transfert to end
at24_TransfertEnd();
// The buffer we wrote is reading back from EEPROM, and stored in rxBuffer
at24_Read( eeprom_address, rxBuffer, AT24C512_PAGE_LENGTH ) ;
printf("READING %d octets from address %d of the EEPROM\n",AT24C512_PAGE_LENGTH, eeprom_address);
// Checking
for ( i=0 ; i < AT24C512_PAGE_LENGTH ; i++)
{
if ( rxBuffer[i] != txBuffer[i])
{
printf("Error @ address %d / Data written : 0x%02x / Data read : 0x%02x \n",i, txBuffer[i], rxBuffer[i]);
error++;
}
}
if ( error == 0 )
{
printf("%d octets written and verified\n", AT24C512_PAGE_LENGTH);
printf("TEST EEPROM OK\n");
}
else
printf("TEST EEPROM FAIL\n");
at24_TransfertEnd();
while(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -