2016-05-19 19:03:18 -07:00
|
|
|
/*
|
2017-08-21 07:55:34 -07:00
|
|
|
* @file accelerometer.cpp
|
|
|
|
*
|
|
|
|
* stm32f4discovery has MEMS LIS302DL
|
|
|
|
* www.st.com/resource/en/datasheet/lis302dl.pdf
|
|
|
|
*
|
|
|
|
* SPI1
|
|
|
|
* LIS302DL_SPI_SCK PA5
|
|
|
|
* LIS302DL_SPI_MISO PA6
|
|
|
|
* LIS302DL_SPI_MOSI PA7
|
|
|
|
* LIS302DL_SPI_CS_PIN PE3
|
|
|
|
*
|
|
|
|
*
|
2016-05-19 19:03:18 -07:00
|
|
|
*
|
|
|
|
* @date May 19, 2016
|
2020-01-13 18:57:43 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2020
|
2016-05-19 19:03:18 -07:00
|
|
|
*/
|
|
|
|
|
2017-08-18 13:18:11 -07:00
|
|
|
#include "accelerometer.h"
|
|
|
|
#include "hardware.h"
|
|
|
|
|
|
|
|
EXTERN_ENGINE;
|
|
|
|
|
2019-04-12 17:52:51 -07:00
|
|
|
#if EFI_MEMS
|
2019-01-03 20:51:29 -08:00
|
|
|
#include "mpu_util.h"
|
2018-11-16 04:40:06 -08:00
|
|
|
#include "lis302dl.h"
|
2019-07-08 00:35:41 -07:00
|
|
|
#include "periodic_thread_controller.h"
|
2018-11-16 04:40:06 -08:00
|
|
|
|
2017-11-19 12:24:36 -08:00
|
|
|
static SPIDriver *driver;
|
2017-08-28 17:11:32 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* SPI1 configuration structure.
|
|
|
|
* Speed 5.25MHz, CPHA=1, CPOL=1, 8bits frames, MSb transmitted first.
|
|
|
|
* The slave select line is the pin GPIOE_CS_SPI on the port GPIOE.
|
|
|
|
*/
|
|
|
|
static const SPIConfig accelerometerCfg = {
|
2020-01-06 05:44:23 -08:00
|
|
|
.spi_bus = NULL,
|
|
|
|
/* HW dependent part.*/
|
|
|
|
.ssport = GPIOE,
|
|
|
|
.sspad = GPIOE_PIN3,
|
|
|
|
.cr1 = SPI_CR1_BR_0 | SPI_CR1_BR_1 | SPI_CR1_CPOL | SPI_CR1_CPHA |
|
|
|
|
SPI_CR1_8BIT_MODE,
|
|
|
|
.cr2 = SPI_CR2_8BIT_MODE
|
2017-08-28 17:11:32 -07:00
|
|
|
};
|
|
|
|
#endif /* EFI_MEMS */
|
|
|
|
|
2017-08-18 13:18:11 -07:00
|
|
|
void configureAccelerometerPins(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2017-10-16 09:26:07 -07:00
|
|
|
// engineConfiguration->LIS302DLCsPin = GPIOE_3; // we have a conflict with VVT output on Miata
|
2019-12-11 14:48:55 -08:00
|
|
|
// CONFIG(is_enabled_spi_1) = true; // we have a conflict with PA5 input pin
|
2017-08-18 13:18:11 -07:00
|
|
|
|
|
|
|
// stm32f4discovery defaults
|
2019-12-11 14:48:55 -08:00
|
|
|
CONFIG(spi1mosiPin) = GPIOA_7;
|
|
|
|
CONFIG(spi1misoPin) = GPIOA_6;
|
|
|
|
CONFIG(spi1sckPin) = GPIOA_5;
|
2017-08-18 13:18:11 -07:00
|
|
|
}
|
|
|
|
|
2016-05-19 19:03:18 -07:00
|
|
|
|
2019-04-12 17:52:51 -07:00
|
|
|
#if EFI_MEMS
|
2017-08-21 07:55:34 -07:00
|
|
|
|
2017-08-28 19:32:29 -07:00
|
|
|
static THD_WORKING_AREA(ivThreadStack, UTILITY_THREAD_STACK_SIZE);
|
|
|
|
|
2019-02-11 12:09:24 -08:00
|
|
|
class AccelController : public PeriodicController<UTILITY_THREAD_STACK_SIZE> {
|
|
|
|
public:
|
|
|
|
AccelController() : PeriodicController("Acc SPI") { }
|
|
|
|
private:
|
2019-12-21 18:11:09 -08:00
|
|
|
void PeriodicTask(efitick_t nowNt) override {
|
2017-08-28 19:32:29 -07:00
|
|
|
// has to be a thread since we want to use blocking method - blocking method only available in threads, not in interrupt handler
|
|
|
|
// todo: migrate to async SPI API?
|
|
|
|
engine->sensors.accelerometer.x = (int8_t)lis302dlReadRegister(driver, LIS302DL_OUTX);
|
|
|
|
engine->sensors.accelerometer.y = (int8_t)lis302dlReadRegister(driver, LIS302DL_OUTY);
|
2017-08-28 19:55:29 -07:00
|
|
|
chThdSleepMilliseconds(20);
|
2017-08-28 19:32:29 -07:00
|
|
|
}
|
2019-02-11 12:09:24 -08:00
|
|
|
};
|
2017-08-28 19:32:29 -07:00
|
|
|
|
2019-02-11 12:09:24 -08:00
|
|
|
static BenchController instance;
|
2017-08-21 07:55:34 -07:00
|
|
|
|
2017-08-28 17:42:27 -07:00
|
|
|
void initAccelerometer(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2021-01-08 17:01:26 -08:00
|
|
|
if (!isBrainPinValid(engineConfiguration->LIS302DLCsPin))
|
2017-08-21 07:55:34 -07:00
|
|
|
return; // not used
|
|
|
|
|
2019-12-11 14:48:55 -08:00
|
|
|
if (!CONFIG(is_enabled_spi_1))
|
2017-08-28 19:32:29 -07:00
|
|
|
return; // temporary
|
2019-04-12 17:52:51 -07:00
|
|
|
#if HAL_USE_SPI
|
2017-11-19 12:24:36 -08:00
|
|
|
driver = getSpiDevice(engineConfiguration->accelerometerSpiDevice);
|
2019-03-26 06:38:23 -07:00
|
|
|
if (driver == NULL) {
|
|
|
|
// error already reported
|
|
|
|
return;
|
|
|
|
}
|
2017-08-28 17:11:32 -07:00
|
|
|
|
2017-11-19 12:24:36 -08:00
|
|
|
turnOnSpi(engineConfiguration->accelerometerSpiDevice);
|
2017-08-28 17:11:32 -07:00
|
|
|
spiStart(driver, &accelerometerCfg);
|
|
|
|
initSpiCs((SPIConfig *)driver->config, engineConfiguration->LIS302DLCsPin);
|
2017-08-21 07:55:34 -07:00
|
|
|
|
2017-08-28 17:11:32 -07:00
|
|
|
// memsCs.initPin("LIS302 CS", engineConfiguration->LIS302DLCsPin);
|
2019-12-11 14:48:55 -08:00
|
|
|
// memsCfg.ssport = getHwPort("mmc", CONFIG(sdCardCsPin));
|
|
|
|
// memsCfg.sspad = getHwPin("mmc", CONFIG(sdCardCsPin));
|
2017-08-21 07:55:34 -07:00
|
|
|
|
|
|
|
|
|
|
|
/* LIS302DL initialization.*/
|
2017-08-28 17:11:32 -07:00
|
|
|
lis302dlWriteRegister(driver, LIS302DL_CTRL_REG1, 0x47); // enable device, enable XYZ
|
|
|
|
lis302dlWriteRegister(driver, LIS302DL_CTRL_REG2, 0x00); // 4 wire mode
|
|
|
|
lis302dlWriteRegister(driver, LIS302DL_CTRL_REG3, 0x00);
|
2017-08-21 07:55:34 -07:00
|
|
|
|
2018-12-27 06:40:40 -08:00
|
|
|
chThdCreateStatic(ivThreadStack, sizeof(ivThreadStack), NORMALPRIO, (tfunc_t)(void*) ivThread, NULL);
|
2018-11-16 04:40:06 -08:00
|
|
|
#endif /* HAL_USE_SPI */
|
2017-08-21 07:55:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* EFI_MEMS */
|
2017-08-28 17:11:32 -07:00
|
|
|
|
|
|
|
|
|
|
|
float getLongitudinalAcceleration(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2017-08-28 19:32:29 -07:00
|
|
|
return engine->sensors.accelerometer.x;
|
2017-08-28 17:11:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
float getTransverseAcceleration(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2017-08-28 19:32:29 -07:00
|
|
|
return engine->sensors.accelerometer.y;
|
2017-08-28 17:11:32 -07:00
|
|
|
}
|