Skip to content

Commit 0445b6a

Browse files
authored
Merge pull request #13537 from OpenNuvoton/nuvoton_fix_downgrade_qspi_5.15
Nuvoton: Fix degrading QSPI to SPI (5.15)
2 parents e435039 + 9c86871 commit 0445b6a

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

targets/TARGET_NUVOTON/TARGET_M2354/spi_api.c

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,21 @@ static struct nu_spi_var spi4_var = {
7676
#endif
7777
};
7878

79+
/* Change to QSPI version functions
80+
*
81+
* In most cases, we can control degraded QSPI H/W to standard through BSP SPI driver
82+
* directly as if it is just SPI H/W. However, BSP SPI driver distinguishes among
83+
* SPI H/W instances in below functions:
84+
*
85+
* SPI_Open
86+
* SPI_Close
87+
* SPI_SetBusClock
88+
* SPI_GetBusClock
89+
*
90+
* In these cases, we must change to QSPI version instead for QSPI H/W.
91+
*/
92+
static int spi_is_qspi(spi_t *obj);
93+
7994
/* Synchronous version of SPI_ENABLE()/SPI_DISABLE() macros
8095
*
8196
* The SPI peripheral clock is asynchronous with the system clock. In order to make sure the SPI
@@ -210,7 +225,11 @@ void spi_free(spi_t *obj)
210225
}
211226
#endif
212227

213-
SPI_Close((SPI_T *) NU_MODBASE(obj->spi.spi));
228+
if (spi_is_qspi(obj)) {
229+
QSPI_Close((QSPI_T *) NU_MODBASE(obj->spi.spi));
230+
} else {
231+
SPI_Close((SPI_T *) NU_MODBASE(obj->spi.spi));
232+
}
214233

215234
const struct nu_modinit_s *modinit = get_modinit(obj->spi.spi, spi_modinit_tab);
216235
MBED_ASSERT(modinit != NULL);
@@ -248,11 +267,19 @@ void spi_format(spi_t *obj, int bits, int mode, int slave)
248267

249268
SPI_DISABLE_SYNC(spi_base);
250269

251-
SPI_Open(spi_base,
252-
slave ? SPI_SLAVE : SPI_MASTER,
253-
(mode == 0) ? SPI_MODE_0 : (mode == 1) ? SPI_MODE_1 : (mode == 2) ? SPI_MODE_2 : SPI_MODE_3,
254-
bits,
255-
SPI_GetBusClock(spi_base));
270+
if (spi_is_qspi(obj)) {
271+
QSPI_Open((QSPI_T *) spi_base,
272+
slave ? QSPI_SLAVE : QSPI_MASTER,
273+
(mode == 0) ? QSPI_MODE_0 : (mode == 1) ? QSPI_MODE_1 : (mode == 2) ? QSPI_MODE_2 : QSPI_MODE_3,
274+
bits,
275+
QSPI_GetBusClock((QSPI_T *)spi_base));
276+
} else {
277+
SPI_Open(spi_base,
278+
slave ? SPI_SLAVE : SPI_MASTER,
279+
(mode == 0) ? SPI_MODE_0 : (mode == 1) ? SPI_MODE_1 : (mode == 2) ? SPI_MODE_2 : SPI_MODE_3,
280+
bits,
281+
SPI_GetBusClock(spi_base));
282+
}
256283
// NOTE: Hardcode to be MSB first.
257284
SPI_SET_MSB_FIRST(spi_base);
258285

@@ -281,7 +308,11 @@ void spi_frequency(spi_t *obj, int hz)
281308

282309
SPI_DISABLE_SYNC(spi_base);
283310

284-
SPI_SetBusClock((SPI_T *) NU_MODBASE(obj->spi.spi), hz);
311+
if (spi_is_qspi(obj)) {
312+
QSPI_SetBusClock((QSPI_T *) NU_MODBASE(obj->spi.spi), hz);
313+
} else {
314+
SPI_SetBusClock((SPI_T *) NU_MODBASE(obj->spi.spi), hz);
315+
}
285316
}
286317

287318

@@ -617,6 +648,13 @@ uint8_t spi_active(spi_t *obj)
617648
return vec ? 1 : 0;
618649
}
619650

651+
static int spi_is_qspi(spi_t *obj)
652+
{
653+
SPI_T *spi_base = (SPI_T *) NU_MODBASE(obj->spi.spi);
654+
655+
return (spi_base == ((SPI_T *) QSPI0));
656+
}
657+
620658
static int spi_writeable(spi_t * obj)
621659
{
622660
// Receive FIFO must not be full to avoid receive FIFO overflow on next transmit/receive

0 commit comments

Comments
 (0)