SPCR, SPE, MSTR, SPSR...... For using CC1101 RF module with chipkit

Talk about anything you want!
Post Reply
francvelab
Posts: 1
Joined: Wed Apr 20, 2016 5:49 am

SPCR, SPE, MSTR, SPSR...... For using CC1101 RF module with chipkit

Post by francvelab » Thu Apr 21, 2016 12:17 am

Hi everybody!

I'm using the RF module CC1101 915Mhz, altough it works with arduino it doesn't with chipkit. I have some errors so that somebody could tell me what about with the next registers and if I can use my code.

Thanks in advance!

Errors:

/Users/blind_1345/Documents/mpide/libraries/ELECHOUSE_CC1101/ELECHOUSE_CC1101.cpp: In member function 'void ELECHOUSE_CC1101::SpiMode(byte)':
/Users/blind_1345/Documents/mpide/libraries/ELECHOUSE_CC1101/ELECHOUSE_CC1101.cpp:65:3: error: 'SPCR' was not declared in this scope
/Users/blind_1345/Documents/mpide/libraries/ELECHOUSE_CC1101/ELECHOUSE_CC1101.cpp:66:32: error: 'SPE' was not declared in this scope
/Users/blind_1345/Documents/mpide/libraries/ELECHOUSE_CC1101/ELECHOUSE_CC1101.cpp:66:43: error: 'MSTR' was not declared in this scope
/Users/blind_1345/Documents/mpide/libraries/ELECHOUSE_CC1101/ELECHOUSE_CC1101.cpp:67:9: error: 'SPSR' was not declared in this scope
/Users/blind_1345/Documents/mpide/libraries/ELECHOUSE_CC1101/ELECHOUSE_CC1101.cpp:68:9: error: 'SPDR' was not declared in this scope
/Users/blind_1345/Documents/mpide/libraries/ELECHOUSE_CC1101/ELECHOUSE_CC1101.cpp: In member function 'byte ELECHOUSE_CC1101::SpiTransfer(byte)':
/Users/blind_1345/Documents/mpide/libraries/ELECHOUSE_CC1101/ELECHOUSE_CC1101.cpp:82:4: error: 'SPDR' was not declared in this scope
/Users/blind_1345/Documents/mpide/libraries/ELECHOUSE_CC1101/ELECHOUSE_CC1101.cpp:83:12: error: 'SPSR' was not declared in this scope
/Users/blind_1345/Documents/mpide/libraries/ELECHOUSE_CC1101/ELECHOUSE_CC1101.cpp:83:23: error: 'SPIF' was not declared in this scope

Code: Select all


void ELECHOUSE_CC1101::SpiInit(void)
{
  // initialize the SPI pins
  pinMode(SCK_PIN, OUTPUT);
  pinMode(MOSI_PIN, OUTPUT);
  pinMode(MISO_PIN, INPUT);
  pinMode(SS_PIN, OUTPUT);

  // enable SPI Master, MSB, SPI mode 0, FOSC/4
  SpiMode(0);
}
/****************************************************************
*FUNCTION NAME:SpiMode
*FUNCTION     :set spi mode
*INPUT        :        config               mode
			   (0<<CPOL) | (0 << CPHA)		 0
			   (0<<CPOL) | (1 << CPHA)		 1
			   (1<<CPOL) | (0 << CPHA)		 2
			   (1<<CPOL) | (1 << CPHA)		 3
*OUTPUT       :none
****************************************************************/
void ELECHOUSE_CC1101::SpiMode(byte config)
{
  byte tmp;

  // enable SPI master with configuration byte specified
  //pspi->sxCon.reg = 0;
 // _SPICON=0; //SPCR = 0;
  //pspi->sxCon.set= (config & 0x7F) | (1<<_SPICON_ON) | (1<<_SPICON_MSTEN);//SPCR = (config & 0x7F) | (1<<SPE) | (1<<MSTR);
  //tmp= pspi->sxStat.reg;//tmp = SPSR;
  //tmp= pspi->sxBuf.reg;//tmp = SPDR;
  SPCR = 0;
  SPCR = (config & 0x7F) | (1<<SPE) | (1<<MSTR);
  tmp = SPSR;
  tmp = SPDR;
}

