Čeština / English
Login

SVN Repository / Prohlížení

Aktuální adresář: FITkit / trunk / apps / demo / ps2 / mcu /

main.c

   1  /*******************************************************************************
   2     main.c : PS/2 demo
   3     Copyright (C) 2009 Brno University of Technology,
   4                        Faculty of Information Technology
   5     Author(s): Zdenek Vasicek <vasicek AT fit.vutbr.cz>
   6  
   7     LICENSE TERMS
   8  
   9     Redistribution and use in source and binary forms, with or without
  10     modification, are permitted provided that the following conditions
  11     are met:
  12     1. Redistributions of source code must retain the above copyright
  13        notice, this list of conditions and the following disclaimer.
  14     2. Redistributions in binary form must reproduce the above copyright
  15        notice, this list of conditions and the following disclaimer in
  16        the documentation and/or other materials provided with the
  17        distribution.
  18     3. All advertising materials mentioning features or use of this software
  19        or firmware must display the following acknowledgement:
  20  
  21          This product includes software developed by the University of
  22          Technology, Faculty of Information Technology, Brno and its
  23          contributors.
  24  
  25     4. Neither the name of the Company nor the names of its contributors
  26        may be used to endorse or promote products derived from this
  27        software without specific prior written permission.
  28  
  29     This software or firmware is provided ``as is'', and any express or implied
  30     warranties, including, but not limited to, the implied warranties of
  31     merchantability and fitness for a particular purpose are disclaimed.
  32     In no event shall the company or contributors be liable for any
  33     direct, indirect, incidental, special, exemplary, or consequential
  34     damages (including, but not limited to, procurement of substitute
  35     goods or services; loss of use, data, or profits; or business
  36     interruption) however caused and on any theory of liability, whether
  37     in contract, strict liability, or tort (including negligence or
  38     otherwise) arising in any way out of the use of this software, even
  39     if advised of the possibility of such damage.
  40  
  41     $Id$
  42  
  43  
  44  *******************************************************************************/
  45  
  46  #include <fitkitlib.h>
  47  #include <irq/fpga_interrupt.h>
  48  
  49  #define BASE_ADDR_PS2 0x02
  50  #define read_byte_ps2() FPGA_SPI_RW_A8_D8(SPI_FPGA_ENABLE_READ, BASE_ADDR_PS2, 0)
  51  #define write_byte_ps2(byte) FPGA_SPI_RW_A8_D8(SPI_FPGA_ENABLE_WRITE, BASE_ADDR_PS2, byte)
  52  
  53  volatile unsigned char ps2_buf;
  54  volatile char ps2_buf_full;
  55  volatile char ps2_readen;
  56  
  57  /*******************************************************************************
  58   * Vypis uzivatelske napovedy (funkce se vola pri vykonavani prikazu "help")
  59  *******************************************************************************/
  60  void print_user_help(void)
  61  {
  62      term_send_str_crlf("User help:");
  63      term_send_str_crlf(" KBLED  ...... led");
  64      term_send_str_crlf(" KBECHO ...... echo");
  65      term_send_str_crlf(" KBID   ...... ID");
  66      term_send_str_crlf(" KBEN   ...... enable");
  67      term_send_str_crlf(" KBDIS  ...... disable");
  68      term_send_str_crlf(" KBCMD XX .... send hexa command");
  69  }
  70  
  71  /*******************************************************************************
  72   * Inicializace periferii/komponent po naprogramovani FPGA
  73  *******************************************************************************/
  74  void fpga_initialized()
  75  {
  76    fpga_interrupt_init(BIT2);
  77  }
  78  
  79  
  80  /*******************************************************************************
  81   * Dekodovani a vykonani uzivatelskych prikazu
  82  *******************************************************************************/
  83  unsigned char decode_user_cmd(char *cmd_ucase, char *cmd)
  84  {
  85    if (strcmp5(cmd_ucase, "KBCMD")) {
  86      write_byte_ps2(hex2chr(cmd + 6));
  87      return (USER_COMMAND);
  88    }
  89  
  90    if (strcmp5(cmd_ucase, "KBLED")) {
  91       ps2_readen = 0;
  92       ps2_buf_full = 0;
  93       write_byte_ps2(0xED);
  94       term_send_str_crlf("KBLED wait for response");
  95       while (ps2_buf_full == 0) _NOP();
  96       if (ps2_buf == 0xFA) {
  97          ps2_buf_full = 0;
  98          term_send_str_crlf("KBLED ACK");
  99          write_byte_ps2(0x01);
 100       } else {
 101          term_send_str_crlf("KBLED NAK");
 102       }
 103       ps2_readen = 1;
 104       return (USER_COMMAND);
 105    }
 106  
 107    if (strcmp6(cmd_ucase, "KBECHO")) {
 108       write_byte_ps2(0xEE);
 109       return (USER_COMMAND);
 110    }
 111  
 112    if (strcmp4(cmd_ucase, "KBID")) {
 113       write_byte_ps2(0xF2);
 114       return (USER_COMMAND);
 115    }
 116  
 117    if (strcmp4(cmd_ucase, "KBEN")) {
 118       write_byte_ps2(0xF4);
 119       return (USER_COMMAND);
 120    }
 121  
 122    if (strcmp5(cmd_ucase, "KBDIS")) {
 123       write_byte_ps2(0xF5);
 124       return (USER_COMMAND);
 125    }
 126    return (CMD_UNKNOWN);
 127  }
 128  
 129  /*******************************************************************************
 130   * Obsluha preruseni z FPGA
 131  *******************************************************************************/
 132  void fpga_interrupt_handler(unsigned char bits)
 133  {
 134    if (bits & BIT2) {
 135       ps2_buf = read_byte_ps2();
 136       ps2_buf_full = 1;
 137    }
 138  }
 139  
 140  /*******************************************************************************
 141   * Hlavni funkce
 142  *******************************************************************************/
 143  int main(void)
 144  {
 145    short counter = 0;
 146    char cnt;
 147  
 148    cnt = 0;
 149    ps2_buf_full = 0;
 150    ps2_readen = 1;
 151  
 152    initialize_hardware();
 153  
 154    set_led_d6(1);                       // rozsviceni D6
 155    set_led_d5(1);                       // rozsviceni D5
 156  
 157    while (1)
 158    {
 159        delay_ms(1);  //zpozdeni 1ms
 160  
 161        counter++;
 162        if (counter == 500)
 163        {
 164           flip_led_d6(); //invertovat LED
 165  
 166           counter = 0;
 167        }
 168  
 169        terminal_idle();  // obsluha terminalu
 170  
 171        if ((ps2_readen == 1) && (ps2_buf_full == 1))
 172        {
 173           //Vypis prijatych znaku na terminal
 174           ps2_buf_full = 0;
 175  
 176           term_send_hex(ps2_buf);
 177           term_send_char(' ');
 178  
 179           cnt++;
 180           if (cnt > 15)
 181           {
 182              term_send_crlf();
 183              cnt=0;
 184           }
 185        }
 186    }
 187  }
 188  
Zobrazeno: 715384x Naposledy: 4.10.2022 07:53:00