pca9555.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009 by Rittal GmbH & Co. KG,
00003  * Ulrich Prinz <prinz.u@rittal.de> All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. Neither the name of the copyright holders nor the names of
00015  *    contributors may be used to endorse or promote products derived
00016  *    from this software without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY EMBEDDED IT AND CONTRIBUTORS
00019  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00021  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EMBEDDED IT
00022  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00023  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00024  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00025  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00027  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00028  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  *
00032  */
00033 
00034 /*
00035  * $Log$
00036  *
00037  * Revision 1.0  2009/04/13 ulrichprinz
00038  * First checkin, new twi driver for pca9555 (currently SAM7X256 is tested
00039  * only)
00040  *
00041  */
00042 #include <cfg/os.h>
00043 #include <dev/twif.h>
00044 
00045 #include <sys/event.h>
00046 #include <dev/pca9555.h>
00047 
00048 #ifndef I2C_SLA_RTC
00049 #define I2C_SLA_IOEXP     0x23
00050 #endif
00051 
00052 static uint8_t portValue[2] = { 0x00, 0x1f };
00053 
00054 /*****************************************************************/
00055 int IOExpInit( void )
00056 /*****************************************************************/
00057 {
00058     uint8_t cmd[2];
00059 
00060     cmd[0] = 0x0f;  /* port 0 input   */
00061     cmd[1] = 0x00;  /* port 1 output  */
00062     if( TwMasterRegWrite( I2C_SLA_IOEXP, 0x06, 1, cmd, 2, 50 ) == -1 )
00063     {
00064         // printf( "Init Chip not responding\n");
00065         return -1;
00066     }
00067 
00068     cmd[0] = 0xff;  /* 4 polarity reg 0 */
00069     if( TwMasterRegWrite( I2C_SLA_IOEXP, 0x04, 1, cmd, 1, 50 ) == -1 )
00070     {
00071         // printf( "Init Chip not responding\n");
00072         return -1;
00073     }
00074     cmd[0] = 0xf0;  /* 4 polarity reg 1 */
00075     if( TwMasterRegWrite( I2C_SLA_IOEXP, 0x02, 1, cmd, 1, 50 ) == -1 )
00076     {
00077         return -1;
00078     }
00079 
00080     return 0;
00081 }
00082 
00083 /*****************************************************************/
00084 int IOExpRawWrite ( uint8_t port, uint8_t value )
00085 /*****************************************************************/
00086 {
00087     if ( port > 1 ) return -1;
00088 
00089     portValue[port] = value;
00090 
00091     if( TwMasterRegWrite( I2C_SLA_IOEXP, 0x02 + port, 1, &portValue[port], 1, 50 ) == -1 )
00092         return -1;
00093     return 0;
00094 }
00095 
00096 /*****************************************************************/
00097 int IOExpRawRead ( uint8_t port, uint8_t *value )
00098 /*****************************************************************/
00099 {
00100     if( port > 1 ) return -1;
00101 
00102     if( TwMasterRegRead( I2C_SLA_IOEXP, port, 1, value, 1, 50) == -1)
00103         return-1;
00104     return 0;
00105 }
00106 
00107 
00108 /*****************************************************************/
00109 int IOExpGetBit ( uint8_t port, uint8_t bit, uint8_t *value )
00110 /*****************************************************************/
00111 {
00112     uint8_t val;
00113 
00114     if( port > 1 ) return -1;
00115 
00116     if( TwMasterRegRead( I2C_SLA_IOEXP, 0, 1, &val, 1, 50))
00117         return -1;
00118     else
00119         *value = (( val & ( 1 << bit )) == 0 ) ? 0 : 1;
00120 
00121     return 0;
00122 }
00123 
00124 /*****************************************************************/
00125 int IOExpSetBit ( uint8_t port, uint8_t bit )
00126 /*****************************************************************/
00127 {
00128     if( port > 1 ) return -1;
00129 
00130     portValue[port] |= ( 1 << bit );
00131 
00132     if( TwMasterRegWrite( I2C_SLA_IOEXP, 0x02+port, 1, &portValue[port], 1, 50 ) == -1 )
00133         return -1;
00134     return 0;
00135 }
00136 
00137 /*****************************************************************/
00138 int IOExpClrBit ( uint8_t port, uint8_t bit )
00139 /*****************************************************************/
00140 {
00141     if( port > 1 ) return 0;
00142 
00143     portValue[port] &= ~( 1 << bit );
00144 
00145     if( TwMasterRegWrite( I2C_SLA_IOEXP, 0x02+port, 1, &portValue[port], 1, 50 ) == -1 )
00146         return -1;
00147     return 0;
00148 }
00149 

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/