rusefi-1/firmware/hw_layer/pwm_generator.cpp

65 lines
2.1 KiB
C++
Raw Normal View History

2014-08-29 07:52:33 -07:00
/**
* @file pwm_generator.cpp
* @brief software PWM generator
*
* Software PWM implementation. Considering how low all frequencies are, we can totally afford a couple of float multiplications.
* By generating PWM programmatically we are saving the timers for better purposes. This implementation also supports generating
* synchronized waves as needed for example to emulate dual Hall-effect crankshaft position sensors.
*
*
* @date May 28, 2013
* @author Andrey Belomutskiy, (c) 2012-2014
*
*/
#include "pwm_generator.h"
#include "pin_repository.h"
#include "datalogging.h"
static Logging logger;
/**
* This method controls the actual hardware pins
2014-11-17 20:03:43 -08:00
*
* This method takes ~350 ticks.
2014-08-29 07:52:33 -07:00
*/
void applyPinState(PwmConfig *state, int stateIndex) {
2014-11-17 17:03:09 -08:00
efiAssertVoid(stateIndex < PWM_PHASE_MAX_COUNT, "invalid stateIndex");
2014-08-29 07:52:33 -07:00
efiAssertVoid(state->multiWave.waveCount <= PWM_PHASE_MAX_WAVE_PER_PWM, "invalid waveCount");
for (int waveIndex = 0; waveIndex < state->multiWave.waveCount; waveIndex++) {
io_pin_e ioPin = state->outputPins[waveIndex];
int value = state->multiWave.waves[waveIndex].pinStates[stateIndex];
setOutputPinValue(ioPin, value);
}
}
2014-11-30 12:03:37 -08:00
void startSimplePwm(PwmConfig *state, const char *msg, io_pin_e ioPin, float frequency, float dutyCycle, pwm_gen_callback *stateChangeCallback) {
2014-08-29 07:52:33 -07:00
efiAssertVoid(dutyCycle >= 0 && dutyCycle <= 1, "dutyCycle");
float switchTimes[] = { dutyCycle, 1 };
int pinStates0[] = { 0, 1 };
int *pinStates[1] = { pinStates0 };
state->outputPins[0] = ioPin;
2014-09-12 21:02:43 -07:00
state->periodNt = US2NT(frequency2periodUs(frequency));
2014-11-30 12:03:37 -08:00
weComplexInit(msg, state, 2, switchTimes, 1, pinStates, NULL, stateChangeCallback);
2014-08-29 07:52:33 -07:00
}
2014-11-17 20:03:43 -08:00
void startSimplePwmExt(PwmConfig *state, const char *msg, brain_pin_e brainPin, io_pin_e ioPin, float frequency,
2014-11-30 12:03:37 -08:00
float dutyCycle, pwm_gen_callback *stateChangeCallback) {
2014-08-29 07:52:33 -07:00
GPIO_TypeDef * port = getHwPort(brainPin);
int pin = getHwPin(brainPin);
outputPinRegister(msg, ioPin, port, pin);
2014-11-30 12:03:37 -08:00
startSimplePwm(state, msg, ioPin, frequency, dutyCycle, stateChangeCallback);
2014-08-29 07:52:33 -07:00
}
void initPwmGenerator(void) {
initLogging(&logger, "PWM gen");
}