Skip to content

Commit

Permalink
kinetis: Clean up SPI module power handling
Browse files Browse the repository at this point in the history
Fixes a problem with hardware CS when using slow SPI speeds,
mentioned in RIOT-OS#6437 (comment)
  • Loading branch information
Joakim Nohlgård authored and pekkanikander committed Jul 10, 2018
1 parent c3e723b commit 159e6c4
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions cpu/kinetis/periph/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,16 @@ static inline void poweron(spi_t bus)
break;
#endif
}

/* Enable the module */
dev(bus)->MCR &= ~(SPI_MCR_MDIS_MASK);
}

static inline void poweroff(spi_t bus)
{
/* Disable the module */
dev(bus)->MCR |= SPI_MCR_MDIS_MASK;

switch((uint32_t)dev(bus)) {
case (uint32_t)SPI0:
case (uint32_t)SPI1:
Expand Down Expand Up @@ -96,11 +102,14 @@ void spi_init(spi_t bus)
dev(bus)->MCR = (SPI_MCR_MSTR_MASK | SPI_MCR_PCSIS_MASK |
SPI_MCR_CLR_RXF_MASK | SPI_MCR_CLR_TXF_MASK |
SPI_MCR_DIS_RXF_MASK | SPI_MCR_DIS_TXF_MASK |
SPI_MCR_DOZE_MASK | SPI_MCR_HALT_MASK | SPI_MCR_MDIS_MASK);
SPI_MCR_DOZE_MASK | SPI_MCR_HALT_MASK);

/* disable all DMA and interrupt requests */
dev(bus)->RSER = 0;

/* Wait for the hardware to acknowledge the halt command */
while (dev(bus)->SR & SPI_SR_TXRXS_MASK) {}

/* and power off the bus until it is actually used */
poweroff(bus);
}
Expand Down Expand Up @@ -153,8 +162,13 @@ int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)

void spi_release(spi_t bus)
{
/* disable, power off, and unlock the bus */
dev(bus)->MCR |= (SPI_MCR_HALT_MASK | SPI_MCR_MDIS_MASK);
/* Halt transfers */
dev(bus)->MCR |= SPI_MCR_HALT_MASK;

/* Wait for the module to acknowledge the stop */
while (dev(bus)->SR & SPI_SR_TXRXS_MASK) {}

/* Disable the module */
poweroff(bus);
mutex_unlock(&locks[bus]);
}
Expand Down

0 comments on commit 159e6c4

Please sign in to comment.