Subversion Repositories pentevo

Rev

Rev 861 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed | ?url?

  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #include <util/delay.h>
  4.  
  5. #include "pins.h"
  6. #include "mytypes.h"
  7.  
  8. #include "main.h"
  9. #include "atx.h"
  10. #include "rs232.h"
  11. #include "zx.h"
  12.  
  13. //if want Log than comment next string
  14. #undef LOGENABLE
  15.  
  16. volatile UWORD atx_counter;
  17.  
  18. void wait_for_atx_power(void)
  19. {
  20.         UBYTE j = MCUCSR;
  21.  
  22.         //clear status register
  23.         MCUCSR = 0;
  24.  
  25. #ifdef LOGENABLE
  26.         char log_ps2keyboard_parse[] = "MC..\r\n";
  27.         log_ps2keyboard_parse[2] = ((j >> 4) <= 9 )?'0'+(j >> 4):'A'+(j >> 4)-10;
  28.         log_ps2keyboard_parse[3] = ((j & 0x0F) <= 9 )?'0'+(j & 0x0F):'A'+(j & 0x0F)-10;
  29.         to_log(log_ps2keyboard_parse);
  30. #endif
  31.  
  32.         //check power
  33.         if ( (nCONFIG_PIN & (1<<nCONFIG)) == 0 )
  34.         {
  35.                 // only after poweron reset we should wait for soft reset button before powering on
  36.                 if(  !(j & ((1<<JTRF)|(1<<WDRF)|(1<<BORF)|(1<<EXTRF)))  ||  (j & (1<<PORF))  )
  37.                 {
  38.                         // have at least 2 sec pause before turning on, as this hopefully gives enough time
  39.                         // for FPGA voltages to drop and it will powerup cleanly without compromising the firmware
  40.                         UBYTE soft_rst_pressed = 0;
  41.                         UBYTE i = PRE_PWRON_WAIT;
  42.  
  43.                         do
  44.                         {
  45.                                 // blink power LED
  46.                                 if( i&7 )
  47.                                         LED_PORT |=  (1<<LED);
  48.                                 else
  49.                                         LED_PORT &= ~(1<<LED);
  50.  
  51.                                 _delay_ms(20);
  52.  
  53.                                 // if soft reset was pressed during wait, remember it and go poweron immediately after the wait ends
  54.                                 soft_rst_pressed |= !(SOFTRES_PIN & (1<<SOFTRES));
  55.  
  56.                         } while(--i);
  57.  
  58.                         // wait further for soft reset press
  59.                         if( !soft_rst_pressed ) while( SOFTRES_PIN&(1<<SOFTRES) );
  60.                 }
  61.  
  62.                 //switch on ATX power (PF3 pin in PORTF)
  63.                 ATXPWRON_PORT |= (1<<ATXPWRON);
  64.  
  65.                 //1 sec delay
  66.                 UBYTE i=50;
  67.                 do {_delay_ms(20);} while(--i);
  68.         }
  69.  
  70.         //clear counter
  71.         atx_counter = 0;
  72. }
  73.  
  74. void atx_power_task(void)
  75. {
  76.         UBYTE i;
  77.        
  78.         static UWORD last_count = 0;
  79.  
  80.         if ( atx_counter > PWROFF_KEY_TIME )
  81.         {
  82.                 //here if either SOFTRES_PIN or F12 held for more than ~5 seconds
  83.  
  84.  
  85.  
  86.                 // no more need in executing mainloop and servicing interrupts
  87.                 cli();
  88.  
  89.                 UBYTE was_soft_rst = !(SOFTRES_PIN & (1<<SOFTRES));
  90.  
  91.                 // switch off ATX power
  92.                 ATXPWRON_PORT &= ~(1<<ATXPWRON);
  93.  
  94.                 // wait for ATX power to actually drop -- for no more than 2 second.
  95.                 // if the wait would be infinite, hang will result on non-ATX power supplies
  96.                 i=PWROFF_WAIT;
  97.                 do
  98.                 {
  99.                         if( !(nCONFIG_PIN & (1<<nCONFIG)) ) break;
  100.                         _delay_ms(20);
  101.                 } while( --i );
  102.  
  103.  
  104.                 // if it was soft reset switch initiated -- wait for it to release
  105.                 if( was_soft_rst )
  106.                 {
  107.                         while( !(SOFTRES_PIN & (1<<SOFTRES)) );
  108.  
  109.                         i=SOFT_RST_DEBOUNCE; // and then debounce it
  110.                         do _delay_ms(20); while(--i);
  111.                 }
  112.  
  113.                 //power led off (timer output disconnect from led pin)
  114.                 TCCR2 &= ~((1<<COM20)|(1<<COM21));
  115.  
  116.                 // signal HARD_RESET to exit out of mainloop in main.c -- and eventually return to wait_for_atx_power()
  117.                 flags_register |= FLAG_HARD_RESET;
  118.         }
  119.         else if ( ( last_count > 0 ) && ( atx_counter == 0 ) )
  120.         {
  121.                 //soft reset (reset Z80 only) -- F12 or softreset pressed for less than 1700 ticks
  122.                 zx_spi_send(SPI_RST_REG, 0, 0x7F);
  123.         }
  124.         last_count = atx_counter;
  125.  
  126.  
  127. }
  128.