From 447b8c96a24fa4823e0e658a83ed66933e8c74a8 Mon Sep 17 00:00:00 2001 From: Gerardo Marx Date: Wed, 28 Oct 2026 08:12:27 -0600 Subject: [PATCH] updating readme for epwm --- Readme.md | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 109 insertions(+), 4 deletions(-) diff --git a/Readme.md b/Readme.md index 3c31395..20fa7a6 100644 --- a/Readme.md +++ b/Readme.md @@ -120,25 +120,130 @@ void main(void) The following sections progressively fill in these steps. Register names are those used in the **provided source**. -**Note: we are in video part 2** +**Note: we are in video ePWM part 2** + +# 5 CPU1 code + +- `CLK_CFG_REGS` Registers (section 3.17.11)-> PERCLKDIVSEL -> F2837xD_sysctrl.h +- `DEV_CFG_REGS` (section 3.17.10), same file +- `CPU_SYS_REGS` (section 3.17.12) -> PCLKCR2 register has the pwm bits -> F2837xD_sysctrl.h -> search for the struct `CPU_SYS_REGS ` +- `TBCLKSYNC` bit +- `EPWM_REGS` Registers (section 15.15.2) + +```c +#include "F28x_Project.h" +#include "driverlib.h" +#include "device.h" + +__interrupt void epwm1_isr(void) { /* TODO */ } +__interrupt void epwm1_trip_isr(void) { /* TODO */ } +__interrupt void epwm2_trip_isr(void) { /* TODO */ } + +void main(void) +{ + // 1. Initialize the system and interrupts. + InitSysCtrl(); + EALLOW; + ClkCfgRegs.PERCLKDIVSEL.bit.EPWMCLKDIV = 1; + EDIS; + + // 2. Select ePWM clocks and assign CPU ownership. + EALLOW; + DevCfgRegs.CPUSEL0.bit.EPWM1 = 0; //cpu0 + DevCfgRegs.CPUSEL0.bit.EPWM2 = 0; + DevCfgRegs.CPUSEL0.bit.EPWM4 = 1; //cpu1 + DevCfgRegs.CPUSEL0.bit.EPWM5 = 1; + EDIS; + + // 3. Enable ePWM1 and ePWM2 clocks. + EALLOW; + CpuSysRegs.PCLKCR2.bit.EPWM1 = 1; + CpuSysRegs.PCLKCR2.bit.EPWM2 = 1; + EDIS; + + // 4. Stop time-base synchronization. + EALLOW; + CpuSysRegs.PCLKCR0.bit.TBCLKSYNC = 1; //stop the timer + EDIS; + + // 5. Configure TB and CC. + // Configure the eWM modules 1 and 2 + // Time base sub-module + // 100 MHz. TBCLK = 100 / (2 * 1) = 50 Mhz + EPwm1Regs.TBCTL.bit.FREE_SOFT = 2; // 2 or 3 + EPwm1Regs.TBCTL.bit.CLKDIV = 0; I + EPwm1Regs.TBCTL.bit.HSPCLKDIV = 1; + EPwm1Regs.TBCTL.bit.PRDLD = 0; + EPwm1Regs.TBCTL.bit.CTRMODE = 0; + + EPwm2Regs.TBCTL.bit.FREE_SOFT = 2; // 2 or 3 + EPwm2Regs.TBCTL.bit.CLKDIV = 0; I + EPwm2Regs.TBCTL.bit.HSPCLKDIV = 1; + EPwm2Regs.TBCTL.bit.PRDLD = 0; + EPwm2Regs.TBCTL.bit.CTRMODE = 2; // up-down count mode + + //Period registers + EPwm1Regs. TBPRD = 5000 - 1; // 10 kHz sawtooth + EPwm2Regs. TBPRD = 5000; // 5 kHz triangular + + // Counter-compare sub-module + EPwm1Regs.CMPCTL.bit.SHDWAMODE = 0; + EPwm1Regs.CMPCTL.bit.LOADAMODE = 0; + + EPwm1Regs.CMPA.bit.CMPA = 1999; + + EPwm2Regs.CMPCTL.bit.SHDWAMODE = 0; + EPwm2Regs.CMPCTL.bit.LOADAMODE = 0; + + EPwm2Regs. CMPA.bit. CMPA = (uint16_t) ( ((-0.3+1)/2.0) * 5000 ); + + // 6. Configure AQ and dead band. + + // Action qualifier sub-module + // ePWM1 - A output to be fed to the upper device of a leg + // B output to be fed to the lower device of a leg + // TBCTR = 0, A will be high, B will be low (ZRO - AQCTLA/B) + // TBCTR = CMPA. A will be low and B will be high (CAU - AQCTLA/B) + EPwm1Regs. AQCTLA.bit.ZRO = 2; + EPwm1Regs .AQCTLB.bit.ZRO = 1; + EPwm1Regs .AQCTLA.bit.CAU = 1; + EPwm1Regs.AQCTLB.bit.CAU = 2; + + // ePWM2 - A output to be fed to the upper device of a leg + // B output to be fed to the lower device of a leg + // TBCTR = CMPA first half, A will be high, B will be low (CAU - AQCTLA/B) + // TBCTR = CMPA second half. A will be low and B will be high (CAD - AQCTLA/B) + + EPwm2Regs. AQCTLA.bit.ZROI = 2; + EPwm2Regs .AQCTLB.bit.ZRO = 1; + EPwm2Regs. AQCTLA.bit.CAU = 1; + EPwm2Regs. AQCTLB.bit.CAU = 2; + // 7. Configure event triggers and trip zones. + // 8. Register and enable interrupts. + // 9. Configure PWM GPIOs, diagnostic pins, and X-BAR. + // 10. Confirm fault conditions and synchronize startup. + for (;;) {} +} + +``` ## 5. CPU1: system initialization and peripheral ownership The provided source initializes the system and interrupt tables: ```c -InitSysCtrl(); +InitSysCtrl(); // <--- InitPieCtrl(); IER = 0x0000; IFR = 0x0000; InitPieVectTable(); ``` -The CPU1 program sets the ePWM module input clock divisor and allocates the peripherals: +The CPU1 program sets the ePWM module input clock divisor and allocates the peripherals —CLK_CFG_REGS Registers -> PERCLKDIVSEL -> F2837xD_sysctrl.h—: ```c EALLOW; -ClkCfgRegs.PERCLKDIVSEL.bit.EPWMCLKDIV = 1; +ClkCfgRegs.PERCLKDIVSEL.bit.EPWMCLKDIV = 1; // <--- DevCfgRegs.CPUSEL0.bit.EPWM1 = 0; DevCfgRegs.CPUSEL0.bit.EPWM2 = 0; DevCfgRegs.CPUSEL0.bit.EPWM4 = 1;