Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

S button code
Tuesday, August 14th, 2007 at 9:14:21am UTC 

  1. /***************************************************************************
  2. *             __________               __   ___.
  3. *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
  4. *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
  5. *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
  6. *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
  7. *                     \/            \/     \/    \/            \/
  8. * $Id: button-imx31.c 13941 2007-07-20 15:14:29Z aliask $
  9. *
  10. * Copyright (C) 2006 by Linus Nielsen Feltzing
  11. *
  12. * All files in this archive are subject to the GNU General Public License.
  13. * See the file COPYING in the source tree root for full license agreement.
  14. *
  15. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  16. * KIND, either express or implied.
  17. *
  18. ****************************************************************************/
  19.  
  20. #include "config.h"
  21. #include "cpu.h"
  22. #include "system.h"
  23. #include "button.h"
  24. #include "kernel.h"
  25. #include "backlight.h"
  26. #include "adc.h"
  27. #include "system.h"
  28. #include "backlight-target.h"
  29. #include "debug.h"
  30. #include "lcd.h"
  31.  
  32. /* Most code in here is taken from the Linux BSP provided by Freescale */
  33.  
  34. void button_init_device(void)
  35. {
  36.     unsigned int reg_val;
  37.     /* Enable keypad clock */
  38.     //mxc_clks_enable(KPP_CLK);
  39.    
  40.    /* Enable number of rows in keypad (KPCR[7:0])
  41.     * Configure keypad columns as open-drain (KPCR[15:8])
  42.     *
  43.     * Configure the rows/cols in KPP
  44.     * LSB nibble in KPP is for 8 rows
  45.     * MSB nibble in KPP is for 8 cols
  46.     */
  47.    reg_val = KPP_KPCR;
  48.    reg_val |= (1 << 8) - 1;   /* LSB */
  49.    reg_val |= ((1 << 8) - 1) << 8;    /* MSB */
  50.    KPP_KPCR = reg_val;
  51.  
  52.    /* Write 0's to KPDR[15:8] */
  53.    reg_val = KPP_KPDR;
  54.    reg_val &= 0x00ff;
  55.    KPP_KPDR = reg_val;
  56.  
  57.    /* Configure columns as output, rows as input (KDDR[15:0]) */
  58.    reg_val = KPP_KDDR;
  59.    reg_val |= 0xff00;
  60.    reg_val &= 0xff00;
  61.    KPP_KDDR = reg_val;
  62.  
  63.    reg_val = KPP_KPSR;
  64.    reg_val &= ~(0x3);
  65.    reg_val |= 0xD;
  66.    KPP_KPSR = reg_val;
  67.    reg_val |= (1 << 8);
  68.    reg_val &= ~(1 << 9);
  69.    KPP_KPSR = reg_val;
  70. }
  71.  
  72. inline bool button_hold(void)
  73. {
  74.     return GPIO3_DR & 0x10;
  75. }
  76.  
  77. int button_read_device(void)
  78. {
  79.     unsigned short reg_val;
  80.     int col, row;
  81.     short scancode = 0;
  82.     int keycnt = 0;     /* How many keys are still pressed */
  83.     static unsigned short *cur_rcmap;
  84.  
  85. #if 0
  86.     /* save cur keypad matrix to prev */
  87.  
  88.     memcpy(prev_rcmap, cur_rcmap, kpp_dev.kpp_rows * sizeof(prev_rcmap[0]));
  89. #endif
  90. //    memset(cur_rcmap, 0, 5*sizeof(cur_rcmap[0]));
  91.     cur_rcmap[0] = 0;
  92.     cur_rcmap[1] = 0;
  93.     cur_rcmap[2] = 0;
  94.     cur_rcmap[3] = 0;
  95.     cur_rcmap[4] = 0;
  96.  
  97.  
  98.     for (col = 0; col < 8; col++) {  /* Col */
  99.         snprintf(buf,sizeof(buf),"Pre-zerowalking: 0x%X", KPP_KPDR)
  100.         lcd_puts(0,8,buf);
  101.         /* 2. Write 1.s to KPDR[15:8] setting column data to 1.s */
  102.         reg_val = KPP_KPDR;
  103.         reg_val |= 0xff00;
  104.         KPP_KPDR = reg_val;
  105.  
  106.         /*
  107.          * 3. Configure columns as totem pole outputs(for quick
  108.          * discharging of keypad capacitance)
  109.          */
  110.         reg_val = KPP_KPCR;
  111.         reg_val &= 0x00ff;
  112.         KPP_KPCR = reg_val;
  113.  
  114.         udelay(2);
  115.  
  116.         /*
  117.          * 4. Configure columns as open-drain
  118.          */
  119.         reg_val = KPP_KPCR;
  120.         reg_val |= ((1 << 8) - 1) << 8;
  121.         KPP_KPCR = reg_val;
  122.  
  123.         /*
  124.          * 5. Write a single column to 0, others to 1.
  125.          * 6. Sample row inputs and save data. Multiple key presses
  126.          * can be detected on a single column.
  127.          * 7. Repeat steps 2 - 6 for remaining columns.
  128.          */
  129.  
  130.         /* Col bit starts at 8th bit in KPDR */
  131.         reg_val = KPP_KPDR;
  132.         reg_val &= ~(1 << (8 + col));
  133.         KPP_KPDR = reg_val;
  134.  
  135.         /* Delay added to avoid propagating the 0 from column to row
  136.          * when scanning. */
  137.  
  138.         udelay(2);
  139.  
  140.         /* Read row input */
  141.         reg_val = KPP_KPDR;
  142.         for (row = 0; row < 5; row++) {  /* sample row */
  143.             snprintf(buf,sizeof(buf),"Post zerowalking: 0x%X", reg_val);
  144.             lcd_puts(0,9,buf);
  145.             if (!(reg_val & (1 << row))) {
  146.                 cur_rcmap[row] |= (1 << col);
  147.                 DEBUGF("keypress at (%d,%d)",row,col);
  148.                 keycnt++;
  149.             }
  150.         }
  151.     }
  152.  
  153.     /*
  154.      * 8. Return all columns to 0 in preparation for standby mode.
  155.      * 9. Clear KPKD and KPKR status bit(s) by writing to a .1.,
  156.      * set the KPKR synchronizer chain by writing "1" to KRSS register,
  157.      * clear the KPKD synchronizer chain by writing "1" to KDSC register
  158.      */
  159.     reg_val = 0x00;
  160.     KPP_KPDR = reg_val;
  161.     reg_val = KPP_KPDR;
  162.     reg_val = KPP_KPSR;
  163.     reg_val |= 0xF;
  164.     KPP_KPSR = reg_val;
  165.  
  166.     return BUTTON_NONE;
  167. }

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

comments powered by Disqus
worth-right