SPI or I2C to UART Bridge
Dependents: SC16IS750_Test mbed_SC16IS750 Xadow_SC16IS750_Test Xadow_MPU9150AHRS
Diff: SC16IS750.h
- Revision:
- 4:12446ee9f9c8
- Parent:
- 3:9783b6bde958
- Child:
- 5:ff3e57bebb6a
--- a/SC16IS750.h Thu Feb 20 19:37:55 2014 +0000 +++ b/SC16IS750.h Mon Dec 22 19:04:38 2014 +0000 @@ -1,6 +1,8 @@ /* SC16IS750 I2C or SPI to UART bridge * v0.1 WH, Nov 2013, Sparkfun WiFly Shield code library alpha 0 used as example, Added I2C I/F and many more methods. * https://forum.sparkfun.com/viewtopic.php?f=13&t=21846 + * v0.2 WH, Feb 2014, Added Doxygen Documentation, Added Hardware Reset pin methods. + * v0.3 WH, Dec 2014, Added support for SC16IS752 dual UART. * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software * and associated documentation files (the "Software"), to deal in the Software without restriction, @@ -270,7 +272,7 @@ #define SPI_READ_MODE_FLAG (0x80) -/** Abstract class SC16IS750 for a converter between either SPI or I2C and a Serial port +/** Abstract class SC16IS750 for a bridge between either SPI or I2C and a Serial port * * Supports both SPI and I2C interfaces through derived classes * @@ -365,6 +367,11 @@ RTSCTS }; +// SC16IS752 Channel definitions (shifted to align) + enum ChannelName { + Channel_A = 0x00 << 1, + Channel_B = 0x01 << 1 + }; // SC16IS750 configuration register values // Several configuration registers are write-only. Need to save values to allow restoring. @@ -407,7 +414,6 @@ */ int writableCount(); - /** * Read char from UART Bridge. * Acts in the same manner as 'Serial.read()'. @@ -421,7 +427,17 @@ * @param value char to be written * @return value written */ - int putc(int value); + int putc(int c); + + +#if DOXYGEN_ONLY + /** Write a formatted string to the UART Bridge. Blocking when no free space in FIFO + * + * @param format A printf-style format string, followed by the + * variables to use in formatting the string. + */ + int printf(const char* format, ...); +#endif /** @@ -580,6 +596,14 @@ void swReset(); +/** Hardware Reset SC16IS750 device. + * Pure virtual, must be declared in derived class. + * This method is only functional when the Reset pin has been declared and is also connected + * @param none + * @return none + */ + virtual void hwReset() =0; + /** Write value to internal register. * Pure virtual, must be declared in derived class. * @param registerAddress The address of the Register (enum RegisterName) @@ -648,6 +672,7 @@ * */ virtual int peek() {return 0;}; + // Save config settings SC16IS750_cfg _config; @@ -659,7 +684,7 @@ -/** Class SC16IS750_SPI for a converter between SPI and a Serial port +/** Class SC16IS750_SPI for a bridge between SPI and a Serial port * * @code * #include "mbed.h" @@ -688,12 +713,21 @@ class SC16IS750_SPI : public SC16IS750 { public: -/** Create a SC16IS750_SPI object using a specified SPI bus and CS +/** Create an SC16IS750_SPI object using a specified SPI bus and CS * * @param SPI &spi the SPI port to connect to - * @param cs the Pin of the CS + * @param cs Pinname of the CS pin (active low) + * @param rst Pinname for Reset pin (active low) Optional, Default = NC */ - SC16IS750_SPI(SPI *spi, PinName cs); + SC16IS750_SPI(SPI *spi, PinName cs, PinName rst = NC); + +/** Destruct SC16IS750_SPI bridge object + * + * @param none + * @return none + */ + virtual ~SC16IS750_SPI(); + /** Write value to internal register. * @param registerAddress The address of the Register (enum RegisterName) @@ -711,13 +745,20 @@ /** Write multiple datavalues to Transmitregister. * More Efficient implementation than writing individual bytes * Assume that previous check confirmed that the FIFO has sufficient free space to store the data - * Pure virtual, must be declared in derived class. + * * @param char* databytes The pointer to the block of data * @param len The number of bytes to write * @return none */ virtual void writeDataBlock (const char *data, int len ); +/** Hardware Reset SC16IS750 device. + * This method is only functional when the Reset pin has been declared and is also connected + * @param none + * @return none + */ + virtual void hwReset(); + protected: //protected is accessible to derived classes, but not to external users @@ -725,13 +766,18 @@ private: SPI *_spi; //SPI bus reference - DigitalOut _cs; //CS of SPI device + DigitalOut _cs; //CS of SPI device (active low) + +/** Optional Hardware Reset pin for the bridge device (active low) + * Default PinName value is NC + */ + DigitalOut* _reset; //Reset the Bridge device (active low) }; -/** Class SC16IS750_I2C for a converter between I2C and a Serial port +/** Class SC16IS750_I2C for a bridge between I2C and a Serial port * * @code * #include "mbed.h" @@ -760,12 +806,22 @@ class SC16IS750_I2C : public SC16IS750 { public: -/** Create a SC16IS750_I2C object using a specified I2C bus and slaveaddress +/** Create an SC16IS750_I2C object using a specified I2C bus and slaveaddress * * @param I2C &i2c the I2C port to connect to * @param char deviceAddress the address of the SC16IS750 + * @param rst Pinname for Reset pin (active low) Optional, Default = NC */ - SC16IS750_I2C(I2C *i2c, uint8_t deviceAddress = SC16IS750_DEFAULT_ADDR); + SC16IS750_I2C(I2C *i2c, uint8_t deviceAddress = SC16IS750_DEFAULT_ADDR, PinName rst = NC); + + +/** Destruct SC16IS750_I2C bridge object + * + * @param none + * @return none + */ + virtual ~SC16IS750_I2C(); + /** Write value to internal register. * @param registerAddress The address of the Register (enum RegisterName) @@ -791,6 +847,15 @@ */ virtual void writeDataBlock (const char *data, int len ); + +/** Hardware Reset SC16IS750 device. + * This method is only functional when the Reset pin has been declared and is also connected + * @param none + * @return none + */ + virtual void hwReset(); + + protected: //protected is accessible to derived classes, but not to external users @@ -798,7 +863,209 @@ private: I2C *_i2c; //I2C bus reference uint8_t _slaveAddress; //I2C Slave address of device - + +/** Optional Hardware Reset pin for the bridge device (active low) + * Default PinName value is NC + */ + DigitalOut* _reset; //Reset the Bridge device (active low) + +}; + + + +/** Class SC16IS752_SPI for a bridge between SPI and a Serial port + * + * @code + * #include "mbed.h" + * #include "SC16IS750.h" + * + * SPI spi(PTD2, PTD3, PTD1); //MOSI, MISO, SCK + * SC16IS750_SPI serial_spi(&spi, PTD0, NC, SC16IS750::Channel_B); + * + * Serial pc(USBTX,USBRX); + * + * int main() { + * pc.printf("\nHello World!\n"); + * + * while(1) { + * serial_spi.ioSetState(0x00); + * wait(0.5); + * serial_spi.ioSetState(0xFF); + * wait(0.5); + * serial_spi.putc('*'); + * pc.putc('*'); + * } + * } + * + * @endcode + */ +class SC16IS752_SPI : public SC16IS750 { +public: + +/** Create an SC16IS752_SPI object using a specified SPI bus and CS + * + * @param SPI &spi the SPI port to connect to + * @param cs Pinname of the CS pin (active low) + * @param rst Pinname for Reset pin (active low) Optional, Default = NC + * @param channel UART ChannelName, Default = Channel_A + */ + SC16IS752_SPI(SPI *spi, PinName cs, PinName rst = NC, ChannelName channel = SC16IS750::Channel_A ); + +/** Destruct SC16IS752_SPI bridge object + * + * @param none + * @return none + */ + virtual ~SC16IS752_SPI(); + + +/** Write value to internal register. + * @param registerAddress The address of the Register (enum RegisterName) + * @param data The 8bit value to write + * @return none + */ + virtual void writeRegister(SC16IS750::RegisterName registerAddress, char data); + +/** Read value from internal register. + * @param registerAddress The address of the Register (enum RegisterName) + * @return char The 8bit value read from the register + */ + virtual char readRegister(SC16IS750::RegisterName registerAddress); + +/** Write multiple datavalues to Transmitregister. + * More Efficient implementation than writing individual bytes + * Assume that previous check confirmed that the FIFO has sufficient free space to store the data + * + * @param char* databytes The pointer to the block of data + * @param len The number of bytes to write + * @return none + */ + virtual void writeDataBlock (const char *data, int len ); + +/** Hardware Reset SC16IS750 device. + * This method is only functional when the Reset pin has been declared and is also connected + * @param none + * @return none + */ + virtual void hwReset(); + + +protected: +//protected is accessible to derived classes, but not to external users + + +private: + SPI *_spi; //SPI bus reference + DigitalOut _cs; //CS of SPI device (active low) + +/** Optional Hardware Reset pin for the bridge device (active low) + * Default PinName value is NC + */ + DigitalOut* _reset; //Reset the Bridge device (active low) + +// Save Channel setting + ChannelName _channel; + +}; + + + +/** Class SC16IS752_I2C for a bridge between I2C and a Serial port + * + * @code + * #include "mbed.h" + * #include "SC16IS750.h" + * + * I2C i2c(PTE0, PTE1); //SDA, SCL + * SC16IS752_I2C serial_i2c(&i2c, DEFAULT_SC16IS750_ADDR, NC, SC16IS750::Channel_A); + * + * Serial pc(USBTX,USBRX); + * + * int main() { + * pc.printf("\nHello World!\n"); + * + * while(1) { + * serial_i2c.ioSetState(0x00); + * wait(0.5); + * serial_i2c.ioSetState(0xFF); + * wait(0.5); + * serial_i2c.putc('*'); + * pc.putc('*'); + * } + * } + * + * @endcode + */ +class SC16IS752_I2C : public SC16IS750 { +public: + +/** Create an SC16IS752_I2C object using a specified I2C bus, slaveaddress and Channel + * + * @param I2C &i2c the I2C port to connect to + * @param char deviceAddress the address of the SC16IS750 + * @param rst Pinname for Reset pin (active low) Optional, Default = NC + * @param channel UART ChannelName, Default = Channel_A + */ + SC16IS752_I2C(I2C *i2c, uint8_t deviceAddress = SC16IS750_DEFAULT_ADDR, PinName rst = NC, ChannelName channel = SC16IS750::Channel_A); + + +/** Destruct SC16IS752_I2C bridge object + * + * @param none + * @return none + */ + virtual ~SC16IS752_I2C(); + + +/** Write value to internal register. + * @param registerAddress The address of the Register (enum RegisterName) + * @param data The 8bit value to write + * @return none + */ + virtual void writeRegister(SC16IS750::RegisterName register_address, char data ); + +/** Read value from internal register. + * @param registerAddress The address of the Register (enum RegisterName) + * @return char The 8bit value read from the register + */ + virtual char readRegister(SC16IS750::RegisterName register_address ); + + +/** Write multiple datavalues to Transmitregister. + * More Efficient implementation than writing individual bytes + * Assume that previous check confirmed that the FIFO has sufficient free space to store the data + * Pure virtual, must be declared in derived class. + * @param char* databytes The pointer to the block of data + * @param len The number of bytes to write + * @return none + */ + virtual void writeDataBlock (const char *data, int len ); + + +/** Hardware Reset SC16IS752 device. + * This method is only functional when the Reset pin has been declared and is also connected + * @param none + * @return none + */ + virtual void hwReset(); + + +protected: +//protected is accessible to derived classes, but not to external users + + +private: + I2C *_i2c; //I2C bus reference + uint8_t _slaveAddress; //I2C Slave address of device + +/** Optional Hardware Reset pin for the bridge device (active low) + * Default PinName value is NC + */ + DigitalOut* _reset; //Reset the Bridge device (active low) + +// Save Channel setting + ChannelName _channel; + };