EmbeddedRelated.com
Forums

AT91SAM7-64 and TWI (i2C) with FM24C64

Started by mr_rossi_1 September 29, 2005
Hello.
I have some probloems with my AT91 and a fram i connected via the TWI.
I am using the sample code from the atmel-homepage. unfortunately i
have some trouble with the code. I am not able to read or write
information into the fram. con anybody give me an sample of a twi
implementation?
thx
MrRossi


This is something I tested a year ago almost -
but AFAICR, it works.

/* "twi.c" */

#include <board.h void twi_init(void)
{
// PIOA-pin setup:
AT91F_TWI_CfgPIO();
// clk enable:
AT91F_TWI_CfgPMC();
// TWI setup:
AT91F_TWI_Configure(AT91C_BASE_TWI);

// set TWI clk:
*AT91C_TWI_CWGR = (7<<16)|(255<<8)|(255); // CKDIV | CHDIV | CLDIV =
111 1111 1111
// set dev.adr to 7-bits, adr.space to 16-bits:
*AT91C_TWI_MMR = 0x550200;

// and roll ...
} void i2cMasterSend(unsigned char dev_adr, unsigned short mem_adr,
unsigned char data)
{
unsigned int status;

*AT91C_TWI_IADR = mem_adr;
//
*AT91C_TWI_MMR &= ( 0xFF00EFFF | (dev_adr<<16) );
//
*AT91C_TWI_CR = AT91C_TWI_START | AT91C_TWI_MSEN | AT91C_TWI_STOP;
//
*AT91C_TWI_THR = data;
//
status = *AT91C_TWI_SR;
while ( !(status & AT91C_TWI_TXCOMP) ) {
status = *AT91C_TWI_SR;
}
} unsigned char i2cMasterReceive(unsigned char dev_adr, unsigned short
mem_adr)
{
unsigned int status;

*AT91C_TWI_IADR = mem_adr;
//
*AT91C_TWI_MMR |= ( 0x00001000 | (dev_adr<<16) );
//
*AT91C_TWI_CR = AT91C_TWI_START | AT91C_TWI_MSEN | AT91C_TWI_STOP;
//
status = *AT91C_TWI_SR;
while ( !(status & AT91C_TWI_TXCOMP) ) {
status = *AT91C_TWI_SR;
}
//
return (unsigned char)*AT91C_TWI_RHR;
} /* "test_24c_eeprom.c" */

/* "i2ceeprom_test.c" */

#include <stdio.h>
#include "twi.h"

#define DEV_ADR 0 // I2C-adresse=0
#define EEPROM_DADR (DEV_ADR | 0x50) // default-adr.bits
[6:3]="1010"

main()
{
unsigned char data, i=1;
unsigned short mem_addr;

// init TWI:
twi_init();

printf("\r\n***** I2C-EEPROM AT24C04 test program nr.2! *****\r\n");

while (1) {
printf("\r\nWriting data=%d to adress=0-255\n", i);
for (mem_addr=0; mem_addr<256; mem_addr++) {
i2cMasterSend(EEPROM_DADR, mem_addr, i);
putchar('.');
}
//
printf("\r\nReading data from EEPROM adr.0-255 ...\n");
for (mem_addr=0; mem_addr<256; mem_addr++) {
data = i2cMasterReceive(EEPROM_DADR, mem_addr);
printf("0%x ", data);
}

printf("\r\nPress ENTER for next iteration!\n");

while ( !getchar() ) ;

i++;
}
}

Hope this helps
Rgds
Morten

> -----Original Message-----
> From: AT91SAM7@AT91...
> [mailto:AT91SAM7@AT91...] On Behalf Of mr_rossi_1
> Sent: Thursday, September 29, 2005 1:35 PM
> To: AT91SAM7@AT91...
> Subject: [AT91SAM7] AT91SAM7-64 and TWI (i2C) with FM24C64
>
> Hello.
> I have some probloems with my AT91 and a fram i connected via
> the TWI.
> I am using the sample code from the atmel-homepage.
> unfortunately i have some trouble with the code. I am not
> able to read or write information into the fram. con anybody
> give me an sample of a twi implementation?
> thx
> MrRossi >
> ------------------------ Yahoo! Groups Sponsor
> --------------------~--> Fair play? Video games influencing
> politics. Click and talk back!
> http://us.click.yahoo.com/T8sf5C/tzNLAA/TtwFAA/dN_tlB/TM
> --------------------------
> ------~- > Yahoo! Groups Links



