// llcpwm.cpp // QSPICE C++ block: 3 analog inputs, 8 Boolean PWM outputs, 3 analog monitors. // // Inputs: // FIN : 0~1 V -> 0~100 kHz // DHV : 0~1 V -> high-voltage-side final duty 0~100% // DLV : 0~1 V -> low-voltage-side final duty 0~100% // // Outputs: // PWM1_A : low-voltage-side group 1, pulse center at T/4 // PWM2_A : low-voltage-side group 2, pulse center at 3T/4 // PWM1_B : high-voltage-side group 1, same phase/center as PWM1_A // PWM2_B : high-voltage-side group 2, same phase/center as PWM2_A // FOUT : 0~1 output corresponds to 10~100 kHz; below 10 kHz gives 0 // DOUT_A : actual PWM1_A high-time / switching period, 0~1 // DOUT_B : actual PWM1_B high-time / switching period, 0~1; zero before HV starts // DOUT_A/DOUT_B include the real deadtime effect, not duty commands // PWM4_A : phase-shifted LV group 1; independent soft-start in sequence mode // PWM5_A : phase-shifted LV group 2; independent soft-start in sequence mode // PWM4_B : phase-shifted HV group 1; starts after PWM4_A/PWM5_A reach target // PWM5_B : phase-shifted HV group 2; starts after PWM4_A/PWM5_A reach target // // PHASE_SHIFT=0.5 means 180 degrees, 0.25 means 90 degrees. // // PWM1_A/PWM2_A are two independent pulse groups, NOT complementary outputs. // PWM1_B/PWM2_B use the same two-group center-aligned pattern. // At D=0 both outputs of the corresponding side are off. // // Sequence mode (START_45_AFTER_AB=1): // 1. PWM1_A/PWM2_A ramp from LV_D_START to DLV. // 2. After DLV is reached, wait HV_DELAY; PWM1_B/PWM2_B ramp to DHV. // 3. After both original A/B duties reach target, wait PWM4A_DELAY. // 4. PWM4_A/PWM5_A independently ramp from LV_D_START to DLV. // 5. PWM4_B/PWM5_B independently ramp from HV_D_START to DHV. // // Synchronous mode (START_45_AFTER_AB=0): // PWM4/5 mirror the original A/B soft-start duties with PHASE_SHIFT. // // When both sides have the same duty: PWM1_A=PWM1_B and PWM2_A=PWM2_B. #include #include extern "C" __declspec(dllexport) void (*bzero)(void *ptr, unsigned int count) = 0; // ---------------- User settings ---------------- // Duty values use 0~1, not percent. static const double LV_D_START = 0.02; // Low-voltage-side initial duty static const double HV_D_START = 0.02; // High-voltage-side initial duty static const double LV_SLEW = 500.0; // duty/s; 500 means 0.5 per ms static const double HV_SLEW = 500.0; // duty/s static const double HV_DELAY = 20e-6; // Delay after 1/2 LV reaches target, s static const double DEADTIME = 100e-9; // Deadtime between the two pulse groups, s // 1: full 1A/2A -> 1B/2B -> 4A/5A -> 4B/5B sequence. // 0: 4/5 start together with 1/2 and mirror their respective A/B duties. static const int START_45_AFTER_AB = 1; // Delay after original 1/2 A and B both reach their targets, before 4A/5A start. static const double PWM4A_DELAY = 1e-3; // Minimum landing correction if floating-point evaluation stops just before an edge. // It is not used by Trunc() and never becomes a global running timestep. static const double EDGE_TTOL = 2e-9; // Phase shift of PWM4/PWM5 relative to PWM1/PWM2, normalized to one period. // 0.5=180 degrees, 0.25=90 degrees, 0.125=45 degrees, 0=0 degree. static const double PHASE_SHIFT = 0.5; // ------------------------------------------------ union uData { bool b; char c; unsigned char uc; short s; unsigned short us; int i; unsigned int ui; float f; double d; long long int i64; unsigned long long int ui64; char *str; unsigned char *bytes; }; // int DllMain() must exist and return 1 for a process to load the .DLL int __stdcall DllMain(void *module, unsigned int reason, void *reserved) { return 1; } #undef FIN #undef DHV #undef DLV #undef PWM1_A #undef PWM1_B #undef PWM2_A #undef PWM2_B #undef FOUT #undef DOUT_A #undef DOUT_B #undef PWM4_A #undef PWM5_A #undef PWM4_B #undef PWM5_B struct sLLCPWM { bool initialized; int stage; // 0=1/2 LV, 1=wait 1/2 HV, 2=1/2 HV, // 3=wait 4/5 LV, 4=4/5 LV, 5=4/5 HV double t_last; double t_hv; double t_45a; // Absolute start time of the independent 4A/5A ramp double phase; // Normalized phase, 0~1 double d_lv; double d_hv; double d4_lv; // Independent 4A/5A duty in sequence mode double d4_hv; // Independent 4B/5B duty in sequence mode double frequency; // Last evaluated input frequency, Hz double max_step; // Next external step limit calculated from upcoming events }; static double clamp01(double x) { if(x < 0.0) return 0.0; if(x > 1.0) return 1.0; return x; } static double approach_target(double current, double target, double slew, double dt) { double max_delta; if(slew <= 0.0) return target; if(dt <= 0.0) return current; max_delta = slew * dt; if(current < target) { current += max_delta; if(current > target) current = target; } else if(current > target) { current -= max_delta; if(current < target) current = target; } return current; } // Two center-aligned PWM groups, not complementary outputs. // PWM1 is centered at phase=0.25; PWM2 is centered at phase=0.75. // Input duty D directly means the actual high-time / full switching period. // Therefore D=0.47 requests a real 47% high duty for each pulse group. // The maximum duty is limited to 0.5-h so the two groups never overlap, // where h=DEADTIME/switching_period. D=0 turns both groups off. static void dual_center_pwm(double phase, double duty, double deadtime_ratio, bool *pwm1, bool *pwm2, double *actual_duty) { double d, h, dmax; double a0, a1, b0, b1; d = clamp01(duty); *pwm1 = false; *pwm2 = false; *actual_duty = 0.0; h = deadtime_ratio; if(h < 0.0) h = 0.0; dmax = 0.5 - h; if(dmax < 0.0) dmax = 0.0; if(d > dmax) d = dmax; if(d <= 0.0) return; // Group 1: center T/4, actual full-period high ratio = d. a0 = 0.25 - 0.5 * d; a1 = 0.25 + 0.5 * d; // Group 2: center 3T/4, actual full-period high ratio = d. b0 = 0.75 - 0.5 * d; b1 = 0.75 + 0.5 * d; // Actual normalized high time of either output, including deadtime reduction. *actual_duty = a1 - a0; *pwm1 = (phase >= a0 && phase < a1); *pwm2 = (phase >= b0 && phase < b1); } // Return the forward normalized distance from phase to edge. // If exactly on an edge, that edge is treated as already evaluated. static double forward_phase_distance(double phase, double edge) { double dp = edge - phase; dp -= floor(dp); if(dp <= 1e-12) dp += 1.0; return dp; } static void consider_event(double dt, double *best) { if(dt > 0.0 && dt < *best) *best = dt; } // Consider all four edges of one center-aligned PWM pair. static void consider_pair_edges(double phase, double duty, double frequency, double *best) { double d = clamp01(duty); double h = DEADTIME * frequency; double dmax = 0.5 - h; double edges[4]; int i; if(dmax < 0.0) dmax = 0.0; if(d > dmax) d = dmax; if(d <= 0.0) return; edges[0] = 0.25 - 0.5 * d; edges[1] = 0.25 + 0.5 * d; edges[2] = 0.75 - 0.5 * d; edges[3] = 0.75 + 0.5 * d; for(i = 0; i < 4; ++i) consider_event(forward_phase_distance(phase, edges[i]) / frequency, best); } // Calculate the next useful evaluation point from the current accepted state. // A baseline of 20 points/period retains response to changing analog inputs. // Upcoming PWM edges are scheduled directly; Trunc() performs no refinement. static void schedule_next_step(struct sLLCPWM *inst, double t, double target_lv, double target_hv) { double best = 1.0; double baseline; double phase_shifted; double dt; if(inst->frequency <= 0.0) { inst->max_step = 1e-6; return; } // Original 1A/2A edges are always active. consider_pair_edges(inst->phase, inst->d_lv, inst->frequency, &best); phase_shifted = inst->phase + PHASE_SHIFT; phase_shifted -= floor(phase_shifted); // Shifted A edges: mirrored duty in synchronous mode, independent duty in // sequence mode. Before stage 4 the sequential 4A/5A outputs are off. if(!START_45_AFTER_AB) consider_pair_edges(phase_shifted, inst->d_lv, inst->frequency, &best); else if(inst->stage >= 4) consider_pair_edges(phase_shifted, inst->d4_lv, inst->frequency, &best); // Original 1B/2B remain active through all later stages. if(inst->stage >= 2) consider_pair_edges(inst->phase, inst->d_hv, inst->frequency, &best); if(!START_45_AFTER_AB) { if(inst->stage >= 2) consider_pair_edges(phase_shifted, inst->d_hv, inst->frequency, &best); } else if(inst->stage >= 5) { consider_pair_edges(phase_shifted, inst->d4_hv, inst->frequency, &best); } // Include soft-start/stage events even if they do not immediately toggle PWM. if(inst->stage == 0 && LV_SLEW > 0.0) { dt = (target_lv - inst->d_lv) / LV_SLEW; consider_event(dt, &best); } if(inst->stage == 1) consider_event(inst->t_hv - t, &best); if(inst->stage == 2) { if(LV_SLEW > 0.0) { dt = (target_lv - inst->d_lv) / LV_SLEW; consider_event(dt, &best); } if(HV_SLEW > 0.0) { dt = (target_hv - inst->d_hv) / HV_SLEW; consider_event(dt, &best); } } if(inst->stage == 3) consider_event(inst->t_45a - t, &best); if(inst->stage == 4 && LV_SLEW > 0.0) { dt = (target_lv - inst->d4_lv) / LV_SLEW; consider_event(dt, &best); } if(inst->stage == 5) { if(LV_SLEW > 0.0) { dt = (target_lv - inst->d4_lv) / LV_SLEW; consider_event(dt, &best); } if(HV_SLEW > 0.0) { dt = (target_hv - inst->d4_hv) / HV_SLEW; consider_event(dt, &best); } } baseline = 1.0 / (20.0 * inst->frequency); if(best > baseline) best = baseline; if(best < EDGE_TTOL) best = EDGE_TTOL; inst->max_step = best; } extern "C" __declspec(dllexport) void llcpwm( struct sLLCPWM **opaque, double t, union uData *data) { double FIN = data[0].d; double DHV = data[1].d; double DLV = data[2].d; bool &PWM1_A = data[3].b; bool &PWM1_B = data[4].b; bool &PWM2_A = data[5].b; bool &PWM2_B = data[6].b; double &FOUT = data[7].d; double &DOUT_A = data[8].d; double &DOUT_B = data[9].d; bool &PWM4_A = data[10].b; bool &PWM5_A = data[11].b; bool &PWM4_B = data[12].b; bool &PWM5_B = data[13].b; struct sLLCPWM *inst; double target_lv, target_hv, frequency; double dt, dt_hv, dt_45, h, actual_duty_a, actual_duty_b; double phase_shifted, actual_duty_dummy; int previous_stage; if(!*opaque) { *opaque = (struct sLLCPWM *)malloc(sizeof(struct sLLCPWM)); bzero(*opaque, sizeof(struct sLLCPWM)); } inst = *opaque; target_lv = clamp01(DLV); target_hv = clamp01(DHV); frequency = 100000.0 * clamp01(FIN); if(!inst->initialized) { inst->initialized = true; inst->stage = 0; inst->t_last = t; inst->t_hv = 0.0; inst->t_45a = 0.0; inst->phase = 0.0; inst->d_lv = clamp01(LV_D_START); inst->d_hv = clamp01(HV_D_START); inst->d4_lv = clamp01(LV_D_START); inst->d4_hv = clamp01(HV_D_START); inst->frequency = frequency; inst->max_step = 1e-6; } dt = t - inst->t_last; if(dt < 0.0) dt = 0.0; // LV duty always follows its target with the configured slew limit. inst->d_lv = approach_target(inst->d_lv, target_lv, LV_SLEW, dt); previous_stage = inst->stage; // Start the HV delay when LV first reaches its target. if(inst->stage == 0 && inst->d_lv == target_lv) { inst->stage = 1; inst->t_hv = t + HV_DELAY; } if(inst->stage == 1 && t >= inst->t_hv) inst->stage = 2; // Original 1B/2B remain active in all later stages. if(inst->stage >= 2) { // If activation occurs inside this timestep, count only the active part. if(previous_stage >= 2) dt_hv = dt; else { dt_hv = t - inst->t_hv; if(dt_hv < 0.0) dt_hv = 0.0; } inst->d_hv = approach_target(inst->d_hv, target_hv, HV_SLEW, dt_hv); } if(START_45_AFTER_AB) { // Both original A and B must have reached their current targets before // the configurable 4A/5A delay starts. Later target changes do not reset it. if(inst->stage == 2 && inst->d_lv == target_lv && inst->d_hv == target_hv) { inst->stage = 3; inst->t_45a = t + PWM4A_DELAY; } if(inst->stage == 3 && t >= inst->t_45a) inst->stage = 4; // Independent 4A/5A soft-start. It starts again from LV_D_START rather // than copying the already completed original LV duty. if(inst->stage >= 4) { if(previous_stage >= 4) dt_45 = dt; else { dt_45 = t - inst->t_45a; if(dt_45 < 0.0) dt_45 = 0.0; } inst->d4_lv = approach_target(inst->d4_lv, target_lv, LV_SLEW, dt_45); if(inst->stage == 4 && inst->d4_lv == target_lv) inst->stage = 5; } // 4B/5B begin only after 4A/5A reach target, with no extra delay. if(inst->stage >= 5) { dt_45 = (previous_stage >= 5) ? dt : 0.0; inst->d4_hv = approach_target(inst->d4_hv, target_hv, HV_SLEW, dt_45); } } // Integrate phase so frequency changes do not cause a phase reset. if(frequency > 0.0 && dt > 0.0) { inst->phase += frequency * dt; inst->phase -= floor(inst->phase); } inst->t_last = t; // FOUT: 0~1 corresponds to 10~100 kHz; frequencies below 10 kHz give 0. FOUT = clamp01((frequency - 10000.0) / 90000.0); DOUT_A = 0.0; DOUT_B = 0.0; PWM1_A = false; PWM1_B = false; PWM2_A = false; PWM2_B = false; PWM4_A = false; PWM5_A = false; PWM4_B = false; PWM5_B = false; inst->frequency = frequency; if(frequency <= 0.0) { inst->max_step = 1e-6; return; } h = DEADTIME * frequency; // Deadtime normalized to the switching period dual_center_pwm(inst->phase, inst->d_lv, h, &PWM1_A, &PWM2_A, &actual_duty_a); DOUT_A = actual_duty_a; phase_shifted = inst->phase + PHASE_SHIFT; phase_shifted -= floor(phase_shifted); // Original high-voltage pair remains on throughout stages 2~5. if(inst->stage >= 2) { dual_center_pwm(inst->phase, inst->d_hv, h, &PWM1_B, &PWM2_B, &actual_duty_b); DOUT_B = actual_duty_b; } if(!START_45_AFTER_AB) { // Synchronous mode preserves the original mirrored behavior. dual_center_pwm(phase_shifted, inst->d_lv, h, &PWM4_A, &PWM5_A, &actual_duty_dummy); if(inst->stage >= 2) dual_center_pwm(phase_shifted, inst->d_hv, h, &PWM4_B, &PWM5_B, &actual_duty_dummy); } else { // Sequential mode uses independent duties and keeps later outputs off // until their own startup stage is reached. if(inst->stage >= 4) dual_center_pwm(phase_shifted, inst->d4_lv, h, &PWM4_A, &PWM5_A, &actual_duty_dummy); if(inst->stage >= 5) dual_center_pwm(phase_shifted, inst->d4_hv, h, &PWM4_B, &PWM5_B, &actual_duty_dummy); } schedule_next_step(inst, t, target_lv, target_hv); } // This predictable PWM has known future events, so use MaxExtStepSize() for // coarse event-to-event travel instead of imposing a fixed tiny global step. extern "C" __declspec(dllexport) double MaxExtStepSize(struct sLLCPWM *inst) { if(!inst || inst->max_step <= 0.0) return 1e-6; return inst->max_step; } extern "C" __declspec(dllexport) void Trunc( struct sLLCPWM *inst, double t, union uData *data, double *timestep) { // All PWM and soft-start events are predictable and already scheduled by // MaxExtStepSize(). Do not force 2 ns retries around every switching edge. (void)inst; (void)t; (void)data; (void)timestep; } extern "C" __declspec(dllexport) void Destroy(struct sLLCPWM *inst) { free(inst); }