Subversion Repositories pentevo

Rev

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