ioexpander.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00049 #include <stdio.h>
00050 #include <io.h>
00051
00052 #include <cfg/arch.h>
00053 #include <dev/board.h>
00054 #include <dev/gpio.h>
00055
00056 #include <sys/thread.h>
00057 #include <sys/timer.h>
00058 #include <dev/pca9555.h>
00059 #include <dev/led.h>
00060
00061 #define KEY1 (1<<0)
00062 #define KEY2 (1<<1)
00063 #define KEY3 (1<<2)
00064 #define KEY4 (1<<3)
00065
00066
00067 THREAD(Thread1, arg)
00068
00069 {
00070 HANDLE ds1, ds2, ds3;
00071 int ledmask = 1;
00072
00073 if( NutRegisterLed( &ds1, IOXP_PORT1, 0) == 0)
00074 printf( "register LED 1 OK\n");
00075 if( NutRegisterLed( &ds2, IOXP_PORT1, 1) == 0)
00076 printf( "register LED 2 OK\n");
00077 if( NutRegisterLed( &ds3, IOXP_PORT1, 2) == 0)
00078 printf( "register LED 3 OK\n");
00079
00080 NutThreadSetPriority(128);
00081 for (;;) {
00082 NutSetLed( ds1, 0, (ledmask>>0) & 1);
00083 NutSetLed( ds2, 0, (ledmask>>1) & 1);
00084 NutSetLed( ds3, 0, (ledmask>>2) & 1);
00085
00086 ledmask <<= 1;
00087 if( ledmask & (1<<3)) ledmask = 1;
00088
00089 NutSleep(250);
00090 }
00091 }
00092
00093
00094 THREAD(Thread2, arg)
00095
00096 {
00097 uint8_t key, oldkey;
00098 uint8_t flag = 1;
00099 int rc;
00100 HANDLE led3;
00101
00102 printf( "Key and LED test for PCA9555\n" );
00103
00104 if( NutRegisterLed( &led3, IOXP_PORT1, 3) == 0)
00105 printf( "register LED 4 OK\n");
00106
00107 oldkey = ~key;
00108
00109 NutThreadSetPriority(128);
00110 for (;;)
00111 {
00112 key = 0;
00113 rc = IOExpRawRead( 0, &key);
00114 if( key != oldkey) {
00115 if( key > oldkey) {
00116
00117 NutSetLed( led3, 5, LED_ONESHOT);
00118 }
00119
00120 oldkey = key;
00121 printf( "IOER rc=%d key=0x%02x\n", rc, key);
00122 if( rc >= 0)
00123 {
00124 if( flag == 0 )
00125 {
00126 flag = 1;
00127
00128 if( key & KEY1) printf( "Key 1 pressed\n" );
00129 if( key & KEY2) printf( "Key 2 pressed\n" );
00130 if( key & KEY3) printf( "Key 3 pressed\n" );
00131 if( key & KEY4) printf( "Key 4 pressed\n" );
00132 }
00133 }
00134 else
00135 {
00136 flag = 0;
00137 }
00138 }
00139 NutSleep(125);
00140 }
00141 }
00142
00143
00144 int main(void)
00145
00146 {
00147 uint32_t baud = 115200;
00148 HANDLE led4;
00149
00150
00151
00152
00153 NutRegisterDevice(&DEV_DEBUG, 0, 0);
00154 freopen(DEV_DEBUG_NAME, "w", stdout);
00155 _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
00156
00157 printf("Init TWI... ");
00158 baud = 400000;
00159 if( TwInit( 0 ) == 0)
00160 printf( "OK\n");
00161 else
00162 printf( "FAIL\n");
00163 TwIOCtl( TWI_SETSPEED, &baud);
00164
00165 printf("Init PCA9555... ");
00166 if( IOExpInit() == 0)
00167 printf( "OK\n");
00168 else
00169 printf( "FAIL\n");
00170
00171 if( NutRegisterLed( &led4, IOXP_PORT1, 4) == 0)
00172 printf( "register LED B OK\n");
00173 NutSetLed( led4, 100, LED_BLINK);
00174
00175
00176
00177
00178
00179 NutThreadCreate("led", Thread1, 0, 512);
00180 NutThreadCreate("key", Thread2, 0, 512);
00181
00182
00183
00184
00185 for (;;)
00186 {
00187 NutSleep(5000);
00188 }
00189 return 0;
00190 }