test version 0.2
Dependents: SC18IS606_Hello SC18IS606_EEPROM_access_test SC18IS606_OS6_Hello
SC18IS606.cpp@7:9fee975998c1, 2021-07-29 (annotated)
- Committer:
- okano
- Date:
- Thu Jul 29 01:35:53 2021 +0000
- Revision:
- 7:9fee975998c1
- Parent:
- 6:cfe7ec4f2b59
added read version, GPIO read
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okano | 1:b44f801ac9f2 | 1 | /* |
okano | 1:b44f801ac9f2 | 2 | * SC18IS606 library |
okano | 1:b44f801ac9f2 | 3 | * |
okano | 1:b44f801ac9f2 | 4 | * @author Akifumi (Tedd) OKANO, NXP Semiconductors |
okano | 1:b44f801ac9f2 | 5 | * @version 0.1 |
okano | 1:b44f801ac9f2 | 6 | * @date 13-July-2021 |
okano | 1:b44f801ac9f2 | 7 | * |
okano | 1:b44f801ac9f2 | 8 | * SC18IS606 is an "I2C-bus to SPI bridge" |
okano | 1:b44f801ac9f2 | 9 | * http://www.nxp.com/ (product infomation page will be updated later) |
okano | 1:b44f801ac9f2 | 10 | */ |
okano | 1:b44f801ac9f2 | 11 | |
okano | 1:b44f801ac9f2 | 12 | #include "mbed.h" |
okano | 1:b44f801ac9f2 | 13 | #include "SC18IS606.h" |
okano | 1:b44f801ac9f2 | 14 | |
okano | 1:b44f801ac9f2 | 15 | SC18IS606::SC18IS606( PinName sda, PinName scl, char i2c_address ) |
okano | 1:b44f801ac9f2 | 16 | : i2c_p( new I2C( sda, scl ) ), i2c( *i2c_p ), device_address( i2c_address ) |
okano | 1:b44f801ac9f2 | 17 | { |
okano | 1:b44f801ac9f2 | 18 | init(); |
okano | 1:b44f801ac9f2 | 19 | } |
okano | 1:b44f801ac9f2 | 20 | |
okano | 1:b44f801ac9f2 | 21 | SC18IS606::SC18IS606( I2C &i2c_, char i2c_address ) |
okano | 1:b44f801ac9f2 | 22 | : i2c_p( NULL ), i2c( i2c_ ), device_address( i2c_address ) |
okano | 1:b44f801ac9f2 | 23 | { |
okano | 1:b44f801ac9f2 | 24 | init(); |
okano | 1:b44f801ac9f2 | 25 | } |
okano | 1:b44f801ac9f2 | 26 | |
okano | 1:b44f801ac9f2 | 27 | SC18IS606::~SC18IS606() |
okano | 1:b44f801ac9f2 | 28 | { |
okano | 1:b44f801ac9f2 | 29 | if ( NULL != i2c_p ) |
okano | 1:b44f801ac9f2 | 30 | delete i2c_p; |
okano | 1:b44f801ac9f2 | 31 | } |
okano | 1:b44f801ac9f2 | 32 | |
okano | 1:b44f801ac9f2 | 33 | int SC18IS606::init( void ) |
okano | 1:b44f801ac9f2 | 34 | { |
okano | 5:436b2c7854e8 | 35 | int err; |
okano | 5:436b2c7854e8 | 36 | |
okano | 4:ac0aef91fd94 | 37 | wait_transfer_completion = NULL; |
okano | 5:436b2c7854e8 | 38 | err = clear_interrupt(); |
okano | 4:ac0aef91fd94 | 39 | |
okano | 5:436b2c7854e8 | 40 | return err; |
okano | 1:b44f801ac9f2 | 41 | } |
okano | 1:b44f801ac9f2 | 42 | |
okano | 2:4e64923032ad | 43 | int SC18IS606::transfer( int slave_select_num, char *send_data_ptr, int length ) |
okano | 1:b44f801ac9f2 | 44 | { |
okano | 1:b44f801ac9f2 | 45 | char *p; |
okano | 5:436b2c7854e8 | 46 | int err; |
okano | 5:436b2c7854e8 | 47 | |
okano | 1:b44f801ac9f2 | 48 | p = new char[ length + 1 ]; |
okano | 1:b44f801ac9f2 | 49 | |
okano | 2:4e64923032ad | 50 | *p = SPI_read_and_write | (0x1 << slave_select_num); |
okano | 1:b44f801ac9f2 | 51 | memcpy( p + 1, send_data_ptr, length ); |
okano | 5:436b2c7854e8 | 52 | err = i2c.write( device_address, p, length + 1 ); |
okano | 1:b44f801ac9f2 | 53 | delete[] p; |
okano | 4:ac0aef91fd94 | 54 | |
okano | 5:436b2c7854e8 | 55 | if ( err ) |
okano | 5:436b2c7854e8 | 56 | return err; |
okano | 5:436b2c7854e8 | 57 | |
okano | 4:ac0aef91fd94 | 58 | if ( NULL != wait_transfer_completion ) |
okano | 4:ac0aef91fd94 | 59 | (*wait_transfer_completion)(); |
okano | 2:4e64923032ad | 60 | |
okano | 5:436b2c7854e8 | 61 | return err; |
okano | 2:4e64923032ad | 62 | } |
okano | 2:4e64923032ad | 63 | |
okano | 2:4e64923032ad | 64 | int SC18IS606::read_buffer( char *receive_data_ptr, int length ) |
okano | 2:4e64923032ad | 65 | { |
okano | 5:436b2c7854e8 | 66 | int err; |
okano | 5:436b2c7854e8 | 67 | |
okano | 1:b44f801ac9f2 | 68 | if ( receive_data_ptr ) |
okano | 5:436b2c7854e8 | 69 | err = i2c.read( device_address, receive_data_ptr, length ); |
okano | 1:b44f801ac9f2 | 70 | |
okano | 5:436b2c7854e8 | 71 | return err; |
okano | 1:b44f801ac9f2 | 72 | } |
okano | 1:b44f801ac9f2 | 73 | |
okano | 1:b44f801ac9f2 | 74 | int SC18IS606::config( FunctionID fid, char data ) |
okano | 1:b44f801ac9f2 | 75 | { |
okano | 1:b44f801ac9f2 | 76 | char s[ 2 ]; |
okano | 5:436b2c7854e8 | 77 | int err; |
okano | 5:436b2c7854e8 | 78 | |
okano | 1:b44f801ac9f2 | 79 | s[ 0 ] = fid; |
okano | 1:b44f801ac9f2 | 80 | s[ 1 ] = data; |
okano | 5:436b2c7854e8 | 81 | err = i2c.write( device_address, s, sizeof( s ) ); |
okano | 1:b44f801ac9f2 | 82 | |
okano | 5:436b2c7854e8 | 83 | return err; |
okano | 1:b44f801ac9f2 | 84 | } |
okano | 1:b44f801ac9f2 | 85 | |
okano | 1:b44f801ac9f2 | 86 | int SC18IS606::clear_interrupt( void ) |
okano | 1:b44f801ac9f2 | 87 | { |
okano | 1:b44f801ac9f2 | 88 | char c = Clear_Interrupt; |
okano | 5:436b2c7854e8 | 89 | int err; |
okano | 5:436b2c7854e8 | 90 | |
okano | 5:436b2c7854e8 | 91 | err = i2c.write( device_address, &c, sizeof( c ) ); |
okano | 1:b44f801ac9f2 | 92 | |
okano | 5:436b2c7854e8 | 93 | return err; |
okano | 1:b44f801ac9f2 | 94 | } |
okano | 6:cfe7ec4f2b59 | 95 | |
okano | 6:cfe7ec4f2b59 | 96 | char* SC18IS606::read_version( void ) |
okano | 6:cfe7ec4f2b59 | 97 | { |
okano | 6:cfe7ec4f2b59 | 98 | static char s[ 16 ]; |
okano | 7:9fee975998c1 | 99 | char func_id = Read_Version; |
okano | 6:cfe7ec4f2b59 | 100 | int err; |
okano | 6:cfe7ec4f2b59 | 101 | |
okano | 6:cfe7ec4f2b59 | 102 | err = i2c.write( device_address, &func_id, sizeof( func_id ) ); |
okano | 6:cfe7ec4f2b59 | 103 | |
okano | 6:cfe7ec4f2b59 | 104 | if ( NULL != wait_transfer_completion ) |
okano | 6:cfe7ec4f2b59 | 105 | (*wait_transfer_completion)(); |
okano | 6:cfe7ec4f2b59 | 106 | |
okano | 6:cfe7ec4f2b59 | 107 | err |= read_buffer( s, sizeof( s ) ); |
okano | 6:cfe7ec4f2b59 | 108 | |
okano | 6:cfe7ec4f2b59 | 109 | if ( err ) |
okano | 6:cfe7ec4f2b59 | 110 | s[ 0 ] = 0; |
okano | 6:cfe7ec4f2b59 | 111 | |
okano | 6:cfe7ec4f2b59 | 112 | return s; |
okano | 6:cfe7ec4f2b59 | 113 | } |
okano | 6:cfe7ec4f2b59 | 114 |