From fcd44d44401e3bc03764468a6ced001331b88b8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Grome=C5=A1?= Date: Fri, 13 Jul 2018 15:36:01 +0200 Subject: [PATCH] SX1231 - Implemented module --- examples/SX1231_Receive/SX1231_Receive.ino | 78 ++++++++++++++++++ examples/SX1231_Transmit/SX1231_Transmit.ino | 72 +++++++++++++++++ keywords.txt | 1 + src/KiteLib.h | 1 + src/modules/SX1231.cpp | 84 ++++++++++++++++++++ src/modules/SX1231.h | 24 ++++++ 6 files changed, 260 insertions(+) create mode 100644 examples/SX1231_Receive/SX1231_Receive.ino create mode 100644 examples/SX1231_Transmit/SX1231_Transmit.ino create mode 100644 src/modules/SX1231.cpp create mode 100644 src/modules/SX1231.h diff --git a/examples/SX1231_Receive/SX1231_Receive.ino b/examples/SX1231_Receive/SX1231_Receive.ino new file mode 100644 index 00000000..bf941ad0 --- /dev/null +++ b/examples/SX1231_Receive/SX1231_Receive.ino @@ -0,0 +1,78 @@ +/* + * KiteLib SX1231 Receive Example + * + * This example receives packets using SX1231 FSK radio module. + */ + +// include the library +#include + +// SX1231 module is in slot A on the shield +SX1231 rf = Kite.ModuleA; + +void setup() { + Serial.begin(9600); + + // initialize SX1231 with default settings + Serial.print(F("[SX1231] Initializing ... ")); + // carrier frequency: 434.0 MHz + // bit rate: 48.0 kbps + // Rx bandwidth: 125.0 kHz + // frequency deviation: 50.0 kHz + // output power: 13 dBm + // sync word: 0x2D 0x01 + byte state = rf.begin(); + if(state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code 0x")); + Serial.println(state, HEX); + while(true); + } + + // you can change the sync word at runtime + // sync word can be up to 8 non-zero bytes + Serial.print(F("[SX1231] Settings sync word ... ")); + uint8_t syncWord[] = {0x01, 0x23}; + // sync word: 0x01 0x23 + // length: 2 + // tolerated error bits: 0 + state == rf.setSyncWord(syncWord, 2); + if(state == ERR_NONE) { + Serial.println(F("success!")); + } else if(state == ERR_INVALID_SYNC_WORD) { + Serial.println(F("invalid!")); + } +} + +void loop() { + Serial.print(F("[SX1231] Waiting for incoming transmission ... ")); + + // you can receive data as an Arduino String + String str; + byte state = rf.receive(str); + + // you can also receive data as byte array + /* + byte byteArr[8]; + byte state = rf.receive(byteArr, 8); + */ + + if(state == ERR_NONE) { + // packet was successfully received + Serial.println(F("success!")); + + // print the data of the packet + Serial.print(F("[SX1231] Data:\t\t")); + Serial.println(str); + + } else if(state == ERR_RX_TIMEOUT) { + // timeout occurred while waiting for a packet + Serial.println(F("timeout!")); + + } else if(state == ERR_CRC_MISMATCH) { + // packet was received, but is malformed + Serial.println(F("CRC error!")); + + } +} diff --git a/examples/SX1231_Transmit/SX1231_Transmit.ino b/examples/SX1231_Transmit/SX1231_Transmit.ino new file mode 100644 index 00000000..5a9ae1bc --- /dev/null +++ b/examples/SX1231_Transmit/SX1231_Transmit.ino @@ -0,0 +1,72 @@ +/* + * KiteLib SX1231 Transmit Example + * + * This example transmits packets using SX1231 FSK radio module. + */ + +// include the library +#include + +// SX1231 module is in slot A on the shield +SX1231 rf = Kite.ModuleA; + +void setup() { + Serial.begin(9600); + + // initialize SX1231 with default settings + Serial.print(F("[SX1231] Initializing ... ")); + // carrier frequency: 434.0 MHz + // bit rate: 48.0 kbps + // Rx bandwidth: 125.0 kHz + // frequency deviation: 50.0 kHz + // output power: 13 dBm + // sync word: 0x2D 0x01 + byte state = rf.begin(); + if(state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code 0x")); + Serial.println(state, HEX); + while(true); + } + + // you can change the sync word at runtime + // sync word can be up to 8 non-zero bytes + Serial.print(F("[SX1231] Settings sync word ... ")); + uint8_t syncWord[] = {0x01, 0x23}; + // sync word: 0x01 0x23 + // length: 2 + // tolerated error bits: 0 + state == rf.setSyncWord(syncWord, 2); + if(state == ERR_NONE) { + Serial.println(F("success!")); + } else if(state == ERR_INVALID_SYNC_WORD) { + Serial.println(F("invalid!")); + } +} + +void loop() { + Serial.print(F("[SX1231] Transmitting packet ... ")); + + // you can transmit C-string or Arduino string up to 256 characters long + byte state = rf.transmit("Hello World!"); + + // you can also transmit byte array up to 256 bytes long + /* + byte byteArr[] = {0x01, 0x23, 0x45, 0x56, 0x78, 0xAB, 0xCD, 0xEF}; + byte state = rf.transmit(byteArr, 8); + */ + + if(state == ERR_NONE) { + // the packet was successfully transmitted + Serial.println(" success!"); + + } else if(state == ERR_PACKET_TOO_LONG) { + // the supplied packet was longer than 256 bytes + Serial.println(" too long!"); + + } + + // wait for a second before transmitting again + delay(1000); +} diff --git a/keywords.txt b/keywords.txt index 00c99167..29819865 100644 --- a/keywords.txt +++ b/keywords.txt @@ -13,6 +13,7 @@ Module KEYWORD1 ESP8266 KEYWORD1 HC05 KEYWORD1 RF69 KEYWORD1 +SX1231 KEYWORD1 SX1272 KEYWORD1 SX1273 KEYWORD1 SX1276 KEYWORD1 diff --git a/src/KiteLib.h b/src/KiteLib.h index b426bf26..6e94809c 100644 --- a/src/KiteLib.h +++ b/src/KiteLib.h @@ -7,6 +7,7 @@ #include "modules/ESP8266.h" #include "modules/HC05.h" #include "modules/RF69.h" +#include "modules/SX1231.h" #include "modules/SX1272.h" #include "modules/SX1273.h" #include "modules/SX1276.h" diff --git a/src/modules/SX1231.cpp b/src/modules/SX1231.cpp new file mode 100644 index 00000000..2408d9fc --- /dev/null +++ b/src/modules/SX1231.cpp @@ -0,0 +1,84 @@ +#include "SX1231.h" + +SX1231::SX1231(Module* mod) : RF69(mod) { + +} + +uint8_t SX1231::begin(float freq = 434.0, float br = 48.0, float rxBw = 125.0, float freqDev = 50.0, int8_t power = 13) { + // set module properties + _mod->init(USE_SPI, INT_BOTH); + + // try to find the SX1231 chip + uint8_t i = 0; + bool flagFound = false; + while((i < 10) && !flagFound) { + uint8_t version = _mod->SPIreadRegister(RF69_REG_VERSION); + if((version == 0x21) || (version == 0x22) || (version == 0x23)) { + flagFound = true; + _chipRevision = version; + } else { + #ifdef KITELIB_DEBUG + Serial.print("SX1231 not found! ("); + Serial.print(i + 1); + Serial.print(" of 10 tries) RF69_REG_VERSION == "); + + char buffHex[5]; + sprintf(buffHex, "0x%02X", version); + Serial.print(buffHex); + Serial.println(); + #endif + delay(1000); + i++; + } + } + + if(!flagFound) { + DEBUG_PRINTLN_STR("No SX1231 found!"); + SPI.end(); + return(ERR_CHIP_NOT_FOUND); + } else { + DEBUG_PRINTLN_STR("Found SX1231!"); + } + + // configure settings not accessible by API + uint8_t state = config(); + if(state != ERR_NONE) { + return(state); + } + + // configure publicly accessible settings + state = setFrequency(freq); + if(state != ERR_NONE) { + return(state); + } + + _rxBw = 125.0; + state = setBitRate(br); + if(state != ERR_NONE) { + return(state); + } + + state = setRxBandwidth(rxBw); + if(state != ERR_NONE) { + return(state); + } + + state = setFrequencyDeviation(freqDev); + if(state != ERR_NONE) { + return(state); + } + + state = setOutputPower(power); + if(state != ERR_NONE) { + return(state); + } + + // default sync word values 0x2D01 is the same as the default in LowPowerLab RFM69 library + uint8_t syncWord[] = {0x2D, 0x01}; + state = setSyncWord(syncWord, 2); + if(state != ERR_NONE) { + return(state); + } + + return(ERR_NONE); +} diff --git a/src/modules/SX1231.h b/src/modules/SX1231.h new file mode 100644 index 00000000..bdf9391d --- /dev/null +++ b/src/modules/SX1231.h @@ -0,0 +1,24 @@ +#ifndef _KITELIB_SX1231_H +#define _KITELIB_SX1231_H + +#include "TypeDef.h" +#include "Module.h" +#include "RF69.h" + +#define SX1231_CHIP_REVISION_2_A 0x21 +#define SX1231_CHIP_REVISION_2_B 0x22 +#define SX1231_CHIP_REVISION_2_C 0x23 + +class SX1231: public RF69 { + public: + // constructor + SX1231(Module* mod); + + // basic methods + uint8_t begin(float freq = 434.0, float br = 48.0, float rxBw = 125.0, float freqDev = 50.0, int8_t power = 13); + + private: + uint8_t _chipRevision; +}; + +#endif