Skip to content

Commit

Permalink
Merge pull request #361 from hansemro/ht32-spi-tx-wc-fix
Browse files Browse the repository at this point in the history
HT32: SPI: avoid write collisions when SPI FIFO is disabled
  • Loading branch information
fpoussin committed Jul 17, 2023
2 parents 65c4c2f + 6aacaa3 commit 3197a82
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 3197a82

Please sign in to comment.