Kinetis: smarter SPI_CS control (#993)

This commit is contained in:
andreika-git 2019-11-01 16:50:22 +02:00 committed by rusefi
parent ee5ee1942c
commit e8fa6135f4
2 changed files with 22 additions and 9 deletions

View File

@ -55,8 +55,9 @@ SPIDriver SPID2;
/* Driver local functions. */ /* Driver local functions. */
/*===========================================================================*/ /*===========================================================================*/
static int32_t spi_detectPCS(bool isMaster, ioportid_t ssport, uint16_t sspad) { static int32_t spi_detectPCS(bool isMaster, ioportid_t ssport, uint16_t sspad, int *alt) {
// todo: check if PCS corresponds to SPI number // todo: check if PCS corresponds to SPI number
*alt = 3;
if (ssport == GPIOA) { if (ssport == GPIOA) {
switch (sspad) { switch (sspad) {
case 6: case 6:
@ -78,9 +79,11 @@ static int32_t spi_detectPCS(bool isMaster, ioportid_t ssport, uint16_t sspad) {
} else if (ssport == GPIOD && sspad == 3) { } else if (ssport == GPIOD && sspad == 3) {
return isMaster ? kLPSPI_MasterPcs0 : kLPSPI_SlavePcs0; return isMaster ? kLPSPI_MasterPcs0 : kLPSPI_SlavePcs0;
} else if (ssport == GPIOE && sspad == 6) { } else if (ssport == GPIOE && sspad == 6) {
*alt = 2;
return isMaster ? kLPSPI_MasterPcs2 : kLPSPI_SlavePcs2; return isMaster ? kLPSPI_MasterPcs2 : kLPSPI_SlavePcs2;
} }
// wrong/unrecognized PCS! // wrong/unrecognized PCS!
*alt = 0;
return -1; return -1;
} }
@ -165,9 +168,17 @@ void spi_lld_start(SPIDriver *spip) {
#endif #endif
spip->isMaster = (spip->config->cr1 & SPI_CR1_MSTR) != 0; spip->isMaster = (spip->config->cr1 & SPI_CR1_MSTR) != 0;
int pcsIdx = spi_detectPCS(spip->isMaster, spip->config->ssport, spip->config->sspad); spip->flags = 0; // kLPSPI_MasterByteSwap;
osalDbgAssert(pcsIdx >= 0, "invalid SPI PCS pin"); int pcsAlt;
spip->flags = pcsIdx; // kLPSPI_MasterByteSwap; int pcsIdx = spi_detectPCS(spip->isMaster, spip->config->ssport, spip->config->sspad, &pcsAlt);
if (pcsIdx >= 0) {
spip->flags |= pcsIdx;
// enable corresponding alt.mode for hardware PCS control
palSetPadMode(spip->config->ssport, spip->config->sspad, PAL_MODE_ALTERNATE(pcsAlt));
} else {
// software PCS control for non-standard pins
palSetPadMode(spip->config->ssport, spip->config->sspad, PAL_MODE_OUTPUT_OPENDRAIN);
}
//CLOCK_SetIpSrc(clockName, kCLOCK_IpSrcSysPllAsync); //CLOCK_SetIpSrc(clockName, kCLOCK_IpSrcSysPllAsync);
@ -245,8 +256,9 @@ void spi_lld_stop(SPIDriver *spip) {
* @notapi * @notapi
*/ */
void spi_lld_select(SPIDriver *spip) { void spi_lld_select(SPIDriver *spip) {
// software PCS control for non-standard pins
//palClearPad(spip->config->ssport, spip->config->sspad); if (!(spip->flags & LPSPI_MASTER_PCS_MASK))
palClearPad(spip->config->ssport, spip->config->sspad);
} }
/** /**
@ -258,8 +270,9 @@ void spi_lld_select(SPIDriver *spip) {
* @notapi * @notapi
*/ */
void spi_lld_unselect(SPIDriver *spip) { void spi_lld_unselect(SPIDriver *spip) {
// software PCS control for non-standard pins
//palSetPad(spip->config->ssport, spip->config->sspad); if (!(spip->flags & LPSPI_MASTER_PCS_MASK))
palSetPad(spip->config->ssport, spip->config->sspad);
} }
/** /**

View File

@ -181,7 +181,7 @@ void initSpiCs(SPIConfig *spiConfig, brain_pin_e csPin) {
ioportmask_t pin = getHwPin("spi", csPin); ioportmask_t pin = getHwPin("spi", csPin);
spiConfig->ssport = port; spiConfig->ssport = port;
spiConfig->sspad = pin; spiConfig->sspad = pin;
// todo: we use hardware CS control? // CS is controlled inside 'hal_spi_lld' driver using both software and hardware methods.
//efiSetPadMode("chip select", csPin, PAL_MODE_OUTPUT_OPENDRAIN); //efiSetPadMode("chip select", csPin, PAL_MODE_OUTPUT_OPENDRAIN);
} }