ioexpander.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (C) 2009 by Rittal GmbH & Co. KG,
  3. * Ulrich Prinz <prinz.u@rittal.de>
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. */
  35. /*
  36. * $Log$
  37. *
  38. * Revision 1.0 2009/04/13 ulrichprinz
  39. * First checkin, driver for PCA9555 I2C I/O-Expander (currently SAM7X256 is
  40. * tested only)
  41. *
  42. */
  43. /*!
  44. * \example ioexpander/ioexpander.c
  45. */
  46. #include <stdio.h>
  47. #include <io.h>
  48. #include <cfg/arch.h>
  49. #include <dev/board.h>
  50. #include <dev/gpio.h>
  51. #include <sys/thread.h>
  52. #include <sys/timer.h>
  53. #include <dev/pca9555.h>
  54. #include <dev/led.h>
  55. #if !defined(MCU_AT91) || defined(MCU_AT91R40008) || defined(MCU_GBA)
  56. int main(void)
  57. {
  58. uint32_t baud = 115200;
  59. NutRegisterDevice(&DEV_DEBUG, 0, 0);
  60. freopen(DEV_DEBUG_NAME, "w", stdout);
  61. _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
  62. puts("TwMasterReg functions are not available on your platform.");
  63. for (;;);
  64. }
  65. #else
  66. #define KEY1 (1<<0)
  67. #define KEY2 (1<<1)
  68. #define KEY3 (1<<2)
  69. #define KEY4 (1<<3)
  70. /******************************************************************/
  71. THREAD(Thread1, arg)
  72. /******************************************************************/
  73. {
  74. HANDLE ds1, ds2, ds3;
  75. int ledmask = 1;
  76. if( NutRegisterLed( &ds1, IOXP_PORT1, 0) == 0)
  77. printf( "register LED 1 OK\n");
  78. if( NutRegisterLed( &ds2, IOXP_PORT1, 1) == 0)
  79. printf( "register LED 2 OK\n");
  80. if( NutRegisterLed( &ds3, IOXP_PORT1, 2) == 0)
  81. printf( "register LED 3 OK\n");
  82. NutThreadSetPriority(128);
  83. for (;;) {
  84. NutSetLed( ds1, (ledmask>>0) & 1, 0, 0);
  85. NutSetLed( ds2, (ledmask>>1) & 1, 0, 0);
  86. NutSetLed( ds3, (ledmask>>2) & 1, 0, 0);
  87. ledmask <<= 1;
  88. if( ledmask & (1<<3)) ledmask = 1;
  89. NutSleep(250);
  90. }
  91. }
  92. /******************************************************************/
  93. THREAD(Thread2, arg)
  94. /******************************************************************/
  95. {
  96. uint8_t key, oldkey;
  97. uint8_t flag = 1;
  98. int rc;
  99. HANDLE led3;
  100. printf( "Key and LED test for PCA9555\n" );
  101. if( NutRegisterLed( &led3, IOXP_PORT1, 3) == 0)
  102. printf( "register LED 4 OK\n");
  103. oldkey = ~key;
  104. NutThreadSetPriority(128);
  105. for (;;)
  106. {
  107. key = 0;
  108. rc = IOExpRawRead( 0, &key);
  109. if( key != oldkey) {
  110. if( key > oldkey) {
  111. /* flash led if key is pressed */
  112. NutSetLed( led3, LED_ON, 200, 0);
  113. }
  114. oldkey = key;
  115. printf( "IOER rc=%d key=0x%02x\n", rc, key);
  116. if( rc >= 0)
  117. {
  118. if( flag == 0 )
  119. {
  120. flag = 1;
  121. if( key & KEY1) printf( "Key 1 pressed\n" );
  122. if( key & KEY2) printf( "Key 2 pressed\n" );
  123. if( key & KEY3) printf( "Key 3 pressed\n" );
  124. if( key & KEY4) printf( "Key 4 pressed\n" );
  125. }
  126. }
  127. else
  128. {
  129. flag = 0;
  130. }
  131. }
  132. NutSleep(125);
  133. }
  134. }
  135. /******************************************************************/
  136. int main(void)
  137. /******************************************************************/
  138. {
  139. uint32_t baud = 115200;
  140. HANDLE led4;
  141. /*
  142. * Register the UART device, open it, assign stdout to it and set
  143. * the baudrate.
  144. */
  145. NutRegisterDevice(&DEV_DEBUG, 0, 0);
  146. freopen(DEV_DEBUG_NAME, "w", stdout);
  147. _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
  148. printf("Init TWI... ");
  149. baud = 400000;
  150. if( TwInit( 0 ) == 0) /* par = slave address but we are master */
  151. printf( "OK\n");
  152. else
  153. printf( "FAIL\n");
  154. TwIOCtl( TWI_SETSPEED, &baud);
  155. printf("Init PCA9555... ");
  156. if( IOExpInit() == 0)
  157. printf( "OK\n");
  158. else
  159. printf( "FAIL\n");
  160. if( NutRegisterLed( &led4, IOXP_PORT1, 4) == 0)
  161. printf( "register LED B OK\n");
  162. NutSetLed( led4, LED_BLINK, 100, 100);
  163. /*
  164. * Start two additional threads. All threads are started with
  165. * priority 64.
  166. */
  167. NutThreadCreate("led", Thread1, 0, 512);
  168. NutThreadCreate("key", Thread2, 0, 512);
  169. /*
  170. * Endless loop in main thread.
  171. */
  172. for (;;)
  173. {
  174. NutSleep(5000);
  175. }
  176. return 0;
  177. }
  178. #endif /* MCU_AT91 */