Message
We've done design with the A91SAM7S256, and I have a couple of sanity check questions..
 
1. If I am using USB and an 18.432MHz crystal, what PLL divisor ratio is used to get a 48MHz MCLK?  Divide by 48, multiply by 125?  Even though it violates the minimum PLL input (and for that matter, the minimum output)?
 
2. To flash the part with USB, I reset the part while the TST, PA0, PA1, and PA2 are all pulled to VCC, and everything will "just work"?
 
THanks.
-----Original Message-----
From: A...@yahoogroups.com [mailto:A...@yahoogroups.com] On Behalf Of mr_rossi_1
Sent: Thursday, September 29, 2005 7:35 AM
To: A...@yahoogroups.com
Subject: [AT91SAM7] AT91SAM7-64 and TWI (i2C) with FM24C64

Hello.
I have some probloems with my AT91 and a fram i connected via the TWI.
I am using the sample code from the atmel-homepage. unfortunately i
have some trouble with the code. I am not able to read or write
information into the fram. con anybody give me an sample of a twi
implementation?
thx
MrRossi



On Sunday 09 October 2005 01:18, James Dabbs wrote:
> We've done design with the A91SAM7S256, and I have a couple of sanity
> check questions..
>
> 1. If I am using USB and an 18.432MHz crystal, what PLL divisor ratio is
> used to get a 48MHz MCLK? Divide by 48, multiply by 125? Even though
> it violates the minimum PLL input (and for that matter, the minimum
> output)?

@ Set up PLL & clock source
@ The main crystal is 18.432 MHz
@ We want as close as we can get to 48MHz
@ We use the PLL to generate 18432000 / 14 * 73 = 96109714
@ We then divide this by 2

Look in the init.s of the gcc example I posted for a full wake up.

I think this is equivalent to the latest IAR examples. >
> 2. To flash the part with USB, I reset the part while the TST, PA0, PA1,
> and PA2 are all pulled to VCC, and everything will "just work"?

Hopefully someone who has used SAM-BA will answer, but I'm guessing you also
need to hook up with a PC and run some software there too :-).



--- In AT91SAM7@AT91..., Charles Manning <manningc2@a...>
wrote:
> >
> > 2. To flash the part with USB, I reset the part while the TST,
PA0, PA1,
> > and PA2 are all pulled to VCC, and everything will "just work"?
>
> Hopefully someone who has used SAM-BA will answer, but I'm guessing
you also
> need to hook up with a PC and run some software there too :-).
>

Download either SAM-BA or SAM-PROG from www.at91.com, take note that
SAM-PROG only works over USB, whereas SAM-BA work over USB and the
DBGU serial interface.

You need to follow the SAM-BA recovery procedure also posted on the
same website. Basically you power-up the device with TST pulled high
(the other pins have internal pull-ups so no need to explicitly pull
them high ), wait for about 10 seconds and then power-up the device
without TST pulled high. You can the connect to the device with either
SAM-BA or SAM-PROG.

We have successfully used the DBGU interface and SAM-BA to flash our
low volume production boards.


