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
|
2017-01-03 03:05:22 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2017
|
2016-05-19 19:03:18 -07:00
|
|
|
*/
|
|
|
|
|
2017-08-18 13:18:11 -07:00
|
|
|
#include "accelerometer.h"
|
|
|
|
#include "lis302dl.h"
|
|
|
|
#include "hardware.h"
|
2017-08-28 17:11:32 -07:00
|
|
|
#include "mpu_util.h"
|
2017-08-18 13:18:11 -07:00
|
|
|
|
|
|
|
EXTERN_ENGINE;
|
|
|
|
|
2017-08-28 17:11:32 -07:00
|
|
|
#if EFI_MEMS || defined(__DOXYGEN__)
|
|
|
|
static SPIDriver *driver = &SPID1; // todo: make this configurable
|
|
|
|
static spi_device_e device = SPI_DEVICE_1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 = {
|
|
|
|
NULL,
|
|
|
|
/* HW dependent part.*/
|
|
|
|
GPIOE,
|
|
|
|
GPIOE_PIN3,
|
|
|
|
SPI_CR1_BR_0 | SPI_CR1_BR_1 | SPI_CR1_CPOL | SPI_CR1_CPHA
|
|
|
|
};
|
|
|
|
#endif /* EFI_MEMS */
|
|
|
|
|
2017-08-18 13:18:11 -07:00
|
|
|
void configureAccelerometerPins(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
|
|
|
engineConfiguration->LIS302DLCsPin = GPIOE_3;
|
|
|
|
// boardConfiguration->is_enabled_spi_1 = true; // we have a conflict with PA5 input pin
|
|
|
|
|
|
|
|
// stm32f4discovery defaults
|
|
|
|
boardConfiguration->spi1mosiPin = GPIOA_7;
|
|
|
|
boardConfiguration->spi1misoPin = GPIOA_6;
|
|
|
|
boardConfiguration->spi1sckPin = GPIOA_5;
|
|
|
|
}
|
|
|
|
|
2016-05-19 19:03:18 -07:00
|
|
|
|
2017-08-21 07:55:34 -07:00
|
|
|
#if EFI_MEMS || defined(__DOXYGEN__)
|
|
|
|
|
2017-08-28 17:11:32 -07:00
|
|
|
void accelerometerPeriodicCallback(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2017-08-28 17:42:27 -07:00
|
|
|
int8_t x = (int8_t)lis302dlReadRegister(driver, LIS302DL_OUTX);
|
|
|
|
int8_t y = (int8_t)lis302dlReadRegister(driver, LIS302DL_OUTY);
|
2017-08-28 17:11:32 -07:00
|
|
|
}
|
2017-08-21 07:55:34 -07:00
|
|
|
|
2017-08-28 17:42:27 -07:00
|
|
|
void initAccelerometer(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2017-08-21 07:55:34 -07:00
|
|
|
if (engineConfiguration->LIS302DLCsPin == GPIOA_0)
|
|
|
|
return; // temporary code to handle old configurations
|
|
|
|
if (engineConfiguration->LIS302DLCsPin == GPIO_UNASSIGNED)
|
|
|
|
return; // not used
|
|
|
|
|
2017-08-28 17:11:32 -07:00
|
|
|
// todo: driver = getSpiDevice(device);
|
|
|
|
|
2017-08-21 07:55:34 -07:00
|
|
|
turnOnSpi(device);
|
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);
|
|
|
|
// memsCfg.ssport = getHwPort("mmc", boardConfiguration->sdCardCsPin);
|
|
|
|
// memsCfg.sspad = getHwPin("mmc", boardConfiguration->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
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* EFI_MEMS */
|
2017-08-28 17:11:32 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float getLongitudinalAcceleration(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
float getTransverseAcceleration(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
|
|
|
return 0;
|
|
|
|
}
|