fome-fw/firmware/controllers/system/pwm_generator_logic.h

124 lines
2.9 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"
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);
/**
* @brief Multi-channel software PWM output configuration
*/
class PwmConfig {
public:
PwmConfig();
PwmConfig(float *switchTimes, single_wave_s *waves);
void baseConstructor();
void init(float *switchTimes, single_wave_s *waves);
void weComplexInit(const char *msg,
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);
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();
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
*/
pwm_gen_callback *stateChangeCallback;
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();
void setSimplePwmDutyCycle(float dutyCycle);
pin_state_t pinStates[2];
single_wave_s sr[1];
float _switchTimes[2];
private:
single_wave_s waveInstance;
};
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()
*/
2018-12-08 14:30:17 -08:00
void startSimplePwm(SimplePwm *state, const char *msg, 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()
*/
2018-12-08 14:30:17 -08:00
void startSimplePwmExt(SimplePwm *state, const char *msg, 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,
int waveCount, pin_state_t **pinStates);
#endif /* PWM_GENERATOR_LOGIC_H_ */