SmartRC-CC1101-Driver-Lib icon indicating copy to clipboard operation
SmartRC-CC1101-Driver-Lib copied to clipboard

ReceiveData is getting wrong results in fixed length mode

Open Gemikro1 opened this issue 1 year ago • 1 comments

The ReceiveData(buffer) function is returning a wrong number of bytes in fixed packet mode e.g. when using setLengthConfig(0) and setPacketLength(10). The function always interprets the first byte as the message length, which is not the case if fixed packet length is set.

This essentially prevents to receive messages from devices in fixed length mode that send a zero or other low value as first byte (like for instance the X-sense devices do).

The function probably needs a case structure, that checks to which mode the length config and packet length registers are set, and then read the correct number of bytes from the rx fifo.

Probably issue #74 is referring to the same problem.

Gemikro1 avatar Mar 05 '23 16:03 Gemikro1

I suggest an improved function which checks for some set options and returns according number of bytes:

byte ELECHOUSE_CC1101::ReceiveData(byte *rxBuffer)
{
	byte size;

	if(SpiReadStatus(CC1101_RXBYTES) & BYTES_IN_RXFIFO)
	{
		Split_PKTCTRL0();
		Split_PKTCTRL1();
		switch(pc0LenConf)
		{
			case 0:						// fixed packet length
				size=SpiReadReg(CC1101_PKTLEN);
				if (pc1APP_ST) { 			// check if additional status bytes enabled 
					size += 2;			// if yes, read two more bytes
					SpiReadBurstReg(CC1101_RXFIFO,(rxBuffer),size);
					}
				else {
					SpiReadBurstReg(CC1101_RXFIFO,(rxBuffer),(size));
				}
				SpiStrobe(CC1101_SFRX);			// clear receive FIFO
				SpiStrobe(CC1101_SRX);
				break;
			case 1:						// variable packet length
				size=SpiReadReg(CC1101_RXFIFO);
				if (pc1APP_ST) { 			// check if additional status bytes enabled 
					size += 2;			// if yes, read two more bytes
					SpiReadBurstReg(CC1101_RXFIFO,(rxBuffer),size);
					}
				else {
					SpiReadBurstReg(CC1101_RXFIFO,(rxBuffer),(size));
				}
				SpiStrobe(CC1101_SFRX);			// clear receive FIFO
				SpiStrobe(CC1101_SRX);
				break;
			case 2:						// infinite lenght mode --to be implemented--
				break;
			default:
				SpiStrobe(CC1101_SFRX);			// clear receive FIFO
				SpiStrobe(CC1101_SRX);
				break;
		}
		return size;
	}
	else
	{
		SpiStrobe(CC1101_SFRX);
		SpiStrobe(CC1101_SRX);
 		return 0;
	}
}


The status bytes are currently not returned from the function and should be added to the buffer (and size). Similar switch structure should be implemented for the send case

Also the modifications on the SPI burst transfer as suggested in #99 are ok and work in this way for Burst Write and Read (tested on Arduino Pro Micro, Wemos D1 mini and ESP8266E):

void ELECHOUSE_CC1101::SpiWriteBurstReg(byte addr, byte *buffer, byte num)
{
  byte i, temp;
  SpiStart();
  temp = addr | WRITE_BURST;
  digitalWrite(SS_PIN, LOW);
  while(digitalRead(MISO_PIN));
  SPI.transfer(temp);
  SPI.transfer((uint8_t *) buffer, (uint32_t) num);
  digitalWrite(SS_PIN, HIGH);
  SpiEnd();

void ELECHOUSE_CC1101::SpiReadBurstReg(byte addr, byte *buffer, byte num)
{
  byte i,temp;
  SpiStart();
  temp = addr | READ_BURST;
  digitalWrite(SS_PIN, LOW);
  while(digitalRead(MISO_PIN));
  SPI.transfer(temp);
  SPI.transfer((uint8_t *) buffer, (uint32_t) num);
  digitalWrite(SS_PIN, HIGH);
  SpiEnd();
}

Gemikro1 avatar Mar 11 '23 15:03 Gemikro1