Problem with SPI in XMEGA

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi.

I write this code for spi on Xmega128A1 but it work just one time! and I have to power down and then power on the micro and then send another time! to get the answer!

does anybody know what is the problem?

void test()
{
	SpiInit();
	
	SpiWriteRead(0x9F);
	
	char TmpShow[10]="\0";
	for (int i=1;i<7;i++)
	{
		sprintf(TmpShow,"%02x",SpiWriteRead(0xFF));
		SendDataToUSB(TmpShow);
	}
}

// initialize SPI interface on PORTE:4-7
// PORTE:4 - SS   (active low)
// PORTE:5 - MOSI
// PORTE:6 - MISO
// PORTE:7 - SCK
void SpiInit(void)
{
	PORTE.DIRSET = 0xB0;  // configure MOSI, SS, CLK as outputs on PORTE
	// enable SPI master mode, CLK/64 (@32MHz=>500KHz)
	SPIE.CTRL = SPI_ENABLE_bm | SPI_MASTER_bm | SPI_MODE_0_gc | SPI_PRESCALER_DIV128_gc;
}

void SpiWrite(char data)
{
	SPIE.DATA = data;     
	while(!(SPIE.STATUS & (1<<7)));
}

char SpiRead(void)
{
	SpiWrite(0xff);
	return SPIE.DATA;
}

char SpiWriteRead(char data)
{
	SpiWrite(data);
	return SPIE.DATA;
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
but it work just one time!
In which case I was sort of keen to see main() but unless my eyes are playing up I don't see it above?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here's my SPI code.


/* Defines needed to make this work
   Usually defined in Config.h

   #define SPIPort PORTC
   #define SPIMO   7
   #define SPIMI   6
   #define SPISC   5
   #define SPISS   4

   */

uint8_t SPIDir = 0 ;

void spiopen(void)
{
    SPIDir = SPIPort.DIR ;
    SPIPort.DIRCLR = (1<<SPIMI);
    SPIPort.DIRSET = (1<<SPIMO) | (1<<SPISC) | (1<<SPISS) ; // Make sure slave select is an output
    SPIC.CTRL = SPI_ENABLE_bm | SPI_MASTER_bm | SPI_MODE_0_gc | SPI_PRESCALER_DIV64_gc;
}



void spiclose(void)
{
    SPIC.CTRL = 0;
    SPIPort.DIR = SPIDir ;
    //SPIPort.DIRSET = (1<<SPIMI);
}

uint8_t spi(uint8_t val)
{
    SPIC.DATA = val ;
    while(!(SPIC.STATUS & (1<<7)));
    return SPIC.DATA;
} 

Call spiopen first to init, then use spi to transfer bytes whenever you want. spiclose exists 'cause silly me shared the spi lines with another function.

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Torby wrote:
Here's my SPI code.


/* Defines needed to make this work
   Usually defined in Config.h

   #define SPIPort PORTC
   #define SPIMO   7
   #define SPIMI   6
   #define SPISC   5
   #define SPISS   4

   */

uint8_t SPIDir = 0 ;

void spiopen(void)
{
    SPIDir = SPIPort.DIR ;
    SPIPort.DIRCLR = (1<<SPIMI);
    SPIPort.DIRSET = (1<<SPIMO) | (1<<SPISC) | (1<<SPISS) ; // Make sure slave select is an output
    SPIC.CTRL = SPI_ENABLE_bm | SPI_MASTER_bm | SPI_MODE_0_gc | SPI_PRESCALER_DIV64_gc;
}



void spiclose(void)
{
    SPIC.CTRL = 0;
    SPIPort.DIR = SPIDir ;
    //SPIPort.DIRSET = (1<<SPIMI);
}

uint8_t spi(uint8_t val)
{
    SPIC.DATA = val ;
    while(!(SPIC.STATUS & (1<<7)));
    return SPIC.DATA;
} 

Call spiopen first to init, then use spi to transfer bytes whenever you want. spiclose exists 'cause silly me shared the spi lines with another function.

Thank you your codes are working very fine.
but I made some changes:

#ifndef SPINRF_H_
#define SPINRF_H_

uint8_t SpiReadWrite(uint8_t val);
void Spiopen(void);
void Spiclose(void);

#define SPIPort PORTE
#define SPICTRL SPIE
#define SPIMO   7
#define SPIMI   6
#define SPISC   5
#define SPISS   4


void test()
{
	SpiReadWrite(0x9F);
	char TmpShow[10]="\0";
	for (int i=1;i<7;i++)
	{
		sprintf(TmpShow,"%02x",SpiReadWrite(0xFF));
		SendDataToUSB(TmpShow);
	}
}

uint8_t SPIDir = 0 ; 

void Spiopen(void) 
{ 
    SPIDir = SPIPort.DIR ; 
    SPIPort.DIRCLR = (1<<SPIMI); 
    SPIPort.DIRSET = (1<<SPIMO) | (1<<SPISC) | (1<<SPISS) ; // Make sure slave select is an output 
    SPICTRL.CTRL = SPI_ENABLE_bm | SPI_MASTER_bm | SPI_MODE_0_gc | SPI_PRESCALER_DIV64_gc; 
} 



void Spiclose(void) 
{ 
    SPICTRL.CTRL = 0; 
    SPIPort.DIR = SPIDir ; 
    SPIPort.DIRSET = (1<<SPIMI); 
} 

uint8_t SpiReadWrite(uint8_t val) 
{ 
	Spiopen();
    SPICTRL.DATA = val ; 
    while(!(SPICTRL.STATUS & (1<<7))); 
    return SPICTRL.DATA; 
	Spiclose();
} 

Thank you again.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Glad you got it working.

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut.