RETRO ROBOT E

Dependents:   RETRO_ROBOT_SC16IS750E

Fork of SC16IS750 by Wim Huiskamp

Committer:
wim
Date:
Wed Jan 22 16:39:37 2014 +0000
Revision:
0:d64854a60f95
Child:
1:0440152c5387
First Test Version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wim 0:d64854a60f95 1 /* SC16IS750 interface
wim 0:d64854a60f95 2 * /////////////////////v1.0 Tedd OKANO, 18 Jul 2012, I2C I/F only, MIT License
wim 0:d64854a60f95 3 * v1.1 WH, Nov 2013, Added SPI I/F and more methods, MIT License
wim 0:d64854a60f95 4 *
wim 0:d64854a60f95 5 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
wim 0:d64854a60f95 6 * and associated documentation files (the "Software"), to deal in the Software without restriction,
wim 0:d64854a60f95 7 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
wim 0:d64854a60f95 8 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
wim 0:d64854a60f95 9 * furnished to do so, subject to the following conditions:
wim 0:d64854a60f95 10 *
wim 0:d64854a60f95 11 * The above copyright notice and this permission notice shall be included in all copies or
wim 0:d64854a60f95 12 * substantial portions of the Software.
wim 0:d64854a60f95 13 *
wim 0:d64854a60f95 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
wim 0:d64854a60f95 15 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
wim 0:d64854a60f95 16 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
wim 0:d64854a60f95 17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wim 0:d64854a60f95 18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
wim 0:d64854a60f95 19 */
wim 0:d64854a60f95 20 #ifndef _SC16IS750_H
wim 0:d64854a60f95 21 #define _SC16IS750_H
wim 0:d64854a60f95 22
wim 0:d64854a60f95 23 //Default I2C Slaveaddress
wim 0:d64854a60f95 24 #define DEFAULT_SC16IS750_ADDR 0x9A
wim 0:d64854a60f95 25
wim 0:d64854a60f95 26 //Default baudrate
wim 0:d64854a60f95 27 #define DEFAULT_BAUD_RATE 9600
wim 0:d64854a60f95 28
wim 0:d64854a60f95 29 #define ENABLE_BULK_TRANSFERS 0x01
wim 0:d64854a60f95 30
wim 0:d64854a60f95 31 //#include "Configuration.h"
wim 0:d64854a60f95 32
wim 0:d64854a60f95 33 #define XTAL_FREQUENCY 14745600UL // On-board crystal (New mid-2010 Version)
wim 0:d64854a60f95 34
wim 0:d64854a60f95 35 // See datasheet section 7.8 for configuring the
wim 0:d64854a60f95 36 // "Programmable baud rate generator"
wim 0:d64854a60f95 37 #define PRESCALER 1 // Default prescaler after reset
wim 0:d64854a60f95 38 #define BAUD_RATE_DIVISOR(baud) ((XTAL_FREQUENCY/PRESCALER)/(baud*16UL))
wim 0:d64854a60f95 39
wim 0:d64854a60f95 40 // See section 8.4 of the datasheet for definitions
wim 0:d64854a60f95 41 // of bits in the Line Control Register (LCR)
wim 0:d64854a60f95 42 #define LCR_BITS5 0x00
wim 0:d64854a60f95 43 #define LCR_BITS6 0x01
wim 0:d64854a60f95 44 #define LCR_BITS7 0x02
wim 0:d64854a60f95 45 #define LCR_BITS8 0x03
wim 0:d64854a60f95 46
wim 0:d64854a60f95 47 #define LCR_BITS1 0x00
wim 0:d64854a60f95 48 #define LCR_BITS2 0x04
wim 0:d64854a60f95 49
wim 0:d64854a60f95 50 #define LCR_NONE 0x00
wim 0:d64854a60f95 51 #define LCR_ODD 0x08
wim 0:d64854a60f95 52 #define LCR_EVEN 0x18
wim 0:d64854a60f95 53 #define LCR_FORCED1 0x28
wim 0:d64854a60f95 54 #define LCR_FORCED0 0x38
wim 0:d64854a60f95 55
wim 0:d64854a60f95 56 #define LCR_BRK_ENA 0x40
wim 0:d64854a60f95 57 #define LCR_BRK_DIS 0x00
wim 0:d64854a60f95 58
wim 0:d64854a60f95 59 #define LCR_DIV_ENA 0x80
wim 0:d64854a60f95 60 #define LCR_DIV_DIS 0x00
wim 0:d64854a60f95 61
wim 0:d64854a60f95 62
wim 0:d64854a60f95 63 // See section 8.10 of the datasheet for definitions
wim 0:d64854a60f95 64 // of bits in the Enhanced Features Register (EFR)
wim 0:d64854a60f95 65 #define EFR_ENABLE_CTS (1 << 7)
wim 0:d64854a60f95 66 #define EFR_ENABLE_RTS (1 << 6)
wim 0:d64854a60f95 67 #define EFR_ENABLE_ENHANCED_FUNCTIONS (1 << 4)
wim 0:d64854a60f95 68
wim 0:d64854a60f95 69 // See Chapter 11 of datasheet
wim 0:d64854a60f95 70 #define SPI_READ_MODE_FLAG 0x80
wim 0:d64854a60f95 71
wim 0:d64854a60f95 72
wim 0:d64854a60f95 73 /** Abstract class SC16IS750 for a converter between either SPI or I2C and a Serial port
wim 0:d64854a60f95 74 *
wim 0:d64854a60f95 75 * Supports both SPI and I2C interfaces through derived classes
wim 0:d64854a60f95 76 *
wim 0:d64854a60f95 77 * @code
wim 0:d64854a60f95 78 *
wim 0:d64854a60f95 79 * @endcode
wim 0:d64854a60f95 80 */
wim 0:d64854a60f95 81 //class SC16IS750 : public Serial { //Fout, geen Serial constr met Serial(NC, NC) toegestaan...
wim 0:d64854a60f95 82 class SC16IS750 {
wim 0:d64854a60f95 83
wim 0:d64854a60f95 84 public:
wim 0:d64854a60f95 85
wim 0:d64854a60f95 86 // SC16IS750 Register definitions (shifted to align)
wim 0:d64854a60f95 87 enum RegisterName {
wim 0:d64854a60f95 88 RHR = 0x00 << 3,
wim 0:d64854a60f95 89 THR = 0x00 << 3,
wim 0:d64854a60f95 90 IER = 0x01 << 3,
wim 0:d64854a60f95 91 FCR = 0x02 << 3,
wim 0:d64854a60f95 92 IIR = 0x02 << 3,
wim 0:d64854a60f95 93 LCR = 0x03 << 3,
wim 0:d64854a60f95 94 MCR = 0x04 << 3,
wim 0:d64854a60f95 95 LSR = 0x05 << 3,
wim 0:d64854a60f95 96 MSR = 0x06 << 3,
wim 0:d64854a60f95 97 SPR = 0x07 << 3,
wim 0:d64854a60f95 98 TCR = 0x06 << 3,
wim 0:d64854a60f95 99 TLR = 0x07 << 3,
wim 0:d64854a60f95 100 TXLVL = 0x08 << 3,
wim 0:d64854a60f95 101 RXLVL = 0x09 << 3,
wim 0:d64854a60f95 102 IODIR = 0x0A << 3,
wim 0:d64854a60f95 103 IOSTATE = 0x0B << 3,
wim 0:d64854a60f95 104 IOINTMSK = 0x0C << 3,
wim 0:d64854a60f95 105 reserved = 0x0D << 3,
wim 0:d64854a60f95 106 IOCTRL = 0x0E << 3,
wim 0:d64854a60f95 107 EFCR = 0x0F << 3,
wim 0:d64854a60f95 108 DLL = 0x00 << 3,
wim 0:d64854a60f95 109 DLH = 0x01 << 3,
wim 0:d64854a60f95 110 EFR = 0x02 << 3,
wim 0:d64854a60f95 111 XON1 = 0x04 << 3,
wim 0:d64854a60f95 112 XON2 = 0x05 << 3,
wim 0:d64854a60f95 113 XOFF1 = 0x06 << 3,
wim 0:d64854a60f95 114 XOFF2 = 0x07 << 3,
wim 0:d64854a60f95 115 } ;
wim 0:d64854a60f95 116
wim 0:d64854a60f95 117
wim 0:d64854a60f95 118 // SC16IS750 configuration register values
wim 0:d64854a60f95 119 struct SC16IS750_cfg {
wim 0:d64854a60f95 120 char baudrate;
wim 0:d64854a60f95 121 char dataformat;
wim 0:d64854a60f95 122 char flowctrl;
wim 0:d64854a60f95 123 char fifoformat;
wim 0:d64854a60f95 124 };
wim 0:d64854a60f95 125
wim 0:d64854a60f95 126 /** Constructor
wim 0:d64854a60f95 127 *
wim 0:d64854a60f95 128 */
wim 0:d64854a60f95 129 SC16IS750();
wim 0:d64854a60f95 130
wim 0:d64854a60f95 131 /** Determine if there is a character available to read.
wim 0:d64854a60f95 132 * @return 1 if there is a character available to read, 0 otherwise
wim 0:d64854a60f95 133 */
wim 0:d64854a60f95 134 int readable();
wim 0:d64854a60f95 135
wim 0:d64854a60f95 136 /** Determine if how many characters available to read.
wim 0:d64854a60f95 137 * @return int Characters available to read
wim 0:d64854a60f95 138 */
wim 0:d64854a60f95 139 int readableCount();
wim 0:d64854a60f95 140
wim 0:d64854a60f95 141 /** Determine if there is space available to write a character.
wim 0:d64854a60f95 142 * @return 1 if there is a space for a character to write, 0 otherwise
wim 0:d64854a60f95 143 */
wim 0:d64854a60f95 144 int writable();
wim 0:d64854a60f95 145
wim 0:d64854a60f95 146 /** Determine if how much space is available to write characters.
wim 0:d64854a60f95 147 * @return int Characterspace available for writing
wim 0:d64854a60f95 148 */
wim 0:d64854a60f95 149 int writableCount();
wim 0:d64854a60f95 150
wim 0:d64854a60f95 151 char getc();
wim 0:d64854a60f95 152 void putc(char value);
wim 0:d64854a60f95 153
wim 0:d64854a60f95 154 void write(const char *str);
wim 0:d64854a60f95 155
wim 0:d64854a60f95 156 /** Set baudrate of the serial port.
wim 0:d64854a60f95 157 * @param baud integer baudrate (4800, 9600 etc)
wim 0:d64854a60f95 158 * @return none
wim 0:d64854a60f95 159 */
wim 0:d64854a60f95 160 void baud(int baudrate = DEFAULT_BAUD_RATE);
wim 0:d64854a60f95 161
wim 0:d64854a60f95 162 /** Set the transmission format used by the serial port.
wim 0:d64854a60f95 163 * @param bits The number of bits in a word (5-8; default = 8)
wim 0:d64854a60f95 164 * @param parity The parity used (Serial::None, Serial::Odd, Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None)
wim 0:d64854a60f95 165 * @param stop_bits The number of stop bits (1 or 2; default = 1)
wim 0:d64854a60f95 166 */
wim 0:d64854a60f95 167 void format(int bits=8, Serial::Parity parity=Serial::None, int stop_bits=1);
wim 0:d64854a60f95 168
wim 0:d64854a60f95 169 /**
wim 0:d64854a60f95 170 * Check that UART is connected and operational.
wim 0:d64854a60f95 171 * @param none
wim 0:d64854a60f95 172 * @return bool true when connected, false otherwise
wim 0:d64854a60f95 173 */
wim 0:d64854a60f95 174 bool connected();
wim 0:d64854a60f95 175
wim 0:d64854a60f95 176
wim 0:d64854a60f95 177 #if ENABLE_BULK_TRANSFERS
wim 0:d64854a60f95 178 void write(const uint8_t *buffer, size_t size);
wim 0:d64854a60f95 179 #else
wim 0:d64854a60f95 180 // using Print::write;
wim 0:d64854a60f95 181 #endif
wim 0:d64854a60f95 182 void flush();
wim 0:d64854a60f95 183
wim 0:d64854a60f95 184 //required for Stream
wim 0:d64854a60f95 185 int peek() {return 0;};
wim 0:d64854a60f95 186
wim 0:d64854a60f95 187 // These are specific to the SPI UART
wim 0:d64854a60f95 188 void ioSetDirection(unsigned char bits);
wim 0:d64854a60f95 189 void ioSetState(unsigned char bits);
wim 0:d64854a60f95 190
wim 0:d64854a60f95 191 /** Write value to internal register.
wim 0:d64854a60f95 192 * Pure virtual, must be declared in derived class.
wim 0:d64854a60f95 193 * @param register_address The address of the Register (enum RegisterName)
wim 0:d64854a60f95 194 * @param data The 8bit value to write
wim 0:d64854a60f95 195 * @return none
wim 0:d64854a60f95 196 */
wim 0:d64854a60f95 197 virtual void writeRegister (RegisterName register_address, char data ) =0;
wim 0:d64854a60f95 198
wim 0:d64854a60f95 199 /** Read value from internal register.
wim 0:d64854a60f95 200 * Pure virtual, must be declared in derived class.
wim 0:d64854a60f95 201 * @param register_address The address of the Register (enum RegisterName)
wim 0:d64854a60f95 202 * @return char The 8bit value read from the register
wim 0:d64854a60f95 203 */
wim 0:d64854a60f95 204 virtual char readRegister (RegisterName register_address ) =0;
wim 0:d64854a60f95 205
wim 0:d64854a60f95 206 protected:
wim 0:d64854a60f95 207 //protected is accessible to derived classes, but not to external users
wim 0:d64854a60f95 208
wim 0:d64854a60f95 209 SC16IS750_cfg _config;
wim 0:d64854a60f95 210
wim 0:d64854a60f95 211 private:
wim 0:d64854a60f95 212 //private is not accessible to derived classes, nor external users
wim 0:d64854a60f95 213 void init();
wim 0:d64854a60f95 214 };
wim 0:d64854a60f95 215
wim 0:d64854a60f95 216
wim 0:d64854a60f95 217
wim 0:d64854a60f95 218 /** Class SC16IS750_SPI for a converter between SPI and a Serial port
wim 0:d64854a60f95 219 *
wim 0:d64854a60f95 220 * @code
wim 0:d64854a60f95 221 * #include "mbed.h"
wim 0:d64854a60f95 222 * #include "SC16IS750.h"
wim 0:d64854a60f95 223 *
wim 0:d64854a60f95 224 * SPI spi(PTD2, PTD3, PTD1); //MOSI, MISO, SCK
wim 0:d64854a60f95 225 * SC16IS750_SPI serial_spi(&spi, PTD0);
wim 0:d64854a60f95 226 *
wim 0:d64854a60f95 227 * Serial pc(USBTX,USBRX);
wim 0:d64854a60f95 228 *
wim 0:d64854a60f95 229 * int main() {
wim 0:d64854a60f95 230 * pc.printf("\nHello World!\n");
wim 0:d64854a60f95 231 *
wim 0:d64854a60f95 232 * while(1) {
wim 0:d64854a60f95 233 * serial_spi.ioSetState(0x00);
wim 0:d64854a60f95 234 * wait(0.5);
wim 0:d64854a60f95 235 * pc.putc('*');
wim 0:d64854a60f95 236 * }
wim 0:d64854a60f95 237 * }
wim 0:d64854a60f95 238 *
wim 0:d64854a60f95 239 * @endcode
wim 0:d64854a60f95 240 */
wim 0:d64854a60f95 241 class SC16IS750_SPI : public SC16IS750 {
wim 0:d64854a60f95 242 public:
wim 0:d64854a60f95 243
wim 0:d64854a60f95 244 /** Create a SC16IS750_SPI object using a specified SPI bus and CS
wim 0:d64854a60f95 245 *
wim 0:d64854a60f95 246 * @param SPI &spi the SPI port to connect to
wim 0:d64854a60f95 247 * @param cs the Pin of the CS
wim 0:d64854a60f95 248 */
wim 0:d64854a60f95 249 SC16IS750_SPI(SPI *spi, PinName cs);
wim 0:d64854a60f95 250
wim 0:d64854a60f95 251 /** Write value to internal register.
wim 0:d64854a60f95 252 * @param register_address The address of the Register (enum RegisterName)
wim 0:d64854a60f95 253 * @param data The 8bit value to write
wim 0:d64854a60f95 254 * @return none
wim 0:d64854a60f95 255 */
wim 0:d64854a60f95 256 virtual void writeRegister(SC16IS750::RegisterName registerAddress, char data);
wim 0:d64854a60f95 257
wim 0:d64854a60f95 258 /** Read value from internal register.
wim 0:d64854a60f95 259 * @param register_address The address of the Register (enum RegisterName)
wim 0:d64854a60f95 260 * @return char The 8bit value read from the register
wim 0:d64854a60f95 261 */
wim 0:d64854a60f95 262 virtual char readRegister(SC16IS750::RegisterName registerAddress);
wim 0:d64854a60f95 263
wim 0:d64854a60f95 264 protected:
wim 0:d64854a60f95 265 //protected is accessible to derived classes, but not to external users
wim 0:d64854a60f95 266
wim 0:d64854a60f95 267
wim 0:d64854a60f95 268 private:
wim 0:d64854a60f95 269 SPI *_spi; //SPI bus reference
wim 0:d64854a60f95 270 DigitalOut _cs; //CS of SPI device
wim 0:d64854a60f95 271
wim 0:d64854a60f95 272 };
wim 0:d64854a60f95 273
wim 0:d64854a60f95 274
wim 0:d64854a60f95 275
wim 0:d64854a60f95 276 /** Class SC16IS750_I2C for a converter between I2C and a Serial port
wim 0:d64854a60f95 277 *
wim 0:d64854a60f95 278 * @code
wim 0:d64854a60f95 279 * #include "mbed.h"
wim 0:d64854a60f95 280 * #include "SC16IS750.h"
wim 0:d64854a60f95 281 *
wim 0:d64854a60f95 282 * I2C i2c(PTE0, PTE1); //SDA, SCL
wim 0:d64854a60f95 283 * SC16IS750_I2C serial_i2c(&i2c, DEFAULT_SC16IS750_ADDR);
wim 0:d64854a60f95 284 *
wim 0:d64854a60f95 285 * Serial pc(USBTX,USBRX);
wim 0:d64854a60f95 286 *
wim 0:d64854a60f95 287 * int main() {
wim 0:d64854a60f95 288 * pc.printf("\nHello World!\n");
wim 0:d64854a60f95 289 *
wim 0:d64854a60f95 290 * while(1) {
wim 0:d64854a60f95 291 * serial_i2c.ioSetState(0x00);
wim 0:d64854a60f95 292 * wait(0.5);
wim 0:d64854a60f95 293 * pc.putc('*');
wim 0:d64854a60f95 294 * }
wim 0:d64854a60f95 295 * }
wim 0:d64854a60f95 296 *
wim 0:d64854a60f95 297 * @endcode
wim 0:d64854a60f95 298 */
wim 0:d64854a60f95 299 class SC16IS750_I2C : public SC16IS750 {
wim 0:d64854a60f95 300 public:
wim 0:d64854a60f95 301
wim 0:d64854a60f95 302 /** Create a SC16IS750_I2C object using a specified I2C bus and slaveaddress
wim 0:d64854a60f95 303 *
wim 0:d64854a60f95 304 * @param I2C &i2c the I2C port to connect to
wim 0:d64854a60f95 305 * @param char deviceAddress the address of the SC16IS750
wim 0:d64854a60f95 306 */
wim 0:d64854a60f95 307 SC16IS750_I2C(I2C *i2c, uint8_t deviceAddress = DEFAULT_SC16IS750_ADDR);
wim 0:d64854a60f95 308
wim 0:d64854a60f95 309 /** Write value to internal register.
wim 0:d64854a60f95 310 * @param register_address The address of the Register (enum RegisterName)
wim 0:d64854a60f95 311 * @param data The 8bit value to write
wim 0:d64854a60f95 312 * @return none
wim 0:d64854a60f95 313 */
wim 0:d64854a60f95 314 virtual void writeRegister(SC16IS750::RegisterName register_address, char data );
wim 0:d64854a60f95 315
wim 0:d64854a60f95 316 /** Read value from internal register.
wim 0:d64854a60f95 317 * @param register_address The address of the Register (enum RegisterName)
wim 0:d64854a60f95 318 * @return char The 8bit value read from the register
wim 0:d64854a60f95 319 */
wim 0:d64854a60f95 320 virtual char readRegister(SC16IS750::RegisterName register_address );
wim 0:d64854a60f95 321
wim 0:d64854a60f95 322 protected:
wim 0:d64854a60f95 323 //protected is accessible to derived classes, but not to external users
wim 0:d64854a60f95 324
wim 0:d64854a60f95 325
wim 0:d64854a60f95 326 private:
wim 0:d64854a60f95 327 I2C *_i2c; //I2C bus reference
wim 0:d64854a60f95 328 uint8_t _slaveAddress; //I2C Slave address of device
wim 0:d64854a60f95 329
wim 0:d64854a60f95 330 };
wim 0:d64854a60f95 331
wim 0:d64854a60f95 332
wim 0:d64854a60f95 333 #endif // _SC16IS750_H