Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 10 of 10

Thread: SPI.transfer() Hangs on in the first call using Teensy 4.0 and CC1101

  1. #1
    Junior Member
    Join Date
    Aug 2020
    Posts
    3

    SPI.transfer() Hangs on in the first call using Teensy 4.0 and CC1101

    Greetings to all!
    I have been trying to use the cc1101 module of texas instrument with my Teensy 4.0 through SPI, I have been based on the library proposed by ELECHOUSE
    [https://github.com/simonmonk/CC1101_arduino]
    I have read the instructions that you leave about the SPI library [https://www.pjrc.com/teensy/td_libs_SPI.html]
    Because of this I know that I have to use the methods:
    -SPI.beginTransaction ()
    -SPI.transfer ()
    -SPI.endTransaction ()
    So I changed some lines from the ELECHOUSE library as I show below:

    Transmitter code on Arduino:
    Code:
    #include <ELECHOUSE_CC1101.h>
    #define size 2 //tamanio de buffer a mandar
    const long int sgn = 32768;  //0x8000; // B1000000000000000;// numero negativo
    long int Byte1 = 0, Byte2 = 0;
    byte data_Tx[size] = {0};
    int tiempo = 20;
    unsigned long timenow;
    byte TX_buffer[size] = {0}; //inicializar
    byte i;
    //int x=0;
    int x, y, z;
    
    void setup() {
      Serial.begin(9600);
      pinMode(8,OUTPUT); // debugger
      delay(1);
      ELECHOUSE_cc1101.Init();
      //Serial.println("Transmisor:");
    }
    
    void loop() {
      Serial.println("EMPEZO");
      x = -90;
      Serial.println(x);
      
      if (x < 0) {
        x = (-1 * (x)) + sgn; // coloca el signo en el bit mas significativo
      }
      Byte1 = x & 255;//enmascaramiento de BYTE inferior
      Byte2 = x & 65280; // enmascaramiento de BYTE superior
      Byte2 = Byte2 >> 8; // haz un bitshift left de 8 posiciones para derechaM
      data_Tx[0] = Byte1;//inferior
      data_Tx[1] = Byte2;//superior
    
      ELECHOUSE_cc1101.SendData(data_Tx, size);
    
      delay(1000);
    
    }
    Modified ELECHOUSE Library:

    Code:
    /*
    /*
    
    	This library was originally copyright of Michael at elechouse.com but permision was
        granted by Wilson Shen on 2016-10-23 for me (Simon Monk) to uodate the code for Arduino 1.0+
        and release the code on github under the MIT license.
    
    
    Wilson Shen <elechouse@elechouse.com>	23 October 2016 at 02:08
    To: Simon Monk 
    Thanks for your email.
    You are free to put it in github and to do and change.
    
    On Oct 22, 2016 10:07 PM, "Simon Monk" <srmonk@gmail.com> wrote:
    	Hi,
    
    	I'm Simon Monk, I'm currently writing the Electronics Cookbook for O'Reilly. I use your 
    	ELECHOUSE_CC1101 library in a 'recipe'. Your library is by far the easiest to use of 
    	the libraries for this device, but the .h and .cpp file both reference WProgram.h which 
    	as replaced by Arduino.h in Arduino 1.0.
    
    	Rather than have to talk my readers through applying a fix to your library, I'd like 
    	your permission to put the modified lib into Github and add an example from the book. 
    	I would of course provide a link to your website in the book and mention that you can buy 
    	the modules there. If its ok, I'd give the code an MIT OS license, to clarify its use.
    
    	Thanks for a great library,
    
    	Kind Regards,
    
    	Simon Monk.
    	
    */
    #include <ELECHOUSE_CC1101.h>
    #include <Arduino.h>
    #include <SPI.h> // For SPI functions
    
    /****************************************************************/
    #define 	WRITE_BURST     	0x40						//write burst
    #define 	READ_SINGLE     	0x80						//read single
    #define 	READ_BURST      	0xC0						//read burst
    #define 	BYTES_IN_RXFIFO     0x7F  						//byte number in RXfifo
    
    /****************************************************************/
    byte PaTabel[8] = {0x60 ,0x60 ,0x60 ,0x60 ,0x60 ,0x60 ,0x60 ,0x60};
    // byte PaTabel[8] = {0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0};
    
    /****************************************************************
    *FUNCTION NAME:SpiInit
    *FUNCTION     :spi communication initialization
    *INPUT        :none
    *OUTPUT       :none
    ****************************************************************/
    void ELECHOUSE_CC1101::SpiInit(void)
    {
    	// initialize the SPI pins
    	SPI.begin(); //
    	pinMode(SCK_PIN, OUTPUT);
    	pinMode(MOSI_PIN, OUTPUT);
    	pinMode(MISO_PIN, INPUT);
    	pinMode(SS_PIN, OUTPUT);
    	//Serial.println("SpiInit-CHECK");
    	// digitalWrite(8,1);
    	// 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
    	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)
    {
    	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);
    	//Serial.println("GDO_Set-CHECK");
    	// digitalWrite(8,1);
    }
    
    /****************************************************************
    *FUNCTION NAME:Reset
    *FUNCTION     :CC1101 reset //details refer datasheet of CC1101/CC1100//
    *INPUT        :none
    *OUTPUT       :none
    ****************************************************************/
    void ELECHOUSE_CC1101::Reset (void)
    {
    	//digitalWrite(8,1);
    	digitalWrite(SS_PIN, LOW);
    	delay(1);
    	digitalWrite(SS_PIN, HIGH);
    	delay(1);
    	SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0)); //
    	digitalWrite(SS_PIN, LOW);
    	//delayMicroseconds(2);          // delay doesn't work
    	while(digitalRead(MISO_PIN));
    	//digitalWrite(8,1);          //Just for debugging purposes
    	SPI.transfer(CC1101_SRES);//SpiTransfer(CC1101_SRES);                    // STUCK IN HERE
    	digitalWrite(8,1);          //Just for debugging purposes
    	while(digitalRead(MISO_PIN));
    	digitalWrite(SS_PIN, HIGH);
    	SPI.endTransaction(); //
    	//Serial.println("Reset-CHECK");
    }
    
    /****************************************************************
    *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);
    	Reset();						//CC1101 reset
    	RegConfigSettings(F_433);				//CC1101 register config
    	SpiWriteBurstReg(CC1101_PATABLE,PaTabel,8);		//CC1101 PATABLE config
    }
    
    /****************************************************************
    *FUNCTION NAME:Init
    *FUNCTION     :CC1101 initialization
    *INPUT        :none
    *OUTPUT       :none
    ****************************************************************/
    void ELECHOUSE_CC1101::Init(byte f)
    {
    	SpiInit();						//spi initialization
    	GDO_Set();						//GDO set
    	digitalWrite(SS_PIN, HIGH);
    	digitalWrite(SCK_PIN, HIGH);
    	digitalWrite(MOSI_PIN, LOW);
    	Reset();						//CC1101 reset
    	RegConfigSettings(f);					//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)
    {
    	SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0)); //
    	digitalWrite(SS_PIN, LOW);
    	while(digitalRead(MISO_PIN));
    	SPI.transfer(addr); // SpiTransfer(addr);
    	SPI.transfer(value); // SpiTransfer(value);
    	digitalWrite(SS_PIN, HIGH);
    	SPI.endTransaction(); //
    }
    
    /****************************************************************
    *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;
    	SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0)); //
        	digitalWrite(SS_PIN, LOW);
        	while(digitalRead(MISO_PIN));
        	SPI.transfer(temp); //SpiTransfer(temp);
        	for (i = 0; i < num; i++)
     	{
            	SPI.transfer(buffer[i]);//SpiTransfer(buffer[i]);
        	}
        	digitalWrite(SS_PIN, HIGH);
    	SPI.endTransaction(); //
    	// Serial.println("SpiWriteBurstReg-CHECK"); // NO PONERLO PORQUE DESEQUILIBRA LOS TIEMPOS
    }
    
    /****************************************************************
    *FUNCTION NAME:SpiStrobe
    *FUNCTION     :CC1101 Strobe
    *INPUT        :strobe: command; //refer define in CC1101.h//
    *OUTPUT       :none
    ****************************************************************/
    void ELECHOUSE_CC1101::SpiStrobe(byte strobe)
    {
    	SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0)); //
    	digitalWrite(SS_PIN, LOW);
    	while(digitalRead(MISO_PIN));
    	SPI.transfer(strobe); //SpiTransfer(strobe);
    	digitalWrite(SS_PIN, HIGH);
    	SPI.endTransaction(); //
    	// Serial.println("SpiStrobe-CHECK");
    }
    
    /****************************************************************
    *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;
    	SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0)); //
    	digitalWrite(SS_PIN, LOW);
    	while(digitalRead(MISO_PIN));
    	SPI.transfer(temp); //SpiTransfer(temp);
    	value = SPI.transfer(0); //SpiTransfer(0);
    	digitalWrite(SS_PIN, HIGH);
    	SPI.endTransaction(); //
    	
    	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;
    	SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0)); //
    	digitalWrite(SS_PIN, LOW);
    	while(digitalRead(MISO_PIN));
    	SPI.transfer(temp); //SpiTransfer(temp);
    	for(i=0;i<num;i++)
    	{
    		buffer[i]=SPI.transfer(0); //SpiTransfer(0);
    	}
    	digitalWrite(SS_PIN, HIGH);
    	SPI.endTransaction(); //
    }
    
    /****************************************************************
    *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;
    	SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0)); //
    	digitalWrite(SS_PIN, LOW);
    	while(digitalRead(MISO_PIN));
    	SPI.transfer(temp); //SpiTransfer(temp);
    	value = SPI.transfer(0); //SpiTransfer(0);
    	digitalWrite(SS_PIN, HIGH);
    	SPI.endTransaction(); //
    	
    	return value;
    }
    
    /****************************************************************
    *FUNCTION NAME:RegConfigSettings
    *FUNCTION     :CC1101 register config //details refer datasheet of CC1101/CC1100//
    *INPUT        :none
    *OUTPUT       :none
    ****************************************************************/
    void ELECHOUSE_CC1101::RegConfigSettings(byte f) 
    {
    	SpiWriteReg(CC1101_FSCTRL1,  0x08);
    	SpiWriteReg(CC1101_FSCTRL0,  0x00);
    	
    	switch(f)
    	{
    		case F_868:
    			SpiWriteReg(CC1101_FREQ2,    F2_868);
    			SpiWriteReg(CC1101_FREQ1,    F1_868);
    			SpiWriteReg(CC1101_FREQ0,    F0_868);
    			break;
    		case F_902:
    			SpiWriteReg(CC1101_FREQ2,    F2_902);
    			SpiWriteReg(CC1101_FREQ1,    F1_902);
    			SpiWriteReg(CC1101_FREQ0,    F0_902);
    			break;
    		case F_433:
    			SpiWriteReg(CC1101_FREQ2,    F2_433);
    			SpiWriteReg(CC1101_FREQ1,    F1_433);
    			SpiWriteReg(CC1101_FREQ0,    F0_433);
    			break;
    		default: // F must be set
    			break;
    	}
    
    	SpiWriteReg(CC1101_MDMCFG4,  0x5B);
    	SpiWriteReg(CC1101_MDMCFG3,  0xF8);
    	SpiWriteReg(CC1101_MDMCFG2,  0x03);
    	SpiWriteReg(CC1101_MDMCFG1,  0x22);
    	SpiWriteReg(CC1101_MDMCFG0,  0xF8);
    	SpiWriteReg(CC1101_CHANNR,   0x00);
    	SpiWriteReg(CC1101_DEVIATN,  0x47);
    	SpiWriteReg(CC1101_FREND1,   0xB6);
    	SpiWriteReg(CC1101_FREND0,   0x10);
    	SpiWriteReg(CC1101_MCSM0 ,   0x18);
    	SpiWriteReg(CC1101_FOCCFG,   0x1D);
    	SpiWriteReg(CC1101_BSCFG,    0x1C);
    	SpiWriteReg(CC1101_AGCCTRL2, 0xC7);
    	SpiWriteReg(CC1101_AGCCTRL1, 0x00);
    	SpiWriteReg(CC1101_AGCCTRL0, 0xB2);
    	SpiWriteReg(CC1101_FSCAL3,   0xEA);
    	SpiWriteReg(CC1101_FSCAL2,   0x2A);
    	SpiWriteReg(CC1101_FSCAL1,   0x00);
    	SpiWriteReg(CC1101_FSCAL0,   0x11);
    	SpiWriteReg(CC1101_FSTEST,   0x59);
    	SpiWriteReg(CC1101_TEST2,    0x81);
    	SpiWriteReg(CC1101_TEST1,    0x35);
    	SpiWriteReg(CC1101_TEST0,    0x09);
    	SpiWriteReg(CC1101_IOCFG2,   0x0B); 	//serial clock.synchronous to the data in synchronous serial mode
    	SpiWriteReg(CC1101_IOCFG0,   0x06);  	//asserts when sync word has been sent/received, and de-asserts at the end of the packet 
    	SpiWriteReg(CC1101_PKTCTRL1, 0x04);	//two status bytes will be appended to the payload of the packet,including RSSI LQI and CRC OK
    							//No address check
    	SpiWriteReg(CC1101_PKTCTRL0, 0x05);	//whitening off;CRC Enable\A3\BBvariable length packets, packet length configured by the first byte after sync word
    	SpiWriteReg(CC1101_ADDR,     0x00);	//address used for packet filtration.
    	SpiWriteReg(CC1101_PKTLEN,   0x3D); 	//61 bytes max length
    	Serial.println("RegConfigSettings-CHECK");
    }
    
    /****************************************************************
    *FUNCTION NAME:SendData
    *FUNCTION     :use CC1101 send data
    *INPUT        :txBuffer: data array to send; size: number of data to send, no more than 61
    *OUTPUT       :none
    ****************************************************************/
    void ELECHOUSE_CC1101::SendData(byte *txBuffer,byte size)
    {
    	SpiWriteReg(CC1101_TXFIFO,size);
    	SpiWriteBurstReg(CC1101_TXFIFO,txBuffer,size);	//write data to send
    	SpiStrobe(CC1101_STX);					//start send	
    	while (!digitalRead(GDO0));				// Wait for GDO0 to be set -> sync transmitted  
    	while (digitalRead(GDO0));				// Wait for GDO0 to be cleared -> end of packet
    	SpiStrobe(CC1101_SFTX);				//flush TXfifo
    	//Serial.println("SendData-CHECK");
    }
    
    /****************************************************************
    *FUNCTION NAME:SetReceive
    *FUNCTION     :set CC1101 to receive state
    *INPUT        :none
    *OUTPUT       :none
    ****************************************************************/
    void ELECHOUSE_CC1101::SetReceive(void)
    {
    	SpiStrobe(CC1101_SRX);
    }
    
    /****************************************************************
    *FUNCTION NAME:CheckReceiveFlag
    *FUNCTION     :check receive data or not
    *INPUT        :none
    *OUTPUT       :flag: 0 no data; 1 receive data 
    ****************************************************************/
    byte ELECHOUSE_CC1101::CheckReceiveFlag(void)
    {
    	if(digitalRead(GDO0))					//receive data
    	{
    		while (digitalRead(GDO0));
    		return 1;
    	}
    	else							// no data
    	{
    		return 0;
    	}
    }
    
    
    /****************************************************************
    *FUNCTION NAME:ReceiveData
    *FUNCTION     :read data received from RXfifo
    *INPUT        :rxBuffer: buffer to store data
    *OUTPUT       :size of data received
    ****************************************************************/
    byte ELECHOUSE_CC1101::ReceiveData(byte *rxBuffer)
    {
    	byte size;
    	byte status[2];
    
    	if(SpiReadStatus(CC1101_RXBYTES) & BYTES_IN_RXFIFO)
    	{
    		size=SpiReadReg(CC1101_RXFIFO);
    		SpiReadBurstReg(CC1101_RXFIFO,rxBuffer,size);
    		SpiReadBurstReg(CC1101_RXFIFO,status,2);
    		SpiStrobe(CC1101_SFRX);
    		return size;
    	}
    	else
    	{
    		SpiStrobe(CC1101_SFRX);
    		return 0;
    	}
    	
    }
    
    ELECHOUSE_CC1101 ELECHOUSE_cc1101;
    However, I have not managed to get it to work on the Teensy 4.0.
    While debugging the code I noticed that the code hangs on the first call of SPI.transfer() on the Reset() function from ELECHOUSE library
    I already read the following threads to try to fix my problem:
    [https://forum.pjrc.com/threads/57507-SPI-on-Teensy-4]
    [https://forum.pjrc.com/threads/61932...y-4-0-and-4-1]
    [https://forum.pjrc.com/threads/59906...on-Teensy-4-0]
    But I am still unsuccessful.
    At the moment I do not have access to an oscilloscope due to the COVID-19 situation so I cannot do an analysis of the SPI signals.
    It is worth mentioning that the library that I modified works on an Arduino nano and Arduino UNO, taking into account that the connections must be at 3.3v
    It is also important to say that I am using the updated SPI library: [https://github.com/PaulStoffregen/SPI]
    Click image for larger version. 

Name:	F19_10.jpg 
Views:	7 
Size:	82.4 KB 
ID:	21306
    The connections on the Teensy 4.0 were direct due to the maximum voltage being 3.3v so I omitted the resistors

    Please, help understand what I'm doing wrong
    Attached Files Attached Files
    Last edited by Aldo Bucio; 08-08-2020 at 07:19 AM.

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,497
    Add SPI.begin() in your setup() function.

  3. #3
    Junior Member
    Join Date
    Aug 2020
    Posts
    3
    Quote Originally Posted by PaulStoffregen View Post
    Add SPI.begin() in your setup() function.
    It is on the ELECHOUSE_cc1101.Init(); when it call ELECHOUSE_CC1101::SpiInit() on the library.
    But it still doesn't work when i add it in the Arduino code.

    Code:
    #include <ELECHOUSE_CC1101.h>
    #include <SPI.h> // For SPI functions
    #define size 2 //tamanio de buffer a mandar
    const long int sgn = 32768;  //0x8000; // B1000000000000000;// numero negativo
    long int Byte1 = 0, Byte2 = 0;
    byte data_Tx[size] = {0};
    int tiempo = 20;
    unsigned long timenow;
    byte TX_buffer[size] = {0}; //inicializar
    byte i;
    //int x=0;
    int x, y, z;
    
    void setup() {
      Serial.begin(9600);
      SPI.begin();
      pinMode(8,OUTPUT); // debugger
      delay(1);
      ELECHOUSE_cc1101.Init();
      //Serial.println("Transmisor:");
    }
    
    void loop() {
      Serial.println("EMPEZO");
      x = -90;
      Serial.println(x);
      
      if (x < 0) {
        x = (-1 * (x)) + sgn; // coloca el signo en el bit mas significativo
      }
      Byte1 = x & 255;//enmascaramiento de BYTE inferior
      Byte2 = x & 65280; // enmascaramiento de BYTE superior
      Byte2 = Byte2 >> 8; // haz un bitshift left de 8 posiciones para derecha
      data_Tx[0] = Byte1;//inferior
      data_Tx[1] = Byte2;//superior
    
      ELECHOUSE_cc1101.SendData(data_Tx, size);
    
      delay(1000);
    
    }
    I don't know what I'm doing wrong, I really sorry for this noob question.

  4. #4
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,574
    Quote Originally Posted by Aldo Bucio View Post
    While debugging the code I noticed that the code hangs on the first call of SPI.transfer() on the Reset() function from ELECHOUSE library
    Normally, SPI does not "hang" -- even if you have nothing connected to SPI pins. I would get rid of all the while(digitalRead(MISO_PIN));, 1) because the while is probably causing the hang and 2) you're using digital IO with the SPI pins while they are in SPI mode, a very non-standard approach.

    and get rid of
    Code:
    	pinMode(SCK_PIN, OUTPUT);
    	pinMode(MOSI_PIN, OUTPUT);
    	pinMode(MISO_PIN, INPUT);
    those pins need to remain in "SPI mode" after the SPI.begin(). Only the SS_PIN should be in OUTPUT mode.
    Last edited by manitou; 08-08-2020 at 10:49 AM.

  5. #5
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    277
    To avoid a problem with the library, first try without the library and perform a basic SPI transfer. Like this:
    Code:
    #include <SPI.h>
    const int SS_PIN = 10;          // assuming MOSI 11, MISO 12, SCK 13
    
    void setup() {
      SPI.begin();
      pinMode(8, OUTPUT);           // debugger pin
      digitalWrite(8, LOW);         // set debugger output low
      pinMode(SS_PIN, OUTPUT);
      digitalWrite(SS_PIN, HIGH);   // set SS pin inactive
    
      SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
      digitalWrite(SS_PIN, LOW);    // set SS low
      delay(1);                     // datasheet states to wait for minimum 150us/20ns depending on chip mode
      //digitalWrite(8,1);          // Just for debugging purposes
      SPI.transfer(0x30);           // CC1101_SRES = 0x30
      digitalWrite(8, 1);           // Just for debugging purposes
      digitalWrite(SS_PIN, HIGH);   // set SS high
      SPI.endTransaction();
    }
    
    void loop() {
    }
    Paul

  6. #6
    Senior Member
    Join Date
    Jul 2014
    Posts
    2,722
    Quote Originally Posted by manitou View Post
    and get rid of
    Code:
    	pinMode(SCK_PIN, OUTPUT);
    	pinMode(MOSI_PIN, OUTPUT);
    	pinMode(MISO_PIN, INPUT);
    those pins need to remain in "SPI mode" after the SPI.begin(). Only the SS_PIN should be in OUTPUT mode.
    In fact, putting SPI pins (SCK,MOSI,MISO) into OUTPUT disables SPI transfer.

  7. #7
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    277
    Quote Originally Posted by WMXZ View Post
    In fact, putting SPI pins (SCK,MOSI,MISO) into OUTPUT disables SPI transfer.
    Well, the ELECHOUSE_CC1101 library is just doing that...
    Code:
    void ELECHOUSE_CC1101::SpiInit(void)
    {
    	// initialize the SPI pins
    	SPI.begin(); //
    	pinMode(SCK_PIN, OUTPUT);
    	pinMode(MOSI_PIN, OUTPUT);
    	pinMode(MISO_PIN, INPUT);
    	pinMode(SS_PIN, OUTPUT);
    	//Serial.println("SpiInit-CHECK");
    	// digitalWrite(8,1);
    	// enable SPI Master, MSB, SPI mode 0, FOSC/4
    	//SpiMode(0);
    }
    Paul

  8. #8
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,574
    Quote Originally Posted by PaulS View Post
    Well, the ELECHOUSE_CC1101 library is just doing that...
    Code:
    void ELECHOUSE_CC1101::SpiInit(void)
    {
    	// initialize the SPI pins
    	SPI.begin(); //
    	pinMode(SCK_PIN, OUTPUT);
    	pinMode(MOSI_PIN, OUTPUT);
    	pinMode(MISO_PIN, INPUT);
    	pinMode(SS_PIN, OUTPUT);
    	//Serial.println("SpiInit-CHECK");
    	// digitalWrite(8,1);
    	// enable SPI Master, MSB, SPI mode 0, FOSC/4
    	//SpiMode(0);
    }
    Paul
    yes, the ELECHOUSE lib is built on the AVR SPI. The Teensy/ARM SPI requires different logic

  9. #9
    Junior Member
    Join Date
    Aug 2020
    Posts
    3

    Talking

    Quote Originally Posted by PaulS View Post
    To avoid a problem with the library, first try without the library and perform a basic SPI transfer. Like this:
    Code:
    #include <SPI.h>
    const int SS_PIN = 10;          // assuming MOSI 11, MISO 12, SCK 13
    
    void setup() {
      SPI.begin();
      pinMode(8, OUTPUT);           // debugger pin
      digitalWrite(8, LOW);         // set debugger output low
      pinMode(SS_PIN, OUTPUT);
      digitalWrite(SS_PIN, HIGH);   // set SS pin inactive
    
      SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
      digitalWrite(SS_PIN, LOW);    // set SS low
      delay(1);                     // datasheet states to wait for minimum 150us/20ns depending on chip mode
      //digitalWrite(8,1);          // Just for debugging purposes
      SPI.transfer(0x30);           // CC1101_SRES = 0x30
      digitalWrite(8, 1);           // Just for debugging purposes
      digitalWrite(SS_PIN, HIGH);   // set SS high
      SPI.endTransaction();
    }
    
    void loop() {
    }
    Paul
    I'm sorry I was slow to respond, I was testing what you mentioned in your comments.
    That code really works, I even removed the "delay(1);" line and it still works.
    I noticed that you removed the declarations from the pins after SPI.begin ():

    SPI.begin (); //
    pinMode (SCK_PIN, OUTPUT); // ERASED
    pinMode (MOSI_PIN, OUTPUT); // ERASED
    pinMode (MISO_PIN, INPUT); // ERASED
    pinMode (SS_PIN, OUTPUT); // still on code

    As MINITOU said, those lines in the SpiInit(void) method were the problem, a problem that only occurs in Teensy 4.0 because I don't have that problem when using an Arduino Nano or an Arduino UNO.

    Code:
    #include <SPI.h>
    const int SS_PIN = 10;          // assuming MOSI 11, MISO 12, SCK 13
    
    const int SCK_PIN = 13;  // Adding just for the test
    const int MOSI_PIN = 11; // Adding just for the test
    const int MISO_PIN = 12; // Adding just for the test
    
    void setup() {
      SPI.begin();
    /*Spanish comment sorry, I'm from Mexico*/
    /* Al descomentarse el SPI en la Teensy NO funciona pero en Arduino sí */
    // These lines works for Arduino UNO/NANO, but doesn't work for Teensy 4.0, that's why are commented.
    //  pinMode(SCK_PIN, OUTPUT);
    //  pinMode(MOSI_PIN, OUTPUT);
    //  pinMode(MISO_PIN, INPUT);  
      
      pinMode(8, OUTPUT);           // debugger pin
      digitalWrite(8, LOW);         // set debugger output low
      pinMode(SS_PIN, OUTPUT);
      digitalWrite(SS_PIN, HIGH);   // set SS pin inactive
    
      SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
      digitalWrite(SS_PIN, LOW);    // set SS low
      delay(1);                     // datasheet states to wait for minimum 150us/20ns depending on chip mode
      //digitalWrite(8,1);          // Just for debugging purposes
      SPI.transfer(0x30);           // CC1101_SRES = 0x30
      digitalWrite(8, 1);           // Just for debugging purposes
      digitalWrite(SS_PIN, HIGH);   // set SS high
      SPI.endTransaction();
    }
    
    void loop() {
      Serial.println("Sí pasó, wey :D");
      delay(1000);
    }
    Thank you very much for the support.
    Last edited by Aldo Bucio; 08-09-2020 at 05:50 AM. Reason: I forget to say: "Thank you"

  10. #10
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    277
    You're welcome.
    With respect to removing delay(1);, I'm not sure whether that is a good idea for the first time you are talking the CC1101 after power up.
    If I read chapter 19 of the CC1101 spec correctly, the chip is in power-down mode after power up, so you have to meet the 150us spec in Table 22: SPI Interface Timing Requirements.
    Here is a screendump of the SPI transfer without the delay(1); inserted: [measured on a Teensy 4.0 @ 600MHz]

    Click image for larger version. 

Name:	DSView-200809-083813.png 
Views:	11 
Size:	44.1 KB 
ID:	21322

    After SS going low, it takes ~240nsec for the SCK to become active. For normal operation, this meets the 20nsec spec but not for power-down mode.

    So my suggestion would be to keep in the delay(1); for the first SPI transaction [SRES command strobe]; subsequent SPI transfers don't need a delay.
    If you are really picky on timing, you could replace delay(1); by delayMicroseconds(150);.

    Paul

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •