Subversion Repositories pentevo

Rev

Rev 180 | Rev 219 | 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 data pointer [far address] (linker symbol). */
  21. extern ULONG fpga PROGMEM;
  22.  
  23. // FPGA data index..
  24. volatile ULONG curFpga;
  25.  
  26. // Common flag register.
  27. volatile UBYTE flags_register;
  28.  
  29. // Common modes register.
  30. volatile UBYTE modes_register;
  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.         to_log("VER:");
  89.         {
  90.                 UBYTE b,i;
  91.                 ULONG version = 0x1DFF0;
  92.                 char VER[]="..";
  93.                 for( i=0; i<12; i++)
  94.                 {
  95.                         dbuf[i] = pgm_read_byte_far(version+i);
  96.                 }
  97.                 dbuf[i]=0;
  98.                 to_log((char*)dbuf);
  99.                 to_log(" ");
  100.                 UBYTE b1 = pgm_read_byte_far(version+12);
  101.                 UBYTE b2 = pgm_read_byte_far(version+13);
  102.                 UBYTE day = b1&0x1F;
  103.                 UBYTE mon = ((b2<<3)+(b1>>5))&0x0F;
  104.                 UBYTE year = (b2>>1)&0x3F;
  105.                 VER[0] = '0'+(day/10);
  106.                 VER[1] = '0'+(day%10);
  107.         to_log(VER);
  108.         to_log(".");
  109.                 VER[0] = '0'+(mon/10);
  110.                 VER[1] = '0'+(mon%10);
  111.         to_log(VER);
  112.         to_log(".");
  113.                 VER[0] = '0'+(year/10);
  114.                 VER[1] = '0'+(year%10);
  115.         to_log(VER);
  116.         to_log("\r\n");
  117.                 //
  118.                 for( i=0; i<16; i++)
  119.                 {
  120.                         b = pgm_read_byte_far(version+i);
  121.                         VER[0] = ((b >> 4) <= 9 )?'0'+(b >> 4):'A'+(b >> 4)-10;
  122.                         VER[1] = ((b & 0x0F) <= 9 )?'0'+(b & 0x0F):'A'+(b & 0x0F)-10;
  123.                         to_log(VER);
  124.                 }
  125.                 to_log("\r\n");
  126.         }
  127. #endif
  128.  
  129.         wait_for_atx_power();
  130.  
  131.         spi_init();
  132.  
  133.         DDRF |= (1<<nCONFIG); // pull low for a time
  134.         _delay_us(40);
  135.         DDRF &= ~(1<<nCONFIG);
  136.         while( !(PINF & (1<<nSTATUS)) ); // wait ready
  137.  
  138.         curFpga = GET_FAR_ADDRESS(fpga); // prepare for data fetching
  139. #ifdef LOGENABLE
  140.         {
  141.         char log_fpga[]="F........\r\n";
  142.         UBYTE b = (UBYTE)((curFpga>>24)&0xFF);
  143.         log_fpga[1] = ((b >> 4) <= 9 )?'0'+(b >> 4):'A'+(b >> 4)-10;
  144.         log_fpga[2] = ((b & 0x0F) <= 9 )?'0'+(b & 0x0F):'A'+(b & 0x0F)-10;
  145.         b = (UBYTE)((curFpga>>16)&0xFF);
  146.         log_fpga[3] = ((b >> 4) <= 9 )?'0'+(b >> 4):'A'+(b >> 4)-10;
  147.         log_fpga[4] = ((b & 0x0F) <= 9 )?'0'+(b & 0x0F):'A'+(b & 0x0F)-10;
  148.         b = (UBYTE)((curFpga>>8)&0xFF);
  149.         log_fpga[5] = ((b >> 4) <= 9 )?'0'+(b >> 4):'A'+(b >> 4)-10;
  150.         log_fpga[6] = ((b & 0x0F) <= 9 )?'0'+(b & 0x0F):'A'+(b & 0x0F)-10;
  151.         b = (UBYTE)(curFpga&0xFF);
  152.         log_fpga[7] = ((b >> 4) <= 9 )?'0'+(b >> 4):'A'+(b >> 4)-10;
  153.         log_fpga[8] = ((b & 0x0F) <= 9 )?'0'+(b & 0x0F):'A'+(b & 0x0F)-10;
  154.         to_log(log_fpga);
  155.         }
  156. #endif
  157.         depacker_dirty();
  158.  
  159.         //power led OFF
  160.         LED_PORT |= 1<<LED;
  161.  
  162.         // start timer (led dimming and timeouts for ps/2)
  163.         TCCR2 = 0b01110011; // FOC2=0, {WGM21,WGM20}=01, {COM21,COM20}=11, {CS22,CS21,CS20}=011
  164.                             // clk/64 clocking,
  165.                             // 1/512 overflow rate, total 11.059/32768 = 337.5 Hz interrupt rate
  166.         TIFR = (1<<TOV2);
  167.         TIMSK = (1<<TOIE2);
  168.  
  169.  
  170.         //init some counters and registers
  171.     ps2keyboard_count = 12;
  172.         ps2keyboard_cmd_count = 0;
  173.         ps2keyboard_cmd = 0;
  174.         ps2mouse_count = 12;
  175.         ps2mouse_initstep = 0;
  176.         ps2mouse_resp_count = 0;
  177.         flags_register = 0;
  178.         modes_register = 0;
  179.  
  180.         //enable mouse
  181.         zx_mouse_reset(1);
  182.  
  183.         //set external interrupt
  184.         //INT4 - PS2 Keyboard  (falling edge)
  185.         //INT5 - PS2 Mouse     (falling edge)
  186.         //INT6 - SPI  (falling edge)
  187.         //INT7 - RTC  (falling edge)
  188.         EICRB = (1<<ISC41)+(0<<ISC40) + (1<<ISC51)+(0<<ISC50) + (1<<ISC61)+(0<<ISC60) + (1<<ISC71)+(0<<ISC70); // set condition for interrupt
  189.         EIFR = (1<<INTF4)|(1<<INTF5)|(1<<INTF6)|(1<<INTF7); // clear spurious ints there
  190.         EIMSK |= (1<<INT4)|(1<<INT5)|(1<<INT6)|(1<<INT7); // enable
  191.  
  192.         zx_init();
  193.         rtc_init();
  194.  
  195.  
  196. #ifdef LOGENABLE
  197.         to_log("zx_init OK\r\n");
  198. #endif
  199.  
  200.  
  201.         sei(); // globally go interrupting
  202.  
  203.         //set led on keyboard
  204.         ps2keyboard_send_cmd(PS2KEYBOARD_CMD_SETLED);
  205.  
  206.         //main loop
  207.         do
  208.     {
  209.                 ps2mouse_task();
  210.         ps2keyboard_task();
  211.         zx_task(ZX_TASK_WORK);
  212.                 zx_mouse_task();
  213.                 joystick_task();
  214.  
  215.                 //
  216.                 if ( flags_register&FLAG_SPI_INT )
  217.                 {
  218.                         //get status byte
  219.                         UBYTE status;
  220.                         nSPICS_PORT &= ~(1<<nSPICS);
  221.                         nSPICS_PORT |= (1<<nSPICS);
  222.                         status = spi_send(0);
  223.                         zx_wait_task( status );
  224.                 }
  225.     }
  226.         while( atx_power_task() );
  227.  
  228.         goto start;
  229. }
  230.