Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hability to share SPI with TFT and Other devices #165

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 109 additions & 53 deletions ELECHOUSE_CC1101_SRC_DRV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
cc1101 Driver for RC Switch. Mod by Little Satan. With permission to modify and publish Wilson Shen (ELECHOUSE).
----------------------------------------------------------------------------------------------------------------
*/
#include <SPI.h>
#include "ELECHOUSE_CC1101_SRC_DRV.h"
#include <Arduino.h>

Expand Down Expand Up @@ -42,7 +41,7 @@ byte SS_PIN_M[max_modul];
byte GDO0_M[max_modul];
byte GDO2_M[max_modul];
byte gdo_set=0;
bool spi = 0;
bool __spi = 0;
bool ccmode = 0;
float MHz = 433.92;
byte m4RxBw = 0;
Expand Down Expand Up @@ -77,6 +76,53 @@ uint8_t PA_TABLE_433[8] {0x12,0x0E,0x1D,0x34,0x60,0x84,0xC8,0xC0,};
uint8_t PA_TABLE_868[10] {0x03,0x17,0x1D,0x26,0x37,0x50,0x86,0xCD,0xC5,0xC0,}; //779 - 899.99
// -30 -20 -15 -10 -6 0 5 7 10 11
uint8_t PA_TABLE_915[10] {0x03,0x0E,0x1E,0x27,0x38,0x8E,0x84,0xCC,0xC3,0xC0,}; //900 - 928

/****************************************************************
*FUNCTION NAME:setBeginEndLogic
*FUNCTION :set a new state to Begin/End logic, in this logic SPI class uses SPI.begin(...) and SPI.end() logic for backwards compatibility
*INPUT :state: true or false
*OUTPUT :none
****************************************************************/
void ELECHOUSE_CC1101::setBeginEndLogic(bool state) {
_begin_end_logic = state;
}


/****************************************************************
*FUNCTION NAME:getBeginEndLogic
*FUNCTION :get the state to Begin/End logic, in this logic SPI class uses SPI.begin(...) and SPI.end() logic for backwards compatibility
*INPUT :state: true or false
*OUTPUT :none
****************************************************************/
bool ELECHOUSE_CC1101::getBeginEndLogic() {
return _begin_end_logic;
}

/****************************************************************
*FUNCTION NAME:setSPIinstance
*FUNCTION :Set the SPI Instance to use if needed to share SPI Bus with other devices
*INPUT :none
*OUTPUT :none
****************************************************************/
void ELECHOUSE_CC1101::setSPIinstance(SPIClass* sspi)
{
// Set the SPI instance to use if needed to share SPI Bus with other devices
cc_spi=sspi;
}
/****************************************************************
*FUNCTION NAME:getSPIinstance
*FUNCTION :Gives the SPI instance in use by CC1101, if none was set, will set the library SPI instance
*INPUT :none
*OUTPUT :none
****************************************************************/
SPIClass* ELECHOUSE_CC1101::getSPIinstance()
{
if(cc_spi==nullptr) {
cc_spi=&_cc_spi;
}
return cc_spi;
}

/****************************************************************
*FUNCTION NAME:SpiStart
*FUNCTION :spi communication start
Expand All @@ -85,18 +131,15 @@ uint8_t PA_TABLE_915[10] {0x03,0x0E,0x1E,0x27,0x38,0x8E,0x84,0xCC,0xC3,0xC0,};
****************************************************************/
void ELECHOUSE_CC1101::SpiStart(void)
{
// initialize the SPI pins
pinMode(SCK_PIN, OUTPUT);
pinMode(MOSI_PIN, OUTPUT);
pinMode(MISO_PIN, INPUT);
pinMode(SS_PIN, OUTPUT);
//End transaction to ensure openin a new session with the right SPISettings
digitalWrite(SS_PIN, HIGH);
cc_spi->endTransaction();
if(_begin_end_logic) cc_spi->end();
if(_begin_end_logic) cc_spi->begin(SCK_PIN,MISO_PIN,MOSI_PIN,SS_PIN);
digitalWrite(SS_PIN, LOW);
cc_spi->beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE0));


