Exemplo n.º 1
0
static msg_t ivThread(int param) {
	(void) param;
	chRegSetThreadName("IdleValve");

	while (true) {
		chThdSleepMilliseconds(boardConfiguration->idleThreadPeriod);

		// this value is not used yet
		if (boardConfiguration->clutchDownPin != GPIO_UNASSIGNED) {
			engine->clutchDownState = palReadPad(getHwPort(boardConfiguration->clutchDownPin),
					getHwPin(boardConfiguration->clutchDownPin));
		}
		if (boardConfiguration->clutchUpPin != GPIO_UNASSIGNED) {
			engine->clutchUpState = palReadPad(getHwPort(boardConfiguration->clutchUpPin),
					getHwPin(boardConfiguration->clutchUpPin));
		}

		finishIdleTestIfNeeded();
		undoIdleBlipIfNeeded();

		if (engineConfiguration->idleMode == IM_MANUAL) {
			// let's re-apply CLT correction
			manualIdleController(boardConfiguration->manIdlePosition);
		} else {
			autoIdle();
		}

	}
#if defined __GNUC__
	return -1;
#endif
}
Exemplo n.º 2
0
void StepperMotor::initialize(brain_pin_e stepPin, brain_pin_e directionPin, pin_output_mode_e directionPinMode,
		float reactionTime, int totalSteps, brain_pin_e enablePin, Logging *sharedLogger) {
	this->reactionTime = maxF(1, reactionTime);
	this->totalSteps = maxI(3, totalSteps);
	
	logger = sharedLogger;

	if (stepPin == GPIO_UNASSIGNED || directionPin == GPIO_UNASSIGNED) {
		return;
	}

	stepPort = getHwPort("step", stepPin);
	this->stepPin = getHwPin("step", stepPin);

	this->directionPinMode = directionPinMode;
	this->directionPin.initPin("stepper dir", directionPin, &this->directionPinMode);

	enablePort = getHwPort("enable", enablePin);
	this->enablePin = getHwPin("enable", enablePin);

	efiSetPadMode("stepper step", stepPin, PAL_MODE_OUTPUT_PUSHPULL);
	efiSetPadMode("stepper enable", enablePin, PAL_MODE_OUTPUT_PUSHPULL);
	palWritePad(this->enablePort, enablePin, true); // disable stepper

	// All pins must be 0 for correct hardware startup (e.g. stepper auto-disabling circuit etc.).
	palWritePad(this->stepPort, this->stepPin, false);
	this->directionPin.setValue(false);
	this->currentDirection = false;

	chThdCreateStatic(stThreadStack, sizeof(stThreadStack), NORMALPRIO, (tfunc_t) stThread, this);
}
Exemplo n.º 3
0
//-----------------------------------------------------------------------------
void lcd_HD44780_write_data(uint8_t data) {
	palSetPad(getHwPort(boardConfiguration->HD44780_rs), getHwPin(boardConfiguration->HD44780_rs));

	lcd_HD44780_write(data);
	lcd_HD44780_write(data << 4);
	currentColumn++;

	palClearPad(getHwPort(boardConfiguration->HD44780_rs), getHwPin(boardConfiguration->HD44780_rs));
}
Exemplo n.º 4
0
static void initWave(const char *name, int index) {
	brain_pin_e brainPin = boardConfiguration->logicAnalyzerPins[index];

	ioportid_t port = getHwPort(brainPin);
	ioportmask_t pin = getHwPin(brainPin);
	ICUDriver *driver = getInputCaptureDriver(brainPin);

	bool mode = boardConfiguration->logicAnalyzerMode[index];

	waveReaderCount++;
	efiAssertVoid(index < MAX_ICU_COUNT, "too many ICUs");
	WaveReader *reader = &readers[index];
	WaveReaderHw *hw = &reader->hw;

	reader->name = name;

	registerCallback(&hw->widthListeners, (IntListener) waAnaWidthCallback, (void*) reader);

	registerCallback(&hw->periodListeners, (IntListener) waIcuPeriodCallback, (void*) reader);

	initWaveAnalyzerDriver(hw, driver, port, pin);

	print("wave%d input on %s%d\r\n", index, portname(reader->hw.port), reader->hw.pin);
	setWaveReaderMode(hw, mode);
}
Exemplo n.º 5
0
static msg_t ivThread(int param) {
	chRegSetThreadName("IdleValve");

	int currentIdleValve = -1;
	while (TRUE) {
		chThdSleepMilliseconds(100);

		// this value is not used yet
		idleSwitchState = palReadPad(getHwPort(boardConfiguration->idleSwitchPin), getHwPin(boardConfiguration->idleSwitchPin));

		if (!isIdleControlActive)
			continue;

		int nowSec = chTimeNowSeconds();

		int newValue = getIdle(&idle, getRpm(), nowSec);

		if (currentIdleValve != newValue) {
			currentIdleValve = newValue;

			setIdleValvePwm(newValue);
		}
	}
#if defined __GNUC__
	return -1;
#endif
}
Exemplo n.º 6
0
void StepperMotor::initialize(brain_pin_e stepPin, brain_pin_e directionPin) {
	if (stepPin == GPIO_UNASSIGNED || directionPin == GPIO_UNASSIGNED) {
		return;
	}

	stepPort = getHwPort(stepPin);
	this->stepPin = getHwPin(stepPin);

	directionPort = getHwPort(directionPin);
	this->directionPin = getHwPin(directionPin);

	mySetPadMode2("st step", stepPin, PAL_MODE_OUTPUT_PUSHPULL);
	mySetPadMode2("st dir", directionPin, PAL_MODE_OUTPUT_PUSHPULL);

	chThdCreateStatic(stThreadStack, sizeof(stThreadStack), NORMALPRIO, (tfunc_t) stThread, this);
}
Exemplo n.º 7
0
void initSpiCs(SPIConfig *spiConfig, brain_pin_e csPin) {
	spiConfig->end_cb = NULL;
	ioportid_t port = getHwPort(csPin);
	ioportmask_t pin = getHwPin(csPin);
	spiConfig->ssport = port;
	spiConfig->sspad = pin;
	mySetPadMode("chip select", port, pin, PAL_STM32_MODE_OUTPUT);
}
Exemplo n.º 8
0
void outputPinRegisterExt2(const char *msg, OutputPin *output, brain_pin_e brainPin, pin_output_mode_e *outputMode) {
	if (brainPin == GPIO_UNASSIGNED)
		return;
	GPIO_TypeDef *hwPort = getHwPort(brainPin);
	int hwPin = getHwPin(brainPin);

	outputPinRegisterExt(msg, output, hwPort, hwPin, outputMode);
}
Exemplo n.º 9
0
bool efiReadPin(brain_pin_e pin) {
	if (brain_pin_is_onchip(pin))
		return palReadPad(getHwPort("readPin", pin), getHwPin("readPin", pin));
	#if (BOARD_EXT_GPIOCHIPS > 0)
		else if (brain_pin_is_ext(pin))
			return (gpiochips_readPad(pin) > 0);
	#endif

	/* incorrect pin */
	return false;
}
Exemplo n.º 10
0
void lcd_HD44780_init(Logging *sharedLogger) {
	logger = sharedLogger;

	addConsoleAction("lcdinfo", lcdInfo);

	if (engineConfiguration->displayMode > DM_HD44780_OVER_PCF8574) {
		firmwareError("Unexpected displayMode %d", engineConfiguration->displayMode);
		return;
	}

	printMsg(logger, "lcd_HD44780_init %d", engineConfiguration->displayMode);

	if (engineConfiguration->displayMode == DM_HD44780) {
		// initialize hardware lines
		mySetPadMode2("lcd RS", boardConfiguration->HD44780_rs, PAL_MODE_OUTPUT_PUSHPULL);
		mySetPadMode2("lcd E", boardConfiguration->HD44780_e, PAL_MODE_OUTPUT_PUSHPULL);
		mySetPadMode2("lcd DB4", boardConfiguration->HD44780_db4, PAL_MODE_OUTPUT_PUSHPULL);
		mySetPadMode2("lcd DB6", boardConfiguration->HD44780_db5, PAL_MODE_OUTPUT_PUSHPULL);
		mySetPadMode2("lcd DB7", boardConfiguration->HD44780_db6, PAL_MODE_OUTPUT_PUSHPULL);
		mySetPadMode2("lcd DB8", boardConfiguration->HD44780_db7, PAL_MODE_OUTPUT_PUSHPULL);
		// and zero values
		palWritePad(getHwPort(boardConfiguration->HD44780_rs), getHwPin(boardConfiguration->HD44780_rs), 0);
		palWritePad(getHwPort(boardConfiguration->HD44780_e), getHwPin(boardConfiguration->HD44780_e), 0);
		palWritePad(getHwPort(boardConfiguration->HD44780_db4), getHwPin(boardConfiguration->HD44780_db4), 0);
		palWritePad(getHwPort(boardConfiguration->HD44780_db5), getHwPin(boardConfiguration->HD44780_db5), 0);
		palWritePad(getHwPort(boardConfiguration->HD44780_db6), getHwPin(boardConfiguration->HD44780_db6), 0);
		palWritePad(getHwPort(boardConfiguration->HD44780_db7), getHwPin(boardConfiguration->HD44780_db7), 0);
	}

	chThdSleepMilliseconds(20); // LCD needs some time to wake up
	lcd_HD44780_write(LCD_HD44780_RESET); // reset 1x
	chThdSleepMilliseconds(1);
	lcd_HD44780_write(LCD_HD44780_RESET); // reset 2x
	lcd_HD44780_write(LCD_HD44780_RESET); // reset 3x

	lcd_HD44780_write(LCD_HD44780_4_BIT_BUS);	// 4 bit, 2 line
	chThdSleepMicroseconds(40);

	lcd_HD44780_write(LCD_HD44780_4_BIT_BUS);	// 4 bit, 2 line
	lcd_HD44780_write(0x80);
	chThdSleepMicroseconds(40);

	lcd_HD44780_write_command(0x08);	// display and cursor control
	chThdSleepMicroseconds(40);

	lcd_HD44780_write_command(LCD_HD44780_DISPLAY_CLEAR);
	chThdSleepMilliseconds(2);

	lcd_HD44780_write_command(LCD_HD44780_SHIFT_CURSOR_RIGHT);
	chThdSleepMilliseconds(2);

	lcd_HD44780_write_command(LCD_HD44780_DISPLAY_ON);

	lcd_HD44780_set_position(0, 0);
	printMsg(logger, "lcd_HD44780_init() done");
}
Exemplo n.º 11
0
static msg_t ivThread(int param) {
	(void) param;
	chRegSetThreadName("board test blinking");

	int value = 0;

	while (TRUE) {
		chThdSleepMilliseconds(250);
		value = 1 - value;
		GPIO_TypeDef *hwPort = getHwPort(currentPin);
		uint32_t hwPin = getHwPin(currentPin);
		palWritePad(hwPort, hwPin, value);
	}
#if defined __GNUC__
	return 0;
#endif        
}
Exemplo n.º 12
0
void initHip9011(Logging *sharedLogger) {
	driver = getSpiDevice(spiDevice);
	logger = sharedLogger;
	addConsoleAction("hipinfo", showHipInfo);
	if (!boardConfiguration->isHip9011Enabled)
		return;


	currentAngleWindowWidth =
	engineConfiguration->knockDetectionWindowEnd - engineConfiguration->knockDetectionWindowStart;

	prepareHip9011RpmLookup(currentAngleWindowWidth);

	// todo: configurable
//	driver = getSpiDevice(boardConfiguration->hip9011SpiDevice);

	spicfg.ssport = getHwPort(boardConfiguration->hip9011CsPin);
	spicfg.sspad = getHwPin(boardConfiguration->hip9011CsPin);

	outputPinRegisterExt2("hip int/hold", &intHold, boardConfiguration->hip9011IntHoldPin,
			&boardConfiguration->hip9011IntHoldPinMode);
	outputPinRegisterExt2("hip CS", &hipCs, boardConfiguration->hip9011CsPin,
			&boardConfiguration->hip9011CsPinMode);

	scheduleMsg(logger, "Starting HIP9011/TPIC8101 driver");
	spiStart(driver, &spicfg);

	currentBandIndex = getBandIndex();

	/**
	 * this engine cycle callback would be scheduling actual integration start and end callbacks
	 */
	addTriggerEventListener(&intHoldCallback, "DD int/hold", engine);

	// MISO PB14
//	palSetPadMode(GPIOB, 14, PAL_MODE_ALTERNATE(EFI_SPI2_AF) | PAL_STM32_PUDR_PULLUP);
	// MOSI PB15
//	palSetPadMode(GPIOB, 15, PAL_MODE_ALTERNATE(EFI_SPI2_AF) | PAL_STM32_OTYPE_OPENDRAIN);

	addConsoleActionF("set_gain", setGain);
	addConsoleActionF("set_band", setBand);
	addConsoleActionI("set_hip_prescalerandsdo", setPrescalerAndSDO);
    addConsoleActionF("set_knock_threshold", setKnockThresh);
    addConsoleActionI("set_max_knock_sub_deg", setMaxKnockSubDeg);
	chThdCreateStatic(hipTreadStack, sizeof(hipTreadStack), NORMALPRIO, (tfunc_t) hipThread, NULL);
}
Exemplo n.º 13
0
static void cjStartSpi(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
	globalInstance.cj125Cs.initPin("cj125 CS", CONFIGB(cj125CsPin),
			&engineConfiguration->cj125CsPinMode);
	// Idle CS pin - SPI CS is high when idle
	globalInstance.cj125Cs.setValue(true);

	cj125spicfg.cr1 += getSpiPrescaler(_150KHz, engineConfiguration->cj125SpiDevice);

	cj125spicfg.ssport = getHwPort("cj125", CONFIGB(cj125CsPin));
	cj125spicfg.sspad = getHwPin("cj125", CONFIGB(cj125CsPin));
	driver = getSpiDevice(engineConfiguration->cj125SpiDevice);
	if (driver == NULL) {
		// error already reported
		return;
	}
	scheduleMsg(logger, "cj125: Starting SPI driver");
	spiStart(driver, &cj125spicfg);
}
Exemplo n.º 14
0
void initHip9011(Logging *sharedLogger) {
	logger = sharedLogger;
	addConsoleAction("hipinfo", showHipInfo);
	if (!CONFIGB(isHip9011Enabled))
		return;


	instance.setAngleWindowWidth();

#if EFI_PROD_CODE
	driver = getSpiDevice(engineConfiguration->hip9011SpiDevice);
	if (driver == NULL) {
		// error already reported
		return;
	}

	hipSpiCfg.ssport = getHwPort("hip", CONFIGB(hip9011CsPin));
	hipSpiCfg.sspad = getHwPin("hip", CONFIGB(hip9011CsPin));
#endif /* EFI_PROD_CODE */

	startHip9001_pins();

	scheduleMsg(logger, "Starting HIP9011/TPIC8101 driver");
	spiStart(driver, &hipSpiCfg);

	instance.currentBandIndex = getBandIndex();

	/**
	 * this engine cycle callback would be scheduling actual integration start and end callbacks
	 */
	addTriggerEventListener(&intHoldCallback, "DD int/hold", engine);

	// MISO PB14
//	palSetPadMode(GPIOB, 14, PAL_MODE_ALTERNATE(EFI_SPI2_AF) | PAL_STM32_PUDR_PULLUP);
	// MOSI PB15
//	palSetPadMode(GPIOB, 15, PAL_MODE_ALTERNATE(EFI_SPI2_AF) | PAL_STM32_OTYPE_OPENDRAIN);

	addConsoleActionF("set_gain", setHipGain);
	addConsoleActionF("set_band", setHipBand);
	addConsoleActionI("set_hip_prescalerandsdo", setPrescalerAndSDO);
    addConsoleActionF("set_knock_threshold", setKnockThresh);
    addConsoleActionI("set_max_knock_sub_deg", setMaxKnockSubDeg);
	chThdCreateStatic(hipTreadStack, sizeof(hipTreadStack), NORMALPRIO, (tfunc_t)(void*) hipThread, NULL);
}
Exemplo n.º 15
0
void efiSetPadUnused(brain_pin_e brainPin)
{
	/* input with pull up, is it safe? */
	iomode_t mode = PAL_STM32_MODE_INPUT | PAL_STM32_PUPDR_PULLUP;

	if (brain_pin_is_onchip(brainPin)) {
		ioportid_t port = getHwPort("unused", brainPin);
		ioportmask_t pin = getHwPin("unused", brainPin);

		/* input with pull up, is it safe? */
		palSetPadMode(port, pin, mode);
	}
	#if (BOARD_EXT_GPIOCHIPS > 0)
		else {
			gpiochips_setPadMode(brainPin, mode);
		}
	#endif

	brain_pin_markUnused(brainPin);
}
Exemplo n.º 16
0
void OutputPin::initPin(const char *msg, brain_pin_e brainPin, pin_output_mode_e *outputMode) {
#if EFI_GPIO_HARDWARE || defined(__DOXYGEN__)
	if (brainPin == GPIO_UNASSIGNED)
		return;
	ioportid_t port = getHwPort(msg, brainPin);
	int pin = getHwPin(msg, brainPin);

	/**
	 * This method is used for digital GPIO pins only, for peripheral pins see mySetPadMode
	 */
	if (port == GPIO_NULL) {
		// that's for GRIO_NONE
		this->port = port;
		return;
	}

	assertOMode(*outputMode);
	iomode_t mode = (*outputMode == OM_DEFAULT || *outputMode == OM_INVERTED) ?
		PAL_MODE_OUTPUT_PUSHPULL : PAL_MODE_OUTPUT_OPENDRAIN;

	/**
	 * @brief Initialize the hardware output pin while also assigning it a logical name
	 */
	if (this->port != NULL && (this->port != port || this->pin != pin)) {
		/**
		 * here we check if another physical pin is already assigned to this logical output
		 */
	// todo: need to clear '&outputs' in io_pins.c
		warning(CUSTOM_OBD_PIN_CONFLICT, "outputPin [%s] already assigned to %x%d", msg, this->port, this->pin);
		engine->withError = true;
		return;
	}
	this->currentLogicValue = INITIAL_PIN_STATE;
	this->port = port;
	this->pin = pin;

	efiSetPadMode(msg, brainPin, mode);

	setDefaultPinState(outputMode);
#endif /* EFI_GPIO_HARDWARE */
}
Exemplo n.º 17
0
/**
 * This method would set an error condition if pin is already used
 */
