SPI prescaler refactoring

This commit is contained in:
rusefi 2019-03-25 22:41:31 -04:00
parent 52ba88fbc2
commit cf740411f2
9 changed files with 46 additions and 13 deletions

View File

@ -538,6 +538,10 @@ typedef enum {
Force_4_bytes_size_uart_device = ENUM_32_BITS,
} uart_device_e;
typedef enum __attribute__ ((__packed__)) {
_5MHz,
_150KHz
} spi_speed_e;
typedef enum {
SPI_NONE = 0,
@ -550,6 +554,8 @@ typedef enum {
Force_4_bytes_size_spi_device = ENUM_32_BITS,
} spi_device_e;
/**
* Frankenso analog #1 PC2 ADC12
* Frankenso analog #2 PC1 ADC11

View File

@ -84,12 +84,12 @@ static Logging *logger;
// todo: nicer method which would mention SPI speed explicitly?
#if EFI_PROD_CODE
static SPIConfig hipSpiCfg = { NULL,
static SPIConfig hipSpiCfg = { /* end_cb */ NULL,
/* HW dependent part.*/
NULL, 0,
SPI_CR1_MSTR |
/* ssport */ NULL, /* sspad */ 0,
/* cr1 */ SPI_CR1_MSTR |
//SPI_CR1_BR_1 // 5MHz
SPI_CR1_CPHA | SPI_CR1_BR_0 | SPI_CR1_BR_1 | SPI_CR1_BR_2 };
SPI_CR1_CPHA | SPI_CR1_BR_0 | SPI_CR1_BR_1 | SPI_CR1_BR_2, /* cr2*/ 0 };
#endif /* EFI_PROD_CODE */
static void checkResponse(void) {
@ -236,6 +236,7 @@ static void endIntegration(void) {
* Shaft Position callback used to start or finish HIP integration
*/
static void intHoldCallback(trigger_event_e ckpEventType, uint32_t index DECLARE_ENGINE_PARAMETER_SUFFIX) {
(void)ckpEventType;
// this callback is invoked on interrupt thread
if (index != 0)
return;
@ -286,6 +287,7 @@ void setHipGain(float value) {
* this is the end of the non-synchronous exchange
*/
static void endOfSpiExchange(SPIDriver *spip) {
(void)spip;
spiUnselectI(driver);
instance.state = READY_TO_INTEGRATE;
checkResponse();
@ -353,6 +355,7 @@ static void hipStartupCode(void) {
static THD_WORKING_AREA(hipTreadStack, UTILITY_THREAD_STACK_SIZE);
static msg_t hipThread(void *arg) {
(void)arg;
chRegSetThreadName("hip9011 init");
// some time to let the hardware start

View File

@ -28,9 +28,9 @@ static SPIDriver *driver;
static OutputPin tle8888Cs;
static SPIConfig spiConfig = { NULL,
static SPIConfig spiConfig = { /* end_cb */ NULL,
/* HW dependent part.*/
NULL, 0, SPI_CR1_MSTR | SPI_CR1_CPHA | SPI_CR1_BR_0 | SPI_CR1_BR_1 | SPI_CR1_BR_2 };
/* ssport */ NULL, /* sspad */ 0, /* cr1 */ SPI_CR1_MSTR | SPI_CR1_CPHA, /* cr2*/ 0 };
void initTle8888(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
if (engineConfiguration->tle8888_cs == GPIO_UNASSIGNED) {
@ -39,9 +39,10 @@ void initTle8888(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
tle8888Cs.initPin("tle8888 CS", engineConfiguration->tle8888_cs,
&engineConfiguration->tle8888_csPinMode);
//spiConfig.cr1 = SPI_BaudRatePrescaler_8;
spiConfig.cr1 += getSpiPrescaler(_150KHz, engineConfiguration->tle8888spiDevice);
driver = getSpiDevice(engineConfiguration->tle8888spiDevice);
// todo: reuse initSpiCs method?
spiConfig.ssport = getHwPort("tle8888", engineConfiguration->tle8888_cs);
spiConfig.sspad = getHwPin("tle8888", engineConfiguration->tle8888_cs);

View File

@ -426,7 +426,7 @@ void initHardware(Logging *l) {
initOutputPins();
#if EFI_MAX_31855
initMax31855(sharedLogger, getSpiDevice(CONFIGB(max31855spiDevice)), CONFIGB(max31855_cs));
initMax31855(sharedLogger, CONFIGB(max31855spiDevice), CONFIGB(max31855_cs));
#endif /* EFI_MAX_31855 */
#if EFI_TLE8888
@ -522,3 +522,20 @@ void initHardware(Logging *l) {
#endif /* EFI_PROD_CODE */
#endif /* EFI_PROD_CODE || EFI_SIMULATOR */
#if HAL_USE_SPI || defined(__DOXYGEN__)
// this is F4 implementation but we will keep it here for now for simplicity
int getSpiPrescaler(spi_speed_e speed, spi_device_e device) {
switch (speed) {
case _5MHz:
return device == SPI_DEVICE_1 ? SPI_BaudRatePrescaler_16 : SPI_BaudRatePrescaler_8;
case _150KHz:
// SPI1 does not support 150KHz, it would be 300KHz for SPI1
return SPI_BaudRatePrescaler_256;
default:
// unexpected
return 0;
}
}
#endif /* HAL_USE_SPI */

View File

@ -31,6 +31,8 @@
// 328.125 KHz 164.06 KHz
#define SPI_BaudRatePrescaler_256 ((uint16_t)0x0038)
int getSpiPrescaler(spi_speed_e speed, spi_device_e device);
SPIDriver * getSpiDevice(spi_device_e spiDevice);
void turnOnSpi(spi_device_e device);
void lockSpi(spi_device_e device);

View File

@ -141,7 +141,8 @@ static void egtRead(void) {
}
}
void initMax31855(Logging *sharedLogger, SPIDriver *drv, egt_cs_array_t max31855_cs) {
void initMax31855(Logging *sharedLogger, spi_device_e device, egt_cs_array_t max31855_cs) {
SPIDriver *drv = getSpiDevice(device);
logger = sharedLogger;
// todo:spi device is now enabled separately - should probably be enabled here
@ -156,7 +157,7 @@ void initMax31855(Logging *sharedLogger, SPIDriver *drv, egt_cs_array_t max31855
initSpiCs(&spiConfig[i], max31855_cs[i]);
spiConfig[i].cr1 = SPI_BaudRatePrescaler_8;
spiConfig[i].cr1 = getSpiPrescaler(_5MHz, device);
}
}
}

View File

@ -12,7 +12,7 @@
#include "engine_configuration.h"
#if HAL_USE_SPI || defined(__DOXYGEN__)
void initMax31855(Logging *sharedLogger, SPIDriver *drv, egt_cs_array_t max31855_cs);
void initMax31855(Logging *sharedLogger, spi_device_e device, egt_cs_array_t max31855_cs);
#endif /* HAL_USE_SPI */
uint16_t getEgtValue(int egtChannel);

View File

@ -430,6 +430,7 @@ void initMmcCard(void) {
return;
}
// todo: reuse initSpiCs method?
hs_spicfg.ssport = ls_spicfg.ssport = getHwPort("mmc", CONFIGB(sdCardCsPin));
hs_spicfg.sspad = ls_spicfg.sspad = getHwPin("mmc", CONFIGB(sdCardCsPin));
mmccfg.spip = getSpiDevice(engineConfiguration->sdCardSpiDevice);

View File

@ -38,9 +38,9 @@ static THD_WORKING_AREA(cjThreadStack, UTILITY_THREAD_STACK_SIZE);
static SPIDriver *driver;
static SPIConfig cj125spicfg = { NULL,
static SPIConfig cj125spicfg = { /* end_cb */ NULL,
/* HW dependent part.*/
NULL, 0, SPI_CR1_MSTR | SPI_CR1_CPHA | SPI_CR1_BR_0 | SPI_CR1_BR_1 | SPI_CR1_BR_2 };
/* ssport */ NULL, /* sspad */ 0, /* cr1 */ SPI_CR1_MSTR | SPI_CR1_CPHA, /* cr2*/ 0 };
static volatile int lastSlowAdcCounter = 0;
@ -333,6 +333,8 @@ static void cjStartSpi(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
// 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);