// enable SPI
#ifdef ESP32
SPI.begin(SCK_PIN, MISO_PIN, MOSI_PIN, SS_PIN);
#else
SPI.begin();
#endif
}
/****************************************************************
*FUNCTION NAME:SpiEnd
Expand All @@ -107,8 +150,10 @@ void ELECHOUSE_CC1101::SpiStart(void)
void ELECHOUSE_CC1101::SpiEnd(void)
{
// disable SPI
SPI.endTransaction();
SPI.end();
cc_spi->endTransaction();
digitalWrite(SS_PIN, HIGH);
if(_begin_end_logic) cc_spi->end();

}
/****************************************************************
*FUNCTION NAME: GDO_Set()
Expand Down Expand Up @@ -139,13 +184,14 @@ void ELECHOUSE_CC1101::GDO0_Set (void)
****************************************************************/
void ELECHOUSE_CC1101::Reset (void)
{
SpiStart();
digitalWrite(SS_PIN, LOW);
delay(1);
digitalWrite(SS_PIN, HIGH);
delay(1);
digitalWrite(SS_PIN, LOW);
while(digitalRead(MISO_PIN));
SPI.transfer(CC1101_SRES);
cc_spi->transfer(CC1101_SRES);
while(digitalRead(MISO_PIN));
digitalWrite(SS_PIN, HIGH);
}
Expand All @@ -157,14 +203,23 @@ void ELECHOUSE_CC1101::Reset (void)
****************************************************************/
void ELECHOUSE_CC1101::Init(void)
{
// check if SPI Pins are set
setSpi();
SpiStart(); //spi initialization
pinMode(SS_PIN, OUTPUT);
// If there were no different SPI Instance, use This lib instance
if(cc_spi==nullptr || _begin_end_logic) {
DEBUG_CC1101("CC1101: Null pointer SPI instance or Begin/end");
_begin_end_logic=true;
cc_spi=&_cc_spi;
cc_spi->begin(SCK_PIN,MISO_PIN,MOSI_PIN,SS_PIN);
delay(1);
} else { DEBUG_CC1101("CC1101: Using other instance"); }

SpiStart(); //Start SPI Transaction
digitalWrite(SS_PIN, HIGH);
digitalWrite(SCK_PIN, HIGH);
digitalWrite(MOSI_PIN, LOW);
Reset(); //CC1101 reset
RegConfigSettings(); //CC1101 register config
SpiEnd();
Reset(); //CC1101 reset
RegConfigSettings(); //CC1101 register config
SpiEnd(); //Stops SPI Transaction
}
/****************************************************************
*FUNCTION NAME:SpiWriteReg
Expand All @@ -175,12 +230,11 @@ void ELECHOUSE_CC1101::Init(void)
void ELECHOUSE_CC1101::SpiWriteReg(byte addr, byte value)
{
SpiStart();
digitalWrite(SS_PIN, LOW);
while(digitalRead(MISO_PIN));
SPI.transfer(addr);
SPI.transfer(value);
digitalWrite(SS_PIN, HIGH);
cc_spi->transfer(addr);
cc_spi->transfer(value);
SpiEnd();
DEBUG_CC1101("Write reg addr: 0x" + String(addr,HEX) + "=0x" + String(value,HEX));
}
/****************************************************************
*FUNCTION NAME:SpiWriteBurstReg
Expand All @@ -193,14 +247,15 @@ 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);
DEBUG_CC11012("\nWrite burst addr: 0x" + String(temp,HEX) + "=");
cc_spi->transfer(temp);
for (i = 0; i < num; i++)
{
SPI.transfer(buffer[i]);
cc_spi->transfer(buffer[i]);
DEBUG_CC11012(" 0x" + String(buffer[i],HEX));
}
digitalWrite(SS_PIN, HIGH);
DEBUG_CC1101()
SpiEnd();
}
/****************************************************************
Expand All @@ -212,11 +267,10 @@ void ELECHOUSE_CC1101::SpiWriteBurstReg(byte addr, byte *buffer, byte num)
void ELECHOUSE_CC1101::SpiStrobe(byte strobe)
{
SpiStart();
digitalWrite(SS_PIN, LOW);
while(digitalRead(MISO_PIN));
SPI.transfer(strobe);
digitalWrite(SS_PIN, HIGH);
cc_spi->transfer(strobe);
SpiEnd();
DEBUG_CC1101("Write Strobe: 0x" + String(strobe,HEX));
}
/****************************************************************
*FUNCTION NAME:SpiReadReg
Expand All @@ -229,12 +283,11 @@ byte ELECHOUSE_CC1101::SpiReadReg(byte addr)
byte temp, value;
SpiStart();
temp = addr| READ_SINGLE;
digitalWrite(SS_PIN, LOW);
while(digitalRead(MISO_PIN));
SPI.transfer(temp);
value=SPI.transfer(0);
digitalWrite(SS_PIN, HIGH);
cc_spi->transfer(temp);
value=cc_spi->transfer(0);
SpiEnd();
DEBUG_CC1101("Reading addr: 0x" + String(addr,HEX) + " = 0x" + String(value,HEX));
return value;
}

Expand All @@ -249,14 +302,15 @@ 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);
DEBUG_CC11012("\nReading addr: 0x" + String(temp,HEX) + " = ");
cc_spi->transfer(temp);
for(i=0;i<num;i++)
{
buffer[i]=SPI.transfer(0);
buffer[i]=cc_spi->transfer(0);
DEBUG_CC11012(" " + String(buffer[i],HEX));
}
digitalWrite(SS_PIN, HIGH);
DEBUG_CC1101()
SpiEnd();
}

Expand All @@ -271,12 +325,11 @@ byte ELECHOUSE_CC1101::SpiReadStatus(byte addr)
byte value,temp;
SpiStart();
temp = addr | READ_BURST;
digitalWrite(SS_PIN, LOW);
while(digitalRead(MISO_PIN));
SPI.transfer(temp);
value=SPI.transfer(0);
digitalWrite(SS_PIN, HIGH);
cc_spi->transfer(temp);
value=cc_spi->transfer(0);
SpiEnd();
DEBUG_CC1101("Reading Reg addr: 0x" + String(addr,HEX) + " = 0x" + String(value,HEX));
return value;
}
/****************************************************************
Expand All @@ -286,7 +339,7 @@ byte ELECHOUSE_CC1101::SpiReadStatus(byte addr)
*OUTPUT :none
****************************************************************/
void ELECHOUSE_CC1101::setSpi(void){
if (spi == 0){
if (__spi == 0){
#if defined __AVR_ATmega168__ || defined __AVR_ATmega328P__
SCK_PIN = 13; MISO_PIN = 12; MOSI_PIN = 11; SS_PIN = 10;
#elif defined __AVR_ATmega1280__ || defined __AVR_ATmega2560__
Expand All @@ -307,7 +360,7 @@ void ELECHOUSE_CC1101::setSpi(void){
*OUTPUT :none
****************************************************************/
void ELECHOUSE_CC1101::setSpiPin(byte sck, byte miso, byte mosi, byte ss){
spi = 1;
__spi = 1;
SCK_PIN = sck;
MISO_PIN = miso;
MOSI_PIN = mosi;
Expand All @@ -320,7 +373,7 @@ void ELECHOUSE_CC1101::setSpiPin(byte sck, byte miso, byte mosi, byte ss){
*OUTPUT :none
****************************************************************/
void ELECHOUSE_CC1101::addSpiPin(byte sck, byte miso, byte mosi, byte ss, byte modul){
spi = 1;
__spi = 1;
SCK_PIN_M[modul] = sck;
MISO_PIN_M[modul] = miso;
MOSI_PIN_M[modul] = mosi;
Expand Down Expand Up @@ -615,11 +668,14 @@ clb4[1]=e;
*OUTPUT :none
****************************************************************/
bool ELECHOUSE_CC1101::getCC1101(void){
setSpi();
if (SpiReadStatus(0x31)>0){
return 1;
}else{
return 0;
setSpi();
byte val=SpiReadStatus(0x31);
if (val>0){
DEBUG_CC1101("getCC1101 result: 0x" + String(val,HEX) + " -> Ok");
return 1;
}else{
DEBUG_CC1101("getCC1101 result: 0x" + String(val,HEX) + " -> Not Found");
return 0;
}
}
/****************************************************************
Expand Down
22 changes: 22 additions & 0 deletions ELECHOUSE_CC1101_SRC_DRV.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,22 @@ cc1101 Driver for RC Switch. Mod by Little Satan. With permission to modify and
#ifndef ELECHOUSE_CC1101_SRC_DRV_h
#define ELECHOUSE_CC1101_SRC_DRV_h

//********************* DEBUG Macros ********************/

// Uncomment line below to have Serial messagens about the transactions
//#define CC1101_LIB_DEBUG
#if defined(CC1101_LIB_DEBUG)
#define DEBUG_CC1101(var) Serial.println(var);
#define DEBUG_CC11012(var) Serial.print(var);
#else
#define DEBUG_CC1101(var) yield();
#define DEBUG_CC11012(var) yield();
#endif

//********************* DEBUG Macros ********************/

#include <Arduino.h>
#include <SPI.h>

//***************************************CC1101 define**************************************************//
// CC1101 CONFIG REGSITER
Expand Down Expand Up @@ -127,9 +142,16 @@ class ELECHOUSE_CC1101
void Split_MDMCFG1(void);
void Split_MDMCFG2(void);
void Split_MDMCFG4(void);
SPIClass _cc_spi;
SPIClass* cc_spi=nullptr;
bool _begin_end_logic=false;
public:
void Init(void);
byte SpiReadStatus(byte addr);
void setBeginEndLogic(bool state);
bool getBeginEndLogic();
void setSPIinstance(SPIClass* sspi);
SPIClass* getSPIinstance();
void setSpiPin(byte sck, byte miso, byte mosi, byte ss);
void addSpiPin(byte sck, byte miso, byte mosi, byte ss, byte modul);
void setGDO(byte gdo0, byte gdo2);
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ The most important functions at a glance:

ELECHOUSE_cc1101.Init(); //Initialize the cc1101. Must be set first!

ELECHOUSE_cc1101.setBeginEndLogic(value) // Set the same logic of begin and end the SPI instance, like the original state

ELECHOUSE_cc1101.getBeginEndLogic() // Gives the actual mode of operation

ELECHOUSE_cc1101.setSPIinstance(&spi_instance); // Used to share SPI bus with other devices (tft, SDCard or other devices)
// to share SPI cus with a display (TFT_eSPI), use ELECHOUSE_cc1101.setSPIinstance(&tft.getSPIinstance());
// if sharing with TFT_eSPI device, is neede to call some SPI Function to "release" the CC1101, like tft.
// like: tft.drawPixel(0,0,0); after finishing setting the CC1101.
// if none is set, it will use this lib instance, instead

ELECHOUSE_cc1101.getSPIinstance(); // Gets the SPI Instance to use with other object, like SDCard or other thing

ELECHOUSE_cc1101.setPA(PA); //Set transmission power.

ELECHOUSE_cc1101.setMHZ(MHZ); //Set the basic frequency.
Expand Down