From 3cef3ce60dae87b327516f21168d3fd9e5f5373d Mon Sep 17 00:00:00 2001 From: Simon Kolotov Date: Mon, 5 Aug 2019 14:42:45 -0700 Subject: [PATCH 1/5] changed README format + added instructions for not installing the library --- README.md | 24 ++++++++++++++++++++++++ README.txt | 10 ---------- 2 files changed, 24 insertions(+), 10 deletions(-) create mode 100644 README.md delete mode 100644 README.txt diff --git a/README.md b/README.md new file mode 100644 index 0000000..849e884 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +# Arduino Library for Sandbox Electronics I2C/SPI to UART Bridge Module [MOD-000020] +================================================================================ + +This module is available at http://sandboxelectronics.com/?product=sc16is750-i2cspi-to-uart-bridge-module + +The installed library should be under: + - My Documents\Arduino\libraries\ (on Windows) + - Documents/Arduino/libraries/ (on Mac or Linux) + +Note: the above instruction is for automatic installation of 3rd party libraries that is supported starting at Arduino IDE 1.0.5. +For users running earlier versions, manual installation is required. For manual installation instruction, +please visit http://arduino.cc/en/Guide/Libraries. +Please remember to close all opened Arduino IDE windows and restart the Arduino IDE if manual installation was used. +Please make sure the new library appears in the Sketch -> Import Library menu item of the software. + +Alternatively, replace the +```c++ +#include +``` +with +```c++ +#include "SC16IS750.h" +``` +and place the file next to the .ino project file \ No newline at end of file diff --git a/README.txt b/README.txt deleted file mode 100644 index 7176e77..0000000 --- a/README.txt +++ /dev/null @@ -1,10 +0,0 @@ -Arduino Library for Sandbox Electronics I2C/SPI to UART Bridge Module [MOD-000020] -================================================================================== - -This module is available at http://sandboxelectronics.com/?product=sc16is750-i2cspi-to-uart-bridge-module - -The installed library should be under: - - My Documents\Arduino\libraries\ (on Windows) - - Documents/Arduino/libraries/ (on Mac or Linux) - -Note: the above instruction is for automatic installation of 3rd party libraries that is supported starting at Arduino IDE 1.0.5. For users running earlier versions, manual installation is required. For manual installation instruction, please visit http://arduino.cc/en/Guide/Libraries. Please remember to close all opened Arduino IDE windows and restart the Arduino IDE if manual installation was used. Please make sure the new library appears in the Sketch -> Import Library menu item of the software. From 58ff065e0d253975de43eb011e82fe28f84439ba Mon Sep 17 00:00:00 2001 From: Simon Kolotov Date: Mon, 5 Aug 2019 14:57:07 -0700 Subject: [PATCH 2/5] Protocol is now an enum rather than a define --- SC16IS750.cpp | 12 ++++++------ SC16IS750.h | 11 ++++------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/SC16IS750.cpp b/SC16IS750.cpp index 11a5cf9..7c89b6a 100644 --- a/SC16IS750.cpp +++ b/SC16IS750.cpp @@ -33,10 +33,10 @@ Please keep the above information when you use this code in your project. #endif -SC16IS750::SC16IS750(uint8_t prtcl, uint8_t addr_sspin) +SC16IS750::SC16IS750(SC16IS750_ComProtocol prtcl, uint8_t addr_sspin) { protocol = prtcl; - if ( protocol == SC16IS750_PROTOCOL_I2C ) { + if ( protocol == SC16IS750_ComProtocol::I2C ) { device_address_sspin = (addr_sspin>>1); } else { device_address_sspin = addr_sspin; @@ -49,7 +49,7 @@ SC16IS750::SC16IS750(uint8_t prtcl, uint8_t addr_sspin) void SC16IS750::begin(uint32_t baud) { //Serial.println("1111111111111111"); - if ( protocol == SC16IS750_PROTOCOL_I2C) { + if ( protocol == SC16IS750_ComProtocol::I2C) { //Serial.println("22222222222222"); WIRE.begin(); } else { @@ -109,14 +109,14 @@ uint8_t SC16IS750::digitalRead(uint8_t pin) uint8_t SC16IS750::ReadRegister(uint8_t reg_addr) { uint8_t result; - if ( protocol == SC16IS750_PROTOCOL_I2C ) { // register read operation via I2C + if ( protocol == SC16IS750_ComProtocol::I2C ) { // register read operation via I2C WIRE.beginTransmission(device_address_sspin); WIRE.write((reg_addr<<3)); WIRE.endTransmission(0); WIRE.requestFrom(device_address_sspin,(uint8_t)1); result = WIRE.read(); - } else if (protocol == SC16IS750_PROTOCOL_SPI) { //register read operation via SPI + } else if (protocol == SC16IS750_ComProtocol::SPI) { //register read operation via SPI ::digitalWrite(device_address_sspin, LOW); delayMicroseconds(10); SPI.transfer(0x80|(reg_addr<<3)); @@ -131,7 +131,7 @@ uint8_t SC16IS750::ReadRegister(uint8_t reg_addr) void SC16IS750::WriteRegister(uint8_t reg_addr, uint8_t val) { - if ( protocol == SC16IS750_PROTOCOL_I2C ) { // register read operation via I2C + if ( protocol == SC16IS750_ComProtocol::I2C ) { // register read operation via I2C WIRE.beginTransmission(device_address_sspin); WIRE.write((reg_addr<<3)); WIRE.write(val); diff --git a/SC16IS750.h b/SC16IS750.h index 22b97c8..23eb987 100644 --- a/SC16IS750.h +++ b/SC16IS750.h @@ -100,17 +100,14 @@ Please keep the above information when you use this code in your project. #define SC16IS750_CRYSTCAL_FREQ (14745600UL) //#define SC16IS750_CRYSTCAL_FREQ (1843200UL) //#define SC16IS750_CRYSTCAL_FREQ (16000000UL) -//#define SC16IS750_DEBUG_PRINT (0) -#define SC16IS750_PROTOCOL_I2C (0) -#define SC16IS750_PROTOCOL_SPI (1) - - +//#define SC16IS750_DEBUG_PRINT (0) +enum class SC16IS750_ComProtocol {SPI = 0, I2C = 1}; class SC16IS750 : public Stream { public: - SC16IS750(uint8_t prtcl = SC16IS750_PROTOCOL_I2C, uint8_t addr = SC16IS750_ADDRESS_AD); + SC16IS750(SC16IS750_ComProtocol prtcl = SC16IS750_ComProtocol::I2C, uint8_t addr = SC16IS750_ADDRESS_AD); void begin(uint32_t baud); int read(); size_t write(uint8_t val); @@ -133,7 +130,7 @@ class SC16IS750 : public Stream private: uint8_t device_address_sspin; - uint8_t protocol; + SC16IS750_ComProtocol protocol; // uint32_t timeout; int16_t SetBaudrate(uint32_t baudrate); uint8_t ReadRegister(uint8_t reg_addr); From 5e1f26db7a4f47991f49a7aeeba020cb2b738236 Mon Sep 17 00:00:00 2001 From: Simon Kolotov Date: Mon, 5 Aug 2019 14:58:01 -0700 Subject: [PATCH 3/5] Crystal frequency is now a property rather than a define --- SC16IS750.cpp | 10 +++++++--- SC16IS750.h | 9 +++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/SC16IS750.cpp b/SC16IS750.cpp index 7c89b6a..1cf07c2 100644 --- a/SC16IS750.cpp +++ b/SC16IS750.cpp @@ -33,7 +33,9 @@ Please keep the above information when you use this code in your project. #endif -SC16IS750::SC16IS750(SC16IS750_ComProtocol prtcl, uint8_t addr_sspin) +SC16IS750::SC16IS750(SC16IS750_ComProtocol prtcl, + uint8_t addr_sspin, + unsigned long freq) { protocol = prtcl; if ( protocol == SC16IS750_ComProtocol::I2C ) { @@ -43,6 +45,8 @@ SC16IS750::SC16IS750(SC16IS750_ComProtocol prtcl, uint8_t addr_sspin) } peek_flag = 0; // timeout = 1000; + + crystalFreq = freq; } @@ -163,7 +167,7 @@ int16_t SC16IS750::SetBaudrate(uint32_t baudrate) //return error of baudrate par prescaler = 4; } - divisor = (SC16IS750_CRYSTCAL_FREQ/prescaler)/(baudrate*16); + divisor = (crystalFreq/prescaler)/(baudrate*16); temp_lcr = ReadRegister(SC16IS750_REG_LCR); temp_lcr |= 0x80; @@ -176,7 +180,7 @@ int16_t SC16IS750::SetBaudrate(uint32_t baudrate) //return error of baudrate par WriteRegister(SC16IS750_REG_LCR,temp_lcr); - actual_baudrate = (SC16IS750_CRYSTCAL_FREQ/prescaler)/(16*divisor); + actual_baudrate = (crystalFreq/prescaler)/(16*divisor); error = ((float)actual_baudrate-baudrate)*1000/baudrate; #ifdef SC16IS750_DEBUG_PRINT Serial.print("Desired baudrate: "); diff --git a/SC16IS750.h b/SC16IS750.h index 23eb987..eb27f24 100644 --- a/SC16IS750.h +++ b/SC16IS750.h @@ -97,9 +97,6 @@ Please keep the above information when you use this code in your project. //Application Related -#define SC16IS750_CRYSTCAL_FREQ (14745600UL) -//#define SC16IS750_CRYSTCAL_FREQ (1843200UL) -//#define SC16IS750_CRYSTCAL_FREQ (16000000UL) //#define SC16IS750_DEBUG_PRINT (0) enum class SC16IS750_ComProtocol {SPI = 0, I2C = 1}; @@ -107,7 +104,10 @@ enum class SC16IS750_ComProtocol {SPI = 0, I2C = 1}; class SC16IS750 : public Stream { public: - SC16IS750(SC16IS750_ComProtocol prtcl = SC16IS750_ComProtocol::I2C, uint8_t addr = SC16IS750_ADDRESS_AD); + SC16IS750(SC16IS750_ComProtocol prtcl = SC16IS750_ComProtocol::I2C, + uint8_t addr = SC16IS750_ADDRESS_AD, + unsigned long freq = 12000000UL); + void begin(uint32_t baud); int read(); size_t write(uint8_t val); @@ -131,6 +131,7 @@ class SC16IS750 : public Stream private: uint8_t device_address_sspin; SC16IS750_ComProtocol protocol; + unsigned long crystalFreq = 12000000UL; // uint32_t timeout; int16_t SetBaudrate(uint32_t baudrate); uint8_t ReadRegister(uint8_t reg_addr); From 77a7aed8306a2ab17daa2055c728ec67015a41d3 Mon Sep 17 00:00:00 2001 From: Simon Kolotov Date: Mon, 5 Aug 2019 15:03:21 -0700 Subject: [PATCH 4/5] Added writeString and readString capability --- SC16IS750.cpp | 30 +++++++++++++++++++++++++++++- SC16IS750.h | 18 +++++++++++++++--- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/SC16IS750.cpp b/SC16IS750.cpp index 1cf07c2..1fe953f 100644 --- a/SC16IS750.cpp +++ b/SC16IS750.cpp @@ -21,7 +21,7 @@ Please keep the above information when you use this code in your project. //#define SC16IS750_DEBUG_PRINT -#include +#include "SC16IS750.h" #include #include @@ -94,6 +94,34 @@ size_t SC16IS750::write(uint8_t val) WriteByte(val); } +//NB: any character after the 64th will be discarded by the FIFO buffer +void SC16IS750::writeString(const char* str) +{ + for (const char* ch = str; *ch != '\0'; ch++) + write((size_t)(*ch)); //write value to device +} + +//NB: we're limited to 64 chars (bytes) by the size of the FIFO buffer +//This function reads into a preallocated char[] to save on dynamic memory allocations +void SC16IS750::readString() +{ + int i = 0; + while (i < 64) + { + int datum = read(); + + if (datum == -1) + { + rxData[i] = 0; + break; //no more data + } + + else + rxData[i++] = (char)datum; //append to string + + }; +} + void SC16IS750::pinMode(uint8_t pin, uint8_t i_o) { GPIOSetPinMode(pin, i_o); diff --git a/SC16IS750.h b/SC16IS750.h index eb27f24..d770d80 100644 --- a/SC16IS750.h +++ b/SC16IS750.h @@ -104,14 +104,26 @@ enum class SC16IS750_ComProtocol {SPI = 0, I2C = 1}; class SC16IS750 : public Stream { public: + + //NB: we're limited to 64 chars (bytes) by the size of the FIFO buffer + char rxData[64]; //this uses a preallocated char array for reading strings from the bridge + SC16IS750(SC16IS750_ComProtocol prtcl = SC16IS750_ComProtocol::I2C, uint8_t addr = SC16IS750_ADDRESS_AD, unsigned long freq = 12000000UL); void begin(uint32_t baud); - int read(); - size_t write(uint8_t val); - int available(); + + //Read/Write single values + int read(); + size_t write(uint8_t val); + + //Read/Write strings + //NB: limited to 64 bytes (characters) long string - FIFO buffer limit + void writeString(const char* str); + void readString(); + + int available(); void pinMode(uint8_t pin, uint8_t io); void digitalWrite(uint8_t pin, uint8_t value); uint8_t digitalRead(uint8_t pin); From 6173ba9a7c2685474e2a6fd6cd3fa44e22229b5e Mon Sep 17 00:00:00 2001 From: Simon Kolotov Date: Mon, 5 Aug 2019 15:21:38 -0700 Subject: [PATCH 5/5] clearing up compilation warnings --- SC16IS750.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/SC16IS750.cpp b/SC16IS750.cpp index 1fe953f..5b10afd 100644 --- a/SC16IS750.cpp +++ b/SC16IS750.cpp @@ -92,6 +92,8 @@ int SC16IS750::read(void) size_t SC16IS750::write(uint8_t val) { WriteByte(val); + + return 1; //we just wrote one byte } //NB: any character after the 64th will be discarded by the FIFO buffer @@ -140,15 +142,16 @@ uint8_t SC16IS750::digitalRead(uint8_t pin) uint8_t SC16IS750::ReadRegister(uint8_t reg_addr) { - uint8_t result; - if ( protocol == SC16IS750_ComProtocol::I2C ) { // register read operation via I2C - + uint8_t result = -1; //default value to clear warning - in fact we don't support other protocols + + if ( protocol == SC16IS750_ComProtocol::I2C ){ // register read operation via I2C WIRE.beginTransmission(device_address_sspin); WIRE.write((reg_addr<<3)); WIRE.endTransmission(0); WIRE.requestFrom(device_address_sspin,(uint8_t)1); result = WIRE.read(); - } else if (protocol == SC16IS750_ComProtocol::SPI) { //register read operation via SPI + + } else if (protocol == SC16IS750_ComProtocol::SPI) { //register read operation via SPI ::digitalWrite(device_address_sspin, LOW); delayMicroseconds(10); SPI.transfer(0x80|(reg_addr<<3)); @@ -312,7 +315,7 @@ uint8_t SC16IS750::GPIOGetPinState(uint8_t pin_number) uint8_t temp_iostate; temp_iostate = ReadRegister(SC16IS750_REG_IOSTATE); - if ( temp_iostate & (0x01 << pin_number)== 0 ) { + if ( temp_iostate & ((0x01 << pin_number)== 0) ) { return 0; } return 1;