ioexpander.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, driver for PCA9555 I2C I/O-Expander (currently SAM7X256 is 
00039  * tested only)
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                 /* flash led if key is pressed */
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      * Register the UART device, open it, assign stdout to it and set
00149      * the baudrate.
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) /* par = slave address but we are master */
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      * Start two additional threads. All threads are started with
00175      * priority 64.
00176      */
00177     NutThreadCreate("led", Thread1, 0, 512);
00178     NutThreadCreate("key", Thread2, 0, 512);
00179 
00180     /*
00181      * Endless loop in main thread.
00182      */
00183     for (;;)
00184     {
00185         NutSleep(5000);
00186     }
00187     return 0;
00188 }

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