add pwm function and some timing fix