void efiSetPadMode(const char *msg, brain_pin_e brainPin, iomode_t mode)
{
	bool wasUsed = brain_pin_markUsed(brainPin, msg);

	if (!wasUsed) {
		/*check if on-chip pin or external */
		if (brain_pin_is_onchip(brainPin)) {
			/* on-cip */
			ioportid_t port = getHwPort(msg, brainPin);
			ioportmask_t pin = getHwPin(msg, brainPin);
			/* paranoid */
			if (port == GPIO_NULL)
				return;

			palSetPadMode(port, pin, mode);
		}
		#if (BOARD_EXT_GPIOCHIPS > 0)
			else {
				gpiochips_setPadMode(brainPin, mode);
			}
		#endif
	}
}
Exemplo n.º 18
0
void startIdleThread() {
	initLogging(&logger, "Idle Valve Control");

	startSimplePwm(&idleValve, "Idle Valve",
			boardConfiguration->idleValvePin,
			0.5,
			IDLE_AIR_CONTROL_VALVE_PWM_FREQUENCY,
			IDLE_VALVE
			);

	idleInit(&idle);
	scheduleMsg(&logger, "initial idle %d", idle.value);

	chThdCreateStatic(ivThreadStack, sizeof(ivThreadStack), NORMALPRIO, (tfunc_t)ivThread, NULL);

	// this is idle switch INPUT - sometimes there is a switch on the throttle pedal
	// this switch is not used yet
	mySetPadMode("idle switch", getHwPort(boardConfiguration->idleSwitchPin), getHwPin(boardConfiguration->idleSwitchPin), PAL_MODE_INPUT);

	addConsoleActionI("set_idle_rpm", setIdleRpmAction);
	addConsoleActionI("set_idle_pwm", setIdleValvePwm);
	addConsoleActionI("set_idle_enabled", setIdleControlEnabled);
}
Exemplo n.º 19
0
//-----------------------------------------------------------------------------
static void lcd_HD44780_write(uint8_t data) {
	if (engineConfiguration->displayMode == DM_HD44780) {
		palWritePad(getHwPort(boardConfiguration->HD44780_db7), getHwPin(boardConfiguration->HD44780_db7),
				data & 0x80 ? 1 : 0);
		palWritePad(getHwPort(boardConfiguration->HD44780_db6), getHwPin(boardConfiguration->HD44780_db6),
				data & 0x40 ? 1 : 0);
		palWritePad(getHwPort(boardConfiguration->HD44780_db5), getHwPin(boardConfiguration->HD44780_db5),
				data & 0x20 ? 1 : 0);
		palWritePad(getHwPort(boardConfiguration->HD44780_db4), getHwPin(boardConfiguration->HD44780_db4),
				data & 0x10 ? 1 : 0);

		palSetPad(getHwPort(boardConfiguration->HD44780_e), getHwPin(boardConfiguration->HD44780_e)); // En high
		lcdSleep(10); // enable pulse must be >450ns
		palClearPad(getHwPort(boardConfiguration->HD44780_e), getHwPin(boardConfiguration->HD44780_e)); // En low
		lcdSleep(40); // commands need > 37us to settle
	} else {

		//	LCD D4_pin -> P4
		//	LCD D5_pin -> P5
		//	LCD D6_pin -> P6
		//	LCD D7_pin -> P7
		//	LCD Pin RS -> P0
		//	LCD Pin RW -> P1
		//	LCD Pin E  -> P2

		// todo: finish all this stuff
//				i2cAcquireBus(&I2CD1);
//
//				txbuf[0] = 4;
//				i2cMasterTransmit(&I2CD1, LCD_PORT_EXP_ADDR, txbuf, 1, NULL, 0);
//				lcdSleep(10); // enable pulse must be >450ns
//
//				txbuf[0] = 0;
//				i2cMasterTransmit(&I2CD1, LCD_PORT_EXP_ADDR, txbuf, 1, NULL, 0);
//
//				i2cReleaseBus(&I2CD1);

	}
}
Exemplo n.º 20
0
//-----------------------------------------------------------------------------
void lcd_HD44780_write_command(uint8_t data) {
	palClearPad(getHwPort(boardConfiguration->HD44780_rs), getHwPin(boardConfiguration->HD44780_rs));

	lcd_HD44780_write(data);
	lcd_HD44780_write(data << 4);
}