Thx for the Sample, but i found the problem.
I found out,that i have some trouble with the hardware, not with the
software!
Now the problem is fixed and the code works perfect!
Thanks for your help!
MrRossi --- In AT91SAM7@AT91..., "Larsen, Morten ActeNO"
<morten.larsen@a...> wrote:
>
> This is something I tested a year ago almost -
> but AFAICR, it works.
>
> /* "twi.c" */
>
> #include <board.h > void twi_init(void)
> {
> // PIOA-pin setup:
> AT91F_TWI_CfgPIO();
> // clk enable:
> AT91F_TWI_CfgPMC();
> // TWI setup:
> AT91F_TWI_Configure(AT91C_BASE_TWI);
>
> // set TWI clk:
> *AT91C_TWI_CWGR = (7<<16)|(255<<8)|(255); // CKDIV | CHDIV | CLDIV =
> 111 1111 1111
> // set dev.adr to 7-bits, adr.space to 16-bits:
> *AT91C_TWI_MMR = 0x550200;
>
> // and roll ...
> } > void i2cMasterSend(unsigned char dev_adr, unsigned short mem_adr,
> unsigned char data)
> {
> unsigned int status;
>
> *AT91C_TWI_IADR = mem_adr;
> //
> *AT91C_TWI_MMR &= ( 0xFF00EFFF | (dev_adr<<16) );
> //
> *AT91C_TWI_CR = AT91C_TWI_START | AT91C_TWI_MSEN | AT91C_TWI_STOP;
> //
> *AT91C_TWI_THR = data;
> //
> status = *AT91C_TWI_SR;
> while ( !(status & AT91C_TWI_TXCOMP) ) {
> status = *AT91C_TWI_SR;
> }
> } > unsigned char i2cMasterReceive(unsigned char dev_adr, unsigned short
> mem_adr)
> {
> unsigned int status;
>
> *AT91C_TWI_IADR = mem_adr;
> //
> *AT91C_TWI_MMR |= ( 0x00001000 | (dev_adr<<16) );
> //
> *AT91C_TWI_CR = AT91C_TWI_START | AT91C_TWI_MSEN | AT91C_TWI_STOP;
> //
> status = *AT91C_TWI_SR;
> while ( !(status & AT91C_TWI_TXCOMP) ) {
> status = *AT91C_TWI_SR;
> }
> //
> return (unsigned char)*AT91C_TWI_RHR;
> } > /* "test_24c_eeprom.c" */
>
> /* "i2ceeprom_test.c" */
>
> #include <stdio.h>
> #include "twi.h"
>
> #define DEV_ADR 0 // I2C-adresse=0
> #define EEPROM_DADR (DEV_ADR | 0x50) // default-adr.bits
> [6:3]="1010"
>
> main()
> {
> unsigned char data, i=1;
> unsigned short mem_addr;
>
> // init TWI:
> twi_init();
>
> printf("\r\n***** I2C-EEPROM AT24C04 test program nr.2! *****\r\n");
>
> while (1) {
> printf("\r\nWriting data=%d to adress=0-255\n", i);
> for (mem_addr=0; mem_addr<256; mem_addr++) {
> i2cMasterSend(EEPROM_DADR, mem_addr, i);
> putchar('.');
> }
> //
> printf("\r\nReading data from EEPROM adr.0-255 ...\n");
> for (mem_addr=0; mem_addr<256; mem_addr++) {
> data = i2cMasterReceive(EEPROM_DADR, mem_addr);
> printf("0%x ", data);
> }
>
> printf("\r\nPress ENTER for next iteration!\n");
>
> while ( !getchar() ) ;
>
> i++;
> }
> }
>
> Hope this helps
> Rgds
> Morten
>
> > -----Original Message-----
> > From: AT91SAM7@AT91...
> > [mailto:AT91SAM7@AT91...] On Behalf Of mr_rossi_1
> > Sent: Thursday, September 29, 2005 1:35 PM
> > To: AT91SAM7@AT91...
> > Subject: [AT91SAM7] AT91SAM7-64 and TWI (i2C) with FM24C64
> >
> > Hello.
> > I have some probloems with my AT91 and a fram i connected via
> > the TWI.
> > I am using the sample code from the atmel-homepage.
> > unfortunately i have some trouble with the code. I am not
> > able to read or write information into the fram. con anybody
> > give me an sample of a twi implementation?
> > thx
> > MrRossi
> >
> >
> >
> >
> >
> >
> > ------------------------ Yahoo! Groups Sponsor
> > --------------------~--> Fair play? Video games influencing
> > politics. Click and talk back!
> > http://us.click.yahoo.com/T8sf5C/tzNLAA/TtwFAA/dN_tlB/TM
> > --------------------------
> > ------~->
> >
> >
> > Yahoo! Groups Links
> >
> >
> >
> >
> >
> >
> >
> >
>