/****************************************************************
*FUNCTION NAME:SpiTransfer
*FUNCTION     :spi transfer
*INPUT        :value: data to send
*OUTPUT       :data to receive
****************************************************************/
byte ELECHOUSE_CC1101::SpiTransfer(byte value)
{
  /*pspi->sxBuf.reg=value;//SPDR = value;
  while (!(pspi->sxStat.reg & (1<<_SPISTAT_SPITBE))) ;
  return pspi->sxBuf.reg;*/
   SPDR = value;
  while (!(SPSR & (1<<SPIF))) ;
  return SPDR;
}

/****************************************************************
*FUNCTION NAME: GDO_Set()
*FUNCTION     : set GDO0,GDO2 pin
*INPUT        : none
*OUTPUT       : none
****************************************************************/
void ELECHOUSE_CC1101::GDO_Set (void)
{
	pinMode(GDO0, INPUT);
	pinMode(GDO2, INPUT);
}

/****************************************************************
*FUNCTION NAME:Reset
*FUNCTION     :CC1101 reset //details refer datasheet of CC1101/CC1100//
*INPUT        :none
*OUTPUT       :none
****************************************************************/
void ELECHOUSE_CC1101::Reset (void)
{
	digitalWrite(SS_PIN, LOW);
	delay(1);
	digitalWrite(SS_PIN, HIGH);
	delay(1);
	digitalWrite(SS_PIN, LOW);
	while(digitalRead(MISO_PIN));
		SpiTransfer(CC1101_SRES);
	/*{
		spi1.setSelect(LOW);
		spi1.transfer(CC1101_SRES);
		spi1.setSelect(HIGH);
	}*/
	
	while(digitalRead(MISO_PIN));
	digitalWrite(SS_PIN, HIGH);
}

/****************************************************************
*FUNCTION NAME:Init
*FUNCTION     :CC1101 initialization
*INPUT        :none
*OUTPUT       :none
****************************************************************/
void ELECHOUSE_CC1101::Init(void)
{
	SpiInit();										//spi initialization
	GDO_Set();										//GDO set
	digitalWrite(SS_PIN, HIGH);
	digitalWrite(SCK_PIN, HIGH);
	digitalWrite(MOSI_PIN, LOW);
	// spi1.begin(10);
 	// spi1.setSpeed(1000000);
 	// spi1.setMode(DSPI_MODE0);
	Reset();										//CC1101 reset
	RegConfigSettings();							//CC1101 register config
	SpiWriteBurstReg(CC1101_PATABLE,PaTabel,8);		//CC1101 PATABLE config
}


/****************************************************************
*FUNCTION NAME:SpiWriteReg
*FUNCTION     :CC1101 write data to register
*INPUT        :addr: register address; value: register value
*OUTPUT       :none
****************************************************************/
void ELECHOUSE_CC1101::SpiWriteReg(byte addr, byte value)
{
	digitalWrite(SS_PIN, LOW);
	while(digitalRead(MISO_PIN));
	SpiTransfer(addr);
	SpiTransfer(value);
	/*{
		spi1.setSelect(LOW);
		spi1.transfer(addr);
		spi1.setSelect(HIGH);
	}
	
	spi1.setSelect(LOW);
	spi1.transfer(value);
	spi1.setSelect(HIGH);*/
	digitalWrite(SS_PIN, HIGH);
}

/****************************************************************
*FUNCTION NAME:SpiWriteBurstReg
*FUNCTION     :CC1101 write burst data to register
*INPUT        :addr: register address; buffer:register value array; num:number to write
*OUTPUT       :none
****************************************************************/
void ELECHOUSE_CC1101::SpiWriteBurstReg(byte addr, byte *buffer, byte num)
{
	byte i, temp;

	temp = addr | WRITE_BURST;
    digitalWrite(SS_PIN, LOW);
    while(digitalRead(MISO_PIN));
	SpiTransfer(temp);
	/*{
    	 spi1.setSelect(LOW);
   		 spi1.transfer(temp);
  		 spi1.setSelect(HIGH);
	}*/
    
   
    for (i = 0; i < num; i++)
 	{
 		 /*spi1.setSelect(LOW);
		 spi1.transfer(buffer[i]);
		 spi1.setSelect(HIGH);*/
        SpiTransfer(buffer[i]);
    }
    digitalWrite(SS_PIN, HIGH);
}

