Subversion Repositories pentevo

Rev

Rev 126 | Rev 151 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #include <avr/pgmspace.h>
  4.  
  5. #include <util/delay.h>
  6.  
  7. #include "mytypes.h"
  8. #include "depacker_dirty.h"
  9. #include "getfaraddress.h"
  10. #include "pins.h"
  11. #include "main.h"
  12. #include "ps2.h"
  13. #include "zx.h"
  14. #include "spi.h"
  15. #include "rs232.h"
  16. #include "rtc.h"
  17. #include "atx.h"
  18. #include "joystick.h"
  19.  
  20. /** FPGA compressed data (linker symbol). */
  21. extern const char fpga[] PROGMEM;
  22.  
  23. //Common flag register.
  24. volatile UBYTE flags_register;
  25.  
  26. // Common modes register.
  27. volatile UBYTE modes_register;
  28.  
  29.  
  30. ULONG indata;
  31.  
  32. //buffer for depacking FPGA configuration
  33. //you can USED for other purposed after setup FPGA
  34. UBYTE dbuf[DBSIZE];
  35.  
  36. void put_buffer(UWORD size)
  37. {
  38.         // writes specified length of buffer to the output
  39.         UBYTE * ptr = dbuf;
  40.  
  41.         do
  42.         {
  43.                 spi_send( *(ptr++) );
  44.  
  45.         } while(--size);
  46. }
  47.  
  48. void hardware_init(void)
  49. {
  50.         //Initialized AVR pins
  51.  
  52.         cli(); // disable interrupts
  53.  
  54.         // configure pins
  55.  
  56.         PORTG = 0b11111111;
  57.         DDRG  = 0b00000000;
  58.  
  59. //      PORTF = 0b11110000; // ATX off (zero output), fpga config/etc inputs
  60.         DDRF  = 0b00001000;
  61.  
  62.         PORTE = 0b11111111;
  63.         DDRE  = 0b00000000; // inputs pulled up
  64.  
  65.         PORTD = 0b11111111;
  66.         DDRD  = 0b00000000; // same
  67.  
  68.         PORTC = 0b11011111;
  69.         DDRC  = 0b00000000; // PWRGOOD input, other pulled up
  70.  
  71.         PORTB = 0b11110001;
  72.         DDRB  = 0b10000111; // LED off, spi outs inactive
  73.  
  74.         PORTA = 0b11111111;
  75.         DDRA  = 0b00000000; // pulled up
  76.  
  77.         ACSR = 0x80; // DISABLE analog comparator
  78. }
  79.  
  80. int main()
  81. {
  82. start:
  83.  
  84.         hardware_init();
  85.  
  86. #ifdef LOGENABLE
  87.         rs232_init();
  88. #endif
  89.  
  90.         wait_for_atx_power();
  91.  
  92.         spi_init();
  93.  
  94.         DDRF |= (1<<nCONFIG); // pull low for a time
  95.         _delay_us(40);
  96.         DDRF &= ~(1<<nCONFIG);
  97.         while( !(PINF & (1<<nSTATUS)) ); // wait ready
  98.         indata = (ULONG)GET_FAR_ADDRESS(fpga); // prepare for data fetching
  99.         depacker_dirty();
  100.  
  101.         //LED off
  102.         PORTB |= (1<<LED);
  103.  
  104.         // start timer (led dimming and timeouts for ps/2)
  105.         TCCR2 = 0b01110011; // FOC2=0, {WGM21,WGM20}=01, {COM21,COM20}=11, {CS22,CS21,CS20}=011
  106.                             // clk/64 clocking,
  107.                             // 1/512 overflow rate, total 11.059/32768 = 337.5 Hz interrupt rate
  108.         TIFR = (1<<TOV2);
  109.         TIMSK = (1<<TOIE2);
  110.  
  111.  
  112.         //init some counters and registers
  113.     ps2keyboard_count = 11;
  114.         ps2mouse_count = 12;
  115.         ps2mouse_initstep = 0;
  116.         ps2mouse_resp_count = 0;
  117.         flags_register = 0;
  118.         modes_register = 0;
  119.  
  120.         //enable mouse
  121.         zx_mouse_reset(1);
  122.  
  123.         //set external interrupt
  124.         //INT4 - PS2 Keyboard  (falling edge)
  125.         //INT5 - PS2 Mouse     (falling edge)
  126.         //INT6 - SPI  (falling edge)
  127.         //INT7 - RTC  (falling edge)
  128.         EICRB = (1<<ISC41)+(0<<ISC40) + (1<<ISC51)+(0<<ISC50) + (1<<ISC61)+(0<<ISC60) + (1<<ISC71)+(0<<ISC70); // set condition for interrupt
  129.         EIFR = (1<<INTF4)|(1<<INTF5)|(1<<INTF6)|(1<<INTF7); // clear spurious ints there
  130.         EIMSK |= (1<<INT4)|(1<<INT5)|(1<<INT6)|(1<<INT7); // enable
  131.  
  132.         rtc_init();
  133.         zx_init();
  134.  
  135. #ifdef LOGENABLE
  136.         to_log("zx_init OK\r\n");
  137. #endif
  138.  
  139.  
  140.         sei(); // globally go interrupting
  141.  
  142.         //main loop
  143.         do
  144.     {
  145.         ps2keyboard_task();
  146.                 ps2mouse_task();
  147.         zx_task(ZX_TASK_WORK);
  148.                 zx_mouse_task();
  149.                 joystick_task();
  150.  
  151.                 //
  152.                 if ( flags_register&FLAG_SPI_INT )
  153.                 {
  154.                         //get status byte
  155.                         UBYTE status;
  156.                         nSPICS_PORT &= ~(1<<nSPICS);
  157.                         nSPICS_PORT |= (1<<nSPICS);
  158.                         status = spi_send(0);
  159.                         zx_wait_task( status );
  160.                 }
  161.     }
  162.         while( atx_power_task() );
  163.  
  164.         goto start;
  165. }
  166.