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
00047 #include <stdio.h>
00048 #include <io.h>
00049
00050 #include <cfg/arch.h>
00051 #include <dev/board.h>
00052 #include <dev/gpio.h>
00053
00054 #include <sys/thread.h>
00055 #include <sys/timer.h>
00056 #include <dev/pca9555.h>
00057 #include <dev/led.h>
00058
00059 #define KEY1 (1<<0)
00060 #define KEY2 (1<<1)
00061 #define KEY3 (1<<2)
00062 #define KEY4 (1<<3)
00063
00064
00065 THREAD(Thread1, arg)
00066
00067 {
00068 HANDLE ds1, ds2, ds3;
00069 int ledmask = 1;
00070
00071 if( NutRegisterLed( &ds1, IOXP_PORT1, 0) == 0)
00072 printf( "register LED 1 OK\n");
00073 if( NutRegisterLed( &ds2, IOXP_PORT1, 1) == 0)
00074 printf( "register LED 2 OK\n");
00075 if( NutRegisterLed( &ds3, IOXP_PORT1, 2) == 0)
00076 printf( "register LED 3 OK\n");
00077
00078 NutThreadSetPriority(128);
00079 for (;;) {
00080 NutSetLed( ds1, 0, (ledmask>>0) & 1);
00081 NutSetLed( ds2, 0, (ledmask>>1) & 1);
00082 NutSetLed( ds3, 0, (ledmask>>2) & 1);
00083
00084 ledmask <<= 1;
00085 if( ledmask & (1<<3)) ledmask = 1;
00086
00087 NutSleep(250);
00088 }
00089 }
00090
00091
00092 THREAD(Thread2, arg)
00093
00094 {
00095 uint8_t key, oldkey;
00096 uint8_t flag = 1;
00097 int rc;
00098 HANDLE led3;
00099
00100 printf( "Key and LED test for PCA9555\n" );
00101
00102 if( NutRegisterLed( &led3, IOXP_PORT1, 3) == 0)
00103 printf( "register LED 4 OK\n");
00104
00105 oldkey = ~key;
00106
00107 NutThreadSetPriority(128);
00108 for (;;)
00109 {
00110 key = 0;
00111 rc = IOExpRawRead( 0, &key);
00112 if( key != oldkey) {
00113 if( key > oldkey) {
00114
00115 NutSetLed( led3, 5, LED_ONESHOT);
00116 }
00117
00118 oldkey = key;
00119 printf( "IOER rc=%d key=0x%02x\n", rc, key);
00120 if( rc >= 0)
00121 {
00122 if( flag == 0 )
00123 {
00124 flag = 1;
00125
00126 if( key & KEY1) printf( "Key 1 pressed\n" );
00127 if( key & KEY2) printf( "Key 2 pressed\n" );
00128 if( key & KEY3) printf( "Key 3 pressed\n" );
00129 if( key & KEY4) printf( "Key 4 pressed\n" );
00130 }
00131 }
00132 else
00133 {
00134 flag = 0;
00135 }
00136 }
00137 NutSleep(125);
00138 }
00139 }
00140
00141
00142 int main(void)
00143
00144 {
00145 u_long baud = 115200;
00146 HANDLE led4;
00147
00148
00149
00150
00151 NutRegisterDevice(&DEV_DEBUG, 0, 0);
00152 freopen(DEV_DEBUG_NAME, "w", stdout);
00153 _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
00154
00155 printf("Init TWI... ");
00156 baud = 400000;
00157 if( TwInit( 0 ) == 0)
00158 printf( "OK\n");
00159 else
00160 printf( "FAIL\n");
00161 TwIOCtl( TWI_SETSPEED, &baud);
00162
00163 printf("Init PCA9555... ");
00164 if( IOExpInit() == 0)
00165 printf( "OK\n");
00166 else
00167 printf( "FAIL\n");
00168
00169 if( NutRegisterLed( &led4, IOXP_PORT1, 4) == 0)
00170 printf( "register LED B OK\n");
00171 NutSetLed( led4, 100, LED_BLINK);
00172
00173
00174
00175
00176
00177 NutThreadCreate("led", Thread1, 0, 512);
00178 NutThreadCreate("key", Thread2, 0, 512);
00179
00180
00181
00182
00183 for (;;)
00184 {
00185 NutSleep(5000);
00186 }
00187 return 0;
00188 }