/****************************************************************
*FUNCTION NAME:SpiStrobe
*FUNCTION     :CC1101 Strobe
*INPUT        :strobe: command; //refer define in CC1101.h//
*OUTPUT       :none
****************************************************************/
void ELECHOUSE_CC1101::SpiStrobe(byte strobe)
{
	digitalWrite(SS_PIN, LOW);
	while(digitalRead(MISO_PIN));
	SpiTransfer(strobe);
	/*{
		spi1.setSelect(LOW);
		spi1.transfer(strobe);
		spi1.setSelect(HIGH);
	}*/
	digitalWrite(SS_PIN, HIGH);
}

/****************************************************************
*FUNCTION NAME:SpiReadReg
*FUNCTION     :CC1101 read data from register
*INPUT        :addr: register address
*OUTPUT       :register value
****************************************************************/
byte ELECHOUSE_CC1101::SpiReadReg(byte addr) 
{
	byte temp, value;

    temp = addr|READ_SINGLE;
	digitalWrite(SS_PIN, LOW);
	while(digitalRead(MISO_PIN));
	SpiTransfer(temp);
	value=SpiTransfer(0);
	/*{
	spi1.setSelect(LOW);
	spi1.transfer(temp);
	spi1.setSelect(HIGH);
	}
	spi1.setSelect(LOW);
	value=spi1.transfer(0);
	spi1.setSelect(HIGH);*/
	digitalWrite(SS_PIN, HIGH);
	return value;
}

/****************************************************************
*FUNCTION NAME:SpiReadBurstReg
*FUNCTION     :CC1101 read burst data from register
*INPUT        :addr: register address; buffer:array to store register value; num: number to read
*OUTPUT       :none
****************************************************************/
void ELECHOUSE_CC1101::SpiReadBurstReg(byte addr, byte *buffer, byte num)
{
	byte i,temp;

	temp = addr | READ_BURST;
	digitalWrite(SS_PIN, LOW);
	while(digitalRead(MISO_PIN));
	SpiTransfer(temp);
	/*{
		spi1.setSelect(LOW);
	spi1.transfer(temp);
	spi1.setSelect(HIGH);
	}*/
	for(i=0;i<num;i++)
	{
	buffer[i]=SpiTransfer(0);
	/*	spi1.setSelect(LOW);
		buffer[i]=spi1.transfer(0);
		spi1.setSelect(HIGH);*/	
	}
	digitalWrite(SS_PIN, HIGH);
}

/****************************************************************
*FUNCTION NAME:SpiReadStatus
*FUNCTION     :CC1101 read status register
*INPUT        :addr: register address
*OUTPUT       :status value
****************************************************************/
byte ELECHOUSE_CC1101::SpiReadStatus(byte addr) 
{
	byte value,temp;

	temp = addr | READ_BURST;
	digitalWrite(SS_PIN, LOW);
	while(digitalRead(MISO_PIN));
		SpiTransfer(temp);
		value=SpiTransfer(0);	
	/*{
		spi1.setSelect(LOW);
		spi1.transfer(temp);
		spi1.setSelect(HIGH);}
	
	spi1.setSelect(LOW);
	spi1.transfer(0);
	spi1.setSelect(HIGH);*/
	digitalWrite(SS_PIN, HIGH);
	return value;
}

EmbeddedMan
Posts: 597
Joined: Tue May 24, 2011 9:24 pm
Location: Minneapolis, MN
Contact:

Re: SPCR, SPE, MSTR, SPSR...... For using CC1101 RF module with chipkit

Post by EmbeddedMan » Thu Apr 21, 2016 2:52 am

The errors you are getting look to me like a result of the library you are trying to compile writing directly from the SPI registers on an AVR. This library will not work as-is with chipKIT (as it will not work with any other architecture than AVR, if it is really accessing those registers directly). Instead, you'll need to go through the library and abstract the SPI calls to use the Arduino SPI library, or our own chipKIT DSPI library. Then it will work on chipKIT.

*Brian

Post Reply