LSatan / SmartRC-CC1101-Driver-Lib

This driver library can be used for many libraries that use a simple RF ASK module, with the advantages of the cc1101 module. It offers many direct setting options as in SmartRF Studio and calculates settings such as MHz directly.
Other
418 stars 94 forks source link

Can't read same status register twice in a row #94

Open johnellinwood opened 2 years ago

johnellinwood commented 2 years ago

If I try to call ELECHOUSE_cc1101.SpiReadStatus(CC1101_VERSION) twice in a row, it hangs.

My only guess is that you never do SPI.beginTransaction() before you call this SPI.transfer() on line 276. In fact, I don't see you ever call SPI.beginTransaction().

I'm using your library version 2.5.7 from the Arduino IDE Library Manager. I'm using an Arduino Nano 33 BLE and the CC1101 on an Ebyte E07-M1101D-TH. Coding in Arduino IDE 1.8.16.

This code generates the problem. It never prints the second "Success" message. Actually, it never prints a second message at all, it hangs trying to read the register.

  // set CC1101 driver to use pins defined for our variant. make sure D10 is connected to SS, all others are shown on the nano 33 ble pinout diagram
  ELECHOUSE_cc1101.setSpiPin(PIN_SPI_SCK, PIN_SPI_MISO, PIN_SPI_MOSI, PIN_SPI_SS); 
  // set CC1101 driver to use gdo pins. 
  // connect D9 to GDO2, which is digital output from the radio. 
  // connect A1/D15 to GDO0, which is digital output from the radio, but can also be used as analog temperature sensor.
  ELECHOUSE_cc1101.setGDO(A1, D9);
  ELECHOUSE_cc1101.Init();
  byte radioVersion = ELECHOUSE_cc1101.SpiReadStatus(CC1101_VERSION); // read version number
  if (radioVersion == 0x14) {
    Serial.println("Successfully read radio version as 0x14");
  } else {
    Serial.println("Failed to correctly read radio verison");
  }
  radioVersion = ELECHOUSE_cc1101.SpiReadStatus(CC1101_VERSION); // read version number
  if (radioVersion == 0x14) {
    Serial.println("Successfully read radio version as 0x14");
  } else {
    Serial.println("Failed to correctly read radio verison");
  }

If I use the Arduino SPI library directly, then I can make this work. The code that I wrote to directly use the arduino spi library that does work looks like:

#include "pins_arduino.h" // variant pin definitions includes PIN_SPI_*
#define RADIO_ADDRESS_MASK 0x3F
#define RADIO_RW_READ 0x80
#define RADIO_ACCESS_BURST 0x40

// arduino runs this first
setup() {
  getRadioStatusRegister(CC1101_PARTNUM);
  getRadioStatusRegister(CC1101_PARTNUM);
}

byte getRadioStatusRegister(byte reg) { // return current value
  byte cmd = buildCommand(RADIO_RW_READ, RADIO_ACCESS_BURST, reg);
  printCmd(cmd);
  byte radioStatus; // status byte response from command
  byte statusRegisterValue; // the value read from the status register
  //500Kb, MSB first, MODE0 is CPOL=0, CPHA=0, see page 30 of CC1101 spec
  SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0)); 
  spiSelectAndWait();
  radioStatus = SPI.transfer(cmd); // send command and get radio status byte response
  statusRegisterValue = SPI.transfer(0); // SPI api is always two-way, so send 0's to just read a byte
  printRadioStatus(radioStatus);
  spiDeselect();
  SPI.endTransaction();
  printStatusRegisterValue(reg, statusRegisterValue);
  return statusRegisterValue;
}

byte buildCommand(byte rw_mode, byte access_mode, byte cmd_reg) {
  return rw_mode | access_mode | (cmd_reg & RADIO_ADDRESS_MASK);
}

void spiWait() {
    while(digitalRead(PIN_SPI_MISO)) ;
}

void spiSelect() { // activates the radio chip for reading commands
  digitalWrite(PIN_SPI_SS, LOW); 
}

void spiDeselect() { // stops the radio chip from reading commands, cancels any commands in progress of writing
  digitalWrite(PIN_SPI_SS, HIGH); 
}

void spiSelectAndWait() {
  spiSelect();
  spiWait();
}
LSatan commented 2 years ago

thank you very much I will check it. if i get the same error i will fix it with the next update. regards