diff --git a/sim/avr/interrupt.c b/sim/avr/interrupt.c index 2bcbe0d..f03c29b 100644 --- a/sim/avr/interrupt.c +++ b/sim/avr/interrupt.c @@ -22,12 +22,14 @@ #include "interrupt.h" #include "io.h" +#include "wdt.h" -//pseudo-Interrupt vector table +//pseudo-Interrupt vector table isr_fp compa_vect[6]={0}; isr_fp compb_vect[6]={0}; isr_fp ovf_vect[6]={0}; - +isr_fp wdt_vect = 0; +isr_fp pc_vect = 0; void sei() {io.sreg|=SEI;} void cli() {io.sreg&=~SEI;} @@ -45,91 +47,108 @@ enum sim_wgm_mode { wgm_RESERVED }; -enum sim_wgm_mode sim_wgm0[4] = {wgm_NORMAL,wgm_PHASE_PWM,wgm_CTC,wgm_FAST_PWM}; -enum sim_wgm_mode sim_wgmN[8] = {wgm_NORMAL,wgm_PHASE_PWM,wgm_PHASE_PWM,wgm_PH_F_PWM, - wgm_CTC, wgm_FAST_PWM, wgm_FAST_PWM, wgm_FAST_PWM}; +//3-bit wgm table for 8-bit timers +enum sim_wgm_mode sim_wgm_3[] = {wgm_NORMAL,wgm_PHASE_PWM,wgm_CTC,wgm_FAST_PWM, + wgm_RESERVED,wgm_PHASE_PWM, wgm_RESERVED, wgm_FAST_PWM}; + +//4-bit wgm modes for 16-bit timers +enum sim_wgm_mode sim_wgm_4[16] = {wgm_NORMAL,wgm_PHASE_PWM,wgm_PHASE_PWM,wgm_PHASE_PWM, + wgm_CTC, wgm_FAST_PWM, wgm_FAST_PWM, wgm_FAST_PWM, + wgm_PH_F_PWM, wgm_PH_F_PWM, wgm_PHASE_PWM, wgm_PHASE_PWM, + wgm_CTC, wgm_RESERVED, wgm_FAST_PWM, wgm_FAST_PWM}; + +static const uint16_t timer_bitdepth[SIM_N_TIMERS] = { + 0xFF,0xFFFF,0xFF, + //0xFFFF,0xFFFF,0xFFFF 3 more for mega +}; void timer_interrupts() { int i; uint8_t ien = io.sreg&SEI; //interrupts enabled? io.prescaler++; - + //all clocks - for (i=0;i<2;i++){ + for (i=0;i>1) | (io.tccra[i]&3); //only using 3 bits for now - mode = sim_wgmN[wgm]; - } - - //tick - io.tcnt[i]++; - //comparators - if ((io.timsk[i]&(1<>1) | (io.tccra[i]&3); + mode = sim_wgm_3[wgm]; + } + else { + uint8_t wgm = ((io.tccrb[i]&0x18)>>1) | (io.tccra[i]&3); //4 wgm bits + mode = sim_wgm_4[wgm]; + } - switch (mode) { - case wgm_NORMAL: //Normal mode - if (i==0) io.tcnt[i]&=0xFF; //timer0 is 8 bit; - if (i==2) io.tcnt[i]&=0xFF; //timer2 is 8 bit; - if (io.tcnt[i]==0) io.tifr[i]|=(1<2 ) { //don't quit until idle - if (sim.speedup) { - //calculate how many ticks to do. - uint32_t ns_now = platform_ns(); - uint32_t ns_elapsed = (ns_now-ns_prev)*sim.speedup; //todo: try multipling nsnow - simulated_ticks += F_CPU/1e9*ns_elapsed; - ns_prev = ns_now; - } - else { - simulated_ticks++; //as fast as possible - } - - while (sim.masterclock < simulated_ticks){ + if (sim.speedup) { + //calculate how many ticks to do. + uint32_t ns_now = platform_ns(); + uint32_t ns_elapsed = (ns_now-ns_prev)*sim.speedup; //todo: try multipling nsnow + simulated_ticks += F_CPU/1e9*ns_elapsed; + ns_prev = ns_now; + } + else { + simulated_ticks++; //as fast as possible + } + + while (sim.masterclock < simulated_ticks){ - //only read serial port as fast as the baud rate allows - bool read_serial = (sim.masterclock >= next_byte_tick); + //only read serial port as fast as the baud rate allows + bool read_serial = (sim.masterclock >= next_byte_tick); - //do low level hardware - simulate_hardware(read_serial); + //do low level hardware + simulate_hardware(read_serial); - //print the steps. - //For further decoupling, could maintain own counter of STEP_PORT pulses, + //print the steps. + //For further decoupling, could maintain own counter of STEP_PORT pulses, // print that instead of sys.position. - print_steps(0); - - if (read_serial){ - next_byte_tick+=sim.baud_ticks; - //recent block can only change after input, so check here. - printBlock(); - } + print_steps(0); + + if (read_serial){ + next_byte_tick+=sim.baud_ticks; + //recent block can only change after input, so check here. + printBlock(); + } - //TODO: - // set limit pins based on position, - // set probe pin when probing. - // if VARIABLE_SPINDLE, measure pwm pin to report speed? - } + //TODO: + // set limit pins based on position, + // set probe pin when probing. + // if VARIABLE_SPINDLE, measure pwm pin to report speed? + } - platform_sleep(0); //yield + platform_sleep(0); //yield } } @@ -143,22 +148,22 @@ void print_steps(bool force) if (sim.next_print_time == 0.0) { return; } //no printing if (current_block != printed_block ) { - //new block. - if (block_number) { //print values from the end of prev block - fprintf(args.step_out_file, "%20.15f %d, %d, %d\n", sim.sim_time, sys.position[X_AXIS], sys.position[Y_AXIS], sys.position[Z_AXIS]); - } - printed_block = current_block; - if (current_block == NULL) { return; } - // print header - fprintf(args.step_out_file, "# block number %d\n", block_number++); + //new block. + if (block_number) { //print values from the end of prev block + fprintf(args.step_out_file, "%20.15f %d, %d, %d\n", sim.sim_time, sys.position[X_AXIS], sys.position[Y_AXIS], sys.position[Z_AXIS]); + } + printed_block = current_block; + if (current_block == NULL) { return; } + // print header + fprintf(args.step_out_file, "# block number %d\n", block_number++); } //print at correct interval while executing block else if ((current_block && sim.sim_time>=sim.next_print_time) || force ) { - fprintf(args.step_out_file, "%20.15f %d, %d, %d\n", sim.sim_time, sys.position[X_AXIS], sys.position[Y_AXIS], sys.position[Z_AXIS]); - fflush(args.step_out_file); + fprintf(args.step_out_file, "%20.15f %d, %d, %d\n", sim.sim_time, sys.position[X_AXIS], sys.position[Y_AXIS], sys.position[Z_AXIS]); + fflush(args.step_out_file); - //make sure the simulation time doesn't get ahead of next_print_time - while (sim.next_print_time<=sim.sim_time) sim.next_print_time += args.step_time; + //make sure the simulation time doesn't get ahead of next_print_time + while (sim.next_print_time<=sim.sim_time) sim.next_print_time += args.step_time; } } @@ -189,15 +194,15 @@ void printBlock() { b= plan_get_recent_block(); if(b!=last_block && b!=NULL) { - int i; - for (i=0;idirection_bits & get_direction_mask(i)) block_position[i]-= b->steps[i]; - else block_position[i]+= b->steps[i]; - fprintf(args.block_out_file,"%d, ", block_position[i]); - } - fprintf(args.block_out_file,"%f", b->entry_speed_sqr); - fprintf(args.block_out_file,"\n"); - fflush(args.block_out_file); //TODO: needed? + int i; + for (i=0;idirection_bits & get_direction_mask(i)) block_position[i]-= b->steps[i]; + else block_position[i]+= b->steps[i]; + fprintf(args.block_out_file,"%d, ", block_position[i]); + } + fprintf(args.block_out_file,"%f", b->entry_speed_sqr); + fprintf(args.block_out_file,"\n"); + fflush(args.block_out_file); //TODO: needed? last_block= b; } @@ -212,12 +217,12 @@ void grbl_out(uint8_t data){ buf[len++]=data; if(data=='\n' || data=='\r' || len>=127) { - if (args.comment_char && !continuation){ - fprintf(args.grbl_out_file,"%c ",args.comment_char); - } - buf[len]=0; - fprintf(args.grbl_out_file,"%s",buf); - continuation = (len>=128); //print comment on next line unless we are only printing to avoid buffer overflow) - len=0; + if (args.comment_char && !continuation){ + fprintf(args.grbl_out_file,"%c ",args.comment_char); + } + buf[len]=0; + fprintf(args.grbl_out_file,"%s",buf); + continuation = (len>=128); //print comment on next line unless we are only printing to avoid buffer overflow) + len=0; } }