fome-fw/firmware/hw_layer/hardware.cpp

295 lines
6.3 KiB
C++
Raw Normal View History

2014-08-29 07:52:33 -07:00
/**
* @file hardware.cpp
* @brief Hardware package entry point
*
* @date May 27, 2013
2015-01-12 15:04:10 -08:00
* @author Andrey Belomutskiy, (c) 2012-2015
2014-08-29 07:52:33 -07:00
*/
#include "main.h"
#include "hardware.h"
#include "pin_repository.h"
#include "io_pins.h"
#include "rtc_helper.h"
#include "rfiutil.h"
#include "console_io.h"
#include "adc_inputs.h"
2014-12-24 18:03:45 -08:00
#include "stepper.h"
2014-12-28 09:03:29 -08:00
#include "vehicle_speed.h"
2014-08-29 07:52:33 -07:00
#include "trigger_input.h"
#include "eficonsole.h"
2015-01-08 12:03:45 -08:00
#include "max31855.h"
#include "can_hw.h"
#if EFI_PROD_CODE
2014-08-29 07:52:33 -07:00
#include "board_test.h"
#include "mcp3208.h"
#include "HIP9011.h"
#include "histogram.h"
#include "mmc_card.h"
#include "neo6m.h"
#include "lcd_HD44780.h"
#include "settings.h"
#include "algo.h"
2015-01-02 14:05:05 -08:00
#include "joystick.h"
2014-08-29 07:52:33 -07:00
#include "trigger_central.h"
#include "svnversion.h"
#include "engine_configuration.h"
2015-01-08 12:03:45 -08:00
#endif
#if EFI_INTERNAL_FLASH
#include "flash_main.h"
#endif /* EFI_INTERNAL_FLASH */
2014-08-29 07:52:33 -07:00
2015-01-02 12:03:28 -08:00
EXTERN_ENGINE
;
2014-11-24 09:03:09 -08:00
extern bool hasFirmwareErrorFlag;
2014-08-29 07:52:33 -07:00
2014-12-24 18:03:45 -08:00
static StepperMotor iacMotor;
2014-12-23 20:03:31 -08:00
static Mutex spiMtx;
2014-12-31 16:03:30 -08:00
int maxNesting = 0;
2014-12-23 20:03:31 -08:00
#if HAL_USE_SPI || defined(__DOXYGEN__)
2014-08-29 07:52:33 -07:00
static bool isSpiInitialized[5] = { false, false, false, false, false };
/**
* Only one consumer can use SPI bus at a given time
*/
void lockSpi(spi_device_e device) {
// todo: different locks for different SPI devices!
chMtxLock(&spiMtx);
}
void unlockSpi(void) {
chMtxUnlock();
}
2014-09-08 15:02:52 -07:00
static void initSpiModules(board_configuration_s *boardConfiguration) {
2014-08-29 07:52:33 -07:00
if (boardConfiguration->is_enabled_spi_2) {
turnOnSpi(SPI_DEVICE_2);
}
if (boardConfiguration->is_enabled_spi_3) {
turnOnSpi(SPI_DEVICE_3);
}
}
2014-12-23 20:03:31 -08:00
SPIDriver * getSpiDevice(spi_device_e spiDevice) {
if (spiDevice == SPI_NONE) {
return NULL;
}
#if STM32_SPI_USE_SPI1 || defined(__DOXYGEN__)
if (spiDevice == SPI_DEVICE_1) {
return &SPID1;
}
#endif
#if STM32_SPI_USE_SPI2 || defined(__DOXYGEN__)
if (spiDevice == SPI_DEVICE_2) {
return &SPID2;
}
#endif
#if STM32_SPI_USE_SPI3 || defined(__DOXYGEN__)
if (spiDevice == SPI_DEVICE_3) {
return &SPID3;
}
#endif
firmwareError("Unexpected SPI device: %d", spiDevice);
return NULL;
}
#endif
#if HAL_USE_I2C || defined(__DOXYGEN__)
2014-08-29 07:52:33 -07:00
static I2CConfig i2cfg = { OPMODE_I2C, 100000, STD_DUTY_CYCLE, };
void initI2Cmodule(void) {
print("Starting I2C module\r\n");
i2cInit();
i2cStart(&I2CD1, &i2cfg);
mySetPadMode("I2C clock", EFI_I2C_SCL_PORT, EFI_I2C_SCL_PIN,
PAL_MODE_ALTERNATE(EFI_I2C_AF) | PAL_STM32_OTYPE_OPENDRAIN);
mySetPadMode("I2C data", EFI_I2C_SDA_PORT, EFI_I2C_SDA_PIN,
PAL_MODE_ALTERNATE(EFI_I2C_AF) | PAL_STM32_OTYPE_OPENDRAIN);
}
//static char txbuf[1];
static void sendI2Cbyte(int addr, int data) {
// i2cAcquireBus(&I2CD1);
// txbuf[0] = data;
// i2cMasterTransmit(&I2CD1, addr, txbuf, 1, NULL, 0);
// i2cReleaseBus(&I2CD1);
}
2014-12-23 20:03:31 -08:00
#endif
2015-01-02 12:03:28 -08:00
static Logging *sharedLogger;
2015-01-08 12:03:45 -08:00
#if EFI_PROD_CODE
2015-01-02 12:03:28 -08:00
void initHardware(Logging *l, Engine *engine) {
sharedLogger = l;
2014-09-08 15:02:52 -07:00
engine_configuration_s *engineConfiguration = engine->engineConfiguration;
2014-09-08 21:02:56 -07:00
efiAssertVoid(engineConfiguration!=NULL, "engineConfiguration");
2014-09-08 15:02:52 -07:00
board_configuration_s *boardConfiguration = &engineConfiguration->bc;
2015-01-02 12:03:28 -08:00
printMsg(sharedLogger, "initHardware()");
2014-08-29 07:52:33 -07:00
// todo: enable protection. it's disabled because it takes
// 10 extra seconds to re-flash the chip
//flashProtect();
chMtxInit(&spiMtx);
#if EFI_HISTOGRAMS
/**
* histograms is a data structure for CPU monitor, it does not depend on configuration
*/
initHistogramsModule();
#endif /* EFI_HISTOGRAMS */
2014-11-02 11:04:37 -08:00
/**
* This is so early because we want to init logger
* which would be used while finding trigger synch index
* while config read
*/
2015-01-08 20:04:09 -08:00
initTriggerDecoderLogger();
2014-11-02 11:04:37 -08:00
2014-08-29 07:52:33 -07:00
/**
* We need the LED_ERROR pin even before we read configuration
*/
initPrimaryPins();
2014-09-08 21:02:56 -07:00
if (hasFirmwareError()) {
2014-08-29 07:52:33 -07:00
return;
2014-09-08 21:02:56 -07:00
}
2014-08-29 07:52:33 -07:00
2014-11-25 08:04:15 -08:00
initDataStructures(PASS_ENGINE_PARAMETER_F);
2014-08-29 07:52:33 -07:00
#if EFI_INTERNAL_FLASH
2015-01-02 12:03:28 -08:00
palSetPadMode(CONFIG_RESET_SWITCH_PORT, CONFIG_RESET_SWITCH_PIN,
PAL_MODE_INPUT_PULLUP);
2014-08-29 07:52:33 -07:00
2014-11-02 10:03:07 -08:00
initFlash(engine);
2014-08-29 07:52:33 -07:00
/**
* this call reads configuration from flash memory or sets default configuration
* if flash state does not look right.
*/
if (SHOULD_INGORE_FLASH()) {
2015-01-02 14:05:05 -08:00
engineConfiguration->engineType = DEFAULT_ENGINE_TYPE;
2015-01-02 12:03:28 -08:00
resetConfigurationExt(sharedLogger, engineConfiguration->engineType,
engine);
2014-08-29 07:52:33 -07:00
writeToFlash();
} else {
readFromFlash();
}
#else
2014-12-26 12:04:16 -08:00
engineConfiguration->engineType = DEFAULT_ENGINE_TYPE;
2014-12-23 20:03:31 -08:00
resetConfigurationExt(logger, engineConfiguration->engineType, engine);
2014-08-29 07:52:33 -07:00
#endif /* EFI_INTERNAL_FLASH */
2014-09-08 21:02:56 -07:00
if (hasFirmwareError()) {
2014-08-29 07:52:33 -07:00
return;
2014-09-08 21:02:56 -07:00
}
2014-08-29 07:52:33 -07:00
2015-01-08 20:04:09 -08:00
initTriggerDecoder();
2015-01-02 12:03:28 -08:00
mySetPadMode2("board test", boardConfiguration->boardTestModeJumperPin,
PAL_MODE_INPUT_PULLUP);
2014-08-29 07:52:33 -07:00
bool isBoardTestMode_b = GET_BOARD_TEST_MODE_VALUE();
2014-12-23 20:03:31 -08:00
#if HAL_USE_ADC || defined(__DOXYGEN__)
2014-10-22 19:03:06 -07:00
initAdcInputs(isBoardTestMode_b);
2014-12-23 20:03:31 -08:00
#endif
2014-08-29 07:52:33 -07:00
if (isBoardTestMode_b) {
2014-11-09 07:03:15 -08:00
// this method never returns
2014-08-29 07:52:33 -07:00
initBoardTest();
}
initRtc();
initOutputPins();
2014-09-17 17:02:53 -07:00
#if EFI_MAX_31855
2015-01-02 12:03:28 -08:00
initMax31855(getSpiDevice(boardConfiguration->max31855spiDevice),
boardConfiguration->max31855_cs);
2014-09-17 17:02:53 -07:00
#endif /* EFI_MAX_31855 */
2014-09-17 09:03:04 -07:00
2014-12-24 18:03:45 -08:00
// iacMotor.initialize(GPIOD_11, GPIOD_10);
2014-08-29 07:52:33 -07:00
#if EFI_CAN_SUPPORT
initCan();
#endif /* EFI_CAN_SUPPORT */
// init_adc_mcp3208(&adcState, &SPID2);
// requestAdcValue(&adcState, 0);
// todo: figure out better startup logic
2014-09-08 15:02:52 -07:00
initTriggerCentral(engine);
2014-08-29 07:52:33 -07:00
2014-12-24 10:05:36 -08:00
#if EFI_SHAFT_POSITION_INPUT || defined(__DOXYGEN__)
2014-08-29 07:52:33 -07:00
initShaftPositionInputCapture();
#endif /* EFI_SHAFT_POSITION_INPUT */
2014-12-23 20:03:31 -08:00
#if HAL_USE_SPI || defined(__DOXYGEN__)
2014-09-08 15:02:52 -07:00
initSpiModules(boardConfiguration);
2014-12-23 20:03:31 -08:00
#endif
2014-08-29 07:52:33 -07:00
2014-12-25 19:03:24 -08:00
#if EFI_HIP_9011
initHip9011();
#endif /* EFI_HIP_9011 */
2014-08-29 07:52:33 -07:00
#if EFI_FILE_LOGGING
initMmcCard();
#endif /* EFI_FILE_LOGGING */
// initFixedLeds();
// initBooleanInputs();
#if EFI_UART_GPS
initGps();
#endif
#if ADC_SNIFFER
initAdcDriver();
#endif
#if EFI_HD44780_LCD
// initI2Cmodule();
lcd_HD44780_init();
if (hasFirmwareError())
return;
lcd_HD44780_print_string(VCS_VERSION);
#endif /* EFI_HD44780_LCD */
2014-12-23 20:03:31 -08:00
#if HAL_USE_I2C || defined(__DOXYGEN__)
2014-08-29 07:52:33 -07:00
addConsoleActionII("i2c", sendI2Cbyte);
2014-12-23 20:03:31 -08:00
#endif
2014-08-29 07:52:33 -07:00
// while (true) {
// for (int addr = 0x20; addr < 0x28; addr++) {
// sendI2Cbyte(addr, 0);
// int err = i2cGetErrors(&I2CD1);
// print("I2C: err=%x from %d\r\n", err, addr);
// chThdSleepMilliseconds(5);
// sendI2Cbyte(addr, 255);
// chThdSleepMilliseconds(5);
// }
// }
2015-01-02 12:03:28 -08:00
initVehicleSpeed(sharedLogger);
2014-12-28 09:03:29 -08:00
2015-01-02 14:05:05 -08:00
initJoystick(sharedLogger);
2015-01-02 12:03:28 -08:00
2015-01-02 14:05:05 -08:00
printMsg(sharedLogger, "initHardware() OK!");
2014-09-25 20:06:56 -07:00
}
2015-01-08 12:03:45 -08:00
#endif /* EFI_PROD_CODE */