SPI or I2C to UART Bridge
Dependents: SC16IS750_Test mbed_SC16IS750 Xadow_SC16IS750_Test Xadow_MPU9150AHRS
SC16IS750.h@5:ff3e57bebb6a, 2014-12-24 (annotated)
- Committer:
- wim
- Date:
- Wed Dec 24 01:05:49 2014 +0000
- Revision:
- 5:ff3e57bebb6a
- Parent:
- 4:12446ee9f9c8
Added Repeated Start for I2C readRegister(). Set I2C clock at 100kb/s. Fixed and added some comments.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wim | 3:9783b6bde958 | 1 | /* SC16IS750 I2C or SPI to UART bridge |
wim | 3:9783b6bde958 | 2 | * v0.1 WH, Nov 2013, Sparkfun WiFly Shield code library alpha 0 used as example, Added I2C I/F and many more methods. |
wim | 3:9783b6bde958 | 3 | * https://forum.sparkfun.com/viewtopic.php?f=13&t=21846 |
wim | 4:12446ee9f9c8 | 4 | * v0.2 WH, Feb 2014, Added Doxygen Documentation, Added Hardware Reset pin methods. |
wim | 4:12446ee9f9c8 | 5 | * v0.3 WH, Dec 2014, Added support for SC16IS752 dual UART. |
wim | 5:ff3e57bebb6a | 6 | * v0.4 WH, Dec 2014, Added Repeated Start for I2C readRegister(). Set I2C clock at 100kb/s. Fixed and added some comments. |
wim | 0:d64854a60f95 | 7 | * |
wim | 0:d64854a60f95 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
wim | 0:d64854a60f95 | 9 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
wim | 0:d64854a60f95 | 10 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
wim | 0:d64854a60f95 | 11 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
wim | 0:d64854a60f95 | 12 | * furnished to do so, subject to the following conditions: |
wim | 0:d64854a60f95 | 13 | * |
wim | 0:d64854a60f95 | 14 | * The above copyright notice and this permission notice shall be included in all copies or |
wim | 0:d64854a60f95 | 15 | * substantial portions of the Software. |
wim | 0:d64854a60f95 | 16 | * |
wim | 0:d64854a60f95 | 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
wim | 0:d64854a60f95 | 18 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
wim | 0:d64854a60f95 | 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
wim | 0:d64854a60f95 | 20 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
wim | 0:d64854a60f95 | 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
wim | 0:d64854a60f95 | 22 | */ |
wim | 0:d64854a60f95 | 23 | #ifndef _SC16IS750_H |
wim | 0:d64854a60f95 | 24 | #define _SC16IS750_H |
wim | 0:d64854a60f95 | 25 | |
wim | 3:9783b6bde958 | 26 | |
wim | 3:9783b6bde958 | 27 | #include "Stream.h" |
wim | 3:9783b6bde958 | 28 | //#include <SerialBase.h> |
wim | 3:9783b6bde958 | 29 | |
wim | 1:0440152c5387 | 30 | //I2C Slaveaddresses A1 A0 |
wim | 2:76cb93b511f2 | 31 | #define SC16IS750_SA0 0x90 /* VDD VDD */ |
wim | 2:76cb93b511f2 | 32 | #define SC16IS750_SA1 0x92 /* VDD VSS */ |
wim | 2:76cb93b511f2 | 33 | #define SC16IS750_SA2 0x94 /* VDD SCL */ |
wim | 2:76cb93b511f2 | 34 | #define SC16IS750_SA3 0x95 /* VDD SDA */ |
wim | 2:76cb93b511f2 | 35 | #define SC16IS750_SA4 0x98 /* VSS VDD */ |
wim | 2:76cb93b511f2 | 36 | #define SC16IS750_SA5 0x9A /* VSS VSS */ |
wim | 2:76cb93b511f2 | 37 | #define SC16IS750_SA6 0x9C /* VSS SCL */ |
wim | 2:76cb93b511f2 | 38 | #define SC16IS750_SA7 0x9E /* VSS SDA */ |
wim | 2:76cb93b511f2 | 39 | #define SC16IS750_SA8 0xA0 /* SCL VDD */ |
wim | 2:76cb93b511f2 | 40 | #define SC16IS750_SA9 0xA2 /* SCL VSS */ |
wim | 2:76cb93b511f2 | 41 | #define SC16IS750_SA10 0xA4 /* SCL SCL */ |
wim | 2:76cb93b511f2 | 42 | #define SC16IS750_SA11 0xA6 /* SCL SDA */ |
wim | 2:76cb93b511f2 | 43 | #define SC16IS750_SA12 0xA8 /* SDA VDD */ |
wim | 2:76cb93b511f2 | 44 | #define SC16IS750_SA13 0xAA /* SDA VSS */ |
wim | 2:76cb93b511f2 | 45 | #define SC16IS750_SA14 0xAC /* SDA SCL */ |
wim | 2:76cb93b511f2 | 46 | #define SC16IS750_SA15 0xAE /* SDA SDA */ |
wim | 1:0440152c5387 | 47 | |
wim | 0:d64854a60f95 | 48 | //Default I2C Slaveaddress |
wim | 2:76cb93b511f2 | 49 | #define SC16IS750_DEFAULT_ADDR SC16IS750_SA0 |
wim | 2:76cb93b511f2 | 50 | |
wim | 2:76cb93b511f2 | 51 | |
wim | 2:76cb93b511f2 | 52 | /** See datasheet section 7.8 for configuring the |
wim | 2:76cb93b511f2 | 53 | * "Programmable baud rate generator" |
wim | 2:76cb93b511f2 | 54 | */ |
wim | 2:76cb93b511f2 | 55 | #define SC16IS750_XTAL_FREQ 14745600UL /* On-board crystal (New mid-2010 Version) */ |
wim | 2:76cb93b511f2 | 56 | #define SC16IS750_PRESCALER_1 1 /* Default prescaler after reset */ |
wim | 2:76cb93b511f2 | 57 | #define SC16IS750_PRESCALER_4 4 /* Selectable by setting MCR[7] */ |
wim | 2:76cb93b511f2 | 58 | #define SC16IS750_PRESCALER SC16IS750_PRESCALER_1 |
wim | 2:76cb93b511f2 | 59 | #define SC16IS750_BAUDRATE_DIVISOR(baud) ((SC16IS750_XTAL_FREQ/SC16IS750_PRESCALER)/(baud*16UL)) |
wim | 0:d64854a60f95 | 60 | |
wim | 0:d64854a60f95 | 61 | //Default baudrate |
wim | 3:9783b6bde958 | 62 | #define SC16IS750_DEFAULT_BAUDRATE 9600 |
wim | 0:d64854a60f95 | 63 | |
wim | 1:0440152c5387 | 64 | |
wim | 2:76cb93b511f2 | 65 | /** See section 8.3 of the datasheet for definitions |
wim | 2:76cb93b511f2 | 66 | * of bits in the FIFO Control Register (FCR) |
wim | 2:76cb93b511f2 | 67 | */ |
wim | 2:76cb93b511f2 | 68 | #define FCR_RX_IRQ_60 (3 << 6) |
wim | 2:76cb93b511f2 | 69 | #define FCR_RX_IRQ_56 (2 << 6) |
wim | 2:76cb93b511f2 | 70 | #define FCR_RX_IRQ_16 (1 << 6) |
wim | 2:76cb93b511f2 | 71 | #define FCR_RX_IRQ_8 (0 << 6) |
wim | 2:76cb93b511f2 | 72 | //TX Level only accessible when EFR[4] is set |
wim | 2:76cb93b511f2 | 73 | #define FCR_TX_IRQ_56 (3 << 4) |
wim | 2:76cb93b511f2 | 74 | #define FCR_TX_IRQ_32 (2 << 4) |
wim | 2:76cb93b511f2 | 75 | #define FCR_TX_IRQ_16 (1 << 4) |
wim | 2:76cb93b511f2 | 76 | #define FCR_TX_IRQ_8 (0 << 4) |
wim | 2:76cb93b511f2 | 77 | //#define FCR_RESERVED (1 << 3) |
wim | 3:9783b6bde958 | 78 | #define FCR_TX_FIFO_RST (1 << 2) |
wim | 3:9783b6bde958 | 79 | #define FCR_RX_FIFO_RST (1 << 1) |
wim | 1:0440152c5387 | 80 | #define FCR_ENABLE_FIFO (1 << 0) |
wim | 1:0440152c5387 | 81 | |
wim | 2:76cb93b511f2 | 82 | //FIFO size |
wim | 2:76cb93b511f2 | 83 | #define SC16IS750_FIFO_RX 64 |
wim | 2:76cb93b511f2 | 84 | #define SC16IS750_FIFO_TX 64 |
wim | 1:0440152c5387 | 85 | |
wim | 1:0440152c5387 | 86 | |
wim | 2:76cb93b511f2 | 87 | /** See section 8.4 of the datasheet for definitions |
wim | 2:76cb93b511f2 | 88 | * of bits in the Line Control Register (LCR) |
wim | 2:76cb93b511f2 | 89 | */ |
wim | 2:76cb93b511f2 | 90 | #define LCR_BITS5 0x00 |
wim | 2:76cb93b511f2 | 91 | #define LCR_BITS6 0x01 |
wim | 2:76cb93b511f2 | 92 | #define LCR_BITS7 0x02 |
wim | 2:76cb93b511f2 | 93 | #define LCR_BITS8 0x03 |
wim | 1:0440152c5387 | 94 | |
wim | 2:76cb93b511f2 | 95 | #define LCR_BITS1 0x00 |
wim | 2:76cb93b511f2 | 96 | #define LCR_BITS2 0x04 |
wim | 1:0440152c5387 | 97 | |
wim | 2:76cb93b511f2 | 98 | #define LCR_NONE 0x00 |
wim | 2:76cb93b511f2 | 99 | #define LCR_ODD 0x08 |
wim | 2:76cb93b511f2 | 100 | #define LCR_EVEN 0x18 |
wim | 2:76cb93b511f2 | 101 | #define LCR_FORCED1 0x28 |
wim | 2:76cb93b511f2 | 102 | #define LCR_FORCED0 0x38 |
wim | 2:76cb93b511f2 | 103 | |
wim | 2:76cb93b511f2 | 104 | #define LCR_BRK_ENA 0x40 |
wim | 2:76cb93b511f2 | 105 | #define LCR_BRK_DIS 0x00 |
wim | 1:0440152c5387 | 106 | |
wim | 2:76cb93b511f2 | 107 | #define LCR_ENABLE_DIV 0x80 |
wim | 2:76cb93b511f2 | 108 | #define LCR_DISABLE_DIV 0x00 |
wim | 2:76cb93b511f2 | 109 | |
wim | 2:76cb93b511f2 | 110 | #define LCR_ENABLE_ENHANCED_FUNCTIONS (0xBF) |
wim | 1:0440152c5387 | 111 | |
wim | 1:0440152c5387 | 112 | |
wim | 2:76cb93b511f2 | 113 | /** See section 8.5 of the datasheet for definitions |
wim | 2:76cb93b511f2 | 114 | * of bits in the Line status register (LSR) |
wim | 2:76cb93b511f2 | 115 | */ |
wim | 3:9783b6bde958 | 116 | #define LSR_DR (0x01) /* Data ready in RX FIFO */ |
wim | 1:0440152c5387 | 117 | #define LSR_OE (0x02) /* Overrun error */ |
wim | 1:0440152c5387 | 118 | #define LSR_PE (0x04) /* Parity error */ |
wim | 1:0440152c5387 | 119 | #define LSR_FE (0x08) /* Framing error */ |
wim | 1:0440152c5387 | 120 | #define LSR_BI (0x10) /* Break interrupt */ |
wim | 1:0440152c5387 | 121 | #define LSR_THRE (0x20) /* Transmitter holding register (FIFO empty) */ |
wim | 1:0440152c5387 | 122 | #define LSR_TEMT (0x40) /* Transmitter empty (FIFO and TSR both empty) */ |
wim | 2:76cb93b511f2 | 123 | #define LSR_FFE (0x80) /* At least one PE, FE or BI in FIFO */ |
wim | 1:0440152c5387 | 124 | |
wim | 2:76cb93b511f2 | 125 | |
wim | 2:76cb93b511f2 | 126 | /** See section 8.6 of the datasheet for definitions |
wim | 2:76cb93b511f2 | 127 | * of bits in the Modem control register (MCR) |
wim | 2:76cb93b511f2 | 128 | */ |
wim | 2:76cb93b511f2 | 129 | #define MCR_MDTR (1 << 0) /* Data Terminal Ready pin control. */ |
wim | 2:76cb93b511f2 | 130 | #define MCR_MRTS (1 << 1) /* Request to Send pin control when not in Auto RTS mode.*/ |
wim | 3:9783b6bde958 | 131 | //MCR[2] only accessible when EFR[4] is set |
wim | 2:76cb93b511f2 | 132 | #define MCR_ENABLE_TCR_TLR (1 << 2) |
wim | 2:76cb93b511f2 | 133 | #define MCR_ENABLE_LOOPBACK (1 << 4) |
wim | 3:9783b6bde958 | 134 | //MCR[7:5] only accessible when EFR[4] is set |
wim | 2:76cb93b511f2 | 135 | #define MCR_ENABLE_XON_ANY_CHAR (1 << 5) |
wim | 2:76cb93b511f2 | 136 | #define MCR_ENABLE_IRDA (1 << 6) |
wim | 2:76cb93b511f2 | 137 | #define MCR_PRESCALE_1 (0 << 7) |
wim | 2:76cb93b511f2 | 138 | #define MCR_PRESCALE_4 (1 << 7) |
wim | 2:76cb93b511f2 | 139 | |
wim | 1:0440152c5387 | 140 | |
wim | 2:76cb93b511f2 | 141 | /** See section 8.7 of the datasheet for definitions |
wim | 2:76cb93b511f2 | 142 | * of bits in the Modem status register (MSR) |
wim | 2:76cb93b511f2 | 143 | */ |
wim | 3:9783b6bde958 | 144 | #define MSR_DCTS (1 << 0) /* Delta CTS - CTS Changed State */ |
wim | 3:9783b6bde958 | 145 | #define MSR_DDSR (1 << 1) /* Delta DSR - DSR Changed State */ |
wim | 3:9783b6bde958 | 146 | #define MSR_DDI (1 << 2) /* Delta DI - DI Changed State */ |
wim | 3:9783b6bde958 | 147 | #define MSR_DCD (1 << 3) /* Delta CD - CD Changed State */ |
wim | 3:9783b6bde958 | 148 | #define MSR_CTS (1 << 4) /* CTS State - Complement of NCTS pin */ |
wim | 3:9783b6bde958 | 149 | //MSR[7:5] only accessible when GPIO[7:4] are set as modem pin |
wim | 3:9783b6bde958 | 150 | #define MSR_DSR (1 << 5) /* DSR State - Complement of NDSR pin */ |
wim | 3:9783b6bde958 | 151 | #define MSR_RI (1 << 6) /* RI State - Complement of NRI pin */ |
wim | 3:9783b6bde958 | 152 | #define MSR_CD (1 << 7) /* CD State - Complement of NCD pin */ |
wim | 2:76cb93b511f2 | 153 | |
wim | 2:76cb93b511f2 | 154 | |
wim | 2:76cb93b511f2 | 155 | /** See section 8.8 of the datasheet for definitions |
wim | 2:76cb93b511f2 | 156 | * of bits in the Interrupt enable register (IER) |
wim | 2:76cb93b511f2 | 157 | */ |
wim | 3:9783b6bde958 | 158 | #define IER_ERHRI (0x01) /* Enable received data available interrupt */ |
wim | 3:9783b6bde958 | 159 | #define IER_ETHRI (0x02) /* Enable transmitter holding register empty interrupt */ |
wim | 2:76cb93b511f2 | 160 | #define IER_ELSI (0x04) /* Enable receiver line status interrupt */ |
wim | 3:9783b6bde958 | 161 | #define IER_EMSI (0x08) /* Enable modem status interrupt */ |
wim | 3:9783b6bde958 | 162 | //IER[7:5] only accessible when EFR[4] is set |
wim | 2:76cb93b511f2 | 163 | #define IER_SLEEP (0x10) /* Enable sleep mode */ |
wim | 3:9783b6bde958 | 164 | #define IER_XOFFI (0x20) /* Enable XOFF interrupt */ |
wim | 3:9783b6bde958 | 165 | #define IER_RTSI (0x40) /* Enable RTS interrupt */ |
wim | 3:9783b6bde958 | 166 | #define IER_CTSI (0x80) /* Enable CTS interrupt */ |
wim | 1:0440152c5387 | 167 | |
wim | 1:0440152c5387 | 168 | |
wim | 2:76cb93b511f2 | 169 | /** See section 8.9 of the datasheet for definitions |
wim | 2:76cb93b511f2 | 170 | * of bits in the Interrupt identification register (IIR) |
wim | 3:9783b6bde958 | 171 | * Bit 0 is set to 0 if an IRQ is pending. |
wim | 3:9783b6bde958 | 172 | * Bits 1..5 are used to identify the IRQ source. |
wim | 2:76cb93b511f2 | 173 | */ |
wim | 3:9783b6bde958 | 174 | #define IIR_IRQ_NOT_PENDING (0x01) /* IRQ Not Pending */ |
wim | 3:9783b6bde958 | 175 | #define IIR_TX_EMPTY (0x02) /* THR Interrupt */ |
wim | 3:9783b6bde958 | 176 | #define IIR_RX_DATA (0x04) /* RHR Interrupt */ |
wim | 3:9783b6bde958 | 177 | #define IIR_RX_ERROR (0x06) /* Line Status Error Interrupt */ |
wim | 3:9783b6bde958 | 178 | #define IIR_RX_TIMEOUT (0x0B) /* RX Timeout Interrupt */ |
wim | 3:9783b6bde958 | 179 | #define IIR_RX_XOFF (0x10) /* RX XOff Interrupt */ |
wim | 3:9783b6bde958 | 180 | #define IIR_DCTS_DRTS (0x20) /* Delta CTS or RTS Interrupt */ |
wim | 3:9783b6bde958 | 181 | #define IIR_DIO (0x30) /* Delta GPIO pin Interrupt */ |
wim | 3:9783b6bde958 | 182 | |
wim | 2:76cb93b511f2 | 183 | #define IIR_BITS_USED (0x07) |
wim | 2:76cb93b511f2 | 184 | |
wim | 2:76cb93b511f2 | 185 | |
wim | 2:76cb93b511f2 | 186 | /** See section 8.10 of the datasheet for definitions |
wim | 2:76cb93b511f2 | 187 | * of bits in the Enhanced Features Register (EFR) |
wim | 2:76cb93b511f2 | 188 | */ |
wim | 2:76cb93b511f2 | 189 | #define EFR_ENABLE_CTS (1 << 7) |
wim | 2:76cb93b511f2 | 190 | #define EFR_ENABLE_RTS (1 << 6) |
wim | 2:76cb93b511f2 | 191 | #define EFR_ENABLE_XOFF2_CHAR_DETECT (1 << 5) |
wim | 2:76cb93b511f2 | 192 | #define EFR_ENABLE_ENHANCED_FUNCTIONS (1 << 4) |
wim | 2:76cb93b511f2 | 193 | // EFR[3:0] are used to define Software Flow Control mode |
wim | 2:76cb93b511f2 | 194 | // See section 7.3 |
wim | 2:76cb93b511f2 | 195 | #define EFR_DISABLE_TX_FLOW_CTRL (0x0 << 2) |
wim | 2:76cb93b511f2 | 196 | #define EFR_TX_XON2_XOFF2 (0x1 << 2) |
wim | 2:76cb93b511f2 | 197 | #define EFR_TX_XON1_XOFF1 (0x2 << 2) |
wim | 2:76cb93b511f2 | 198 | #define EFR_TX_XON2_1_XOFF2_1 (0x3 << 2) |
wim | 2:76cb93b511f2 | 199 | |
wim | 2:76cb93b511f2 | 200 | #define EFR_DISABLE_RX_FLOW_CTRL (0x0 << 0) |
wim | 2:76cb93b511f2 | 201 | #define EFR_RX_XON2_XOFF2 (0x1 << 0) |
wim | 2:76cb93b511f2 | 202 | #define EFR_RX_XON1_XOFF1 (0x2 << 0) |
wim | 2:76cb93b511f2 | 203 | #define EFR_RX_XON2_1_XOFF2_1 (0x3 << 0) |
wim | 2:76cb93b511f2 | 204 | |
wim | 2:76cb93b511f2 | 205 | #define EFR_TX_XON2_XOFF2_RX_FLOW (0x1 << 2) | (0x3 << 0) |
wim | 2:76cb93b511f2 | 206 | #define EFR_TX_XON1_XOFF1_RX_FLOW (0x2 << 2) | (0x3 << 0) |
wim | 2:76cb93b511f2 | 207 | #define EFR_TX_XON2_1_XOFF2_1_RX_FLOW (0x3 << 2) | (0x3 << 0) |
wim | 1:0440152c5387 | 208 | |
wim | 1:0440152c5387 | 209 | |
wim | 1:0440152c5387 | 210 | |
wim | 2:76cb93b511f2 | 211 | /** See section 8.12 of the datasheet for definitions |
wim | 2:76cb93b511f2 | 212 | * of bits in the Transmission Control Register (TCR) |
wim | 3:9783b6bde958 | 213 | * These levels control when RTS is asserted or de-asserted and auto RTS is enabled. Note that XON/XOFF is not supported in this lib. |
wim | 3:9783b6bde958 | 214 | * Trigger level to halt transmission to the device : 0..15 (meaning 0-60 with a granularity of 4) |
wim | 3:9783b6bde958 | 215 | * RTS is de-asserted when RX FIFO is above the set trigger level (i.e. buffer is getting full) |
wim | 3:9783b6bde958 | 216 | * Trigger level to resume transmission to the device : 0..15 (meaning 0-60 with a granularity of 4) |
wim | 3:9783b6bde958 | 217 | * RTS is asserted again when RX FIFO drops below the set trigger level (i.e. buffer has room again) |
wim | 2:76cb93b511f2 | 218 | */ |
wim | 2:76cb93b511f2 | 219 | #define TCR_HALT_DEFAULT (0x0E) |
wim | 3:9783b6bde958 | 220 | #define TCR_RESUME_DEFAULT (0x08) |
wim | 2:76cb93b511f2 | 221 | |
wim | 3:9783b6bde958 | 222 | /** See section 8.12 of the datasheet for definitions |
wim | 3:9783b6bde958 | 223 | * Note: The device will stop transmissions from the TX FIFO when CTS is de-asserted by external receiver and |
wim | 3:9783b6bde958 | 224 | * auto CTS is enabled. Note that XON/XOFF is not supported in this lib. |
wim | 3:9783b6bde958 | 225 | */ |
wim | 2:76cb93b511f2 | 226 | |
wim | 3:9783b6bde958 | 227 | |
wim | 3:9783b6bde958 | 228 | /** See section 7.5 and 8.13 of the datasheet for definitions |
wim | 3:9783b6bde958 | 229 | * of bits in the Trigger Level Register (TLR) control when an IRQ is generated. |
wim | 2:76cb93b511f2 | 230 | * Trigger level for TX interrupt: 0..15 (meaning 0-60 with a granularity of 4) |
wim | 3:9783b6bde958 | 231 | * IRQ when TX FIFO is above the set trigger level (i.e. buffer is getting full) |
wim | 2:76cb93b511f2 | 232 | * Trigger level for RX interrupt: 0..15 (meaning 0-60 with a granularity of 4) |
wim | 3:9783b6bde958 | 233 | * IRQ when RX FIFO is above the set trigger level (i.e. data is waiting to be read) |
wim | 2:76cb93b511f2 | 234 | */ |
wim | 2:76cb93b511f2 | 235 | #define TLR_TX_DEFAULT (0x0E) |
wim | 2:76cb93b511f2 | 236 | #define TLR_RX_DEFAULT (0x04) |
wim | 2:76cb93b511f2 | 237 | |
wim | 2:76cb93b511f2 | 238 | |
wim | 2:76cb93b511f2 | 239 | /** |
wim | 3:9783b6bde958 | 240 | * See section 8.16, 8.17, 8.18 of the datasheet for definitions |
wim | 3:9783b6bde958 | 241 | * of bits in the IO Direction (IODIR), IO State (IOSTATE) and IO Interrupt Enable register (IOINTENA) |
wim | 3:9783b6bde958 | 242 | * |
wim | 3:9783b6bde958 | 243 | * Basically a direct mapping of register bits to GPIO pin. |
wim | 3:9783b6bde958 | 244 | */ |
wim | 3:9783b6bde958 | 245 | |
wim | 3:9783b6bde958 | 246 | |
wim | 3:9783b6bde958 | 247 | /** |
wim | 2:76cb93b511f2 | 248 | * See section 8.19 of the datasheet for definitions |
wim | 2:76cb93b511f2 | 249 | * of bits in the IO Control register (IOC) |
wim | 2:76cb93b511f2 | 250 | * |
wim | 2:76cb93b511f2 | 251 | * Bit 0 is set to 0 to enable latch of IO inputs. |
wim | 2:76cb93b511f2 | 252 | * Bit 1 is set to enable GPIO[7-4] as /RI, /CD, /DTR, /DST. |
wim | 2:76cb93b511f2 | 253 | * Bit 2 is set to enable software reset. |
wim | 2:76cb93b511f2 | 254 | */ |
wim | 2:76cb93b511f2 | 255 | #define IOC_ENA_LATCH (0x01) |
wim | 3:9783b6bde958 | 256 | #define IOC_ENA_MODEM (0x02) /* Set GPIO[7:4] pins to modem functions */ |
wim | 3:9783b6bde958 | 257 | #define IOC_SW_RST (0x04) |
wim | 3:9783b6bde958 | 258 | |
wim | 2:76cb93b511f2 | 259 | |
wim | 3:9783b6bde958 | 260 | /** |
wim | 3:9783b6bde958 | 261 | * See section 8.20 of the datasheet for definitions |
wim | 3:9783b6bde958 | 262 | * of bits in the Extra Features Control register (EFCR) |
wim | 3:9783b6bde958 | 263 | * |
wim | 3:9783b6bde958 | 264 | */ |
wim | 3:9783b6bde958 | 265 | #define EFCR_ENA_RS485 (0x01) |
wim | 3:9783b6bde958 | 266 | #define EFCR_DIS_RX (0x02) |
wim | 3:9783b6bde958 | 267 | #define EFCR_DIS_TX (0x04) |
wim | 3:9783b6bde958 | 268 | #define EFCR_ENA_TX_RTS (0x10) |
wim | 3:9783b6bde958 | 269 | #define EFCR_INV_RTS_RS485 (0x20) |
wim | 3:9783b6bde958 | 270 | #define EFCR_ENA_IRDA (0x80) |
wim | 2:76cb93b511f2 | 271 | |
wim | 0:d64854a60f95 | 272 | // See Chapter 11 of datasheet |
wim | 2:76cb93b511f2 | 273 | #define SPI_READ_MODE_FLAG (0x80) |
wim | 0:d64854a60f95 | 274 | |
wim | 0:d64854a60f95 | 275 | |
wim | 4:12446ee9f9c8 | 276 | /** Abstract class SC16IS750 for a bridge between either SPI or I2C and a Serial port |
wim | 0:d64854a60f95 | 277 | * |
wim | 0:d64854a60f95 | 278 | * Supports both SPI and I2C interfaces through derived classes |
wim | 0:d64854a60f95 | 279 | * |
wim | 0:d64854a60f95 | 280 | * @code |
wim | 0:d64854a60f95 | 281 | * |
wim | 0:d64854a60f95 | 282 | * @endcode |
wim | 0:d64854a60f95 | 283 | */ |
wim | 3:9783b6bde958 | 284 | //class SC16IS750 { |
wim | 3:9783b6bde958 | 285 | //class SC16IS750 : public SerialBase, public Stream { // Wrong, Serialbase can not be constructed for NC,NC |
wim | 3:9783b6bde958 | 286 | class SC16IS750 : public Stream { |
wim | 0:d64854a60f95 | 287 | public: |
wim | 0:d64854a60f95 | 288 | |
wim | 0:d64854a60f95 | 289 | // SC16IS750 Register definitions (shifted to align) |
wim | 0:d64854a60f95 | 290 | enum RegisterName { |
wim | 1:0440152c5387 | 291 | /* |
wim | 1:0440152c5387 | 292 | * 16750 addresses. Registers accessed when LCR[7] = 0. |
wim | 1:0440152c5387 | 293 | */ |
wim | 1:0440152c5387 | 294 | RHR = 0x00 << 3, /* Rx buffer register - Read access */ |
wim | 1:0440152c5387 | 295 | THR = 0x00 << 3, /* Tx holding register - Write access */ |
wim | 1:0440152c5387 | 296 | IER = 0x01 << 3, /* Interrupt enable reg - RD/WR access */ |
wim | 1:0440152c5387 | 297 | |
wim | 1:0440152c5387 | 298 | /* |
wim | 1:0440152c5387 | 299 | * 16750 addresses. Registers accessed when LCR[7] = 1. |
wim | 1:0440152c5387 | 300 | */ |
wim | 1:0440152c5387 | 301 | DLL = 0x00 << 3, /* Divisor latch (LSB) - RD/WR access */ |
wim | 1:0440152c5387 | 302 | DLH = 0x01 << 3, /* Divisor latch (MSB) - RD/WR access */ |
wim | 1:0440152c5387 | 303 | |
wim | 1:0440152c5387 | 304 | /* |
wim | 1:0440152c5387 | 305 | * 16750 addresses. IIR/FCR is accessed when LCR[7:0] <> 0xBF. |
wim | 1:0440152c5387 | 306 | * Bit 5 of the FCR register is accessed when LCR[7] = 1. |
wim | 1:0440152c5387 | 307 | */ |
wim | 1:0440152c5387 | 308 | IIR = 0x02 << 3, /* Interrupt id. register - Read only */ |
wim | 1:0440152c5387 | 309 | FCR = 0x02 << 3, /* FIFO control register - Write only */ |
wim | 1:0440152c5387 | 310 | /* |
wim | 1:0440152c5387 | 311 | * 16750 addresses. EFR is accessed when LCR[7:0] = 0xBF. |
wim | 1:0440152c5387 | 312 | */ |
wim | 1:0440152c5387 | 313 | EFR = 0x02 << 3, /* Enhanced features reg - RD/WR access */ |
wim | 1:0440152c5387 | 314 | |
wim | 1:0440152c5387 | 315 | /* |
wim | 1:0440152c5387 | 316 | * 16750 addresses. |
wim | 1:0440152c5387 | 317 | */ |
wim | 1:0440152c5387 | 318 | LCR = 0x03 << 3, /* Line control register - RD/WR access */ |
wim | 1:0440152c5387 | 319 | /* |
wim | 1:0440152c5387 | 320 | * 16750 addresses. MCR/LSR is accessed when LCR[7:0] <> 0xBF. |
wim | 1:0440152c5387 | 321 | * Bit 7 of the MCR register is accessed when EFR[4] = 1. |
wim | 1:0440152c5387 | 322 | */ |
wim | 1:0440152c5387 | 323 | MCR = 0x04 << 3, /* Modem control register - RD/WR access */ |
wim | 1:0440152c5387 | 324 | LSR = 0x05 << 3, /* Line status register - Read only */ |
wim | 1:0440152c5387 | 325 | |
wim | 1:0440152c5387 | 326 | /* |
wim | 1:0440152c5387 | 327 | * 16750 addresses. MSR/SPR is accessed when LCR[7:0] <> 0xBF. |
wim | 1:0440152c5387 | 328 | * MSR, SPR register is accessed when EFR[1]=0 and MCR[2]=0. |
wim | 1:0440152c5387 | 329 | */ |
wim | 1:0440152c5387 | 330 | MSR = 0x06 << 3, /* Modem status register - Read only */ |
wim | 1:0440152c5387 | 331 | SPR = 0x07 << 3, /* Scratchpad register - RD/WR access */ |
wim | 1:0440152c5387 | 332 | /* |
wim | 1:0440152c5387 | 333 | * 16750 addresses. TCR/TLR is accessed when LCR[7:0] <> 0xBF. |
wim | 1:0440152c5387 | 334 | * TCR, TLR register is accessed when EFR[1]=1 and MCR[2]=1. |
wim | 1:0440152c5387 | 335 | */ |
wim | 1:0440152c5387 | 336 | TCR = 0x06 << 3, /* Transmission control register - RD/WR access */ |
wim | 1:0440152c5387 | 337 | TLR = 0x07 << 3, /* Trigger level register - RD/WR access */ |
wim | 1:0440152c5387 | 338 | |
wim | 1:0440152c5387 | 339 | /* |
wim | 1:0440152c5387 | 340 | * 16750 addresses. XON, XOFF is accessed when LCR[7:0] = 0xBF. |
wim | 1:0440152c5387 | 341 | */ |
wim | 1:0440152c5387 | 342 | XON1 = 0x04 << 3, /* XON1 register - RD/WR access */ |
wim | 1:0440152c5387 | 343 | XON2 = 0x05 << 3, /* XON2 register - RD/WR access */ |
wim | 1:0440152c5387 | 344 | XOFF1 = 0x06 << 3, /* XOFF1 register - RD/WR access */ |
wim | 1:0440152c5387 | 345 | XOFF2 = 0x07 << 3, /* XOFF2 register - RD/WR access */ |
wim | 1:0440152c5387 | 346 | |
wim | 1:0440152c5387 | 347 | /* |
wim | 1:0440152c5387 | 348 | * 16750 addresses. |
wim | 1:0440152c5387 | 349 | */ |
wim | 1:0440152c5387 | 350 | TXLVL = 0x08 << 3, /* TX FIFO Level register - Read only */ |
wim | 1:0440152c5387 | 351 | RXLVL = 0x09 << 3, /* RX FIFO Level register - Read only */ |
wim | 1:0440152c5387 | 352 | IODIR = 0x0A << 3, /* IO Pin Direction reg - RD/WR access */ |
wim | 5:ff3e57bebb6a | 353 | IOSTATE = 0x0B << 3, /* IO Pin State reg - RD/WR access */ |
wim | 1:0440152c5387 | 354 | IOINTENA = 0x0C << 3, /* IO Interrupt Enable - RD/WR access */ |
wim | 1:0440152c5387 | 355 | // reserved = 0x0D << 3, |
wim | 1:0440152c5387 | 356 | IOCTRL = 0x0E << 3, /* IO Control register - RD/WR access */ |
wim | 1:0440152c5387 | 357 | EFCR = 0x0F << 3, /* Extra features reg - RD/WR access */ |
wim | 1:0440152c5387 | 358 | |
wim | 0:d64854a60f95 | 359 | } ; |
wim | 0:d64854a60f95 | 360 | |
wim | 0:d64854a60f95 | 361 | |
wim | 1:0440152c5387 | 362 | // This enum used to be part of SerialBase class (access via SerialBase.h). |
wim | 1:0440152c5387 | 363 | // It seems not be supported anymore. The enums for Parity have moved to Serial now.. |
wim | 1:0440152c5387 | 364 | enum Flow { |
wim | 1:0440152c5387 | 365 | Disabled = 0, |
wim | 1:0440152c5387 | 366 | RTS, |
wim | 1:0440152c5387 | 367 | CTS, |
wim | 1:0440152c5387 | 368 | RTSCTS |
wim | 1:0440152c5387 | 369 | }; |
wim | 1:0440152c5387 | 370 | |
wim | 4:12446ee9f9c8 | 371 | // SC16IS752 Channel definitions (shifted to align) |
wim | 4:12446ee9f9c8 | 372 | enum ChannelName { |
wim | 4:12446ee9f9c8 | 373 | Channel_A = 0x00 << 1, |
wim | 4:12446ee9f9c8 | 374 | Channel_B = 0x01 << 1 |
wim | 4:12446ee9f9c8 | 375 | }; |
wim | 1:0440152c5387 | 376 | |
wim | 0:d64854a60f95 | 377 | // SC16IS750 configuration register values |
wim | 1:0440152c5387 | 378 | // Several configuration registers are write-only. Need to save values to allow restoring. |
wim | 0:d64854a60f95 | 379 | struct SC16IS750_cfg { |
wim | 0:d64854a60f95 | 380 | char baudrate; |
wim | 0:d64854a60f95 | 381 | char dataformat; |
wim | 0:d64854a60f95 | 382 | char flowctrl; |
wim | 1:0440152c5387 | 383 | char fifoformat; |
wim | 1:0440152c5387 | 384 | bool fifoenable; |
wim | 0:d64854a60f95 | 385 | }; |
wim | 0:d64854a60f95 | 386 | |
wim | 0:d64854a60f95 | 387 | |
wim | 0:d64854a60f95 | 388 | /** Determine if there is a character available to read. |
wim | 2:76cb93b511f2 | 389 | * This is data that's already arrived and stored in the receive |
wim | 2:76cb93b511f2 | 390 | * buffer (which holds 64 chars). |
wim | 2:76cb93b511f2 | 391 | * |
wim | 0:d64854a60f95 | 392 | * @return 1 if there is a character available to read, 0 otherwise |
wim | 0:d64854a60f95 | 393 | */ |
wim | 1:0440152c5387 | 394 | int readable(); |
wim | 0:d64854a60f95 | 395 | |
wim | 2:76cb93b511f2 | 396 | /** Determine how many characters are available to read. |
wim | 2:76cb93b511f2 | 397 | * This is data that's already arrived and stored in the receive |
wim | 2:76cb93b511f2 | 398 | * buffer (which holds 64 chars). |
wim | 2:76cb93b511f2 | 399 | * |
wim | 0:d64854a60f95 | 400 | * @return int Characters available to read |
wim | 2:76cb93b511f2 | 401 | */ |
wim | 1:0440152c5387 | 402 | int readableCount(); |
wim | 0:d64854a60f95 | 403 | |
wim | 0:d64854a60f95 | 404 | /** Determine if there is space available to write a character. |
wim | 0:d64854a60f95 | 405 | * @return 1 if there is a space for a character to write, 0 otherwise |
wim | 0:d64854a60f95 | 406 | */ |
wim | 1:0440152c5387 | 407 | int writable(); |
wim | 0:d64854a60f95 | 408 | |
wim | 2:76cb93b511f2 | 409 | |
wim | 1:0440152c5387 | 410 | /** Determine how much space available for writing characters. |
wim | 2:76cb93b511f2 | 411 | * This considers data that's already stored in the transmit |
wim | 2:76cb93b511f2 | 412 | * buffer (which holds 64 chars). |
wim | 2:76cb93b511f2 | 413 | * |
wim | 1:0440152c5387 | 414 | * @return int character space available to write |
wim | 2:76cb93b511f2 | 415 | */ |
wim | 1:0440152c5387 | 416 | int writableCount(); |
wim | 1:0440152c5387 | 417 | |
wim | 1:0440152c5387 | 418 | /** |
wim | 1:0440152c5387 | 419 | * Read char from UART Bridge. |
wim | 1:0440152c5387 | 420 | * Acts in the same manner as 'Serial.read()'. |
wim | 1:0440152c5387 | 421 | * @param none |
wim | 1:0440152c5387 | 422 | * @return char read or -1 if no data available. |
wim | 1:0440152c5387 | 423 | */ |
wim | 1:0440152c5387 | 424 | int getc(); |
wim | 1:0440152c5387 | 425 | |
wim | 1:0440152c5387 | 426 | /** |
wim | 1:0440152c5387 | 427 | * Write char to UART Bridge. Blocking when no free space in FIFO |
wim | 1:0440152c5387 | 428 | * @param value char to be written |
wim | 1:0440152c5387 | 429 | * @return value written |
wim | 1:0440152c5387 | 430 | */ |
wim | 4:12446ee9f9c8 | 431 | int putc(int c); |
wim | 4:12446ee9f9c8 | 432 | |
wim | 4:12446ee9f9c8 | 433 | |
wim | 4:12446ee9f9c8 | 434 | #if DOXYGEN_ONLY |
wim | 4:12446ee9f9c8 | 435 | /** Write a formatted string to the UART Bridge. Blocking when no free space in FIFO |
wim | 4:12446ee9f9c8 | 436 | * |
wim | 4:12446ee9f9c8 | 437 | * @param format A printf-style format string, followed by the |
wim | 4:12446ee9f9c8 | 438 | * variables to use in formatting the string. |
wim | 4:12446ee9f9c8 | 439 | */ |
wim | 4:12446ee9f9c8 | 440 | int printf(const char* format, ...); |
wim | 4:12446ee9f9c8 | 441 | #endif |
wim | 1:0440152c5387 | 442 | |
wim | 2:76cb93b511f2 | 443 | |
wim | 2:76cb93b511f2 | 444 | /** |
wim | 2:76cb93b511f2 | 445 | * Write char string to UART Bridge. Blocking when no free space in FIFO |
wim | 2:76cb93b511f2 | 446 | * @param *str char string to be written |
wim | 2:76cb93b511f2 | 447 | * @return none |
wim | 2:76cb93b511f2 | 448 | */ |
wim | 3:9783b6bde958 | 449 | void writeString(const char *str); |
wim | 3:9783b6bde958 | 450 | |
wim | 3:9783b6bde958 | 451 | |
wim | 3:9783b6bde958 | 452 | /** |
wim | 3:9783b6bde958 | 453 | * Write byte array to UART Bridge. Blocking when no free space in FIFO |
wim | 3:9783b6bde958 | 454 | * @param *data byte array to be written |
wim | 3:9783b6bde958 | 455 | * @param len number of bytes to write |
wim | 3:9783b6bde958 | 456 | * @return none |
wim | 3:9783b6bde958 | 457 | */ |
wim | 3:9783b6bde958 | 458 | void writeBytes(const char *data, int len); |
wim | 0:d64854a60f95 | 459 | |
wim | 0:d64854a60f95 | 460 | /** Set baudrate of the serial port. |
wim | 0:d64854a60f95 | 461 | * @param baud integer baudrate (4800, 9600 etc) |
wim | 0:d64854a60f95 | 462 | * @return none |
wim | 0:d64854a60f95 | 463 | */ |
wim | 2:76cb93b511f2 | 464 | void baud(int baudrate = SC16IS750_DEFAULT_BAUDRATE); |
wim | 0:d64854a60f95 | 465 | |
wim | 0:d64854a60f95 | 466 | /** Set the transmission format used by the serial port. |
wim | 0:d64854a60f95 | 467 | * @param bits The number of bits in a word (5-8; default = 8) |
wim | 0:d64854a60f95 | 468 | * @param parity The parity used (Serial::None, Serial::Odd, Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None) |
wim | 0:d64854a60f95 | 469 | * @param stop_bits The number of stop bits (1 or 2; default = 1) |
wim | 2:76cb93b511f2 | 470 | * @return none |
wim | 0:d64854a60f95 | 471 | */ |
wim | 1:0440152c5387 | 472 | void format(int bits=8, Serial::Parity parity=Serial::None, int stop_bits=1); |
wim | 2:76cb93b511f2 | 473 | |
wim | 1:0440152c5387 | 474 | #if(0) |
wim | 1:0440152c5387 | 475 | /** Attach a function to call whenever a serial interrupt is generated |
wim | 1:0440152c5387 | 476 | * |
wim | 1:0440152c5387 | 477 | * @param fptr A pointer to a void function, or 0 to set as none |
wim | 1:0440152c5387 | 478 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
wim | 1:0440152c5387 | 479 | */ |
wim | 1:0440152c5387 | 480 | void attach(void (*fptr)(void), IrqType type=RxIrq); |
wim | 1:0440152c5387 | 481 | |
wim | 1:0440152c5387 | 482 | /** Attach a member function to call whenever a serial interrupt is generated |
wim | 1:0440152c5387 | 483 | * |
wim | 1:0440152c5387 | 484 | * @param tptr pointer to the object to call the member function on |
wim | 1:0440152c5387 | 485 | * @param mptr pointer to the member function to be called |
wim | 1:0440152c5387 | 486 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
wim | 2:76cb93b511f2 | 487 | * @return none |
wim | 1:0440152c5387 | 488 | */ |
wim | 1:0440152c5387 | 489 | template<typename T> |
wim | 1:0440152c5387 | 490 | void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) { |
wim | 1:0440152c5387 | 491 | if((mptr != NULL) && (tptr != NULL)) { |
wim | 1:0440152c5387 | 492 | _irq[type].attach(tptr, mptr); |
wim | 1:0440152c5387 | 493 | serial_irq_set(&_serial, (SerialIrq)type, 1); |
wim | 1:0440152c5387 | 494 | } |
wim | 1:0440152c5387 | 495 | } |
wim | 1:0440152c5387 | 496 | #endif |
wim | 1:0440152c5387 | 497 | |
wim | 1:0440152c5387 | 498 | /** Generate a break condition on the serial line |
wim | 2:76cb93b511f2 | 499 | * @param none |
wim | 2:76cb93b511f2 | 500 | * @return none |
wim | 1:0440152c5387 | 501 | */ |
wim | 1:0440152c5387 | 502 | void send_break(); |
wim | 0:d64854a60f95 | 503 | |
wim | 1:0440152c5387 | 504 | |
wim | 1:0440152c5387 | 505 | /** Set a break condition on the serial line |
wim | 1:0440152c5387 | 506 | * @param enable break condition |
wim | 2:76cb93b511f2 | 507 | * @return none |
wim | 1:0440152c5387 | 508 | */ |
wim | 1:0440152c5387 | 509 | void set_break(bool enable=false); |
wim | 1:0440152c5387 | 510 | |
wim | 1:0440152c5387 | 511 | |
wim | 1:0440152c5387 | 512 | /** Set the flow control type on the serial port |
wim | 1:0440152c5387 | 513 | * Added for compatibility with Serial Class. |
wim | 2:76cb93b511f2 | 514 | * SC16IS750 supports only Flow, Pins can not be selected. |
wim | 3:9783b6bde958 | 515 | * This method sets hardware flow control levels. SC16IS750 supports XON/XOFF, but this is not implemented. |
wim | 1:0440152c5387 | 516 | * |
wim | 1:0440152c5387 | 517 | * @param type the flow control type (Disabled, RTS, CTS, RTSCTS) |
wim | 1:0440152c5387 | 518 | * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS) |
wim | 1:0440152c5387 | 519 | * @param flow2 the second flow control pin (CTS for RTSCTS) |
wim | 2:76cb93b511f2 | 520 | * @return none |
wim | 1:0440152c5387 | 521 | */ |
wim | 1:0440152c5387 | 522 | void set_flow_control(Flow type=Disabled, PinName flow1=NC, PinName flow2=NC); |
wim | 2:76cb93b511f2 | 523 | |
wim | 2:76cb93b511f2 | 524 | |
wim | 2:76cb93b511f2 | 525 | /** Set the RX FIFO flow control levels |
wim | 3:9783b6bde958 | 526 | * This method sets hardware flow control levels. SC16IS750 supports XON/XOFF, but this is not implemented. |
wim | 2:76cb93b511f2 | 527 | * Should be called BEFORE Auto RTS is enabled. |
wim | 2:76cb93b511f2 | 528 | * |
wim | 2:76cb93b511f2 | 529 | * @param resume trigger level to resume transmission (0..15, meaning 0-60 with a granularity of 4) |
wim | 2:76cb93b511f2 | 530 | * @param halt trigger level to resume transmission (0..15, meaning 0-60 with granularity of 4) |
wim | 2:76cb93b511f2 | 531 | * @return none |
wim | 2:76cb93b511f2 | 532 | */ |
wim | 2:76cb93b511f2 | 533 | void set_flow_triggers(int resume = TCR_RESUME_DEFAULT, int halt = TCR_HALT_DEFAULT); |
wim | 2:76cb93b511f2 | 534 | |
wim | 2:76cb93b511f2 | 535 | |
wim | 2:76cb93b511f2 | 536 | /** Set the Modem Control register |
wim | 2:76cb93b511f2 | 537 | * This method sets prescaler, enables TCR and TLR |
wim | 2:76cb93b511f2 | 538 | * |
wim | 2:76cb93b511f2 | 539 | * @param none |
wim | 2:76cb93b511f2 | 540 | * @return none |
wim | 2:76cb93b511f2 | 541 | */ |
wim | 2:76cb93b511f2 | 542 | void set_modem_control(); |
wim | 2:76cb93b511f2 | 543 | |
wim | 1:0440152c5387 | 544 | |
wim | 0:d64854a60f95 | 545 | /** |
wim | 0:d64854a60f95 | 546 | * Check that UART is connected and operational. |
wim | 0:d64854a60f95 | 547 | * @param none |
wim | 0:d64854a60f95 | 548 | * @return bool true when connected, false otherwise |
wim | 0:d64854a60f95 | 549 | */ |
wim | 0:d64854a60f95 | 550 | bool connected(); |
wim | 0:d64854a60f95 | 551 | |
wim | 0:d64854a60f95 | 552 | |
wim | 2:76cb93b511f2 | 553 | |
wim | 3:9783b6bde958 | 554 | /** FIFO control, sets TX and RX IRQ trigger levels and enables FIFO and save in _config |
wim | 2:76cb93b511f2 | 555 | * Note FCR[5:4] (=TX_IRQ_LVL) only accessible when EFR[4] is set (enhanced functions enable) |
wim | 2:76cb93b511f2 | 556 | * Note TLR only accessible when EFR[4] is set (enhanced functions enable) and MCR[2] is set |
wim | 2:76cb93b511f2 | 557 | * @param none |
wim | 2:76cb93b511f2 | 558 | * @return none |
wim | 2:76cb93b511f2 | 559 | */ |
wim | 2:76cb93b511f2 | 560 | void set_fifo_control(); |
wim | 2:76cb93b511f2 | 561 | |
wim | 1:0440152c5387 | 562 | |
wim | 1:0440152c5387 | 563 | /** Flush the UART FIFOs while maintaining current FIFO mode. |
wim | 1:0440152c5387 | 564 | * @param none |
wim | 1:0440152c5387 | 565 | * @return none |
wim | 1:0440152c5387 | 566 | */ |
wim | 1:0440152c5387 | 567 | void flush(); |
wim | 0:d64854a60f95 | 568 | |
wim | 1:0440152c5387 | 569 | |
wim | 1:0440152c5387 | 570 | /** Set direction of I/O port pins. |
wim | 1:0440152c5387 | 571 | * This method is specific to the SPI-I2C UART and not found on the 16750 |
wim | 5:ff3e57bebb6a | 572 | * Note: The SC16IS752 does not have separate GPIOs for Channel_A and Channel_B. |
wim | 1:0440152c5387 | 573 | * @param bits Bitpattern for I/O (1=output, 0=input) |
wim | 1:0440152c5387 | 574 | * @return none |
wim | 1:0440152c5387 | 575 | */ |
wim | 1:0440152c5387 | 576 | void ioSetDirection(unsigned char bits); |
wim | 0:d64854a60f95 | 577 | |
wim | 1:0440152c5387 | 578 | /** Set bits of I/O port pins. |
wim | 1:0440152c5387 | 579 | * This method is specific to the SPI-I2C UART and not found on the 16750 |
wim | 5:ff3e57bebb6a | 580 | * Note: The SC16IS752 does not have separate GPIOs for Channel_A and Channel_B. |
wim | 1:0440152c5387 | 581 | * @param bits Bitpattern for I/O (1= set output bit, 0 = clear output bit) |
wim | 1:0440152c5387 | 582 | * @return none |
wim | 1:0440152c5387 | 583 | */ |
wim | 1:0440152c5387 | 584 | void ioSetState(unsigned char bits); |
wim | 1:0440152c5387 | 585 | |
wim | 1:0440152c5387 | 586 | /** Get bits of I/O port pins. |
wim | 1:0440152c5387 | 587 | * This method is specific to the SPI-I2C UART and not found on the 16750 |
wim | 5:ff3e57bebb6a | 588 | * Note: The SC16IS752 does not have separate GPIOs for Channel_A and Channel_B. |
wim | 1:0440152c5387 | 589 | * @param none |
wim | 1:0440152c5387 | 590 | * @return bits Bitpattern for I/O (1= bit set, 0 = bit cleared) |
wim | 1:0440152c5387 | 591 | */ |
wim | 1:0440152c5387 | 592 | unsigned char ioGetState(); |
wim | 1:0440152c5387 | 593 | |
wim | 1:0440152c5387 | 594 | |
wim | 1:0440152c5387 | 595 | /** Software Reset SC16IS750 device. |
wim | 1:0440152c5387 | 596 | * This method is specific to the SPI-I2C UART and not found on the 16750 |
wim | 5:ff3e57bebb6a | 597 | * Note: The SC16IS752 does not have separate Reset for Channel_A and Channel_B. |
wim | 1:0440152c5387 | 598 | * @param none |
wim | 1:0440152c5387 | 599 | * @return none |
wim | 1:0440152c5387 | 600 | */ |
wim | 1:0440152c5387 | 601 | void swReset(); |
wim | 1:0440152c5387 | 602 | |
wim | 0:d64854a60f95 | 603 | |
wim | 4:12446ee9f9c8 | 604 | /** Hardware Reset SC16IS750 device. |
wim | 4:12446ee9f9c8 | 605 | * Pure virtual, must be declared in derived class. |
wim | 4:12446ee9f9c8 | 606 | * This method is only functional when the Reset pin has been declared and is also connected |
wim | 4:12446ee9f9c8 | 607 | * @param none |
wim | 4:12446ee9f9c8 | 608 | * @return none |
wim | 4:12446ee9f9c8 | 609 | */ |
wim | 4:12446ee9f9c8 | 610 | virtual void hwReset() =0; |
wim | 4:12446ee9f9c8 | 611 | |
wim | 0:d64854a60f95 | 612 | /** Write value to internal register. |
wim | 0:d64854a60f95 | 613 | * Pure virtual, must be declared in derived class. |
wim | 1:0440152c5387 | 614 | * @param registerAddress The address of the Register (enum RegisterName) |
wim | 0:d64854a60f95 | 615 | * @param data The 8bit value to write |
wim | 0:d64854a60f95 | 616 | * @return none |
wim | 0:d64854a60f95 | 617 | */ |
wim | 1:0440152c5387 | 618 | virtual void writeRegister (RegisterName register_address, char data ) =0; |
wim | 0:d64854a60f95 | 619 | |
wim | 0:d64854a60f95 | 620 | /** Read value from internal register. |
wim | 0:d64854a60f95 | 621 | * Pure virtual, must be declared in derived class. |
wim | 1:0440152c5387 | 622 | * @param registerAddress The address of the Register (enum RegisterName) |
wim | 0:d64854a60f95 | 623 | * @return char The 8bit value read from the register |
wim | 0:d64854a60f95 | 624 | */ |
wim | 1:0440152c5387 | 625 | virtual char readRegister (RegisterName register_address ) =0; |
wim | 1:0440152c5387 | 626 | |
wim | 3:9783b6bde958 | 627 | /** Write multiple datavalues to Transmitregister. |
wim | 3:9783b6bde958 | 628 | * More Efficient implementation than writing individual bytes |
wim | 3:9783b6bde958 | 629 | * Pure virtual, must be declared in derived class. |
wim | 3:9783b6bde958 | 630 | * @param char* databytes The pointer to the block of data |
wim | 3:9783b6bde958 | 631 | * @param len The number of bytes to write |
wim | 3:9783b6bde958 | 632 | * @return none |
wim | 3:9783b6bde958 | 633 | */ |
wim | 3:9783b6bde958 | 634 | virtual void writeDataBlock (const char *data, int len ) =0; |
wim | 3:9783b6bde958 | 635 | |
wim | 0:d64854a60f95 | 636 | |
wim | 1:0440152c5387 | 637 | /** Initialise internal registers |
wim | 1:0440152c5387 | 638 | * Should be in protection section. Public for testing purposes |
wim | 1:0440152c5387 | 639 | * If initialisation fails this method does not return. |
wim | 1:0440152c5387 | 640 | * @param none |
wim | 1:0440152c5387 | 641 | * @return none |
wim | 1:0440152c5387 | 642 | */ |
wim | 1:0440152c5387 | 643 | void _init(); |
wim | 1:0440152c5387 | 644 | |
wim | 0:d64854a60f95 | 645 | protected: |
wim | 0:d64854a60f95 | 646 | //protected is accessible to derived classes, but not to external users |
wim | 0:d64854a60f95 | 647 | |
wim | 2:76cb93b511f2 | 648 | |
wim | 2:76cb93b511f2 | 649 | /** Constructor is protected for this abstract Class |
wim | 1:0440152c5387 | 650 | * |
wim | 1:0440152c5387 | 651 | */ |
wim | 1:0440152c5387 | 652 | SC16IS750(); |
wim | 1:0440152c5387 | 653 | |
wim | 3:9783b6bde958 | 654 | /** Needed to implement Stream |
wim | 3:9783b6bde958 | 655 | * |
wim | 3:9783b6bde958 | 656 | * Read char from UART Bridge. |
wim | 3:9783b6bde958 | 657 | * Acts in the same manner as 'Serial.read()'. |
wim | 3:9783b6bde958 | 658 | * @param none |
wim | 3:9783b6bde958 | 659 | * @return char read or -1 if no data available. |
wim | 3:9783b6bde958 | 660 | */ |
wim | 3:9783b6bde958 | 661 | virtual int _getc() { |
wim | 3:9783b6bde958 | 662 | return getc(); |
wim | 3:9783b6bde958 | 663 | } |
wim | 3:9783b6bde958 | 664 | |
wim | 3:9783b6bde958 | 665 | |
wim | 3:9783b6bde958 | 666 | /** Needed to implement Stream |
wim | 3:9783b6bde958 | 667 | * |
wim | 3:9783b6bde958 | 668 | * Write char to UART Bridge. Blocking when no free space in FIFO |
wim | 3:9783b6bde958 | 669 | * @param value char to be written |
wim | 3:9783b6bde958 | 670 | * @return value written |
wim | 3:9783b6bde958 | 671 | */ |
wim | 3:9783b6bde958 | 672 | virtual int _putc(int c) { |
wim | 3:9783b6bde958 | 673 | return putc(c); |
wim | 3:9783b6bde958 | 674 | } |
wim | 3:9783b6bde958 | 675 | |
wim | 3:9783b6bde958 | 676 | /** Needed to implement Stream |
wim | 3:9783b6bde958 | 677 | * |
wim | 3:9783b6bde958 | 678 | */ |
wim | 3:9783b6bde958 | 679 | virtual int peek() {return 0;}; |
wim | 4:12446ee9f9c8 | 680 | |
wim | 3:9783b6bde958 | 681 | |
wim | 2:76cb93b511f2 | 682 | // Save config settings |
wim | 0:d64854a60f95 | 683 | SC16IS750_cfg _config; |
wim | 0:d64854a60f95 | 684 | |
wim | 0:d64854a60f95 | 685 | private: |
wim | 0:d64854a60f95 | 686 | //private is not accessible to derived classes, nor external users |
wim | 1:0440152c5387 | 687 | |
wim | 0:d64854a60f95 | 688 | }; |
wim | 0:d64854a60f95 | 689 | |
wim | 0:d64854a60f95 | 690 | |
wim | 0:d64854a60f95 | 691 | |
wim | 4:12446ee9f9c8 | 692 | /** Class SC16IS750_SPI for a bridge between SPI and a Serial port |
wim | 0:d64854a60f95 | 693 | * |
wim | 0:d64854a60f95 | 694 | * @code |
wim | 0:d64854a60f95 | 695 | * #include "mbed.h" |
wim | 0:d64854a60f95 | 696 | * #include "SC16IS750.h" |
wim | 0:d64854a60f95 | 697 | * |
wim | 0:d64854a60f95 | 698 | * SPI spi(PTD2, PTD3, PTD1); //MOSI, MISO, SCK |
wim | 0:d64854a60f95 | 699 | * SC16IS750_SPI serial_spi(&spi, PTD0); |
wim | 0:d64854a60f95 | 700 | * |
wim | 0:d64854a60f95 | 701 | * Serial pc(USBTX,USBRX); |
wim | 0:d64854a60f95 | 702 | * |
wim | 0:d64854a60f95 | 703 | * int main() { |
wim | 0:d64854a60f95 | 704 | * pc.printf("\nHello World!\n"); |
wim | 0:d64854a60f95 | 705 | * |
wim | 0:d64854a60f95 | 706 | * while(1) { |
wim | 0:d64854a60f95 | 707 | * serial_spi.ioSetState(0x00); |
wim | 0:d64854a60f95 | 708 | * wait(0.5); |
wim | 2:76cb93b511f2 | 709 | * serial_spi.ioSetState(0xFF); |
wim | 2:76cb93b511f2 | 710 | * wait(0.5); |
wim | 2:76cb93b511f2 | 711 | * serial_spi.putc('*'); |
wim | 0:d64854a60f95 | 712 | * pc.putc('*'); |
wim | 0:d64854a60f95 | 713 | * } |
wim | 0:d64854a60f95 | 714 | * } |
wim | 0:d64854a60f95 | 715 | * |
wim | 0:d64854a60f95 | 716 | * @endcode |
wim | 0:d64854a60f95 | 717 | */ |
wim | 0:d64854a60f95 | 718 | class SC16IS750_SPI : public SC16IS750 { |
wim | 0:d64854a60f95 | 719 | public: |
wim | 0:d64854a60f95 | 720 | |
wim | 4:12446ee9f9c8 | 721 | /** Create an SC16IS750_SPI object using a specified SPI bus and CS |
wim | 0:d64854a60f95 | 722 | * |
wim | 0:d64854a60f95 | 723 | * @param SPI &spi the SPI port to connect to |
wim | 4:12446ee9f9c8 | 724 | * @param cs Pinname of the CS pin (active low) |
wim | 4:12446ee9f9c8 | 725 | * @param rst Pinname for Reset pin (active low) Optional, Default = NC |
wim | 0:d64854a60f95 | 726 | */ |
wim | 4:12446ee9f9c8 | 727 | SC16IS750_SPI(SPI *spi, PinName cs, PinName rst = NC); |
wim | 4:12446ee9f9c8 | 728 | |
wim | 4:12446ee9f9c8 | 729 | /** Destruct SC16IS750_SPI bridge object |
wim | 4:12446ee9f9c8 | 730 | * |
wim | 4:12446ee9f9c8 | 731 | * @param none |
wim | 4:12446ee9f9c8 | 732 | * @return none |
wim | 4:12446ee9f9c8 | 733 | */ |
wim | 4:12446ee9f9c8 | 734 | virtual ~SC16IS750_SPI(); |
wim | 4:12446ee9f9c8 | 735 | |
wim | 0:d64854a60f95 | 736 | |
wim | 0:d64854a60f95 | 737 | /** Write value to internal register. |
wim | 1:0440152c5387 | 738 | * @param registerAddress The address of the Register (enum RegisterName) |
wim | 0:d64854a60f95 | 739 | * @param data The 8bit value to write |
wim | 0:d64854a60f95 | 740 | * @return none |
wim | 0:d64854a60f95 | 741 | */ |
wim | 0:d64854a60f95 | 742 | virtual void writeRegister(SC16IS750::RegisterName registerAddress, char data); |
wim | 0:d64854a60f95 | 743 | |
wim | 0:d64854a60f95 | 744 | /** Read value from internal register. |
wim | 1:0440152c5387 | 745 | * @param registerAddress The address of the Register (enum RegisterName) |
wim | 0:d64854a60f95 | 746 | * @return char The 8bit value read from the register |
wim | 0:d64854a60f95 | 747 | */ |
wim | 0:d64854a60f95 | 748 | virtual char readRegister(SC16IS750::RegisterName registerAddress); |
wim | 0:d64854a60f95 | 749 | |
wim | 3:9783b6bde958 | 750 | /** Write multiple datavalues to Transmitregister. |
wim | 3:9783b6bde958 | 751 | * More Efficient implementation than writing individual bytes |
wim | 3:9783b6bde958 | 752 | * Assume that previous check confirmed that the FIFO has sufficient free space to store the data |
wim | 4:12446ee9f9c8 | 753 | * |
wim | 3:9783b6bde958 | 754 | * @param char* databytes The pointer to the block of data |
wim | 3:9783b6bde958 | 755 | * @param len The number of bytes to write |
wim | 3:9783b6bde958 | 756 | * @return none |
wim | 3:9783b6bde958 | 757 | */ |
wim | 3:9783b6bde958 | 758 | virtual void writeDataBlock (const char *data, int len ); |
wim | 3:9783b6bde958 | 759 | |
wim | 4:12446ee9f9c8 | 760 | /** Hardware Reset SC16IS750 device. |
wim | 4:12446ee9f9c8 | 761 | * This method is only functional when the Reset pin has been declared and is also connected |
wim | 4:12446ee9f9c8 | 762 | * @param none |
wim | 4:12446ee9f9c8 | 763 | * @return none |
wim | 4:12446ee9f9c8 | 764 | */ |
wim | 4:12446ee9f9c8 | 765 | virtual void hwReset(); |
wim | 4:12446ee9f9c8 | 766 | |
wim | 3:9783b6bde958 | 767 | |
wim | 0:d64854a60f95 | 768 | protected: |
wim | 0:d64854a60f95 | 769 | //protected is accessible to derived classes, but not to external users |
wim | 0:d64854a60f95 | 770 | |
wim | 0:d64854a60f95 | 771 | |
wim | 0:d64854a60f95 | 772 | private: |
wim | 0:d64854a60f95 | 773 | SPI *_spi; //SPI bus reference |
wim | 4:12446ee9f9c8 | 774 | DigitalOut _cs; //CS of SPI device (active low) |
wim | 4:12446ee9f9c8 | 775 | |
wim | 4:12446ee9f9c8 | 776 | /** Optional Hardware Reset pin for the bridge device (active low) |
wim | 4:12446ee9f9c8 | 777 | * Default PinName value is NC |
wim | 4:12446ee9f9c8 | 778 | */ |
wim | 4:12446ee9f9c8 | 779 | DigitalOut* _reset; //Reset the Bridge device (active low) |
wim | 0:d64854a60f95 | 780 | |
wim | 0:d64854a60f95 | 781 | }; |
wim | 0:d64854a60f95 | 782 | |
wim | 0:d64854a60f95 | 783 | |
wim | 0:d64854a60f95 | 784 | |
wim | 4:12446ee9f9c8 | 785 | /** Class SC16IS750_I2C for a bridge between I2C and a Serial port |
wim | 0:d64854a60f95 | 786 | * |
wim | 0:d64854a60f95 | 787 | * @code |
wim | 0:d64854a60f95 | 788 | * #include "mbed.h" |
wim | 0:d64854a60f95 | 789 | * #include "SC16IS750.h" |
wim | 0:d64854a60f95 | 790 | * |
wim | 0:d64854a60f95 | 791 | * I2C i2c(PTE0, PTE1); //SDA, SCL |
wim | 5:ff3e57bebb6a | 792 | * SC16IS750_I2C serial_i2c(&i2c, SC16IS750_DEFAULT_ADDR); |
wim | 0:d64854a60f95 | 793 | * |
wim | 0:d64854a60f95 | 794 | * Serial pc(USBTX,USBRX); |
wim | 0:d64854a60f95 | 795 | * |
wim | 0:d64854a60f95 | 796 | * int main() { |
wim | 0:d64854a60f95 | 797 | * pc.printf("\nHello World!\n"); |
wim | 0:d64854a60f95 | 798 | * |
wim | 0:d64854a60f95 | 799 | * while(1) { |
wim | 0:d64854a60f95 | 800 | * serial_i2c.ioSetState(0x00); |
wim | 0:d64854a60f95 | 801 | * wait(0.5); |
wim | 2:76cb93b511f2 | 802 | * serial_i2c.ioSetState(0xFF); |
wim | 2:76cb93b511f2 | 803 | * wait(0.5); |
wim | 2:76cb93b511f2 | 804 | * serial_i2c.putc('*'); |
wim | 0:d64854a60f95 | 805 | * pc.putc('*'); |
wim | 0:d64854a60f95 | 806 | * } |
wim | 0:d64854a60f95 | 807 | * } |
wim | 0:d64854a60f95 | 808 | * |
wim | 0:d64854a60f95 | 809 | * @endcode |
wim | 0:d64854a60f95 | 810 | */ |
wim | 0:d64854a60f95 | 811 | class SC16IS750_I2C : public SC16IS750 { |
wim | 0:d64854a60f95 | 812 | public: |
wim | 0:d64854a60f95 | 813 | |
wim | 4:12446ee9f9c8 | 814 | /** Create an SC16IS750_I2C object using a specified I2C bus and slaveaddress |
wim | 0:d64854a60f95 | 815 | * |
wim | 0:d64854a60f95 | 816 | * @param I2C &i2c the I2C port to connect to |
wim | 0:d64854a60f95 | 817 | * @param char deviceAddress the address of the SC16IS750 |
wim | 4:12446ee9f9c8 | 818 | * @param rst Pinname for Reset pin (active low) Optional, Default = NC |
wim | 0:d64854a60f95 | 819 | */ |
wim | 4:12446ee9f9c8 | 820 | SC16IS750_I2C(I2C *i2c, uint8_t deviceAddress = SC16IS750_DEFAULT_ADDR, PinName rst = NC); |
wim | 4:12446ee9f9c8 | 821 | |
wim | 4:12446ee9f9c8 | 822 | |
wim | 4:12446ee9f9c8 | 823 | /** Destruct SC16IS750_I2C bridge object |
wim | 4:12446ee9f9c8 | 824 | * |
wim | 4:12446ee9f9c8 | 825 | * @param none |
wim | 4:12446ee9f9c8 | 826 | * @return none |
wim | 4:12446ee9f9c8 | 827 | */ |
wim | 4:12446ee9f9c8 | 828 | virtual ~SC16IS750_I2C(); |
wim | 4:12446ee9f9c8 | 829 | |
wim | 0:d64854a60f95 | 830 | |
wim | 0:d64854a60f95 | 831 | /** Write value to internal register. |
wim | 1:0440152c5387 | 832 | * @param registerAddress The address of the Register (enum RegisterName) |
wim | 0:d64854a60f95 | 833 | * @param data The 8bit value to write |
wim | 0:d64854a60f95 | 834 | * @return none |
wim | 0:d64854a60f95 | 835 | */ |
wim | 0:d64854a60f95 | 836 | virtual void writeRegister(SC16IS750::RegisterName register_address, char data ); |
wim | 0:d64854a60f95 | 837 | |
wim | 0:d64854a60f95 | 838 | /** Read value from internal register. |
wim | 1:0440152c5387 | 839 | * @param registerAddress The address of the Register (enum RegisterName) |
wim | 0:d64854a60f95 | 840 | * @return char The 8bit value read from the register |
wim | 0:d64854a60f95 | 841 | */ |
wim | 0:d64854a60f95 | 842 | virtual char readRegister(SC16IS750::RegisterName register_address ); |
wim | 0:d64854a60f95 | 843 | |
wim | 3:9783b6bde958 | 844 | |
wim | 3:9783b6bde958 | 845 | /** Write multiple datavalues to Transmitregister. |
wim | 3:9783b6bde958 | 846 | * More Efficient implementation than writing individual bytes |
wim | 3:9783b6bde958 | 847 | * Assume that previous check confirmed that the FIFO has sufficient free space to store the data |
wim | 3:9783b6bde958 | 848 | * Pure virtual, must be declared in derived class. |
wim | 3:9783b6bde958 | 849 | * @param char* databytes The pointer to the block of data |
wim | 3:9783b6bde958 | 850 | * @param len The number of bytes to write |
wim | 3:9783b6bde958 | 851 | * @return none |
wim | 3:9783b6bde958 | 852 | */ |
wim | 3:9783b6bde958 | 853 | virtual void writeDataBlock (const char *data, int len ); |
wim | 3:9783b6bde958 | 854 | |
wim | 4:12446ee9f9c8 | 855 | |
wim | 4:12446ee9f9c8 | 856 | /** Hardware Reset SC16IS750 device. |
wim | 4:12446ee9f9c8 | 857 | * This method is only functional when the Reset pin has been declared and is also connected |
wim | 4:12446ee9f9c8 | 858 | * @param none |
wim | 4:12446ee9f9c8 | 859 | * @return none |
wim | 4:12446ee9f9c8 | 860 | */ |
wim | 4:12446ee9f9c8 | 861 | virtual void hwReset(); |
wim | 4:12446ee9f9c8 | 862 | |
wim | 4:12446ee9f9c8 | 863 | |
wim | 0:d64854a60f95 | 864 | protected: |
wim | 0:d64854a60f95 | 865 | //protected is accessible to derived classes, but not to external users |
wim | 0:d64854a60f95 | 866 | |
wim | 0:d64854a60f95 | 867 | |
wim | 0:d64854a60f95 | 868 | private: |
wim | 0:d64854a60f95 | 869 | I2C *_i2c; //I2C bus reference |
wim | 0:d64854a60f95 | 870 | uint8_t _slaveAddress; //I2C Slave address of device |
wim | 4:12446ee9f9c8 | 871 | |
wim | 4:12446ee9f9c8 | 872 | /** Optional Hardware Reset pin for the bridge device (active low) |
wim | 4:12446ee9f9c8 | 873 | * Default PinName value is NC |
wim | 4:12446ee9f9c8 | 874 | */ |
wim | 4:12446ee9f9c8 | 875 | DigitalOut* _reset; //Reset the Bridge device (active low) |
wim | 4:12446ee9f9c8 | 876 | |
wim | 4:12446ee9f9c8 | 877 | }; |
wim | 4:12446ee9f9c8 | 878 | |
wim | 4:12446ee9f9c8 | 879 | |
wim | 4:12446ee9f9c8 | 880 | |
wim | 4:12446ee9f9c8 | 881 | /** Class SC16IS752_SPI for a bridge between SPI and a Serial port |
wim | 4:12446ee9f9c8 | 882 | * |
wim | 4:12446ee9f9c8 | 883 | * @code |
wim | 4:12446ee9f9c8 | 884 | * #include "mbed.h" |
wim | 4:12446ee9f9c8 | 885 | * #include "SC16IS750.h" |
wim | 4:12446ee9f9c8 | 886 | * |
wim | 4:12446ee9f9c8 | 887 | * SPI spi(PTD2, PTD3, PTD1); //MOSI, MISO, SCK |
wim | 4:12446ee9f9c8 | 888 | * SC16IS750_SPI serial_spi(&spi, PTD0, NC, SC16IS750::Channel_B); |
wim | 4:12446ee9f9c8 | 889 | * |
wim | 4:12446ee9f9c8 | 890 | * Serial pc(USBTX,USBRX); |
wim | 4:12446ee9f9c8 | 891 | * |
wim | 4:12446ee9f9c8 | 892 | * int main() { |
wim | 4:12446ee9f9c8 | 893 | * pc.printf("\nHello World!\n"); |
wim | 4:12446ee9f9c8 | 894 | * |
wim | 4:12446ee9f9c8 | 895 | * while(1) { |
wim | 4:12446ee9f9c8 | 896 | * serial_spi.ioSetState(0x00); |
wim | 4:12446ee9f9c8 | 897 | * wait(0.5); |
wim | 4:12446ee9f9c8 | 898 | * serial_spi.ioSetState(0xFF); |
wim | 4:12446ee9f9c8 | 899 | * wait(0.5); |
wim | 4:12446ee9f9c8 | 900 | * serial_spi.putc('*'); |
wim | 4:12446ee9f9c8 | 901 | * pc.putc('*'); |
wim | 4:12446ee9f9c8 | 902 | * } |
wim | 4:12446ee9f9c8 | 903 | * } |
wim | 4:12446ee9f9c8 | 904 | * |
wim | 4:12446ee9f9c8 | 905 | * @endcode |
wim | 4:12446ee9f9c8 | 906 | */ |
wim | 4:12446ee9f9c8 | 907 | class SC16IS752_SPI : public SC16IS750 { |
wim | 4:12446ee9f9c8 | 908 | public: |
wim | 4:12446ee9f9c8 | 909 | |
wim | 4:12446ee9f9c8 | 910 | /** Create an SC16IS752_SPI object using a specified SPI bus and CS |
wim | 5:ff3e57bebb6a | 911 | * Note: The SC16IS752 does not have separate GPIOs for Channel_A and Channel_B. |
wim | 5:ff3e57bebb6a | 912 | * Note: The SC16IS752 does not have separate Reset for Channel_A and Channel_B. |
wim | 4:12446ee9f9c8 | 913 | * |
wim | 4:12446ee9f9c8 | 914 | * @param SPI &spi the SPI port to connect to |
wim | 4:12446ee9f9c8 | 915 | * @param cs Pinname of the CS pin (active low) |
wim | 4:12446ee9f9c8 | 916 | * @param rst Pinname for Reset pin (active low) Optional, Default = NC |
wim | 4:12446ee9f9c8 | 917 | * @param channel UART ChannelName, Default = Channel_A |
wim | 4:12446ee9f9c8 | 918 | */ |
wim | 4:12446ee9f9c8 | 919 | SC16IS752_SPI(SPI *spi, PinName cs, PinName rst = NC, ChannelName channel = SC16IS750::Channel_A ); |
wim | 4:12446ee9f9c8 | 920 | |
wim | 4:12446ee9f9c8 | 921 | /** Destruct SC16IS752_SPI bridge object |
wim | 4:12446ee9f9c8 | 922 | * |
wim | 4:12446ee9f9c8 | 923 | * @param none |
wim | 4:12446ee9f9c8 | 924 | * @return none |
wim | 4:12446ee9f9c8 | 925 | */ |
wim | 4:12446ee9f9c8 | 926 | virtual ~SC16IS752_SPI(); |
wim | 4:12446ee9f9c8 | 927 | |
wim | 4:12446ee9f9c8 | 928 | |
wim | 4:12446ee9f9c8 | 929 | /** Write value to internal register. |
wim | 4:12446ee9f9c8 | 930 | * @param registerAddress The address of the Register (enum RegisterName) |
wim | 4:12446ee9f9c8 | 931 | * @param data The 8bit value to write |
wim | 4:12446ee9f9c8 | 932 | * @return none |
wim | 4:12446ee9f9c8 | 933 | */ |
wim | 4:12446ee9f9c8 | 934 | virtual void writeRegister(SC16IS750::RegisterName registerAddress, char data); |
wim | 4:12446ee9f9c8 | 935 | |
wim | 4:12446ee9f9c8 | 936 | /** Read value from internal register. |
wim | 4:12446ee9f9c8 | 937 | * @param registerAddress The address of the Register (enum RegisterName) |
wim | 4:12446ee9f9c8 | 938 | * @return char The 8bit value read from the register |
wim | 4:12446ee9f9c8 | 939 | */ |
wim | 4:12446ee9f9c8 | 940 | virtual char readRegister(SC16IS750::RegisterName registerAddress); |
wim | 4:12446ee9f9c8 | 941 | |
wim | 4:12446ee9f9c8 | 942 | /** Write multiple datavalues to Transmitregister. |
wim | 4:12446ee9f9c8 | 943 | * More Efficient implementation than writing individual bytes |
wim | 4:12446ee9f9c8 | 944 | * Assume that previous check confirmed that the FIFO has sufficient free space to store the data |
wim | 4:12446ee9f9c8 | 945 | * |
wim | 4:12446ee9f9c8 | 946 | * @param char* databytes The pointer to the block of data |
wim | 4:12446ee9f9c8 | 947 | * @param len The number of bytes to write |
wim | 4:12446ee9f9c8 | 948 | * @return none |
wim | 4:12446ee9f9c8 | 949 | */ |
wim | 4:12446ee9f9c8 | 950 | virtual void writeDataBlock (const char *data, int len ); |
wim | 4:12446ee9f9c8 | 951 | |
wim | 4:12446ee9f9c8 | 952 | /** Hardware Reset SC16IS750 device. |
wim | 4:12446ee9f9c8 | 953 | * This method is only functional when the Reset pin has been declared and is also connected |
wim | 4:12446ee9f9c8 | 954 | * @param none |
wim | 4:12446ee9f9c8 | 955 | * @return none |
wim | 4:12446ee9f9c8 | 956 | */ |
wim | 4:12446ee9f9c8 | 957 | virtual void hwReset(); |
wim | 4:12446ee9f9c8 | 958 | |
wim | 4:12446ee9f9c8 | 959 | |
wim | 4:12446ee9f9c8 | 960 | protected: |
wim | 4:12446ee9f9c8 | 961 | //protected is accessible to derived classes, but not to external users |
wim | 4:12446ee9f9c8 | 962 | |
wim | 4:12446ee9f9c8 | 963 | |
wim | 4:12446ee9f9c8 | 964 | private: |
wim | 4:12446ee9f9c8 | 965 | SPI *_spi; //SPI bus reference |
wim | 4:12446ee9f9c8 | 966 | DigitalOut _cs; //CS of SPI device (active low) |
wim | 4:12446ee9f9c8 | 967 | |
wim | 4:12446ee9f9c8 | 968 | /** Optional Hardware Reset pin for the bridge device (active low) |
wim | 4:12446ee9f9c8 | 969 | * Default PinName value is NC |
wim | 4:12446ee9f9c8 | 970 | */ |
wim | 4:12446ee9f9c8 | 971 | DigitalOut* _reset; //Reset the Bridge device (active low) |
wim | 4:12446ee9f9c8 | 972 | |
wim | 4:12446ee9f9c8 | 973 | // Save Channel setting |
wim | 4:12446ee9f9c8 | 974 | ChannelName _channel; |
wim | 4:12446ee9f9c8 | 975 | }; |
wim | 4:12446ee9f9c8 | 976 | |
wim | 4:12446ee9f9c8 | 977 | |
wim | 4:12446ee9f9c8 | 978 | |
wim | 4:12446ee9f9c8 | 979 | /** Class SC16IS752_I2C for a bridge between I2C and a Serial port |
wim | 4:12446ee9f9c8 | 980 | * |
wim | 4:12446ee9f9c8 | 981 | * @code |
wim | 4:12446ee9f9c8 | 982 | * #include "mbed.h" |
wim | 4:12446ee9f9c8 | 983 | * #include "SC16IS750.h" |
wim | 4:12446ee9f9c8 | 984 | * |
wim | 4:12446ee9f9c8 | 985 | * I2C i2c(PTE0, PTE1); //SDA, SCL |
wim | 5:ff3e57bebb6a | 986 | * SC16IS752_I2C serial_i2c(&i2c, SC16IS750_DEFAULT_ADDR, NC, SC16IS750::Channel_A); |
wim | 4:12446ee9f9c8 | 987 | * |
wim | 4:12446ee9f9c8 | 988 | * Serial pc(USBTX,USBRX); |
wim | 4:12446ee9f9c8 | 989 | * |
wim | 4:12446ee9f9c8 | 990 | * int main() { |
wim | 4:12446ee9f9c8 | 991 | * pc.printf("\nHello World!\n"); |
wim | 4:12446ee9f9c8 | 992 | * |
wim | 4:12446ee9f9c8 | 993 | * while(1) { |
wim | 4:12446ee9f9c8 | 994 | * serial_i2c.ioSetState(0x00); |
wim | 4:12446ee9f9c8 | 995 | * wait(0.5); |
wim | 4:12446ee9f9c8 | 996 | * serial_i2c.ioSetState(0xFF); |
wim | 4:12446ee9f9c8 | 997 | * wait(0.5); |
wim | 4:12446ee9f9c8 | 998 | * serial_i2c.putc('*'); |
wim | 4:12446ee9f9c8 | 999 | * pc.putc('*'); |
wim | 4:12446ee9f9c8 | 1000 | * } |
wim | 4:12446ee9f9c8 | 1001 | * } |
wim | 4:12446ee9f9c8 | 1002 | * |
wim | 4:12446ee9f9c8 | 1003 | * @endcode |
wim | 4:12446ee9f9c8 | 1004 | */ |
wim | 4:12446ee9f9c8 | 1005 | class SC16IS752_I2C : public SC16IS750 { |
wim | 4:12446ee9f9c8 | 1006 | public: |
wim | 4:12446ee9f9c8 | 1007 | |
wim | 4:12446ee9f9c8 | 1008 | /** Create an SC16IS752_I2C object using a specified I2C bus, slaveaddress and Channel |
wim | 5:ff3e57bebb6a | 1009 | * Note: The SC16IS752 does not have separate GPIOs for Channel_A and Channel_B. |
wim | 5:ff3e57bebb6a | 1010 | * Note: The SC16IS752 does not have separate Reset for Channel_A and Channel_B. |
wim | 4:12446ee9f9c8 | 1011 | * |
wim | 4:12446ee9f9c8 | 1012 | * @param I2C &i2c the I2C port to connect to |
wim | 4:12446ee9f9c8 | 1013 | * @param char deviceAddress the address of the SC16IS750 |
wim | 4:12446ee9f9c8 | 1014 | * @param rst Pinname for Reset pin (active low) Optional, Default = NC |
wim | 4:12446ee9f9c8 | 1015 | * @param channel UART ChannelName, Default = Channel_A |
wim | 4:12446ee9f9c8 | 1016 | */ |
wim | 4:12446ee9f9c8 | 1017 | SC16IS752_I2C(I2C *i2c, uint8_t deviceAddress = SC16IS750_DEFAULT_ADDR, PinName rst = NC, ChannelName channel = SC16IS750::Channel_A); |
wim | 4:12446ee9f9c8 | 1018 | |
wim | 4:12446ee9f9c8 | 1019 | |
wim | 4:12446ee9f9c8 | 1020 | /** Destruct SC16IS752_I2C bridge object |
wim | 4:12446ee9f9c8 | 1021 | * |
wim | 4:12446ee9f9c8 | 1022 | * @param none |
wim | 4:12446ee9f9c8 | 1023 | * @return none |
wim | 4:12446ee9f9c8 | 1024 | */ |
wim | 4:12446ee9f9c8 | 1025 | virtual ~SC16IS752_I2C(); |
wim | 4:12446ee9f9c8 | 1026 | |
wim | 4:12446ee9f9c8 | 1027 | |
wim | 4:12446ee9f9c8 | 1028 | /** Write value to internal register. |
wim | 4:12446ee9f9c8 | 1029 | * @param registerAddress The address of the Register (enum RegisterName) |
wim | 4:12446ee9f9c8 | 1030 | * @param data The 8bit value to write |
wim | 4:12446ee9f9c8 | 1031 | * @return none |
wim | 4:12446ee9f9c8 | 1032 | */ |
wim | 4:12446ee9f9c8 | 1033 | virtual void writeRegister(SC16IS750::RegisterName register_address, char data ); |
wim | 4:12446ee9f9c8 | 1034 | |
wim | 4:12446ee9f9c8 | 1035 | /** Read value from internal register. |
wim | 4:12446ee9f9c8 | 1036 | * @param registerAddress The address of the Register (enum RegisterName) |
wim | 4:12446ee9f9c8 | 1037 | * @return char The 8bit value read from the register |
wim | 4:12446ee9f9c8 | 1038 | */ |
wim | 4:12446ee9f9c8 | 1039 | virtual char readRegister(SC16IS750::RegisterName register_address ); |
wim | 4:12446ee9f9c8 | 1040 | |
wim | 4:12446ee9f9c8 | 1041 | |
wim | 4:12446ee9f9c8 | 1042 | /** Write multiple datavalues to Transmitregister. |
wim | 4:12446ee9f9c8 | 1043 | * More Efficient implementation than writing individual bytes |
wim | 4:12446ee9f9c8 | 1044 | * Assume that previous check confirmed that the FIFO has sufficient free space to store the data |
wim | 4:12446ee9f9c8 | 1045 | * Pure virtual, must be declared in derived class. |
wim | 4:12446ee9f9c8 | 1046 | * @param char* databytes The pointer to the block of data |
wim | 4:12446ee9f9c8 | 1047 | * @param len The number of bytes to write |
wim | 4:12446ee9f9c8 | 1048 | * @return none |
wim | 4:12446ee9f9c8 | 1049 | */ |
wim | 4:12446ee9f9c8 | 1050 | virtual void writeDataBlock (const char *data, int len ); |
wim | 4:12446ee9f9c8 | 1051 | |
wim | 4:12446ee9f9c8 | 1052 | |
wim | 4:12446ee9f9c8 | 1053 | /** Hardware Reset SC16IS752 device. |
wim | 4:12446ee9f9c8 | 1054 | * This method is only functional when the Reset pin has been declared and is also connected |
wim | 4:12446ee9f9c8 | 1055 | * @param none |
wim | 4:12446ee9f9c8 | 1056 | * @return none |
wim | 4:12446ee9f9c8 | 1057 | */ |
wim | 4:12446ee9f9c8 | 1058 | virtual void hwReset(); |
wim | 4:12446ee9f9c8 | 1059 | |
wim | 4:12446ee9f9c8 | 1060 | |
wim | 4:12446ee9f9c8 | 1061 | protected: |
wim | 4:12446ee9f9c8 | 1062 | //protected is accessible to derived classes, but not to external users |
wim | 4:12446ee9f9c8 | 1063 | |
wim | 4:12446ee9f9c8 | 1064 | |
wim | 4:12446ee9f9c8 | 1065 | private: |
wim | 4:12446ee9f9c8 | 1066 | I2C *_i2c; //I2C bus reference |
wim | 4:12446ee9f9c8 | 1067 | uint8_t _slaveAddress; //I2C Slave address of device |
wim | 4:12446ee9f9c8 | 1068 | |
wim | 4:12446ee9f9c8 | 1069 | /** Optional Hardware Reset pin for the bridge device (active low) |
wim | 4:12446ee9f9c8 | 1070 | * Default PinName value is NC |
wim | 4:12446ee9f9c8 | 1071 | */ |
wim | 4:12446ee9f9c8 | 1072 | DigitalOut* _reset; //Reset the Bridge device (active low) |
wim | 4:12446ee9f9c8 | 1073 | |
wim | 4:12446ee9f9c8 | 1074 | // Save Channel setting |
wim | 4:12446ee9f9c8 | 1075 | ChannelName _channel; |
wim | 4:12446ee9f9c8 | 1076 | |
wim | 0:d64854a60f95 | 1077 | }; |
wim | 0:d64854a60f95 | 1078 | |
wim | 0:d64854a60f95 | 1079 | |
wim | 0:d64854a60f95 | 1080 | #endif // _SC16IS750_H |