📄 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"
/* Driver description */
#include "drivers/com/com.h" //* Communication driver
#include "get_usart.h"
//* Include files for ATEB40x
#ifdef AT91R40008
#include "EB40008.h"
#include "parts/r40008/lib_r40008.h"
#define TARGET "ATEB40x"
#endif //AT91R40008
//* Include files for EB42
#ifdef AT91M42800
#include "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 "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 "eb63.h"
#include "parts/m63200/lib_m63200.h"
#define PIO_DESC PIOA_DESC
#define TARGET "EB63"
#endif //AT91M63200
#define BAUD_RATE 38400//115200
ComDesc COM;
UsartDesc USART0_DESCRIPTOR ;
PioCtrlDesc GPIO_DESC ;
u_char txBuffer[AT24C512_PAGE_LENGTH];
u_char rxBuffer[AT24C512_PAGE_LENGTH];
int mcki;
UsartDesc * init_UART( int *mcki );
//*--------------------------------------------------------------------------------------
//* 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 ;
char message[512];
u_short cd_baud;
COM.usart= init_UART ( &mcki );
cd_baud = at91_baud_com(mcki, BAUD_RATE);
at91_open_com(&COM,(COM_8_BIT|COM_PAR_NONE|COM_NBSTOP_1|COM_FLOW_CONTROL_NONE),cd_baud);
at91_print(&COM,"\n--- SOFTWARE I2C TEST ---\n\r");
// Open the communication with the two-wire EEPROM
at24_Init( &PIO_DESC, i2c_speed, SDA, SCL ) ;
sprintf(message,"I2C EEPROM : AT24C512\n\r\
SPEED Communication set @ %dKHz with %s running @ %dMHz\n\r",com_speed/1000, TARGET, MCKKHz/1000);
at91_print(&COM,message);
// 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();
sprintf(message,"WRITING %d octets from address %d of the EEPROM\n\r",AT24C512_PAGE_LENGTH,eeprom_address);
at91_print(&COM,message);
// 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 ) ;
sprintf(message,"READING %d octets from address %d of the EEPROM\n\r",AT24C512_PAGE_LENGTH,eeprom_address);
at91_print(&COM,message);
// Checking
for ( i=0 ; i < AT24C512_PAGE_LENGTH ; i++)
{
if ( rxBuffer[i] != txBuffer[i])
{
sprintf(message,"Error @ address %d / Data written : 0x%02x / Data read : 0x%02x \n\r",i, txBuffer[i], rxBuffer[i]);
at91_print(&COM,message);
error++;
}
}
if ( error == 0 )
{
sprintf(message,"%d octets written and verified\n\r",AT24C512_PAGE_LENGTH);
at91_print(&COM,message);
}
else
at91_print(&COM,"TEST EEPROM FAIL\n\r");
at24_TransfertEnd();
while(1);
}
UsartDesc * init_UART( int *mcki )
{
#ifdef ATEB40x
*mcki = 66000000; /* ATEB40x */
#endif
//* Common values
USART0_DESCRIPTOR.pio_ctrl = &GPIO_DESC; /* IO controller descriptor */
USART0_DESCRIPTOR.periph_id = 2; /* USART Peripheral Identifier */
//* define Usart
USART0_DESCRIPTOR.usart_base = ((StructUSART*) 0xFFFD0000); /* Peripheral base */
USART0_DESCRIPTOR.pin_rxd = 15; /* RXD pin number in the PIO */
USART0_DESCRIPTOR.pin_txd = 14; /* TXD pin number in the PIO */
USART0_DESCRIPTOR.pin_sck = 13; /* SCK pin number in the PIO */
GPIO_DESC.pio_base = ((StructPIO*) 0xFFFF0000); /* Base Address */
GPIO_DESC.periph_id = 8; /* Peripheral Identifier */
GPIO_DESC.pio_number = 31; /* Total Pin Number */
return ( &USART0_DESCRIPTOR);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -