Skip to content

Commit bed6e15

Browse files
committed
Merge pull request #1320 from Sissors/lookmartinImadeanewbranch
[HAL][K20XX] Fixed (possible) glitches when changing SPI mode on K20 …
2 parents 748fea3 + 7423bd3 commit bed6e15

File tree

1 file changed

+15
-4
lines changed
  • libraries/mbed/targets/hal/TARGET_Freescale/TARGET_K20XX

1 file changed

+15
-4
lines changed

libraries/mbed/targets/hal/TARGET_Freescale/TARGET_K20XX/spi_api.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,27 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) {
6060
MBED_ASSERT((bits > 4) || (bits < 16));
6161
MBED_ASSERT((mode >= 0) && (mode <= 3));
6262

63-
uint32_t polarity = (mode & 0x2) ? 1 : 0;
64-
uint32_t phase = (mode & 0x1) ? 1 : 0;
63+
uint8_t polarity = (mode & 0x2) ? 1 : 0;
64+
uint8_t phase = (mode & 0x1) ? 1 : 0;
65+
uint8_t old_polarity = (obj->spi->CTAR[0] & SPI_CTAR_CPOL_MASK) != 0;
6566

6667
// set master/slave
67-
obj->spi->MCR &= ~SPI_MCR_MSTR_MASK;
68-
obj->spi->MCR |= ((!slave) << SPI_MCR_MSTR_SHIFT);
68+
if (slave) {
69+
obj->spi->MCR &= ~SPI_MCR_MSTR_MASK;
70+
} else {
71+
obj->spi->MCR |= (1UL << SPI_MCR_MSTR_SHIFT);
72+
}
6973

7074
// CTAR0 is used
7175
obj->spi->CTAR[0] &= ~(SPI_CTAR_CPHA_MASK | SPI_CTAR_CPOL_MASK | SPI_CTAR_FMSZ_MASK);
7276
obj->spi->CTAR[0] |= (polarity << SPI_CTAR_CPOL_SHIFT) | (phase << SPI_CTAR_CPHA_SHIFT) | ((bits - 1) << SPI_CTAR_FMSZ_SHIFT);
77+
78+
//If clk idle state was changed, start a dummy transmission
79+
//This is a 'feature' in DSPI: https://community.freescale.com/thread/105526
80+
if ((old_polarity != polarity) && (slave == 0)) {
81+
//Start transfer (CS should be high, so shouldn't matter)
82+
spi_master_write(obj, 0xFFFF);
83+
}
7384
}
7485

7586
static const uint8_t baudrate_prescaler[] = {2,3,5,7};

0 commit comments

Comments
 (0)