Skip to content

Commit

Permalink
HT32: SPI: avoid write collisions when SPI FIFO is disabled
Browse files Browse the repository at this point in the history
This fixes possible write collisions experienced when SPI FIFO is
disabled due to not checking whether the SPI shift register is empty
before writing. This is done by checking TXE flag instead of TXBE flag
when the FIFO is not used.
  • Loading branch information
hansemro committed Jan 30, 2023
1 parent a224be1 commit 6aacaa3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions os/common/ext/CMSIS/HT32/HT32F165x/ht32f165x_reg.h
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ typedef struct {
#define SPI_CR1_FORMAT_MODE2 (0x6U << 8)
#define SPI_CR1_FORMAT_MODE3 (0x5U << 8)
#define SPI_IER_RXBNEIEN (1U << 2)
#define SPI_IER_TXEIEN (1U << 1)
#define SPI_IER_TXBEIEN (1U << 0)
#define SPI_SR_RXBNE (1U << 2)
#define SPI_SR_TXE (1U << 1)
Expand Down
1 change: 1 addition & 0 deletions os/common/ext/CMSIS/HT32/HT32F523xx/ht32f523x2_reg.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ typedef struct {
#define SPI_CR1_FORMAT_MODE2 (0x6U << 8)
#define SPI_CR1_FORMAT_MODE3 (0x5U << 8)
#define SPI_IER_RXBNEIEN (1U << 2)
#define SPI_IER_TXEIEN (1U << 1)
#define SPI_IER_TXBEIEN (1U << 0)
#define SPI_SR_RXBNE (1U << 2)
#define SPI_SR_TXE (1U << 1)
Expand Down
12 changes: 9 additions & 3 deletions os/hal/ports/HT32/LLD/SPIv1/hal_spi_lld.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,14 @@ static void spi_lld_tx(SPIDriver * const spip) {

while (spip->txcnt) {
sr = spip->SPI->SR;
if ((sr & SPI_SR_TXBE) == 0)
return;
// avoid write collision
if (spip->SPI->FCR & SPI_FCR_FIFOEN) {
if ((sr & SPI_SR_TXBE) == 0)
return;
} else {
if ((sr & SPI_SR_TXE) == 0)
return;
}
if (spip->txptr) {
fd = *spip->txptr++;
} else {
Expand Down Expand Up @@ -260,7 +266,7 @@ void spi_lld_exchange(SPIDriver *spip, size_t n,
spip->txptr = txbuf;
spip->rxptr = rxbuf;
spip->rxcnt = spip->txcnt = n;
spip->SPI->IER = SPI_IER_RXBNEIEN | SPI_IER_TXBEIEN;
spip->SPI->IER = SPI_IER_RXBNEIEN | SPI_IER_TXBEIEN | SPI_IER_TXEIEN;
}

/**
Expand Down

0 comments on commit 6aacaa3

Please sign in to comment.