Skip to content
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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 <SC16IS750.h>
```
with
```c++
#include "SC16IS750.h"
```
and place the file next to the .ino project file
10 changes: 0 additions & 10 deletions README.txt

This file was deleted.

59 changes: 47 additions & 12 deletions SC16IS750.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Please keep the above information when you use this code in your project.


//#define SC16IS750_DEBUG_PRINT
#include <SC16IS750.h>
#include "SC16IS750.h"
#include <SPI.h>
#include <Wire.h>

Expand All @@ -33,23 +33,27 @@ 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,
unsigned long freq)
{
protocol = prtcl;
if ( protocol == SC16IS750_PROTOCOL_I2C ) {
if ( protocol == SC16IS750_ComProtocol::I2C ) {
device_address_sspin = (addr_sspin>>1);
} else {
device_address_sspin = addr_sspin;
}
peek_flag = 0;
// timeout = 1000;

crystalFreq = freq;
}


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 {
Expand Down Expand Up @@ -88,6 +92,36 @@ 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
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)
Expand All @@ -108,15 +142,16 @@ 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

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_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));
Expand All @@ -131,7 +166,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);
Expand Down Expand Up @@ -163,7 +198,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;
Expand All @@ -176,7 +211,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: ");
Expand Down Expand Up @@ -280,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;
Expand Down
36 changes: 23 additions & 13 deletions SC16IS750.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,33 @@ 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)
#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);

//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);
Expand All @@ -133,7 +142,8 @@ class SC16IS750 : public Stream

private:
uint8_t device_address_sspin;
uint8_t protocol;
SC16IS750_ComProtocol protocol;
unsigned long crystalFreq = 12000000UL;
// uint32_t timeout;
int16_t SetBaudrate(uint32_t baudrate);
uint8_t ReadRegister(uint8_t reg_addr);
Expand Down