- A fork of Damien's program.
Dependencies: WebSocketClient WiflyInterface mbed messages
Fork of BatteryModelTester by
headers/pwm.h@10:e8b66718a103, 2016-11-29 (annotated)
- Committer:
- defrost
- Date:
- Tue Nov 29 15:02:01 2016 +0000
- Revision:
- 10:e8b66718a103
- Works
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
defrost | 10:e8b66718a103 | 1 | // ************ |
defrost | 10:e8b66718a103 | 2 | // * iQ_PWM.h * |
defrost | 10:e8b66718a103 | 3 | // ************ |
defrost | 10:e8b66718a103 | 4 | // |
defrost | 10:e8b66718a103 | 5 | // Created: 2015/03/19 |
defrost | 10:e8b66718a103 | 6 | // By: Damien Frost |
defrost | 10:e8b66718a103 | 7 | // |
defrost | 10:e8b66718a103 | 8 | // Description: |
defrost | 10:e8b66718a103 | 9 | // This file provides all of the functions used to configure the |
defrost | 10:e8b66718a103 | 10 | // high speed PWM module of the STM32F401RE microcontroller. |
defrost | 10:e8b66718a103 | 11 | |
defrost | 10:e8b66718a103 | 12 | #ifndef PWWM_H |
defrost | 10:e8b66718a103 | 13 | #define PWWM_H |
defrost | 10:e8b66718a103 | 14 | |
defrost | 10:e8b66718a103 | 15 | #define SETUDIS TIM1->CR1 |= TIM_CR1_UDIS |
defrost | 10:e8b66718a103 | 16 | #define CLEARUDIS TIM1->CR1 &= ~TIM_CR1_UDIS |
defrost | 10:e8b66718a103 | 17 | #define PWMHIGHPRIORITY 0 |
defrost | 10:e8b66718a103 | 18 | #define PWMLOWPRIORITY 1 |
defrost | 10:e8b66718a103 | 19 | |
defrost | 10:e8b66718a103 | 20 | #define PS_RESET 0 |
defrost | 10:e8b66718a103 | 21 | #define PS_SHIFT 1 |
defrost | 10:e8b66718a103 | 22 | |
defrost | 10:e8b66718a103 | 23 | #define PWMST_SETUPCONV 0 |
defrost | 10:e8b66718a103 | 24 | #define PWMST_STARTCONV 1 |
defrost | 10:e8b66718a103 | 25 | #define PWMST_RUNSCSKDC 2 |
defrost | 10:e8b66718a103 | 26 | #define PWMST_CALCNEWPH 3 |
defrost | 10:e8b66718a103 | 27 | #define PWMST_SETNEWPH 4 |
defrost | 10:e8b66718a103 | 28 | #define PWMST_SHIFT 5 |
defrost | 10:e8b66718a103 | 29 | #define PWMST_SAMPLECHTROUGH 6 |
defrost | 10:e8b66718a103 | 30 | #define PWMST_WAITSAMPLETROUGH 7 |
defrost | 10:e8b66718a103 | 31 | #define PWMST_WAIT 8 |
defrost | 10:e8b66718a103 | 32 | #define PWMST_SAMPLECHHILL 9 |
defrost | 10:e8b66718a103 | 33 | #define PWMST_WAITSAMPLEHILL 10 |
defrost | 10:e8b66718a103 | 34 | #define PWMST_SKIPONECYCLE 11 |
defrost | 10:e8b66718a103 | 35 | #define PWMST_MAXST 12 |
defrost | 10:e8b66718a103 | 36 | |
defrost | 10:e8b66718a103 | 37 | #define PWMSTEP_US (10.0f/839.0f) |
defrost | 10:e8b66718a103 | 38 | #define TS (PWMPER_US/1000000) |
defrost | 10:e8b66718a103 | 39 | #define PWMARRMAX (65535) |
defrost | 10:e8b66718a103 | 40 | |
defrost | 10:e8b66718a103 | 41 | |
defrost | 10:e8b66718a103 | 42 | |
defrost | 10:e8b66718a103 | 43 | // Global variables: |
defrost | 10:e8b66718a103 | 44 | extern float CurrentPhaseShift; |
defrost | 10:e8b66718a103 | 45 | extern unsigned int OneCycleSkipExitState; |
defrost | 10:e8b66718a103 | 46 | extern void (*const pwmsm_StateFunction [PWMST_MAXST]) (void); |
defrost | 10:e8b66718a103 | 47 | |
defrost | 10:e8b66718a103 | 48 | // Function prototypes: |
defrost | 10:e8b66718a103 | 49 | |
defrost | 10:e8b66718a103 | 50 | // Configures the PWM for use, using an initial duty cycle of 'duty' |
defrost | 10:e8b66718a103 | 51 | void ConfigurePWM(float duty_us, float period_us); |
defrost | 10:e8b66718a103 | 52 | |
defrost | 10:e8b66718a103 | 53 | // PWM ISR, used fo rdebugging purposes: |
defrost | 10:e8b66718a103 | 54 | void TIM1_CC_IRQHandler(void); |
defrost | 10:e8b66718a103 | 55 | |
defrost | 10:e8b66718a103 | 56 | // Configures the Dead time of the PWM, using an initial dead time of 'dt': |
defrost | 10:e8b66718a103 | 57 | void ConfigureDeadTime(float dt_ns); |
defrost | 10:e8b66718a103 | 58 | |
defrost | 10:e8b66718a103 | 59 | // Set duty by specifying the duty cycle in us: |
defrost | 10:e8b66718a103 | 60 | float SetDuty_us(float duty_us); |
defrost | 10:e8b66718a103 | 61 | |
defrost | 10:e8b66718a103 | 62 | // Set a new duty cycle by specifying it as a number between 0 and 1: |
defrost | 10:e8b66718a103 | 63 | float SetDuty(float duty); |
defrost | 10:e8b66718a103 | 64 | |
defrost | 10:e8b66718a103 | 65 | // This function checks to make sure the duty cycle is within the min and max constraints, it is called by the SetDuty functions. |
defrost | 10:e8b66718a103 | 66 | float CheckMinMaxDuty(float duty); |
defrost | 10:e8b66718a103 | 67 | |
defrost | 10:e8b66718a103 | 68 | // Turn on the the PWM signal: |
defrost | 10:e8b66718a103 | 69 | void TurnOnPWM(bool trueForOn); |
defrost | 10:e8b66718a103 | 70 | |
defrost | 10:e8b66718a103 | 71 | // Apply a phase shift: |
defrost | 10:e8b66718a103 | 72 | int PhaseShift(unsigned int cmd, float phase); |
defrost | 10:e8b66718a103 | 73 | |
defrost | 10:e8b66718a103 | 74 | // Set a new PWM Period: |
defrost | 10:e8b66718a103 | 75 | void SetPWMPeriodAndDuty(int pwmper); |
defrost | 10:e8b66718a103 | 76 | void SetPWMPeriodAndDuty_us(float period); |
defrost | 10:e8b66718a103 | 77 | |
defrost | 10:e8b66718a103 | 78 | // Set the time at which the PWM isr fires: |
defrost | 10:e8b66718a103 | 79 | void SetISRTime(unsigned int isrtime); |
defrost | 10:e8b66718a103 | 80 | |
defrost | 10:e8b66718a103 | 81 | // PWM State Machine functions: |
defrost | 10:e8b66718a103 | 82 | void pwmsm_SetupConv(void); // PWMST_SETUPCONV 0 |
defrost | 10:e8b66718a103 | 83 | void pwmsm_StartConv(void); // PWMST_STARTCONV 1 |
defrost | 10:e8b66718a103 | 84 | void pwmsm_RunScskdc(void); // PWMST_RUNSCSKDC 2 |
defrost | 10:e8b66718a103 | 85 | void pwmsm_CalcNewPh(void); // PWMST_CALCNEWPH 3 |
defrost | 10:e8b66718a103 | 86 | void pwmsm_SetNewPh(void); // PWMST_SETNEWPH 4 |
defrost | 10:e8b66718a103 | 87 | void pwmsm_Shift(void); // PWMST_SHIFT 5 |
defrost | 10:e8b66718a103 | 88 | void pwmsm_SampleChTrough(void); // PWMST_SAMPLECHTROUGH 6 |
defrost | 10:e8b66718a103 | 89 | void pwmsm_WaitSampleTrough(void); // PWMST_WAITSAMPLETROUGH 7 |
defrost | 10:e8b66718a103 | 90 | void pwmsm_Wait(void); // PWMST_WAIT 8 |
defrost | 10:e8b66718a103 | 91 | void pwmsm_SampleChHill(void); // PWMST_SAMPLECHHILL 9 |
defrost | 10:e8b66718a103 | 92 | void pwmsm_WaitSampleHill(void); // PWMST_WAITSAMPLEHILL 10 |
defrost | 10:e8b66718a103 | 93 | void pwmsm_SkipOneCycle(void); // PWMST_SKIPONECYCLE 11 |
defrost | 10:e8b66718a103 | 94 | void SetupOneCycleSkip(unsigned int nextState); |
defrost | 10:e8b66718a103 | 95 | |
defrost | 10:e8b66718a103 | 96 | #endif /* PWWM_H */ |