├── README.txt ├── examples ├── I2CSELFTEST │ └── I2CSELFTEST.ino └── SPISELFTEST │ └── SPISELFTEST.ino ├── SC16IS750.h └── SC16IS750.cpp /README.txt: -------------------------------------------------------------------------------- 1 | Arduino Library for Sandbox Electronics I2C/SPI to UART Bridge Module [MOD-000020] 2 | ================================================================================== 3 | 4 | This module is available at http://sandboxelectronics.com/?product=sc16is750-i2cspi-to-uart-bridge-module 5 | 6 | The installed library should be under: 7 | - My Documents\Arduino\libraries\ (on Windows) 8 | - Documents/Arduino/libraries/ (on Mac or Linux) 9 | 10 | 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. 11 | -------------------------------------------------------------------------------- /examples/I2CSELFTEST/I2CSELFTEST.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | SC16IS750 i2cuart = SC16IS750(SC16IS750_PROTOCOL_I2C,SC16IS750_ADDRESS_AD); 7 | 8 | //Connect TX and RX with a wire and run this sketch 9 | 10 | void setup() 11 | { 12 | Serial.begin(9600); 13 | 14 | // UART to Serial Bridge Initialization 15 | i2cuart.begin(9600); //baudrate setting 16 | if (i2cuart.ping()!=1) { 17 | Serial.println("device not found"); 18 | while(1); 19 | } else { 20 | Serial.println("device found"); 21 | } 22 | Serial.println("start serial communication"); 23 | 24 | 25 | 26 | }; 27 | 28 | void loop() 29 | { 30 | 31 | i2cuart.write(0x55); 32 | while(i2cuart.available()==0); 33 | if (i2cuart.read()!=0x55) { 34 | Serial.println("serial communication error"); 35 | while(1); 36 | } 37 | delay(200); 38 | 39 | i2cuart.write(0xAA); 40 | while(i2cuart.available()==0); 41 | if (i2cuart.read()!=0xAA) { 42 | Serial.println("serial communication error"); 43 | while(1); 44 | } 45 | 46 | delay(200); 47 | 48 | 49 | 50 | 51 | 52 | 53 | }; 54 | 55 | -------------------------------------------------------------------------------- /examples/SPISELFTEST/SPISELFTEST.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | SC16IS750 spiuart = SC16IS750(SC16IS750_PROTOCOL_SPI,6); 8 | 9 | //Connect TX and RX with a wire and run this sketch 10 | //Remove A0, A1 resistors which set the I2C address 11 | //Remove SCL pull up resistors if you are using Duemilanove 12 | //Pin 6 should be connected to CS of the module. 13 | void setup() 14 | { 15 | //delay(500); 16 | Serial.begin(9600); 17 | Serial.println("Start testing"); 18 | // UART to Serial Bridge Initialization 19 | spiuart.begin(9600); //baudrate setting 20 | Serial.println("BAUDRATE SET"); 21 | if (spiuart.ping()!=1) { 22 | Serial.println("Device not found"); 23 | while(1); 24 | } else { 25 | Serial.println("Device found"); 26 | } 27 | Serial.println("Start serial communication"); 28 | }; 29 | 30 | void loop() 31 | { 32 | 33 | spiuart.write(0x55); 34 | delay(10); 35 | if (spiuart.available()==0) { 36 | Serial.println("Please connnect TX and RX with a wire and reset your Arduino"); 37 | while(1); 38 | } 39 | if (spiuart.read()!=0x55) { 40 | Serial.println("Serial communication error"); 41 | while(1); 42 | } 43 | delay(200); 44 | 45 | spiuart.write(0xAA); 46 | delay(10); 47 | if (spiuart.available()==0) { 48 | Serial.println("Please connnect TX and RX with a wire and reset your Arduino"); 49 | while(1); 50 | } 51 | if (spiuart.read()!=0xAA) { 52 | Serial.println("Serial communication error"); 53 | while(1); 54 | } 55 | 56 | delay(200); 57 | 58 | 59 | 60 | }; 61 | 62 | -------------------------------------------------------------------------------- /SC16IS750.h: -------------------------------------------------------------------------------- 1 | /* 2 | Description: 3 | This is a example code for Sandbox Electronics' I2C/SPI to UART bridge module. 4 | You can get one of those products on 5 | http://sandboxelectronics.com 6 | 7 | Version: 8 | V0.1 9 | 10 | Release Date: 11 | 2014-02-16 12 | 13 | Author: 14 | Tiequan Shao info@sandboxelectronics.com 15 | 16 | Lisence: 17 | CC BY-NC-SA 3.0 18 | 19 | Please keep the above information when you use this code in your project. 20 | */ 21 | 22 | 23 | #ifndef _SC16IS750_H_ 24 | #define _SC16IS750_H_ 25 | 26 | #if ARDUINO >= 100 27 | #include "Arduino.h" 28 | #else 29 | #include "WProgram.h" 30 | #endif 31 | 32 | //Device Address 33 | 34 | //A:VDD 35 | //B:GND 36 | //C:SCL 37 | //D:SDA 38 | #define SC16IS750_ADDRESS_AA (0X90) 39 | #define SC16IS750_ADDRESS_AB (0X92) 40 | #define SC16IS750_ADDRESS_AC (0X94) 41 | #define SC16IS750_ADDRESS_AD (0X96) 42 | #define SC16IS750_ADDRESS_BA (0X98) 43 | #define SC16IS750_ADDRESS_BB (0X9A) 44 | #define SC16IS750_ADDRESS_BC (0X9C) 45 | #define SC16IS750_ADDRESS_BD (0X9E) 46 | #define SC16IS750_ADDRESS_CA (0XA0) 47 | #define SC16IS750_ADDRESS_CB (0XA2) 48 | #define SC16IS750_ADDRESS_CC (0XA4) 49 | #define SC16IS750_ADDRESS_CD (0XA6) 50 | #define SC16IS750_ADDRESS_DA (0XA8) 51 | #define SC16IS750_ADDRESS_DB (0XAA) 52 | #define SC16IS750_ADDRESS_DC (0XAC) 53 | #define SC16IS750_ADDRESS_DD (0XAE) 54 | 55 | 56 | //General Registers 57 | #define SC16IS750_REG_RHR (0x00) 58 | #define SC16IS750_REG_THR (0X00) 59 | #define SC16IS750_REG_IER (0X01) 60 | #define SC16IS750_REG_FCR (0X02) 61 | #define SC16IS750_REG_IIR (0X02) 62 | #define SC16IS750_REG_LCR (0X03) 63 | #define SC16IS750_REG_MCR (0X04) 64 | #define SC16IS750_REG_LSR (0X05) 65 | #define SC16IS750_REG_MSR (0X06) 66 | #define SC16IS750_REG_SPR (0X07) 67 | #define SC16IS750_REG_TCR (0X06) 68 | #define SC16IS750_REG_TLR (0X07) 69 | #define SC16IS750_REG_TXLVL (0X08) 70 | #define SC16IS750_REG_RXLVL (0X09) 71 | #define SC16IS750_REG_IODIR (0X0A) 72 | #define SC16IS750_REG_IOSTATE (0X0B) 73 | #define SC16IS750_REG_IOINTENA (0X0C) 74 | #define SC16IS750_REG_IOCONTROL (0X0E) 75 | #define SC16IS750_REG_EFCR (0X0F) 76 | 77 | //Special Registers 78 | #define SC16IS750_REG_DLL (0x00) 79 | #define SC16IS750_REG_DLH (0X01) 80 | 81 | //Enhanced Registers 82 | #define SC16IS750_REG_EFR (0X02) 83 | #define SC16IS750_REG_XON1 (0X04) 84 | #define SC16IS750_REG_XON2 (0X05) 85 | #define SC16IS750_REG_XOFF1 (0X06) 86 | #define SC16IS750_REG_XOFF2 (0X07) 87 | 88 | // 89 | #define SC16IS750_INT_CTS (0X80) 90 | #define SC16IS750_INT_RTS (0X40) 91 | #define SC16IS750_INT_XOFF (0X20) 92 | #define SC16IS750_INT_SLEEP (0X10) 93 | #define SC16IS750_INT_MODEM (0X08) 94 | #define SC16IS750_INT_LINE (0X04) 95 | #define SC16IS750_INT_THR (0X02) 96 | #define SC16IS750_INT_RHR (0X01) 97 | 98 | //Application Related 99 | 100 | #define SC16IS750_CRYSTCAL_FREQ (14745600UL) 101 | //#define SC16IS750_CRYSTCAL_FREQ (1843200UL) 102 | //#define SC16IS750_CRYSTCAL_FREQ (16000000UL) 103 | //#define SC16IS750_DEBUG_PRINT (0) 104 | #define SC16IS750_PROTOCOL_I2C (0) 105 | #define SC16IS750_PROTOCOL_SPI (1) 106 | 107 | 108 | 109 | 110 | class SC16IS750 : public Stream 111 | { 112 | public: 113 | SC16IS750(uint8_t prtcl = SC16IS750_PROTOCOL_I2C, uint8_t addr = SC16IS750_ADDRESS_AD); 114 | void begin(uint32_t baud); 115 | int read(); 116 | size_t write(uint8_t val); 117 | int available(); 118 | void pinMode(uint8_t pin, uint8_t io); 119 | void digitalWrite(uint8_t pin, uint8_t value); 120 | uint8_t digitalRead(uint8_t pin); 121 | uint8_t ping(); 122 | // void setTimeout(uint32_t); 123 | // size_t readBytes(char *buffer, size_t length); 124 | int peek(); 125 | void flush(); 126 | uint8_t GPIOGetPortState(void); 127 | uint8_t InterruptPendingTest(void); 128 | void SetPinInterrupt(uint8_t io_int_ena); 129 | void InterruptControl(uint8_t int_ena); 130 | void ModemPin(uint8_t gpio); //gpio == 0, gpio[7:4] are modem pins, gpio == 1 gpio[7:4] are gpios 131 | void GPIOLatch(uint8_t latch); 132 | 133 | 134 | private: 135 | uint8_t device_address_sspin; 136 | uint8_t protocol; 137 | // uint32_t timeout; 138 | int16_t SetBaudrate(uint32_t baudrate); 139 | uint8_t ReadRegister(uint8_t reg_addr); 140 | void WriteRegister(uint8_t reg_addr, uint8_t val); 141 | void SetLine(uint8_t data_length, uint8_t parity_select, uint8_t stop_length ); 142 | void GPIOSetPinMode(uint8_t pin_number, uint8_t i_o); 143 | void GPIOSetPinState(uint8_t pin_number, uint8_t pin_state); 144 | 145 | uint8_t GPIOGetPinState(uint8_t pin_number); 146 | void GPIOSetPortMode(uint8_t port_io); 147 | void GPIOSetPortState(uint8_t port_state); 148 | void ResetDevice(void); 149 | 150 | 151 | 152 | void __isr(void); 153 | void FIFOEnable(uint8_t fifo_enable); 154 | void FIFOReset(uint8_t rx_fifo); 155 | void FIFOSetTriggerLevel(uint8_t rx_fifo, uint8_t length); 156 | uint8_t FIFOAvailableData(void); 157 | uint8_t FIFOAvailableSpace(void); 158 | void WriteByte(uint8_t val); 159 | int ReadByte(void); 160 | void EnableTransmit(uint8_t tx_enable); 161 | // int16_t readwithtimeout(); 162 | int peek_buf; 163 | uint8_t peek_flag; 164 | 165 | }; 166 | 167 | #endif 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | -------------------------------------------------------------------------------- /SC16IS750.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Description: 3 | This is a example code for Sandbox Electronics' I2C/SPI to UART bridge module. 4 | You can get one of those products on 5 | http://sandboxelectronics.com 6 | 7 | Version: 8 | V0.1 9 | 10 | Release Date: 11 | 2014-02-16 12 | 13 | Author: 14 | Tiequan Shao info@sandboxelectronics.com 15 | 16 | Lisence: 17 | CC BY-NC-SA 3.0 18 | 19 | Please keep the above information when you use this code in your project. 20 | */ 21 | 22 | 23 | //#define SC16IS750_DEBUG_PRINT 24 | #include 25 | #include 26 | #include 27 | 28 | 29 | #ifdef __AVR__ 30 | #define WIRE Wire 31 | #else // Arduino Due 32 | #define WIRE Wire1 33 | #endif 34 | 35 | 36 | SC16IS750::SC16IS750(uint8_t prtcl, uint8_t addr_sspin) 37 | { 38 | protocol = prtcl; 39 | if ( protocol == SC16IS750_PROTOCOL_I2C ) { 40 | device_address_sspin = (addr_sspin>>1); 41 | } else { 42 | device_address_sspin = addr_sspin; 43 | } 44 | peek_flag = 0; 45 | // timeout = 1000; 46 | } 47 | 48 | 49 | void SC16IS750::begin(uint32_t baud) 50 | { 51 | //Serial.println("1111111111111111"); 52 | if ( protocol == SC16IS750_PROTOCOL_I2C) { 53 | //Serial.println("22222222222222"); 54 | WIRE.begin(); 55 | } else { 56 | //Serial.println("3333333333333333333"); 57 | ::pinMode(device_address_sspin, OUTPUT); 58 | ::digitalWrite(device_address_sspin, HIGH); 59 | SPI.setDataMode(SPI_MODE0); 60 | SPI.setClockDivider(SPI_CLOCK_DIV4); 61 | SPI.setBitOrder(MSBFIRST); 62 | SPI.begin(); 63 | //SPI.setClockDivider(32); 64 | 65 | //Serial.println("4444444444444444444"); 66 | }; 67 | ResetDevice(); 68 | FIFOEnable(1); 69 | SetBaudrate(baud); 70 | SetLine(8,0,1); 71 | } 72 | 73 | int SC16IS750::available(void) 74 | { 75 | return FIFOAvailableData(); 76 | } 77 | 78 | int SC16IS750::read(void) 79 | { 80 | if ( peek_flag == 0) { 81 | return ReadByte(); 82 | } else { 83 | peek_flag = 0; 84 | return peek_buf; 85 | } 86 | } 87 | 88 | size_t SC16IS750::write(uint8_t val) 89 | { 90 | WriteByte(val); 91 | } 92 | 93 | void SC16IS750::pinMode(uint8_t pin, uint8_t i_o) 94 | { 95 | GPIOSetPinMode(pin, i_o); 96 | } 97 | 98 | void SC16IS750::digitalWrite(uint8_t pin, uint8_t value) 99 | { 100 | GPIOSetPinState(pin, value); 101 | } 102 | 103 | uint8_t SC16IS750::digitalRead(uint8_t pin) 104 | { 105 | return GPIOGetPinState(pin); 106 | } 107 | 108 | 109 | uint8_t SC16IS750::ReadRegister(uint8_t reg_addr) 110 | { 111 | uint8_t result; 112 | if ( protocol == SC16IS750_PROTOCOL_I2C ) { // register read operation via I2C 113 | 114 | WIRE.beginTransmission(device_address_sspin); 115 | WIRE.write((reg_addr<<3)); 116 | WIRE.endTransmission(0); 117 | WIRE.requestFrom(device_address_sspin,(uint8_t)1); 118 | result = WIRE.read(); 119 | } else if (protocol == SC16IS750_PROTOCOL_SPI) { //register read operation via SPI 120 | ::digitalWrite(device_address_sspin, LOW); 121 | delayMicroseconds(10); 122 | SPI.transfer(0x80|(reg_addr<<3)); 123 | result = SPI.transfer(0xff); 124 | delayMicroseconds(10); 125 | ::digitalWrite(device_address_sspin, HIGH); 126 | } 127 | 128 | return result; 129 | 130 | } 131 | 132 | void SC16IS750::WriteRegister(uint8_t reg_addr, uint8_t val) 133 | { 134 | if ( protocol == SC16IS750_PROTOCOL_I2C ) { // register read operation via I2C 135 | WIRE.beginTransmission(device_address_sspin); 136 | WIRE.write((reg_addr<<3)); 137 | WIRE.write(val); 138 | WIRE.endTransmission(1); 139 | } else { 140 | ::digitalWrite(device_address_sspin, LOW); 141 | delayMicroseconds(10); 142 | SPI.transfer(reg_addr<<3); 143 | SPI.transfer(val); 144 | delayMicroseconds(10); 145 | ::digitalWrite(device_address_sspin, HIGH); 146 | 147 | } 148 | 149 | 150 | return ; 151 | } 152 | 153 | int16_t SC16IS750::SetBaudrate(uint32_t baudrate) //return error of baudrate parts per thousand 154 | { 155 | uint16_t divisor; 156 | uint8_t prescaler; 157 | uint32_t actual_baudrate; 158 | int16_t error; 159 | uint8_t temp_lcr; 160 | if ( (ReadRegister(SC16IS750_REG_MCR)&0x80) == 0) { //if prescaler==1 161 | prescaler = 1; 162 | } else { 163 | prescaler = 4; 164 | } 165 | 166 | divisor = (SC16IS750_CRYSTCAL_FREQ/prescaler)/(baudrate*16); 167 | 168 | temp_lcr = ReadRegister(SC16IS750_REG_LCR); 169 | temp_lcr |= 0x80; 170 | WriteRegister(SC16IS750_REG_LCR,temp_lcr); 171 | //write to DLL 172 | WriteRegister(SC16IS750_REG_DLL,(uint8_t)divisor); 173 | //write to DLH 174 | WriteRegister(SC16IS750_REG_DLH,(uint8_t)(divisor>>8)); 175 | temp_lcr &= 0x7F; 176 | WriteRegister(SC16IS750_REG_LCR,temp_lcr); 177 | 178 | 179 | actual_baudrate = (SC16IS750_CRYSTCAL_FREQ/prescaler)/(16*divisor); 180 | error = ((float)actual_baudrate-baudrate)*1000/baudrate; 181 | #ifdef SC16IS750_DEBUG_PRINT 182 | Serial.print("Desired baudrate: "); 183 | Serial.println(baudrate,DEC); 184 | Serial.print("Calculated divisor: "); 185 | Serial.println(divisor,DEC); 186 | Serial.print("Actual baudrate: "); 187 | Serial.println(actual_baudrate,DEC); 188 | Serial.print("Baudrate error: "); 189 | Serial.println(error,DEC); 190 | #endif 191 | 192 | return error; 193 | 194 | } 195 | 196 | void SC16IS750::SetLine(uint8_t data_length, uint8_t parity_select, uint8_t stop_length ) 197 | { 198 | uint8_t temp_lcr; 199 | temp_lcr = ReadRegister(SC16IS750_REG_LCR); 200 | temp_lcr &= 0xC0; //Clear the lower six bit of LCR (LCR[0] to LCR[5] 201 | #ifdef SC16IS750_DEBUG_PRINT 202 | Serial.print("LCR Register:0x"); 203 | Serial.println(temp_lcr,DEC); 204 | #endif 205 | switch (data_length) { //data length settings 206 | case 5: 207 | break; 208 | case 6: 209 | temp_lcr |= 0x01; 210 | break; 211 | case 7: 212 | temp_lcr |= 0x02; 213 | break; 214 | case 8: 215 | temp_lcr |= 0x03; 216 | break; 217 | default: 218 | temp_lcr |= 0x03; 219 | break; 220 | } 221 | 222 | if ( stop_length == 2 ) { 223 | temp_lcr |= 0x04; 224 | } 225 | 226 | switch (parity_select) { //parity selection length settings 227 | case 0: //no parity 228 | break; 229 | case 1: //odd parity 230 | temp_lcr |= 0x08; 231 | break; 232 | case 2: //even parity 233 | temp_lcr |= 0x18; 234 | break; 235 | case 3: //force '1' parity 236 | temp_lcr |= 0x03; 237 | break; 238 | case 4: //force '0' parity 239 | break; 240 | default: 241 | break; 242 | } 243 | 244 | WriteRegister(SC16IS750_REG_LCR,temp_lcr); 245 | } 246 | 247 | void SC16IS750::GPIOSetPinMode(uint8_t pin_number, uint8_t i_o) 248 | { 249 | uint8_t temp_iodir; 250 | 251 | temp_iodir = ReadRegister(SC16IS750_REG_IODIR); 252 | if ( i_o == OUTPUT ) { 253 | temp_iodir |= (0x01 << pin_number); 254 | } else { 255 | temp_iodir &= (uint8_t)~(0x01 << pin_number); 256 | } 257 | 258 | WriteRegister(SC16IS750_REG_IODIR, temp_iodir); 259 | return; 260 | } 261 | 262 | void SC16IS750::GPIOSetPinState(uint8_t pin_number, uint8_t pin_state) 263 | { 264 | uint8_t temp_iostate; 265 | 266 | temp_iostate = ReadRegister(SC16IS750_REG_IOSTATE); 267 | if ( pin_state == 1 ) { 268 | temp_iostate |= (0x01 << pin_number); 269 | } else { 270 | temp_iostate &= (uint8_t)~(0x01 << pin_number); 271 | } 272 | 273 | WriteRegister(SC16IS750_REG_IOSTATE, temp_iostate); 274 | return; 275 | } 276 | 277 | 278 | uint8_t SC16IS750::GPIOGetPinState(uint8_t pin_number) 279 | { 280 | uint8_t temp_iostate; 281 | 282 | temp_iostate = ReadRegister(SC16IS750_REG_IOSTATE); 283 | if ( temp_iostate & (0x01 << pin_number)== 0 ) { 284 | return 0; 285 | } 286 | return 1; 287 | } 288 | 289 | uint8_t SC16IS750::GPIOGetPortState(void) 290 | { 291 | 292 | return ReadRegister(SC16IS750_REG_IOSTATE); 293 | 294 | } 295 | 296 | void SC16IS750::GPIOSetPortMode(uint8_t port_io) 297 | { 298 | WriteRegister(SC16IS750_REG_IODIR, port_io); 299 | return; 300 | } 301 | 302 | void SC16IS750::GPIOSetPortState(uint8_t port_state) 303 | { 304 | WriteRegister(SC16IS750_REG_IOSTATE, port_state); 305 | return; 306 | } 307 | 308 | void SC16IS750::SetPinInterrupt(uint8_t io_int_ena) 309 | { 310 | WriteRegister(SC16IS750_REG_IOINTENA, io_int_ena); 311 | return; 312 | } 313 | 314 | void SC16IS750::ResetDevice(void) 315 | { 316 | uint8_t reg; 317 | 318 | reg = ReadRegister(SC16IS750_REG_IOCONTROL); 319 | reg |= 0x08; 320 | WriteRegister(SC16IS750_REG_IOCONTROL, reg); 321 | 322 | return; 323 | } 324 | 325 | void SC16IS750::ModemPin(uint8_t gpio) //gpio == 0, gpio[7:4] are modem pins, gpio == 1 gpio[7:4] are gpios 326 | { 327 | uint8_t temp_iocontrol; 328 | 329 | temp_iocontrol = ReadRegister(SC16IS750_REG_IOCONTROL); 330 | if ( gpio == 0 ) { 331 | temp_iocontrol |= 0x02; 332 | } else { 333 | temp_iocontrol &= 0xFD; 334 | } 335 | WriteRegister(SC16IS750_REG_IOCONTROL, temp_iocontrol); 336 | 337 | return; 338 | } 339 | 340 | void SC16IS750::GPIOLatch(uint8_t latch) 341 | { 342 | uint8_t temp_iocontrol; 343 | 344 | temp_iocontrol = ReadRegister(SC16IS750_REG_IOCONTROL); 345 | if ( latch == 0 ) { 346 | temp_iocontrol &= 0xFE; 347 | } else { 348 | temp_iocontrol |= 0x01; 349 | } 350 | WriteRegister(SC16IS750_REG_IOCONTROL, temp_iocontrol); 351 | 352 | return; 353 | } 354 | 355 | void SC16IS750::InterruptControl(uint8_t int_ena) 356 | { 357 | WriteRegister(SC16IS750_REG_IER, int_ena); 358 | } 359 | 360 | uint8_t SC16IS750::InterruptPendingTest(void) 361 | { 362 | return (ReadRegister(SC16IS750_REG_IIR) & 0x01); 363 | } 364 | 365 | void SC16IS750::__isr(void) 366 | { 367 | uint8_t irq_src; 368 | 369 | irq_src = ReadRegister(SC16IS750_REG_IIR); 370 | irq_src = (irq_src >> 1); 371 | irq_src &= 0x3F; 372 | 373 | switch (irq_src) { 374 | case 0x06: //Receiver Line Status Error 375 | break; 376 | case 0x0c: //Receiver time-out interrupt 377 | break; 378 | case 0x04: //RHR interrupt 379 | break; 380 | case 0x02: //THR interrupt 381 | break; 382 | case 0x00: //modem interrupt; 383 | break; 384 | case 0x30: //input pin change of state 385 | break; 386 | case 0x10: //XOFF 387 | break; 388 | case 0x20: //CTS,RTS 389 | break; 390 | default: 391 | break; 392 | } 393 | return; 394 | } 395 | 396 | void SC16IS750::FIFOEnable(uint8_t fifo_enable) 397 | { 398 | uint8_t temp_fcr; 399 | 400 | temp_fcr = ReadRegister(SC16IS750_REG_FCR); 401 | 402 | if (fifo_enable == 0){ 403 | temp_fcr &= 0xFE; 404 | } else { 405 | temp_fcr |= 0x01; 406 | } 407 | WriteRegister(SC16IS750_REG_FCR,temp_fcr); 408 | 409 | return; 410 | } 411 | 412 | void SC16IS750::FIFOReset(uint8_t rx_fifo) 413 | { 414 | uint8_t temp_fcr; 415 | 416 | temp_fcr = ReadRegister(SC16IS750_REG_FCR); 417 | 418 | if (rx_fifo == 0){ 419 | temp_fcr |= 0x04; 420 | } else { 421 | temp_fcr |= 0x02; 422 | } 423 | WriteRegister(SC16IS750_REG_FCR,temp_fcr); 424 | 425 | return; 426 | 427 | } 428 | 429 | void SC16IS750::FIFOSetTriggerLevel(uint8_t rx_fifo, uint8_t length) 430 | { 431 | uint8_t temp_reg; 432 | 433 | temp_reg = ReadRegister(SC16IS750_REG_MCR); 434 | temp_reg |= 0x04; 435 | WriteRegister(SC16IS750_REG_MCR,temp_reg); //SET MCR[2] to '1' to use TLR register or trigger level control in FCR register 436 | 437 | temp_reg = ReadRegister(SC16IS750_REG_EFR); 438 | WriteRegister(SC16IS750_REG_EFR, temp_reg|0x10); //set ERF[4] to '1' to use the enhanced features 439 | if (rx_fifo == 0) { 440 | WriteRegister(SC16IS750_REG_TLR, length<<4); //Tx FIFO trigger level setting 441 | } else { 442 | WriteRegister(SC16IS750_REG_TLR, length); //Rx FIFO Trigger level setting 443 | } 444 | WriteRegister(SC16IS750_REG_EFR, temp_reg); //restore EFR register 445 | 446 | return; 447 | } 448 | 449 | uint8_t SC16IS750::FIFOAvailableData(void) 450 | { 451 | #ifdef SC16IS750_DEBUG_PRINT 452 | Serial.print("=====Available data:"); 453 | Serial.println(ReadRegister(SC16IS750_REG_RXLVL), DEC); 454 | #endif 455 | return ReadRegister(SC16IS750_REG_RXLVL); 456 | // return ReadRegister(SC16IS750_REG_LSR) & 0x01; 457 | } 458 | 459 | uint8_t SC16IS750::FIFOAvailableSpace(void) 460 | { 461 | return ReadRegister(SC16IS750_REG_TXLVL); 462 | 463 | } 464 | 465 | void SC16IS750::WriteByte(uint8_t val) 466 | { 467 | uint8_t tmp_lsr; 468 | /* while ( FIFOAvailableSpace() == 0 ){ 469 | #ifdef SC16IS750_DEBUG_PRINT 470 | Serial.println("No available space"); 471 | #endif 472 | 473 | }; 474 | 475 | #ifdef SC16IS750_DEBUG_PRINT 476 | Serial.println("++++++++++++Data sent"); 477 | #endif 478 | WriteRegister(SC16IS750_REG_THR,val); 479 | */ 480 | do { 481 | tmp_lsr = ReadRegister(SC16IS750_REG_LSR); 482 | } while ((tmp_lsr&0x20) ==0); 483 | 484 | WriteRegister(SC16IS750_REG_THR,val); 485 | 486 | 487 | 488 | } 489 | 490 | int SC16IS750::ReadByte(void) 491 | { 492 | volatile uint8_t val; 493 | if (FIFOAvailableData() == 0) { 494 | #ifdef SC16IS750_DEBUG_PRINT 495 | Serial.println("No data available"); 496 | #endif 497 | return -1; 498 | 499 | } else { 500 | 501 | #ifdef SC16IS750_DEBUG_PRINT 502 | Serial.println("***********Data available***********"); 503 | #endif 504 | val = ReadRegister(SC16IS750_REG_RHR); 505 | return val; 506 | } 507 | 508 | 509 | } 510 | 511 | void SC16IS750::EnableTransmit(uint8_t tx_enable) 512 | { 513 | uint8_t temp_efcr; 514 | temp_efcr = ReadRegister(SC16IS750_REG_EFCR); 515 | if ( tx_enable == 0) { 516 | temp_efcr |= 0x04; 517 | } else { 518 | temp_efcr &= 0xFB; 519 | } 520 | WriteRegister(SC16IS750_REG_EFCR,temp_efcr); 521 | 522 | return; 523 | } 524 | 525 | uint8_t SC16IS750::ping() 526 | { 527 | WriteRegister(SC16IS750_REG_SPR,0x55); 528 | if (ReadRegister(SC16IS750_REG_SPR) !=0x55) { 529 | return 0; 530 | } 531 | 532 | WriteRegister(SC16IS750_REG_SPR,0xAA); 533 | if (ReadRegister(SC16IS750_REG_SPR) !=0xAA) { 534 | return 0; 535 | } 536 | 537 | return 1; 538 | 539 | } 540 | /* 541 | void SC16IS750::setTimeout(uint32_t time_out) 542 | { 543 | timeout = time_out; 544 | } 545 | 546 | size_t SC16IS750::readBytes(char *buffer, size_t length) 547 | { 548 | size_t count=0; 549 | int16_t tmp; 550 | 551 | while (count < length) { 552 | tmp = readwithtimeout(); 553 | if (tmp < 0) { 554 | break; 555 | } 556 | *buffer++ = (char)tmp; 557 | count++; 558 | } 559 | 560 | return count; 561 | } 562 | 563 | int16_t SC16IS750::readwithtimeout() 564 | { 565 | int16_t tmp; 566 | uint32_t time_stamp; 567 | time_stamp = millis(); 568 | do { 569 | tmp = read(); 570 | if (tmp >= 0) return tmp; 571 | } while(millis() - time_stamp < timeout); 572 | return -1; // -1 indicates timeout 573 | } 574 | */ 575 | 576 | void SC16IS750::flush() 577 | { 578 | uint8_t tmp_lsr; 579 | 580 | do { 581 | tmp_lsr = ReadRegister(SC16IS750_REG_LSR); 582 | } while ((tmp_lsr&0x20) ==0); 583 | 584 | 585 | } 586 | 587 | int SC16IS750:: peek() 588 | { 589 | if ( peek_flag == 0 ) { 590 | peek_buf =ReadByte(); 591 | if ( peek_buf >= 0 ) { 592 | peek_flag = 1; 593 | } 594 | } 595 | 596 | return peek_buf; 597 | 598 | } 599 | --------------------------------------------------------------------------------