for SC16IS750 test program. Confirming register access method

Committer:
okano
Date:
Wed Jul 18 01:24:33 2012 +0000
Revision:
0:bae17cf3178e
Child:
1:d9a4b7a4a159
re-written in C++ style

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okano 0:bae17cf3178e 1 #include "mbed.h"
okano 0:bae17cf3178e 2 #include "SC16IS750.h"
okano 0:bae17cf3178e 3
okano 0:bae17cf3178e 4 #define XTAL_FREQUENCY 14784000 // Hz
okano 0:bae17cf3178e 5 #define REGISTER_ACCESS_SIZE 2
okano 0:bae17cf3178e 6
okano 0:bae17cf3178e 7 char init[][ REGISTER_ACCESS_SIZE ] = {
okano 0:bae17cf3178e 8 { SC16IS750::LCR, 0x80 },
okano 0:bae17cf3178e 9 { SC16IS750::DLL, 0x60 },
okano 0:bae17cf3178e 10 { SC16IS750::DLH, 0x00 },
okano 0:bae17cf3178e 11 { SC16IS750::LCR, 0xBF },
okano 0:bae17cf3178e 12 { SC16IS750::EFR, 0x10 },
okano 0:bae17cf3178e 13 { SC16IS750::LCR, 0x03 },
okano 0:bae17cf3178e 14 { SC16IS750::IER, 0x00 },
okano 0:bae17cf3178e 15 { SC16IS750::FCR, 0x07 },
okano 0:bae17cf3178e 16 };
okano 0:bae17cf3178e 17
okano 0:bae17cf3178e 18
okano 0:bae17cf3178e 19 SC16IS750::SC16IS750(
okano 0:bae17cf3178e 20 PinName I2C_sda,
okano 0:bae17cf3178e 21 PinName I2C_scl,
okano 0:bae17cf3178e 22 int baud_rate,
okano 0:bae17cf3178e 23 char I2C_address
okano 0:bae17cf3178e 24 ) : i2c( I2C_sda, I2C_scl ), i2c_addr( I2C_address ) {
okano 0:bae17cf3178e 25
okano 0:bae17cf3178e 26 int baud_setting;
okano 0:bae17cf3178e 27
okano 0:bae17cf3178e 28 i2c.frequency( 400 * 1000 );
okano 0:bae17cf3178e 29
okano 0:bae17cf3178e 30 baud_setting = XTAL_FREQUENCY / (baud_rate * 16);
okano 0:bae17cf3178e 31 init[ 1 ][ 1 ] = (char)baud_setting;
okano 0:bae17cf3178e 32 init[ 2 ][ 1 ] = (char)(baud_setting >> 8);
okano 0:bae17cf3178e 33
okano 0:bae17cf3178e 34 init_registers();
okano 0:bae17cf3178e 35 }
okano 0:bae17cf3178e 36
okano 0:bae17cf3178e 37 void SC16IS750::init_registers( void ) {
okano 0:bae17cf3178e 38 for ( int i = 0; i < (sizeof( init ) / REGISTER_ACCESS_SIZE); i++ )
okano 0:bae17cf3178e 39 i2c.write( i2c_addr, init[ i ], REGISTER_ACCESS_SIZE );
okano 0:bae17cf3178e 40
okano 0:bae17cf3178e 41 i2c.stop();
okano 0:bae17cf3178e 42 }
okano 0:bae17cf3178e 43
okano 0:bae17cf3178e 44 void SC16IS750::set_baud( int baud ) {
okano 0:bae17cf3178e 45 int baud_setting;
okano 0:bae17cf3178e 46
okano 0:bae17cf3178e 47 baud_rate = baud;
okano 0:bae17cf3178e 48 baud_setting = XTAL_FREQUENCY / (baud_rate * 16);
okano 0:bae17cf3178e 49
okano 0:bae17cf3178e 50 register_write( SC16IS750::LCR, 0x80 );
okano 0:bae17cf3178e 51 register_write( SC16IS750::DLL, (char)baud_setting );
okano 0:bae17cf3178e 52 register_write( SC16IS750::DLH, (char)(baud_setting >> 8) );
okano 0:bae17cf3178e 53 register_write( SC16IS750::LCR, 0x03 );
okano 0:bae17cf3178e 54 }
okano 0:bae17cf3178e 55
okano 0:bae17cf3178e 56 void SC16IS750::send_str( char *s ) {
okano 0:bae17cf3178e 57 char buffer[ 65 ];
okano 0:bae17cf3178e 58 int i = 1;
okano 0:bae17cf3178e 59
okano 0:bae17cf3178e 60 buffer[ 0 ] = 0x00; // register address
okano 0:bae17cf3178e 61
okano 0:bae17cf3178e 62 #if 0
okano 0:bae17cf3178e 63 while ( buffer[ i++ ] = *s++ )
okano 0:bae17cf3178e 64 ;
okano 0:bae17cf3178e 65 #else
okano 0:bae17cf3178e 66 for ( i = 1; i < 65; i++ )
okano 0:bae17cf3178e 67 if ( !(buffer[ i ] = *s++) )
okano 0:bae17cf3178e 68 break;
okano 0:bae17cf3178e 69 #endif
okano 0:bae17cf3178e 70
okano 0:bae17cf3178e 71 i2c.write( i2c_addr, buffer, i );
okano 0:bae17cf3178e 72 }
okano 0:bae17cf3178e 73
okano 0:bae17cf3178e 74
okano 0:bae17cf3178e 75 void SC16IS750::register_write( char register_address, char data ) {
okano 0:bae17cf3178e 76 char w[ 2 ];
okano 0:bae17cf3178e 77
okano 0:bae17cf3178e 78 w[ 0 ] = register_address;
okano 0:bae17cf3178e 79 w[ 1 ] = data;
okano 0:bae17cf3178e 80
okano 0:bae17cf3178e 81 i2c.write( i2c_addr, w, 2 );
okano 0:bae17cf3178e 82 }
okano 0:bae17cf3178e 83
okano 0:bae17cf3178e 84 char SC16IS750::register_read( char register_address ) {
okano 0:bae17cf3178e 85 char r;
okano 0:bae17cf3178e 86
okano 0:bae17cf3178e 87 i2c.write( i2c_addr, &register_address, 1 );
okano 0:bae17cf3178e 88 i2c.read( i2c_addr, &r, 1 );
okano 0:bae17cf3178e 89
okano 0:bae17cf3178e 90 return ( r );
okano 0:bae17cf3178e 91 }