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>
00004  *
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  *
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  * 3. Neither the name of the copyright holders nor the names of
00017  *    contributors may be used to endorse or promote products derived
00018  *    from this software without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00027  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00028  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00029  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00030  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00031  * SUCH DAMAGE.
00032  *
00033  * For additional information see http://www.ethernut.de/
00034  */
00035 
00036 /*
00037  * $Log$
00038  *
00039  * Revision 1.0  2009/04/13 ulrichprinz
00040  * First checkin, driver for PCA9555 I2C I/O-Expander (currently SAM7X256 is 
00041  * tested only)
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                 /* flash led if key is pressed */
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      * Register the UART device, open it, assign stdout to it and set
00151      * the baudrate.
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) /* par = slave address but we are master */
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      * Start two additional threads. All threads are started with
00177      * priority 64.
00178      */
00179     NutThreadCreate("led", Thread1, 0, 512);
00180     NutThreadCreate("key", Thread2, 0, 512);
00181 
00182     /*
00183      * Endless loop in main thread.
00184      */
00185     for (;;)
00186     {
00187         NutSleep(5000);
00188     }
00189     return 0;
00190 }

© 2000-2010 by contributors - visit http://www.ethernut.de/