rusefi-1/firmware/controllers/system/pwm_generator_logic.h

146 lines
3.2 KiB
C
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file pwm_generator_logic.h
*
* @date Mar 2, 2014
2017-01-03 03:05:22 -08:00
* @author Andrey Belomutskiy, (c) 2012-2017
2015-07-10 06:01:56 -07:00
*/
#ifndef PWM_GENERATOR_LOGIC_H_
#define PWM_GENERATOR_LOGIC_H_
#include "global.h"
#include "EfiWave.h"
#include "scheduler.h"
#include "efiGpio.h"
2018-12-08 19:57:00 -08:00
#define NAN_FREQUENCY_SLEEP_PERIOD_MS 100
2015-07-10 06:01:56 -07:00
typedef struct {
/**
* a copy so that all phases are executed on the same period, even if another thread
* would be adjusting PWM parameters
*/
float periodNt;
/**
* Iteration counter
*/
int iteration;
/**
* Start time of current iteration
*/
efitick_t startNt;
int phaseIndex;
} pwm_config_safe_state_s;
class PwmConfig;
typedef void (pwm_cycle_callback)(PwmConfig *state);
typedef void (pwm_gen_callback)(PwmConfig *state, int stateIndex);
2018-12-08 19:57:00 -08:00
typedef enum {
PM_ZERO,
PM_NORMAL,
PM_FULL
} pwm_mode_e;
2015-07-10 06:01:56 -07:00
/**
* @brief Multi-channel software PWM output configuration
*/
class PwmConfig {
public:
PwmConfig();
2018-12-25 05:27:52 -08:00
PwmConfig(float *switchTimes, SingleWave *waves);
2015-07-10 06:01:56 -07:00
void baseConstructor();
2018-12-25 05:27:52 -08:00
void init(float *switchTimes, SingleWave *waves);
2015-07-10 06:01:56 -07:00
void weComplexInit(const char *msg,
ExecutorInterface *executor,
2015-07-10 06:01:56 -07:00
int phaseCount, float *swithcTimes, int waveCount, pin_state_t **pinStates,
2018-02-05 14:16:16 -08:00
pwm_cycle_callback *pwmCycleCallback,
2015-07-10 06:01:56 -07:00
pwm_gen_callback *callback);
ExecutorInterface *executor;
2018-12-08 19:57:00 -08:00
/**
* We need to handle zero duty cycle and 100% duty cycle in a special way
*/
pwm_mode_e mode;
2018-01-21 12:28:03 -08:00
/**
* @param use NAN frequency to pause PWM
*/
void setFrequency(float frequency);
2015-07-10 06:01:56 -07:00
void handleCycleStart();
const char *name;
2015-07-10 06:01:56 -07:00
OutputPin *outputPins[PWM_PHASE_MAX_WAVE_PER_PWM];
2018-12-08 13:59:16 -08:00
MultiWave multiWave;
2018-01-21 12:28:03 -08:00
efitimeus_t togglePwmState();
2015-07-10 06:01:56 -07:00
int dbgNestingLevel;
scheduling_s scheduling;
pwm_config_safe_state_s safe;
/**
* Number of events in the cycle
*/
int phaseCount;
/**
* this callback is invoked before each wave generation cycle
*/
2018-02-05 14:16:16 -08:00
pwm_cycle_callback *pwmCycleCallback;
2015-07-10 06:01:56 -07:00
/**
* this main callback is invoked when it's time to switch level on any of the output channels
*/
2019-02-01 19:37:21 -08:00
pwm_gen_callback *stateChangeCallback = NULL;
2018-01-21 12:34:20 -08:00
private:
/**
* float value of PWM period
* PWM generation is not happening while this value is NAN
*/
float periodNt;
2015-07-10 06:01:56 -07:00
};
class SimplePwm : public PwmConfig {
public:
SimplePwm();
SimplePwm(const char *name);
2015-07-10 06:01:56 -07:00
void setSimplePwmDutyCycle(float dutyCycle);
pin_state_t pinStates[2];
2018-12-25 05:27:52 -08:00
SingleWave sr[1];
2015-07-10 06:01:56 -07:00
float _switchTimes[2];
private:
2018-12-25 05:27:52 -08:00
SingleWave waveInstance;
2015-07-10 06:01:56 -07:00
};
2018-11-02 10:38:31 -07:00
/**
* Start a one-channel software PWM driver.
*
* This method should be called after scheduling layer is started by initSignalExecutor()
*/
void startSimplePwm(SimplePwm *state, const char *msg,
ExecutorInterface *executor,
OutputPin *output,
2018-11-02 10:38:31 -07:00
float dutyCycle, float frequency, pwm_gen_callback *stateChangeCallback);
/**
* initialize GPIO pin and start a one-channel software PWM driver.
*
* This method should be called after scheduling layer is started by initSignalExecutor()
*/
void startSimplePwmExt(SimplePwm *state,
const char *msg,
ExecutorInterface *executor,
brain_pin_e brainPin, OutputPin *output,
2018-11-02 10:38:31 -07:00
float frequency, float dutyCycle, pwm_gen_callback *stateChangeCallback);
2015-07-10 06:01:56 -07:00
void copyPwmParameters(PwmConfig *state, int phaseCount, float *switchTimes,
2019-02-02 22:04:24 -08:00
int waveCount, pin_state_t *const *pinStates);
2015-07-10 06:01:56 -07:00
#endif /* PWM_GENERATOR_LOGIC_H_ */