Reply by razvan_ionut_stoian November 24, 20102010-11-24
One more thing. You should know exactly the number of CS of your slave device.

--- In A..., basilnpearl@... wrote:
>
> Hi there,
>
> I have no experience with SPI so I am coming across problems. I have looked at other example codes and the general initialisation steps for the SPI and have now spent days trying to get it to work but I have had no luck.
>
> Basically I have the AT91SAM7S-EK development board and I have put on a FM25CL64 NVRAM chip which I want to communicate to the board via SPI. Below is my Initialisation code + test code.
>
> I have only one SPI device and will not need any more. It is connected to NPSC0. I have not used the last transfer bit as i do not wish to deassert the SPI (I don't think i need to?)
>
> void SpiInit(void)
> {
> DBGUprint("Initializing SPI....\n\r");
>
> PIO_Configure(spitransfer, PIO_LISTSIZE(spitransfer));
> // Enable the clock of the SPI
> PMC_EnablePeripheral(AT91C_ID_SPI);
> // DBGUprint("1. Peripheral Enabled\n\r");
>
> AT91C_BASE_SPI->SPI_CR = AT91C_SPI_SPIDIS |AT91C_SPI_SWRST; //Reset
> SPI_Enable(AT91C_BASE_SPI);
>
> SPI_Configure(AT91C_BASE_SPI,
> AT91C_ID_SPI,
> AT91C_SPI_MSTR |
> AT91C_SPI_PS_FIXED |
> AT91C_SPI_MODFDIS |
> SPI_PCS(0)
> //AT91C_SPI_PCS_NPCS0 //| Not sure about this one - I've modified it to be PCS = xxx0 - See page 280 of Datasheet
> // AT91C_SPI_PCSDEC_DISABLE
> );
>
> // DBGUprint("2. SPI Configured\n\r");
>
> //Configure SPI Chip Select Register
> SPI_ConfigureNPCS(AT91C_BASE_SPI,
> 0, // NPCS0
> AT91C_SPI_NCPHA | // Mode 0 (CPOL=0,CPHA=0)
> (64 << 8) | // SCBR: SPCK=MCK/SCBR //8 or 64
> (0 << 16) | // DLYBS: Delay from NPCS to SPCK in MCK cycles
> (0 << 24) | // DLYBCT: Delay between consec xfers
> AT91C_SPI_BITS_8 | // 8-data bits
> AT91C_SPI_CSAAT //The current Chip Select is programmed to remained Active After Transfer
> );
>
>
> // DBGUprint("3. NPCS Configuredn\r");
>
> // PIO_Configure(spitransfer, PIO_LISTSIZE(spitransfer));
> // DBGUprint("4. SPI pins configured\n\r");
>
> // DBGUprint("5. SPI Enabled\n\r");
> DBGUprint("SPI Initialization Complete\n\n\r");
> }
> and the test code:
> void testfunction()
> {
> char temp[100] = "123456789123456789123456789123456789";
> char testdata[15] = "HELLO WORLD";
> int size = 0;
>
> SPI_Enable(AT91C_BASE_SPI); //ENABLE SPI
>
> NVRAM_WriteProtectDisable(); //don't think this is needed
>
> DBGUprint("\n\rData is: ");
> DBGUprint(temp);
> DBGUprint("\n\r");
>
> size = strsize(testdata); //Just work out the string size
> NVRAM_WriteDataBlock(testdata, size, 0x0000);
>
> NVRAM_ReadDataBlock(temp, size, 0x0000);
>
> DBGUprint("\n\rData is: ");
> DBGUprint(temp);
> DBGUprint("\n\r");
> }
> DBGUprint is just a the debug UART port so I can see what is going on using HyperTerminal on my PC.
> So, this is what i would expect to be printed:
>
> Data is: 123456789123456789123456789123456789
>
> Data is: HELLO WORLD3456789123456789123456789
>
> and this is what i get:
>
> Data is: 123456789123456789123456789123456789
>
> Data is: 3456789123456789123456789
>
> Even changing the value in SPI_ConfigureNPCS to anything other than 0 (1,2,3,4) has no effect and still does that. I literally have no idea what to do now?
>
> I look forward to any replies, cheers!
>

Reply by razvan_ionut_stoian November 24, 20102010-11-24
I think you should insert the LASTXFER bit in the last transmitted sequence. This is what makes the CS line go high.
--- In A..., "Frog Twissell, Blue Sky Solutions" wrote:
>
> Hi Basil,
> I'm not famailiar with the FM25CL64 specifically, but many/most SPI devices do require the enable to be de-asserted at the end of each transaction. I'd suggest you check this in the datasheet.
>
> Good luck,
> Frog
>
> _____
>
> From: A... [mailto:A...] On Behalf Of basilnpearl@...
> Sent: Wednesday, 24 November 2010 11:50 p.m.
> To: A...
> Subject: [AT91SAM] AT91SAM7S-EK (AT91SAM7S256) SPI Problems
>
>
> Hi there,
>
> I have no experience with SPI so I am coming across problems. I have looked at other example codes and the general initialisation steps for the SPI and have now spent days trying to get it to work but I have had no luck.
>
> Basically I have the AT91SAM7S-EK development board and I have put on a FM25CL64 NVRAM chip which I want to communicate to the board via SPI. Below is my Initialisation code + test code.
>
> I have only one SPI device and will not need any more. It is connected to NPSC0. I have not used the last transfer bit as i do not wish to deassert the SPI (I don't think i need to?)
>
> void SpiInit(void)
> {
> DBGUprint("Initializing SPI....\n\r");
>
> PIO_Configure(spitransfer, PIO_LISTSIZE(spitransfer));
> // Enable the clock of the SPI
> PMC_EnablePeripheral(AT91C_ID_SPI);
> // DBGUprint("1. Peripheral Enabled\n\r");
>
> AT91C_BASE_SPI->SPI_CR = AT91C_SPI_SPIDIS |AT91C_SPI_SWRST; //Reset
> SPI_Enable(AT91C_BASE_SPI);
>
> SPI_Configure(AT91C_BASE_SPI,
> AT91C_ID_SPI,
> AT91C_SPI_MSTR |
> AT91C_SPI_PS_FIXED |
> AT91C_SPI_MODFDIS |
> SPI_PCS(0)
> //AT91C_SPI_PCS_NPCS0 //| Not sure about this one - I've modified it to be PCS = xxx0 - See page 280 of Datasheet
> // AT91C_SPI_PCSDEC_DISABLE
> );
>
> // DBGUprint("2. SPI Configured\n\r");
>
> //Configure SPI Chip Select Register
> SPI_ConfigureNPCS(AT91C_BASE_SPI,
> 0, // NPCS0
> AT91C_SPI_NCPHA | // Mode 0 (CPOL=0,CPHA=0)
> (64 << 8) | // SCBR: SPCK=MCK/SCBR //8 or 64
> (0 << 16) | // DLYBS: Delay from NPCS to SPCK in MCK cycles
> (0 << 24) | // DLYBCT: Delay between consec xfers
> AT91C_SPI_BITS_8 | // 8-data bits
> AT91C_SPI_CSAAT //The current Chip Select is programmed to remained Active After Transfer
> );
> // DBGUprint("3. NPCS Configuredn\r");
>
> // PIO_Configure(spitransfer, PIO_LISTSIZE(spitransfer));
> // DBGUprint("4. SPI pins configured\n\r");
>
> // DBGUprint("5. SPI Enabled\n\r");
> DBGUprint("SPI Initialization Complete\n\n\r");
> }
>
> and the test code:
>
> void testfunction()
> {
> char temp[100] = "123456789123456789123456789123456789";
> char testdata[15] = "HELLO WORLD";
> int size = 0;
>
> SPI_Enable(AT91C_BASE_SPI); //ENABLE SPI
>
> NVRAM_WriteProtectDisable(); //don't think this is needed
>
> DBGUprint("\n\rData is: ");
> DBGUprint(temp);
> DBGUprint("\n\r");
>
> size = strsize(testdata); //Just work out the string size
> NVRAM_WriteDataBlock(testdata, size, 0x0000);
>
> NVRAM_ReadDataBlock(temp, size, 0x0000);
>
> DBGUprint("\n\rData is: ");
> DBGUprint(temp);
> DBGUprint("\n\r");
> }
>
> DBGUprint is just a the debug UART port so I can see what is going on using HyperTerminal on my PC.
> So, this is what i would expect to be printed:
>
> Data is: 123456789123456789123456789123456789
>
> Data is: HELLO WORLD3456789123456789123456789
>
> and this is what i get:
>
> Data is: 123456789123456789123456789123456789
>
> Data is: 3456789123456789123456789
>
> Even changing the value in SPI_ConfigureNPCS to anything other than 0 (1,2,3,4) has no effect and still does that. I literally have no idea what to do now?
>
> I look forward to any replies, cheers!
>
> _____
> No virus found in this message.
> Checked by AVG - www.avg.com
> Version: 10.0.1136 / Virus Database: 426/3275 - Release Date: 11/23/10
>

Reply by "Frog Twissell, Blue Sky Solutions" November 24, 20102010-11-24
Hi Basil,
I'm not famailiar with the FM25CL64 specifically, but many/most SPI devices do require the enable to be de-asserted at the end of each transaction. I'd suggest you check this in the datasheet.

Good luck,
Frog

_____

From: A... [mailto:A...] On Behalf Of b...@hotmail.com
Sent: Wednesday, 24 November 2010 11:50 p.m.
To: A...
Subject: [AT91SAM] AT91SAM7S-EK (AT91SAM7S256) SPI Problems

Hi there,

I have no experience with SPI so I am coming across problems. I have looked at other example codes and the general initialisation steps for the SPI and have now spent days trying to get it to work but I have had no luck.

Basically I have the AT91SAM7S-EK development board and I have put on a FM25CL64 NVRAM chip which I want to communicate to the board via SPI. Below is my Initialisation code + test code.

I have only one SPI device and will not need any more. It is connected to NPSC0. I have not used the last transfer bit as i do not wish to deassert the SPI (I don't think i need to?)

void SpiInit(void)
{
DBGUprint("Initializing SPI....\n\r");

PIO_Configure(spitransfer, PIO_LISTSIZE(spitransfer));
// Enable the clock of the SPI
PMC_EnablePeripheral(AT91C_ID_SPI);
// DBGUprint("1. Peripheral Enabled\n\r");

AT91C_BASE_SPI->SPI_CR = AT91C_SPI_SPIDIS |AT91C_SPI_SWRST; //Reset
SPI_Enable(AT91C_BASE_SPI);

SPI_Configure(AT91C_BASE_SPI,
AT91C_ID_SPI,
AT91C_SPI_MSTR |
AT91C_SPI_PS_FIXED |
AT91C_SPI_MODFDIS |
SPI_PCS(0)
//AT91C_SPI_PCS_NPCS0 //| Not sure about this one - I've modified it to be PCS = xxx0 - See page 280 of Datasheet
// AT91C_SPI_PCSDEC_DISABLE
);

// DBGUprint("2. SPI Configured\n\r");

//Configure SPI Chip Select Register
SPI_ConfigureNPCS(AT91C_BASE_SPI,
0, // NPCS0
AT91C_SPI_NCPHA | // Mode 0 (CPOL=0,CPHA=0)
(64 << 8) | // SCBR: SPCK=MCK/SCBR //8 or 64
(0 << 16) | // DLYBS: Delay from NPCS to SPCK in MCK cycles
(0 << 24) | // DLYBCT: Delay between consec xfers
AT91C_SPI_BITS_8 | // 8-data bits
AT91C_SPI_CSAAT //The current Chip Select is programmed to remained Active After Transfer
);
// DBGUprint("3. NPCS Configuredn\r");

// PIO_Configure(spitransfer, PIO_LISTSIZE(spitransfer));
// DBGUprint("4. SPI pins configured\n\r");

// DBGUprint("5. SPI Enabled\n\r");
DBGUprint("SPI Initialization Complete\n\n\r");
}

and the test code:

void testfunction()
{
char temp[100] = "123456789123456789123456789123456789";
char testdata[15] = "HELLO WORLD";
int size = 0;

SPI_Enable(AT91C_BASE_SPI); //ENABLE SPI

NVRAM_WriteProtectDisable(); //don't think this is needed

DBGUprint("\n\rData is: ");
DBGUprint(temp);
DBGUprint("\n\r");

size = strsize(testdata); //Just work out the string size
NVRAM_WriteDataBlock(testdata, size, 0x0000);

NVRAM_ReadDataBlock(temp, size, 0x0000);

DBGUprint("\n\rData is: ");
DBGUprint(temp);
DBGUprint("\n\r");
}

DBGUprint is just a the debug UART port so I can see what is going on using HyperTerminal on my PC.
So, this is what i would expect to be printed:

Data is: 123456789123456789123456789123456789

Data is: HELLO WORLD3456789123456789123456789

and this is what i get:

Data is: 123456789123456789123456789123456789

Data is: 3456789123456789123456789

Even changing the value in SPI_ConfigureNPCS to anything other than 0 (1,2,3,4) has no effect and still does that. I literally have no idea what to do now?

I look forward to any replies, cheers!

_____
No virus found in this message.
Checked by AVG - www.avg.com
Version: 10.0.1136 / Virus Database: 426/3275 - Release Date: 11/23/10
Reply by basi...@hotmail.com November 24, 20102010-11-24
Hi there,

I have no experience with SPI so I am coming across problems. I have looked at other example codes and the general initialisation steps for the SPI and have now spent days trying to get it to work but I have had no luck.

Basically I have the AT91SAM7S-EK development board and I have put on a FM25CL64 NVRAM chip which I want to communicate to the board via SPI. Below is my Initialisation code + test code.

I have only one SPI device and will not need any more. It is connected to NPSC0. I have not used the last transfer bit as i do not wish to deassert the SPI (I don't think i need to?)

void SpiInit(void)
{
DBGUprint("Initializing SPI....\n\r");

PIO_Configure(spitransfer, PIO_LISTSIZE(spitransfer));
// Enable the clock of the SPI
PMC_EnablePeripheral(AT91C_ID_SPI);
// DBGUprint("1. Peripheral Enabled\n\r");

AT91C_BASE_SPI->SPI_CR = AT91C_SPI_SPIDIS |AT91C_SPI_SWRST; //Reset
SPI_Enable(AT91C_BASE_SPI);

SPI_Configure(AT91C_BASE_SPI,
AT91C_ID_SPI,
AT91C_SPI_MSTR |
AT91C_SPI_PS_FIXED |
AT91C_SPI_MODFDIS |
SPI_PCS(0)
//AT91C_SPI_PCS_NPCS0 //| Not sure about this one - I've modified it to be PCS = xxx0 - See page 280 of Datasheet
// AT91C_SPI_PCSDEC_DISABLE
);

// DBGUprint("2. SPI Configured\n\r");

//Configure SPI Chip Select Register
SPI_ConfigureNPCS(AT91C_BASE_SPI,
0, // NPCS0
AT91C_SPI_NCPHA | // Mode 0 (CPOL=0,CPHA=0)
(64 << 8) | // SCBR: SPCK=MCK/SCBR //8 or 64
(0 << 16) | // DLYBS: Delay from NPCS to SPCK in MCK cycles
(0 << 24) | // DLYBCT: Delay between consec xfers
AT91C_SPI_BITS_8 | // 8-data bits
AT91C_SPI_CSAAT //The current Chip Select is programmed to remained Active After Transfer
);

// DBGUprint("3. NPCS Configuredn\r");

// PIO_Configure(spitransfer, PIO_LISTSIZE(spitransfer));
// DBGUprint("4. SPI pins configured\n\r");

// DBGUprint("5. SPI Enabled\n\r");
DBGUprint("SPI Initialization Complete\n\n\r");
}
and the test code:
void testfunction()
{
char temp[100] = "123456789123456789123456789123456789";
char testdata[15] = "HELLO WORLD";
int size = 0;

SPI_Enable(AT91C_BASE_SPI); //ENABLE SPI

NVRAM_WriteProtectDisable(); //don't think this is needed

DBGUprint("\n\rData is: ");
DBGUprint(temp);
DBGUprint("\n\r");

size = strsize(testdata); //Just work out the string size
NVRAM_WriteDataBlock(testdata, size, 0x0000);

NVRAM_ReadDataBlock(temp, size, 0x0000);

DBGUprint("\n\rData is: ");
DBGUprint(temp);
DBGUprint("\n\r");
}
DBGUprint is just a the debug UART port so I can see what is going on using HyperTerminal on my PC.
So, this is what i would expect to be printed:

Data is: 123456789123456789123456789123456789

Data is: HELLO WORLD3456789123456789123456789

and this is what i get:

Data is: 123456789123456789123456789123456789

Data is: 3456789123456789123456789

Even changing the value in SPI_ConfigureNPCS to anything other than 0 (1,2,3,4) has no effect and still does that. I literally have no idea what to do now?

I look forward to any replies, cheers!