pca9555.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (C) 2009 by Rittal GmbH & Co. KG,
  3. * Ulrich Prinz <prinz.u@rittal.de> All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the copyright holders nor the names of
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY EMBEDDED IT AND CONTRIBUTORS
  19. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  21. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EMBEDDED IT
  22. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  25. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. */
  33. /*
  34. * $Log$
  35. *
  36. * Revision 1.0 2009/04/13 ulrichprinz
  37. * First checkin, new twi driver for pca9555 (currently SAM7X256 is tested
  38. * only)
  39. *
  40. */
  41. #include <compiler.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <memdebug.h>
  45. #include <sys/heap.h>
  46. #include <sys/event.h>
  47. #include <cfg/os.h>
  48. #include <dev/twif.h>
  49. #include <dev/gpio.h>
  50. #include <cfg/pca9555.h>
  51. #include <dev/pca9555.h>
  52. #ifndef I2C_SLA_IOEXP
  53. #define I2C_SLA_IOEXP 0x23
  54. #endif
  55. #define PCA_PINP 0 /**< PCA Input register offset */
  56. #define PCA_POUT 2 /**< PCA Output register offset */
  57. #define PCA_PINV 4 /**< PCA Polarity inversion register offset */
  58. #define PCA_CONF 6 /**< PCA Configuration register offset */
  59. typedef struct NUT_PACKED_TYPE
  60. {
  61. uint8_t out[2];
  62. uint8_t pol[2];
  63. uint8_t con[2];
  64. } pca_regs_t;
  65. pca_regs_t *pca_ctrl = NULL;
  66. /*****************************************************************/
  67. int IOExpInit( void )
  68. /*****************************************************************/
  69. {
  70. pca_ctrl = malloc( sizeof( pca_regs_t));
  71. if( pca_ctrl == NULL) return -1;
  72. memset( pca_ctrl, 0, sizeof( pca_regs_t));
  73. pca_ctrl->out[0] = 0xff;
  74. pca_ctrl->out[1] = 0xff;
  75. pca_ctrl->con[0] = 0xff;
  76. pca_ctrl->con[1] = 0xff;
  77. pca_ctrl->pol[0] = 0x00;
  78. pca_ctrl->pol[1] = 0x00;
  79. if( TwMasterRegWrite( I2C_SLA_IOEXP, PCA_POUT, 1, &pca_ctrl->out[0], 2, 50) == -1)
  80. return -1;
  81. if( TwMasterRegWrite( I2C_SLA_IOEXP, PCA_CONF , 1, &pca_ctrl->con[0], 2, 50) == -1)
  82. return -1;
  83. if( TwMasterRegWrite( I2C_SLA_IOEXP, PCA_PINV , 1, &pca_ctrl->pol[0], 2, 50) == -1)
  84. return -1;
  85. return 0;
  86. }
  87. /*****************************************************************/
  88. int IOExpPinConfigSet( int bank, int bit, uint32_t flags)
  89. /*****************************************************************/
  90. {
  91. bank &= 0xf;
  92. if( flags == 0) /* Input */
  93. pca_ctrl->con[bank] |= (1<<bit);
  94. if( flags & GPIO_CFG_OUTPUT)
  95. pca_ctrl->con[bank] &= ~(1<<bit);
  96. if( flags & GPIO_CFG_INVERT)
  97. pca_ctrl->pol[bank] |= (1<<bit);
  98. if( flags & GPIO_CFG_NORM)
  99. pca_ctrl->pol[bank] &= ~(1<<bit);
  100. if( TwMasterRegWrite( I2C_SLA_IOEXP, PCA_POUT+bank, 1, &pca_ctrl->out[bank], 1, 50) == -1)
  101. return -1;
  102. if( TwMasterRegWrite( I2C_SLA_IOEXP, PCA_CONF+bank, 1, &pca_ctrl->con[bank], 1, 50) == -1)
  103. return -1;
  104. if( TwMasterRegWrite( I2C_SLA_IOEXP, PCA_PINV+bank, 1, &pca_ctrl->pol[bank], 1, 50) == -1)
  105. return -1;
  106. return 0;
  107. }
  108. /*****************************************************************/
  109. int IOExpRawWrite ( int bank, int value )
  110. /*****************************************************************/
  111. {
  112. bank &= 0x0f;
  113. if ( bank > 1 ) return -1;
  114. pca_ctrl->out[bank] = value;
  115. if( TwMasterRegWrite( I2C_SLA_IOEXP, PCA_POUT+bank, 1, &pca_ctrl->out[bank], 1, 50 ) == -1 )
  116. return -1;
  117. return 0;
  118. }
  119. /*****************************************************************/
  120. int IOExpRawRead ( int bank, int *value )
  121. /*****************************************************************/
  122. {
  123. bank &= 0x0f;
  124. if( bank > 1 ) return -1;
  125. if( TwMasterRegRead( I2C_SLA_IOEXP, PCA_PINP+bank, 1, value, 1, 50) == -1)
  126. return-1;
  127. return 0;
  128. }
  129. /*****************************************************************/
  130. int IOExpGetBit ( int bank, int bit, int *value )
  131. /*****************************************************************/
  132. {
  133. int val;
  134. bank &= 0x0f;
  135. if( bank > 1 ) return -1;
  136. if( TwMasterRegRead( I2C_SLA_IOEXP, PCA_PINP+bank, 1, &val, 1, 50)==-1)
  137. return -1;
  138. else
  139. *value = (( val & ( 1 << bit )) == 0 ) ? 0 : 1;
  140. return 0;
  141. }
  142. /*****************************************************************/
  143. int IOExpSetBitHigh( int bank, int bit )
  144. /*****************************************************************/
  145. {
  146. bank &= 0x0f;
  147. if( bank > 1 ) return -1;
  148. pca_ctrl->out[bank] |= ( 1 << bit );
  149. if( TwMasterRegWrite( I2C_SLA_IOEXP, PCA_POUT+bank, 1, &pca_ctrl->out[bank], 1, 50 ) == -1 )
  150. return -1;
  151. return 0;
  152. }
  153. /*****************************************************************/
  154. int IOExpSetBitLow( int bank, int bit )
  155. /*****************************************************************/
  156. {
  157. bank &= 0x0f;
  158. if( bank > 1 ) return -1;
  159. pca_ctrl->out[bank] &= ~( 1 << bit );
  160. if( TwMasterRegWrite( I2C_SLA_IOEXP, PCA_POUT+bank, 1, &pca_ctrl->out[bank], 1, 50 ) == -1 )
  161. return -1;
  162. return 0;
  163. }
  164. /*****************************************************************/
  165. int IOExpSetBit( int bank, int bit, int value )
  166. /*****************************************************************/
  167. {
  168. if( value)
  169. return IOExpSetBitHigh( bank, bit);
  170. else
  171. return IOExpSetBitLow( bank, bit);
  172. }