at91sam7x256 - time between interrupt and pin toggle varies

hi,

i’m a trying to set a port pin after a pwm interrupt has occured,

the function draw() waits until the draw_flag has been set in the pwm

ISR, then the function starts to set port pins,

the problem is that the time between the pwm-signal-period-end and the setting of the portpin changes after every pwm period, there are 5 different delay times.

i’m using the olimex at1sam7x256 (sam7-ex256) board

has anybody a idea what i can do to prevent the changing delay times?

here is the code:

The PWM ISR

void ISR_PWM(void){

	draw_flag 	= 1;
	//acknowledge interrupt
    if ((AT91C_BASE_PWMC->PWMC_ISR & AT91C_PWMC_CHID2) == AT91C_PWMC_CHID2) {	 
	}
}

The draw function:

void draw(void){

	unsigned int pix_count;
	unsigned int delay;

	while(1){

  		while(draw_flag != 1);
		
		for(pix_count=0;pix_count<13;pix_count++){ //38
			__asm__ __volatile__( "NOP" );
		}

		for(pix_count=0;pix_count<8;pix_count++){
			for(delay=0;delay<2;delay++){
				pPIO_B->PIO_ODSR 	= 	0x78840000;
			}
			for(delay=0;delay<2;delay++){
			pPIO_B->PIO_ODSR 	= 	0x00000000;
			}
   		
		}
	draw_flag = 0;

	}

	//line_count++;
}

and IRQ handler:

/* ======================================================================== */
AT91F_Irq_Handler:

/* Manage Exception Entry  				*/
/* Adjust and save LR_irq in IRQ stack  */
				sub			lr, lr, #4
				stmfd		sp!, {lr}

/* Save r0 and SPSR (need to be saved for nested interrupt)  */
				mrs			r14, SPSR
				stmfd		sp!, {r0,r14}

/* Write in the IVR to support Protect Mode  				*/
/* No effect in Normal Mode  								*/
/* De-assert the NIRQ and clear the source in Protect Mode  */
				ldr			r14, =AT91C_BASE_AIC
				ldr			r0 , [r14, #AIC_IVR]
				str			r14, [r14, #AIC_IVR]

/* Enable Interrupt and Switch in Supervisor Mode  */
				msr			CPSR_c, #ARM_MODE_SVC

/* Save scratch/used registers and LR in User Stack  */
				stmfd		sp!, { r1-r3, r12, r14}

/* Branch to the routine pointed by the AIC_IVR  */
				mov			r14, pc
				bx			r0

/* Manage Exception Exit  								  */
/* Restore scratch/used registers and LR from User Stack  */
				ldmia		sp!, { r1-r3, r12, r14}

/* Disable Interrupt and switch back in IRQ mode  */
				msr			CPSR_c, #I_BIT | ARM_MODE_IRQ

/* Mark the End of Interrupt on the AIC  */
				ldr			r14, =AT91C_BASE_AIC   
				str			r14, [r14, #AIC_EOICR]	

/* Restore SPSR_irq and r0 from IRQ stack  */
				ldmia		sp!, {r0,r14}
				msr			SPSR_cxsf, r14

/* Restore adjusted  LR_irq from IRQ stack directly in the PC  */
				ldmia		sp!, {pc}^

thanks for any comments or ideas!

How much does the times vary ?

How much variation can you tolerate ?